rails_navigation 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +58 -21
- data/app/helpers/rails_navigation_helper.rb +6 -2
- data/lib/rails_navigation/matcher.rb +13 -15
- data/lib/rails_navigation/version.rb +1 -1
- data/spec/rails_navigation_spec.rb +26 -20
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f58a4745dfc48238bd43a08be6e3b12f959ca556
|
4
|
+
data.tar.gz: d9b27e8707cc331fcce6f61fea6e62f819f50260
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1c104d54fd50cb0e4b11814dea82aec78b174af7a61a35c307f953eedd858b032cebd9426c292cac11e4a77d73691adfa63ab1c2c9201cad376ae598bddc7465
|
7
|
+
data.tar.gz: b542b987886d6c619025b4b24578fd82807eb4075a49567ba656fe862d512fae9ac83b74c7454a925c32715ad245eadb485e368e607e339bbd4bed8cf644cd19
|
data/README.md
CHANGED
@@ -1,16 +1,63 @@
|
|
1
1
|
# rails_navigation
|
2
2
|
|
3
|
-
Simplify matching of Rails routes.
|
3
|
+
Simplify matching of Rails routes. You are unhappy with the [`link_to_if`](http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to_if)
|
4
|
+
helper?
|
5
|
+
|
6
|
+
**Then check out this gem!** You can now match controllers and actions as they are defined in your routes.rb.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 'rails_navigation'
|
14
|
+
```
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
|
18
|
+
$ bundle
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
### matches_page?
|
23
|
+
|
24
|
+
Works like current_page? Example:
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
matches_page?('controller#action')
|
28
|
+
```
|
29
|
+
|
30
|
+
with multiple conditions:
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
matches_page?(['controller#action', 'other_controller#action'])
|
34
|
+
```
|
35
|
+
|
36
|
+
### nav_to
|
37
|
+
|
38
|
+
Uses `matches_page?` and creates a link and a li element
|
39
|
+
|
40
|
+
*Source code, if you want to customize it:*
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
def nav_to(name, path, conditions = nil)
|
44
|
+
content_tag :li, link_to(name, path), class: ('active' if matches_page?(conditions))
|
45
|
+
end
|
46
|
+
```
|
4
47
|
|
5
|
-
<%= link_to_if(@current_user.nil?, "Login", { controller: "sessions", action: "new" }) %>
|
6
|
-
|
7
|
-
Then try this gem! You can now match controllers and actions like defined in the routes.rb.
|
8
48
|
|
9
49
|
## Examples
|
10
50
|
|
51
|
+
You have a webseite full of books and want to show an edit button on the books#show page.
|
52
|
+
```ruby
|
53
|
+
matches_page?('books#show')
|
54
|
+
```
|
55
|
+
|
56
|
+
## It's like your routes.rb
|
57
|
+
|
11
58
|
Matching controllers:
|
12
59
|
|
13
|
-
{ controller: "pages"
|
60
|
+
{ controller: "pages" } becomes 'pages'
|
14
61
|
|
15
62
|
Matching controllers and actions:
|
16
63
|
|
@@ -23,28 +70,18 @@ Matching controllers, actions and params:
|
|
23
70
|
Or even simpler (id is the default):
|
24
71
|
|
25
72
|
{ controller: "pages", action: "show", params: { id: 'homepage' } } becomes 'pages#new?homepage'
|
73
|
+
|
74
|
+
## What it does:
|
26
75
|
|
76
|
+
**rails_navigation** adds 2 helpers
|
27
77
|
|
28
|
-
|
29
|
-
|
30
|
-
Add this line to your application's Gemfile:
|
31
|
-
|
32
|
-
```ruby
|
33
|
-
gem 'rails_navigation'
|
34
|
-
```
|
35
|
-
|
36
|
-
And then execute:
|
37
|
-
|
38
|
-
$ bundle
|
39
|
-
|
40
|
-
## Usage
|
41
|
-
|
42
|
-
TODO: Write usage instructions here
|
78
|
+
* matches_page?(conditions)
|
79
|
+
* nav_to(name, path, conditions)
|
43
80
|
|
44
81
|
|
45
82
|
## TODO
|
46
83
|
|
47
|
-
*
|
84
|
+
* support `not` operator
|
48
85
|
|
49
86
|
## Contributing
|
50
87
|
|
@@ -1,5 +1,9 @@
|
|
1
1
|
module RailsNavigationHelper
|
2
|
-
def
|
3
|
-
|
2
|
+
def matches_page?(conditions)
|
3
|
+
RailsNavigation::Matcher.match_multiple_with_or?(controller_name, action_name, params, conditions)
|
4
|
+
end
|
5
|
+
|
6
|
+
def nav_to(name, path, conditions = nil)
|
7
|
+
content_tag :li, link_to(name, path), class: ('active' if matches_page?(conditions))
|
4
8
|
end
|
5
9
|
end
|
@@ -1,33 +1,31 @@
|
|
1
1
|
module RailsNavigation
|
2
|
-
|
3
|
-
def
|
4
|
-
@controller_name = controller_name
|
5
|
-
@action_name = action_name
|
6
|
-
@params = params
|
7
|
-
end
|
8
|
-
|
9
|
-
def check_controller(str)
|
2
|
+
module Matcher
|
3
|
+
def self.check_controller(controller_name, str)
|
10
4
|
result = Array(str.match(/^([a-z_\-]+)/))[1]
|
11
|
-
result ? (result ==
|
5
|
+
result ? (result == controller_name) : true
|
12
6
|
end
|
13
7
|
|
14
|
-
def check_action(str)
|
8
|
+
def self.check_action(action_name, str)
|
15
9
|
result = Array(str.match(/#([a-z_\-]+)/))[1]
|
16
|
-
result ? (result ==
|
10
|
+
result ? (result == action_name) : true
|
17
11
|
end
|
18
12
|
|
19
|
-
def check_parameters(str)
|
13
|
+
def self.check_parameters(params, str)
|
20
14
|
result = Array(str.match(/\?([!a-z0-9&_\-=]+)/))[1]
|
21
15
|
if result
|
22
16
|
key, value = result.split('=')
|
23
|
-
result.include?('=') ? value ==
|
17
|
+
result.include?('=') ? value == params[key.to_sym] : result == params[:id]
|
24
18
|
else
|
25
19
|
true
|
26
20
|
end
|
27
21
|
end
|
28
22
|
|
29
|
-
def
|
30
|
-
check_controller(str) && check_action(str) && check_parameters(str)
|
23
|
+
def self.match_single?(controller_name, action_name, params, str)
|
24
|
+
check_controller(controller_name, str) && check_action(action_name, str) && check_parameters(params, str)
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.match_multiple_with_or?(controller_name, action_name, params, arr)
|
28
|
+
[*arr].any? { |str| match_single?(controller_name, action_name, params, str) }
|
31
29
|
end
|
32
30
|
end
|
33
31
|
end
|
@@ -6,8 +6,8 @@ describe RailsNavigation::Matcher do
|
|
6
6
|
it 'matches a controller' do
|
7
7
|
condition = 'pages'
|
8
8
|
controller_name, action_name, params = 'pages', 'edit', {}
|
9
|
-
|
10
|
-
expect(
|
9
|
+
result = RailsNavigation::Matcher.match_single?(controller_name, action_name, params, condition)
|
10
|
+
expect(result).to be_truthy
|
11
11
|
end
|
12
12
|
end
|
13
13
|
|
@@ -15,15 +15,15 @@ describe RailsNavigation::Matcher do
|
|
15
15
|
it 'matches a controller with action' do
|
16
16
|
condition = 'pages#edit'
|
17
17
|
controller_name, action_name, params = 'pages', 'edit', {}
|
18
|
-
|
19
|
-
expect(
|
18
|
+
result = RailsNavigation::Matcher.match_single?(controller_name, action_name, params, condition)
|
19
|
+
expect(result).to be_truthy
|
20
20
|
end
|
21
21
|
|
22
22
|
it 'does not match a controller with a wrong action' do
|
23
23
|
condition = 'pages#show'
|
24
24
|
controller_name, action_name, params = 'pages', 'edit', {}
|
25
|
-
|
26
|
-
expect(
|
25
|
+
result = RailsNavigation::Matcher.match_single?(controller_name, action_name, params, condition)
|
26
|
+
expect(result).to be_falsey
|
27
27
|
end
|
28
28
|
end
|
29
29
|
|
@@ -31,38 +31,44 @@ describe RailsNavigation::Matcher do
|
|
31
31
|
it 'matches a controller, action and param id as default' do
|
32
32
|
condition = 'pages#edit?about'
|
33
33
|
controller_name, action_name, params = 'pages', 'edit', { id: 'about' }
|
34
|
-
|
35
|
-
expect(
|
34
|
+
result = RailsNavigation::Matcher.match_single?(controller_name, action_name, params, condition)
|
35
|
+
expect(result).to be_truthy
|
36
36
|
end
|
37
37
|
|
38
38
|
it 'does not match a controller, action and param id as default' do
|
39
39
|
condition = 'pages#edit?about'
|
40
40
|
controller_name, action_name, params = 'pages', 'edit', { id: 'imprint' }
|
41
|
-
|
42
|
-
expect(
|
41
|
+
result = RailsNavigation::Matcher.match_single?(controller_name, action_name, params, condition)
|
42
|
+
expect(result).to be_falsy
|
43
43
|
end
|
44
44
|
|
45
45
|
it 'matches a controller, action and param' do
|
46
46
|
condition = 'pages#edit?from=homepage'
|
47
47
|
controller_name, action_name, params = 'pages', 'edit', { from: 'homepage' }
|
48
|
-
|
49
|
-
expect(
|
48
|
+
result = RailsNavigation::Matcher.match_single?(controller_name, action_name, params, condition)
|
49
|
+
expect(result).to be_truthy
|
50
50
|
end
|
51
51
|
|
52
52
|
it 'does not match a wrong controller, action and param' do
|
53
53
|
condition = 'pages#edit?from=start'
|
54
54
|
controller_name, action_name, params = 'pages', 'edit', { from: 'homepage' }
|
55
|
-
|
56
|
-
expect(
|
55
|
+
result = RailsNavigation::Matcher.match_single?(controller_name, action_name, params, condition)
|
56
|
+
expect(result).to be_falsy
|
57
57
|
end
|
58
58
|
end
|
59
59
|
|
60
60
|
describe 'with array of conditions' do
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
61
|
+
it 'matches one of multiple conditions' do
|
62
|
+
conditions = ['pages#edit', 'recipes#show']
|
63
|
+
controller_name, action_name, params = 'pages', 'edit', { }
|
64
|
+
result = RailsNavigation::Matcher.match_multiple_with_or?(controller_name, action_name, params, conditions)
|
65
|
+
expect(result).to be_truthy
|
66
|
+
controller_name, action_name, params = 'recipes', 'show', { }
|
67
|
+
result = RailsNavigation::Matcher.match_multiple_with_or?(controller_name, action_name, params, conditions)
|
68
|
+
expect(result).to be_truthy
|
69
|
+
controller_name, action_name, params = 'books', 'index', { }
|
70
|
+
result = RailsNavigation::Matcher.match_multiple_with_or?(controller_name, action_name, params, conditions)
|
71
|
+
expect(result).to be_falsey
|
72
|
+
end
|
67
73
|
end
|
68
74
|
end
|