Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configure Gem via a .configure block #69

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,28 @@ Here's a list of available options that can be used as the `:active` value
* Hash -> { param_a: 1, param_b: 2 }
```

## Configuration
Several of the options that can be set in a calling of the helper can also be set in an
initializer. For example, if you want to use `is-active` as the active class all the time,
create `config/initializers/active_link_to.rb` with:

```ruby
ActiveLinkTo.configure do |config|
config.class_active = 'is-active'
end
```

The full list of configurable values is:

- `class_active`
- `class_inactive`
- `active_disable`
- `wrap_tag`
- `wrap_class`

Setting these values in configuration just changes the default -- setting them in a
use of the helper should always override the default, as you'd expect.

## More Examples
Most of the functionality of `active_link_to` depends on the current
url. Specifically, `request.original_fullpath` value. We covered the basic example
Expand Down
1 change: 1 addition & 0 deletions lib/active_link_to.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'addressable/uri'
require 'active_link_to/active_link_to'
require 'active_link_to/configuration'
require 'active_link_to/version'
13 changes: 7 additions & 6 deletions lib/active_link_to/active_link_to.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
module ActiveLinkTo

# Wrapper around link_to. Accepts following params:
# :active => Boolean | Symbol | Regex | Controller/Action Pair
# :class_active => String
Expand Down Expand Up @@ -28,8 +27,8 @@ def active_link_to(*args, &block)

css_class = link_options.delete(:class).to_s + ' '

wrap_tag = active_options[:wrap_tag].present? ? active_options[:wrap_tag] : nil
wrap_class = active_options[:wrap_class].present? ? active_options[:wrap_class] + ' ' : ''
wrap_tag = active_options[:wrap_tag].present? ? active_options[:wrap_tag] : ActiveLinkTo.configuration.wrap_tag
wrap_class = active_options[:wrap_class].present? ? active_options[:wrap_class] + ' ' : ActiveLinkTo.configuration.wrap_class.dup

if wrap_tag.present?
wrap_class << active_link_to_class(url, active_options)
Expand All @@ -42,7 +41,9 @@ def active_link_to(*args, &block)
link_options[:class] = css_class if css_class.present?
link_options['aria-current'] = 'page' if is_active_link?(url, active_options[:active])

link = if active_options[:active_disable] === true && is_active_link?(url, active_options[:active])
active_disable = active_options.member?(:active_disable) ? active_options[:active_disable] : ActiveLinkTo.configuration.active_disable

link = if active_disable && is_active_link?(url, active_options[:active])
content_tag(:span, name, link_options)
else
link_to(name, url, link_options)
Expand All @@ -57,9 +58,9 @@ def active_link_to(*args, &block)
#
def active_link_to_class(url, options = {})
if is_active_link?(url, options[:active])
options[:class_active] || 'active'
options[:class_active] || ActiveLinkTo.configuration.class_active
else
options[:class_inactive] || ''
options[:class_inactive] || ActiveLinkTo.configuration.class_inactive
end
end

Expand Down
25 changes: 25 additions & 0 deletions lib/active_link_to/configuration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module ActiveLinkTo
class Configuration
attr_accessor :class_active, :class_inactive, :active_disable, :wrap_tag, :wrap_class

def initialize
@class_active = 'active'.freeze
@class_inactive = ''.freeze
@active_disable = false
@wrap_tag = nil
@wrap_class = ''.freeze
end
end

def self.configuration
@configuration ||= Configuration.new
end

def self.configuration=(config)
@configuration = config
end

def self.configure
yield configuration
end
end
94 changes: 94 additions & 0 deletions test/active_link_to_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,33 @@
require_relative 'test_helper'

class ActiveLinkToTest < MiniTest::Test
def setup
reset_configuration
end

def reset_configuration
ActiveLinkTo.configuration = ActiveLinkTo::Configuration.new
end

def test_default_active_class
assert_equal 'active', ActiveLinkTo.configuration.class_active
end

def test_default_inactive_class
assert_equal '', ActiveLinkTo.configuration.class_inactive
end

def test_default_active_disable
refute ActiveLinkTo.configuration.active_disable
end

def test_default_wrap_tag
assert_nil ActiveLinkTo.configuration.wrap_tag
end

def test_default_wrap_class
assert_equal '', ActiveLinkTo.configuration.wrap_class
end

def test_is_active_link_booleans_test
assert is_active_link?('/', true)
Expand Down Expand Up @@ -166,6 +193,17 @@ def test_active_link_to
assert_html link, 'a[href="/other"]', 'label'
end

def test_active_link_to_with_configured_alternative_class_active_value
ActiveLinkTo.configuration.class_active = 'is-active'

set_path('/root')
link = active_link_to('label', '/root')
assert_html link, 'a.is-active[href="/root"]', 'label'

link = active_link_to('label', '/other')
assert_html link, 'a[href="/other"]', 'label'
end

def test_active_link_to_with_existing_class
set_path('/root')
link = active_link_to('label', '/root', class: 'current')
Expand All @@ -184,6 +222,29 @@ def test_active_link_to_with_custom_classes
assert_html link, 'a.off[href="/other"]', 'label'
end

def test_active_link_to_inactive_class_configuration
ActiveLinkTo.configuration.class_inactive = 'inactive'

set_path('/root')
link = active_link_to('label', '/root')
assert_html link, 'a.active[href="/root"]', 'label'

link = active_link_to('label', '/other')
assert_html link, 'a.inactive[href="/other"]', 'label'
end

def test_active_link_to_with_custom_classes_overrides_configured_values
ActiveLinkTo.configuration.class_active = 'is-active'
ActiveLinkTo.configuration.class_inactive = 'is-inactive'

set_path('/root')
link = active_link_to('label', '/root', class_active: 'on')
assert_html link, 'a.on[href="/root"]', 'label'

link = active_link_to('label', '/other', class_inactive: 'off')
assert_html link, 'a.off[href="/other"]', 'label'
end

def test_active_link_to_with_wrap_tag
set_path('/root')
link = active_link_to('label', '/root', wrap_tag: :li)
Expand All @@ -196,12 +257,45 @@ def test_active_link_to_with_wrap_tag
assert_html link, 'li.active a.testing[href="/root"]', 'label'
end

def test_active_link_to_with_wrap_tag_configuration_set
ActiveLinkTo.configuration.wrap_tag = :li

set_path('/root')
link = active_link_to('label', '/root')
assert_html link, 'li.active a[href="/root"]', 'label'

link = active_link_to('label', '/root', active_disable: true)
assert_html link, 'li.active span', 'label'

link = active_link_to('label', '/root', class: 'testing')
assert_html link, 'li.active a.testing[href="/root"]', 'label'
end

def test_active_link_to_with_active_disable
set_path('/root')
link = active_link_to('label', '/root', active_disable: true)
assert_html link, 'span.active', 'label'
end

def test_active_link_to_with_active_disable_configuration
ActiveLinkTo.configuration.active_disable = true

set_path('/root')
link = active_link_to('label', '/root')
assert_html link, 'span.active', 'label'
end

def test_active_link_to_with_active_disable_configuration_override
ActiveLinkTo.configuration.active_disable = true

set_path('/root')
link = active_link_to('label', '/root')
assert_html link, 'span.active', 'label'

link = active_link_to('label', '/root', active_disable: false)
assert_html link, 'a.active', 'label'
end

def test_should_not_modify_passed_params
set_path('/root')
params = {class: 'testing', active: :inclusive}
Expand Down