Skip to content

Commit

Permalink
Merge branch 'patch-3' of https://github.com/maurogeorge/active_link_to
Browse files Browse the repository at this point in the history
… into maurogeorge-patch-3
  • Loading branch information
GBH committed Mar 17, 2017
2 parents e6c5006 + 4b0f8b3 commit 0a0fe13
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@ active_link_to 'Users', '/users', active: :inclusive
Here's a list of available options that can be used as the `:active` value

```
* Boolean -> true | false
* Symbol -> :exclusive | :inclusive | :exact
* Regex -> /regex/
* Controller/Action Pair -> [[:controller], [:action_a, :action_b]]
* Hash -> { param_a: 1, param_b: 2 }
<<<<<<< HEAD
* Boolean -> true | false
* Symbol -> :exclusive | :inclusive | :exact
* Regex -> /regex/
* Controller/Action Pair -> [[:controller], [:action_a, :action_b]]
* Controller/Specific Action Pair -> [controller: :action_a, controller_b: :action_b]
* Hash -> { param_a: 1, param_b: 2 }
```

## More Examples
Expand Down Expand Up @@ -76,6 +78,9 @@ or action, or both? Or any number of those at the same time? Sure, why not:
# For matching multiple controllers and actions:
active_link_to 'User Edit', edit_user_path(@user), active: [['people', 'news'], ['show', 'edit']]

# For matching specific controllers and actions:
active_link_to 'User Edit', edit_user_path(@user), :active => [people: :show, news: :edit]

# for matching all actions under given controllers:
active_link_to 'User Edit', edit_user_path(@user), active: [['people', 'news'], []]

Expand Down
5 changes: 4 additions & 1 deletion lib/active_link_to/active_link_to.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,10 @@ def is_active_link?(url, condition = nil)
controllers = [*condition[0]]
actions = [*condition[1]]
(controllers.blank? || controllers.member?(params[:controller])) &&
(actions.blank? || actions.member?(params[:action]))
(actions.blank? || actions.member?(params[:action])) ||
controllers.any? do |controller, action|
params[:controller] == controller.to_s && params[:action] == action.to_s
end
when TrueClass
true
when FalseClass
Expand Down
15 changes: 15 additions & 0 deletions test/active_link_to_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,21 @@ def test_is_active_link_array
refute is_active_link?('/', ['controller', 'action_a'])
end

def test_is_active_link_array_with_hash
params[:controller], params[:action] = 'controller', 'action'

assert is_active_link?('/', [controller: :action])
assert is_active_link?('/', ['controller' => 'action'])

refute is_active_link?('/', [controller_b: :action])
refute is_active_link?('/', [controller: :action_b])
refute is_active_link?('/', [controller_b: :action_b])

params[:controller], params[:action] = 'controller_b', 'action_b'

assert is_active_link?('/', [controller: :action, controller_b: :action_b])
end

def test_is_active_link_hash
params[:a] = 1

Expand Down

0 comments on commit 0a0fe13

Please sign in to comment.