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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: aae613c2e8f7248f1dafd41a9b6f18785b3ace6b
4
- data.tar.gz: b6a32d1b639ec4145de44cd67c55a1af829a16a5
3
+ metadata.gz: f58a4745dfc48238bd43a08be6e3b12f959ca556
4
+ data.tar.gz: d9b27e8707cc331fcce6f61fea6e62f819f50260
5
5
  SHA512:
6
- metadata.gz: f80224ce1656d606479c1d6ff61606b8148a4aad4b783981172b1825c704c74b1cb32f53153b3d8b0e13713e17d413631ad2b038323d9f619af3f7993b228e44
7
- data.tar.gz: 3938909fcae239fce32fb82bc1d0f6903995544d92870c3edb0a0fe935951ec9ca25cf26e45779a483aeccfbf679d0b98fbfb67d12e755e1cebc31f827a8f9fd
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. Did you ever hate the link_to_if helper? E.g.:
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", action: "new" } becomes '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
- ## Installation
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
- * Match multiple conditions
84
+ * support `not` operator
48
85
 
49
86
  ## Contributing
50
87
 
@@ -1,5 +1,9 @@
1
1
  module RailsNavigationHelper
2
- def nav_to(name, path, controller_names = nil)
3
- content_tag :li, link_to(name, path), class: ('active' if RailsNavigation::Matcher.new(controller_name, action_name, params).active?(controller_names))
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
- class Matcher
3
- def initialize(controller_name, action_name, params)
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 == @controller_name) : true
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 == @action_name) : true
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 == @params[key.to_sym] : result == @params[:id]
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 active?(str)
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
@@ -1,3 +1,3 @@
1
1
  module RailsNavigation
2
- VERSION = '0.0.1'
2
+ VERSION = '0.0.2'
3
3
  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
- matcher = RailsNavigation::Matcher.new(controller_name, action_name, params)
10
- expect(matcher.active?(condition)).to be_truthy
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
- matcher = RailsNavigation::Matcher.new(controller_name, action_name, params)
19
- expect(matcher.active?(condition)).to be_truthy
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
- matcher = RailsNavigation::Matcher.new(controller_name, action_name, params)
26
- expect(matcher.active?(condition)).to be_falsey
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
- matcher = RailsNavigation::Matcher.new(controller_name, action_name, params)
35
- expect(matcher.active?(condition)).to be_truthy
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
- matcher = RailsNavigation::Matcher.new(controller_name, action_name, params)
42
- expect(matcher.active?(condition)).to be_falsy
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
- matcher = RailsNavigation::Matcher.new(controller_name, action_name, params)
49
- expect(matcher.active?(condition)).to be_truthy
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
- matcher = RailsNavigation::Matcher.new(controller_name, action_name, params)
56
- expect(matcher.active?(condition)).to be_falsy
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
- # it 'matches one of multiple conditions' do
62
- # conditions = ['pages#edit', 'recipes#show']
63
- # controller_name, action_name, params = 'pages', 'edit', { }
64
- # matcher = RailsNavigation::Matcher.new(controller_name, action_name, params)
65
- # expect(matcher.active?(conditions)).to be_truthy
66
- # end
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_navigation
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sascha Brink