stateful_link 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +5 -2
- data/Gemfile.lock +2 -1
- data/README.rdoc +8 -6
- data/Rakefile +2 -2
- data/lib/stateful_link.rb +2 -1
- data/lib/stateful_link/action_any_of.rb +79 -0
- data/lib/stateful_link/helper.rb +2 -70
- data/lib/stateful_link/railtie.rb +2 -1
- data/spec/controllers/action_any_of_spec.rb +23 -0
- data/spec/helpers/stateful_link_spec.rb +13 -21
- data/spec/spec_helper.rb +1 -1
- metadata +102 -8
data/Gemfile
CHANGED
@@ -4,8 +4,11 @@ gem "rails", ">= 3"
|
|
4
4
|
gem "capybara", ">= 0.3.9"
|
5
5
|
gem "sqlite3-ruby", :require => "sqlite3"
|
6
6
|
|
7
|
-
|
8
|
-
gem
|
7
|
+
group :development, :test do
|
8
|
+
gem 'childprocess'
|
9
|
+
if RUBY_VERSION < '1.9'
|
10
|
+
gem "ruby-debug", ">= 0.10.3"
|
11
|
+
end
|
9
12
|
end
|
10
13
|
|
11
14
|
gem "rspec-rails", ">= 2.0.0.beta"
|
data/Gemfile.lock
CHANGED
data/README.rdoc
CHANGED
@@ -3,11 +3,13 @@
|
|
3
3
|
StatefulLink is a helper that simplifies displaying stateful navigation links. State
|
4
4
|
depends on current controller and action.
|
5
5
|
|
6
|
-
|
6
|
+
== Installation
|
7
7
|
|
8
|
-
|
8
|
+
Rails3 only Add the following line to your Gemfile:
|
9
9
|
|
10
|
-
|
10
|
+
gem 'stateful_link'
|
11
|
+
|
12
|
+
== Examples
|
11
13
|
|
12
14
|
Let we have RESTful PostsController and two navigation links in layout.
|
13
15
|
|
@@ -40,7 +42,7 @@ Solution:
|
|
40
42
|
stateful_link(
|
41
43
|
active,
|
42
44
|
chosen,
|
43
|
-
:active => proc { label
|
45
|
+
:active => proc { label },
|
44
46
|
:chosen => proc { content_tag :b, link_to(label, url) },
|
45
47
|
:inactive => proc { link_to label, url }
|
46
48
|
)
|
@@ -51,9 +53,9 @@ Solution:
|
|
51
53
|
|
52
54
|
<ul>
|
53
55
|
<li>
|
54
|
-
<%= navigation_link("Posts", "posts
|
56
|
+
<%= navigation_link("Posts", "posts#index", "posts#*", posts_url) %>
|
55
57
|
<ul>
|
56
|
-
<%= navigation_link("New post", ["posts
|
58
|
+
<%= navigation_link("New post", ["posts#new", "posts#create"], nil, new_post_url) %>
|
57
59
|
</ul>
|
58
60
|
</li>
|
59
61
|
</ul>
|
data/Rakefile
CHANGED
@@ -3,7 +3,7 @@ require 'rake'
|
|
3
3
|
require 'rspec/core'
|
4
4
|
require 'rspec/core/rake_task'
|
5
5
|
|
6
|
-
|
6
|
+
RSpec::Core::RakeTask.new(:spec)
|
7
7
|
task :default => :spec
|
8
8
|
|
9
9
|
begin
|
@@ -13,7 +13,7 @@ begin
|
|
13
13
|
gem.summary = "Helper to generate stateful navigation links."
|
14
14
|
gem.description = "Helper to generate stateful navigation links."
|
15
15
|
gem.files = FileList["[A-Z]*", "lib/**/*"]
|
16
|
-
gem.version = "0.0.
|
16
|
+
gem.version = "0.0.2"
|
17
17
|
gem.email = "gzigzigzeo@gmail.com"
|
18
18
|
gem.authors = ["Victor Sokolov"]
|
19
19
|
gem.homepage = "http://github.com/gzigzigzeo/stateful_link"
|
data/lib/stateful_link.rb
CHANGED
@@ -0,0 +1,79 @@
|
|
1
|
+
module StatefulLink
|
2
|
+
module ActionAnyOf#:doc:
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
helper_method :action_any_of?, :extract_controller_and_action, :action_state if respond_to?(:helper_method)
|
7
|
+
end
|
8
|
+
|
9
|
+
module InstanceMethods
|
10
|
+
# Returns true if current controller and action names equals to any of passed.
|
11
|
+
# Asterik as action name matches all controller's action.
|
12
|
+
#
|
13
|
+
# Examples:
|
14
|
+
# <%= "we are in PostsController::index" if action_any_of?("posts#index") %>
|
15
|
+
#
|
16
|
+
# <%= "we are not in PostsController::index" unless action_any_of?("posts#index") %>
|
17
|
+
#
|
18
|
+
# <% if action_any_of?("posts#index", "messages#index") %>
|
19
|
+
# we are in PostsController or in MessagesController
|
20
|
+
# <% end %>
|
21
|
+
#
|
22
|
+
def action_any_of?(*actions)
|
23
|
+
actions.any? do |sub_ca|
|
24
|
+
unless sub_ca.empty?
|
25
|
+
sub_controller, sub_action = extract_controller_and_action(sub_ca)
|
26
|
+
self.controller_path == sub_controller && (self.action_name == sub_action || (sub_action == '' || sub_action == '*'))
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
# Extracts controller and action names from a string.
|
32
|
+
#
|
33
|
+
# Examples:
|
34
|
+
#
|
35
|
+
# extract_controller_and_action("posts#index") # ["posts", "index"]
|
36
|
+
# extract_controller_and_action("admin/posts#index") # ["admin/posts", "index"]
|
37
|
+
# extract_controller_and_action("admin/posts#index") # raises ArgumentError
|
38
|
+
#
|
39
|
+
def extract_controller_and_action(ca)
|
40
|
+
raise ArgumentError, "Pass the string" if ca.nil?
|
41
|
+
slash_pos = ca.rindex('#')
|
42
|
+
raise ArgumentError, "Invalid action: #{ca}" if slash_pos.nil?
|
43
|
+
controller = ca[0, slash_pos]
|
44
|
+
action = ca[slash_pos+1..-1] || ""
|
45
|
+
raise ArgumentError, "Invalid action or controller" if controller.nil?
|
46
|
+
|
47
|
+
[controller, action]
|
48
|
+
end
|
49
|
+
|
50
|
+
# Returns link state related to current controller and action: :inactive, :active or :chosen.
|
51
|
+
# A list of active actions is the first argument, chosen actions are the second argument.
|
52
|
+
#
|
53
|
+
# Examples:
|
54
|
+
# # :active for PostsController::index, :chosen for PostsController::* (except :index),
|
55
|
+
# # :inactive otherwise.
|
56
|
+
# action_state("posts#index", "posts#")
|
57
|
+
#
|
58
|
+
# # :active for PostsController::new and PostsController::create, :inactive otherwise.
|
59
|
+
# action_state(["posts#new", "posts#create"])
|
60
|
+
#
|
61
|
+
# # :active for PostsController::index, :chosen for MessagesController::* and
|
62
|
+
# # PostsController::* (except :index), :inactive otherwise.
|
63
|
+
# action_state("posts#index", ["posts#", "messages#"])
|
64
|
+
#
|
65
|
+
def action_state(active, chosen = nil)
|
66
|
+
active = active.is_a?(String) ? [active] : active
|
67
|
+
chosen = chosen.is_a?(String) ? [chosen] : chosen
|
68
|
+
|
69
|
+
if action_any_of?(*active)
|
70
|
+
:active
|
71
|
+
elsif !chosen.nil? && action_any_of?(*chosen)
|
72
|
+
:chosen
|
73
|
+
else
|
74
|
+
:inactive
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
data/lib/stateful_link/helper.rb
CHANGED
@@ -1,73 +1,5 @@
|
|
1
1
|
module StatefulLink
|
2
|
-
module Helper#:doc:
|
3
|
-
# Returns true if current controller and action names equals to any of passed.
|
4
|
-
# Asterik as action name matches all controller's action.
|
5
|
-
#
|
6
|
-
# Examples:
|
7
|
-
# <%= "we are in PostsController::index" if action_any_of?("posts#index") %>
|
8
|
-
#
|
9
|
-
# <%= "we are not in PostsController::index" unless action_any_of?("posts#index") %>
|
10
|
-
#
|
11
|
-
# <% if action_any_of?("posts#index", "messages#index") %>
|
12
|
-
# we are in PostsController or in MessagesController
|
13
|
-
# <% end %>
|
14
|
-
#
|
15
|
-
def action_any_of?(*actions)
|
16
|
-
actions.any? do |sub_ca|
|
17
|
-
sub_controller, sub_action = extract_controller_and_action(sub_ca)
|
18
|
-
controller.controller_path == sub_controller && (controller.action_name == sub_action || sub_action == '')
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
# Returns link state related to current controller and action: :inactive, :active or :chosen.
|
23
|
-
# A list of active actions is the first argument, chosen actions are the second argument.
|
24
|
-
#
|
25
|
-
# Examples:
|
26
|
-
# # :active for PostsController::index, :chosen for PostsController::* (except :index),
|
27
|
-
# # :inactive otherwise.
|
28
|
-
# action_state("posts#index", "posts#")
|
29
|
-
#
|
30
|
-
# # :active for PostsController::new and PostsController::create, :inactive otherwise.
|
31
|
-
# action_state(["posts#new", "posts#create"])
|
32
|
-
#
|
33
|
-
# # :active for PostsController::index, :chosen for MessagesController::* and
|
34
|
-
# # PostsController::* (except :index), :inactive otherwise.
|
35
|
-
# action_state("posts#index", ["posts#", "messages#"])
|
36
|
-
#
|
37
|
-
def action_state(active, chosen = nil)
|
38
|
-
active = active.is_a?(String) ? [active] : active
|
39
|
-
chosen = chosen.is_a?(String) ? [chosen] : chosen
|
40
|
-
|
41
|
-
if action_any_of?(*active)
|
42
|
-
:active
|
43
|
-
else
|
44
|
-
if !chosen.nil? && action_any_of?(*chosen)
|
45
|
-
:chosen
|
46
|
-
else
|
47
|
-
:inactive
|
48
|
-
end
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
# Extracts controller and action names from a string.
|
53
|
-
#
|
54
|
-
# Examples:
|
55
|
-
#
|
56
|
-
# extract_controller_and_action("posts#index") # ["posts", "index"]
|
57
|
-
# extract_controller_and_action("admin/posts#index") # ["admin/posts", "index"]
|
58
|
-
# extract_controller_and_action("admin/posts#index") # raises ArgumentError
|
59
|
-
#
|
60
|
-
def extract_controller_and_action(ca)
|
61
|
-
raise ArgumentError, "Pass the string" if ca.nil?
|
62
|
-
slash_pos = ca.rindex('#')
|
63
|
-
raise ArgumentError, "Invalid action: #{ca}" if slash_pos.nil?
|
64
|
-
controller = ca[0, slash_pos]
|
65
|
-
action = ca[slash_pos+1..-1] || ""
|
66
|
-
raise ArgumentError, "Invalid action or controller" if controller.nil?
|
67
|
-
|
68
|
-
[controller, action]
|
69
|
-
end
|
70
|
-
|
2
|
+
module Helper#:doc:
|
71
3
|
# Generates stateful link to something.
|
72
4
|
#
|
73
5
|
# Options:
|
@@ -97,4 +29,4 @@ module StatefulLink
|
|
97
29
|
current.is_a?(Proc) ? instance_exec(¤t) : current
|
98
30
|
end
|
99
31
|
end
|
100
|
-
end
|
32
|
+
end
|
@@ -1,7 +1,8 @@
|
|
1
1
|
module StatefulLink
|
2
2
|
class Railtie < ::Rails::Railtie
|
3
3
|
config.to_prepare do
|
4
|
-
|
4
|
+
ActionController::Base.send(:include, StatefulLink::ActionAnyOf)
|
5
|
+
ActionController::Base.helper(StatefulLink::Helper)
|
5
6
|
end
|
6
7
|
end
|
7
8
|
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ApplicationController do
|
4
|
+
it "should extract controller and action" do
|
5
|
+
subject.extract_controller_and_action("friendships#index").should == ['friendships', 'index']
|
6
|
+
subject.extract_controller_and_action("friendships#").should == ['friendships', '']
|
7
|
+
end
|
8
|
+
|
9
|
+
it "action_any_of should work" do
|
10
|
+
get :index
|
11
|
+
subject.action_any_of?("bars#index").should be_false
|
12
|
+
subject.action_any_of?("application#index").should be_true
|
13
|
+
subject.action_any_of?("bars#index", "application#index").should be_true
|
14
|
+
end
|
15
|
+
|
16
|
+
it "action_state should work" do
|
17
|
+
get :index
|
18
|
+
subject.action_state("bars#index", "application#index").should == :chosen
|
19
|
+
subject.action_state("bars#index", ["application#index", "foobars#index"]).should == :chosen
|
20
|
+
subject.action_state("application#index", "tests#index").should == :active
|
21
|
+
subject.action_state("application#index").should == :active
|
22
|
+
end
|
23
|
+
end
|
@@ -2,28 +2,11 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe StatefulLink::Helper do
|
4
4
|
before(:each) do
|
5
|
+
ActionView::Base.send(:include, StatefulLink::ActionAnyOf)
|
5
6
|
controller.controller_path = 'foos'
|
6
|
-
controller.action_name = 'bar'
|
7
|
-
end
|
7
|
+
controller.action_name = 'bar'
|
8
|
+
end
|
8
9
|
|
9
|
-
it "should extract controller and action" do
|
10
|
-
helper.extract_controller_and_action("friendships#index").should == ['friendships', 'index']
|
11
|
-
helper.extract_controller_and_action("friendships#").should == ['friendships', '']
|
12
|
-
end
|
13
|
-
|
14
|
-
it "action_any_of should work" do
|
15
|
-
helper.action_any_of?("bars#index").should be_false
|
16
|
-
helper.action_any_of?("foos#bar").should be_true
|
17
|
-
helper.action_any_of?("bars#index", "foos#bar").should be_true
|
18
|
-
end
|
19
|
-
|
20
|
-
it "action_state should work" do
|
21
|
-
helper.action_state("bars#index", "foos#bar").should == :chosen
|
22
|
-
helper.action_state("bars#index", ["foos#", "foobars#index"]).should == :chosen
|
23
|
-
helper.action_state("foos#bar", "tests#index").should == :active
|
24
|
-
helper.action_state("foos#bar").should == :active
|
25
|
-
end
|
26
|
-
|
27
10
|
it "should generate stateful links" do
|
28
11
|
helper.stateful_link_to(
|
29
12
|
"foos#bar",
|
@@ -39,6 +22,15 @@ describe StatefulLink::Helper do
|
|
39
22
|
:inactive => "2",
|
40
23
|
:chosen => proc { root_path }
|
41
24
|
).should == root_path
|
25
|
+
|
26
|
+
# Check for controller#*
|
27
|
+
helper.stateful_link_to(
|
28
|
+
"bars#foo",
|
29
|
+
["foos#trala", "foos#*"],
|
30
|
+
:active => "1",
|
31
|
+
:inactive => "2",
|
32
|
+
:chosen => "3"
|
33
|
+
).should == "3"
|
42
34
|
|
43
35
|
helper.stateful_link_to(
|
44
36
|
:active => "1",
|
@@ -54,4 +46,4 @@ describe StatefulLink::Helper do
|
|
54
46
|
:state => proc { false }
|
55
47
|
).should == "2"
|
56
48
|
end
|
57
|
-
end
|
49
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -22,7 +22,7 @@ ActiveRecord::Migrator.migrate File.expand_path("../dummy/db/migrate/", __FILE__
|
|
22
22
|
# Load support files
|
23
23
|
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
|
24
24
|
|
25
|
-
|
25
|
+
RSpec.configure do |config|
|
26
26
|
# Remove this line if you don't want Rspec's should and should_not
|
27
27
|
# methods or matchers
|
28
28
|
require 'rspec/expectations'
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: stateful_link
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 2
|
10
|
+
version: 0.0.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Victor Sokolov
|
@@ -15,13 +15,103 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date:
|
18
|
+
date: 2011-01-21 00:00:00 +03:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: 5
|
29
|
+
segments:
|
30
|
+
- 3
|
31
|
+
version: "3"
|
22
32
|
name: rails
|
33
|
+
requirement: *id001
|
34
|
+
type: :runtime
|
35
|
+
- !ruby/object:Gem::Dependency
|
23
36
|
prerelease: false
|
24
|
-
|
37
|
+
version_requirements: &id002 !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
hash: 1
|
43
|
+
segments:
|
44
|
+
- 0
|
45
|
+
- 3
|
46
|
+
- 9
|
47
|
+
version: 0.3.9
|
48
|
+
name: capybara
|
49
|
+
requirement: *id002
|
50
|
+
type: :runtime
|
51
|
+
- !ruby/object:Gem::Dependency
|
52
|
+
prerelease: false
|
53
|
+
version_requirements: &id003 !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
hash: 3
|
59
|
+
segments:
|
60
|
+
- 0
|
61
|
+
version: "0"
|
62
|
+
name: sqlite3-ruby
|
63
|
+
requirement: *id003
|
64
|
+
type: :runtime
|
65
|
+
- !ruby/object:Gem::Dependency
|
66
|
+
prerelease: false
|
67
|
+
version_requirements: &id004 !ruby/object:Gem::Requirement
|
68
|
+
none: false
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
hash: 31098209
|
73
|
+
segments:
|
74
|
+
- 2
|
75
|
+
- 0
|
76
|
+
- 0
|
77
|
+
- beta
|
78
|
+
version: 2.0.0.beta
|
79
|
+
name: rspec-rails
|
80
|
+
requirement: *id004
|
81
|
+
type: :runtime
|
82
|
+
- !ruby/object:Gem::Dependency
|
83
|
+
prerelease: false
|
84
|
+
version_requirements: &id005 !ruby/object:Gem::Requirement
|
85
|
+
none: false
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
hash: 3
|
90
|
+
segments:
|
91
|
+
- 0
|
92
|
+
version: "0"
|
93
|
+
name: childprocess
|
94
|
+
requirement: *id005
|
95
|
+
type: :development
|
96
|
+
- !ruby/object:Gem::Dependency
|
97
|
+
prerelease: false
|
98
|
+
version_requirements: &id006 !ruby/object:Gem::Requirement
|
99
|
+
none: false
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
hash: 49
|
104
|
+
segments:
|
105
|
+
- 0
|
106
|
+
- 10
|
107
|
+
- 3
|
108
|
+
version: 0.10.3
|
109
|
+
name: ruby-debug
|
110
|
+
requirement: *id006
|
111
|
+
type: :development
|
112
|
+
- !ruby/object:Gem::Dependency
|
113
|
+
prerelease: false
|
114
|
+
version_requirements: &id007 !ruby/object:Gem::Requirement
|
25
115
|
none: false
|
26
116
|
requirements:
|
27
117
|
- - ">="
|
@@ -32,8 +122,9 @@ dependencies:
|
|
32
122
|
- 0
|
33
123
|
- 0
|
34
124
|
version: 3.0.0
|
125
|
+
name: rails
|
126
|
+
requirement: *id007
|
35
127
|
type: :runtime
|
36
|
-
version_requirements: *id001
|
37
128
|
description: Helper to generate stateful navigation links.
|
38
129
|
email: gzigzigzeo@gmail.com
|
39
130
|
executables: []
|
@@ -49,8 +140,10 @@ files:
|
|
49
140
|
- README.rdoc
|
50
141
|
- Rakefile
|
51
142
|
- lib/stateful_link.rb
|
143
|
+
- lib/stateful_link/action_any_of.rb
|
52
144
|
- lib/stateful_link/helper.rb
|
53
145
|
- lib/stateful_link/railtie.rb
|
146
|
+
- spec/controllers/action_any_of_spec.rb
|
54
147
|
- spec/dummy/app/controllers/application_controller.rb
|
55
148
|
- spec/dummy/app/helpers/application_helper.rb
|
56
149
|
- spec/dummy/config/application.rb
|
@@ -72,8 +165,8 @@ homepage: http://github.com/gzigzigzeo/stateful_link
|
|
72
165
|
licenses: []
|
73
166
|
|
74
167
|
post_install_message:
|
75
|
-
rdoc_options:
|
76
|
-
|
168
|
+
rdoc_options: []
|
169
|
+
|
77
170
|
require_paths:
|
78
171
|
- lib
|
79
172
|
required_ruby_version: !ruby/object:Gem::Requirement
|
@@ -102,6 +195,7 @@ signing_key:
|
|
102
195
|
specification_version: 3
|
103
196
|
summary: Helper to generate stateful navigation links.
|
104
197
|
test_files:
|
198
|
+
- spec/controllers/action_any_of_spec.rb
|
105
199
|
- spec/dummy/app/controllers/application_controller.rb
|
106
200
|
- spec/dummy/app/helpers/application_helper.rb
|
107
201
|
- spec/dummy/config/application.rb
|