link2 0.1.6 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -19,7 +19,7 @@ This is *not* a re-implementation of these helpers; rather it wraps these but pa
19
19
  * *DRY* - based on specified arguments: Use what's know to make smart assumptions for more readable and maintainable view code.
20
20
  * *I18n deluxe* - Lookup scoped translations based on action, model, etc., for more flexible translations - with lean defaults. Enhanced with some nifty interpolation features as well. Code first, translate later.
21
21
  * *DOM Selectors* - Why defining DOM classes for RESTful links all the time when same patterns occur? Optionally DONE.
22
- * *Stable* - Full test-coverage for stability. Unit + Rails integration tests, check: @54 tests: all passed@
22
+ * *Stable* - Full test-coverage for stability. Unit + Rails integration tests, check: @59 tests: all passed@
23
23
  * *Maintainable* - Well-documented code.
24
24
 
25
25
  h2. Installation
@@ -116,18 +116,11 @@ h2. Usage
116
116
  ...
117
117
  </pre>
118
118
 
119
- ...and for the record (not yet implemented, but soon):
119
+ Same works for @button@, and you also can optionally use the branded aliases: @link2@ and @button2@.
120
120
 
121
- <pre>
122
- link :edit, [@post, @comment]
123
- # => ::Link2::NotImplementedYetError, "case link(:action, [...]) not yet supported. Need to refactor some stuff."
124
- </pre>
125
-
126
- Same works for @button_to@, and you also can optionally use the branded aliases: @link2@ and @button2@.
121
+ h2. URL + HTML Options
127
122
 
128
- h2. Options hashes: URL + HTML Options
129
-
130
- Link2 link helpers accept options in the same way as the core helpers @link_to@/@button_to@: first @options@ (a.k.a. @url_options@) and then @html_options@. See the "Rails core UrlHelpers documentation":http://railsapi.com/doc/rails-v2.3.5/classes/ActionView/Helpers/UrlHelper.html#M002452 for details on this. Link2 helpers just pass any non-Link2-related options to the Rails core helpers. In other words no need to learn a new API; just pass the needed options like in the past.
123
+ Link2 link helpers accept options in the same way as the core helpers @link_to@ and @button_to@: first @options@ (a.k.a. @url_options@) and then @html_options@. See the "Rails core UrlHelpers documentation":http://railsapi.com/doc/rails-v2.3.5/classes/ActionView/Helpers/UrlHelper.html#M002452 for details on this. Link2 helpers just pass any non-Link2-related options to the Rails core helpers. In other words no need to learn a new API; just pass the needed options like in the past.
131
124
 
132
125
  h2. Expected arguments (...but the examples should be enough)
133
126
 
@@ -248,13 +241,49 @@ This behavior is enabled by default but can be disabled just in case; preferably
248
241
  end
249
242
  </pre>
250
243
 
251
- h2. TODO
244
+ h2. The other fellows...
252
245
 
253
- See "TODO":http://github.com/grimen/link2/blob/master/TODO
246
+ *@link_to_function@*
247
+
248
+ To get the Link2 features for @link_to_function@ - which is *deprecated in Rails 3* because of it's obtrusive nature (note: obtrusive should be avoided) - you can get such behaviour with Link2 (if you must) by specifying @:onclick@ HTML attribute like so:
249
+
250
+ <pre>
251
+ link :hello, :onclick => "alert('Hello world!')"
252
+ </pre>
253
+
254
+ *@link_to_remote@*
255
+
256
+ No Link2-wrapper for this one. Also, it's *deprecated in Rails 3* as well for same reasons as above. In Rails 3 it should be possible to call it like so:
257
+
258
+ <pre>
259
+ link :hello, say_hello_path(@world), :remote => true
260
+ </pre>
254
261
 
255
- h2. A note on design decisions
262
+ ...but as Link2 is *not tested with Rails 3* yet; use the old-school @link_to@ for now.
256
263
 
257
- I didn't extend the behavior for the helper @link_to_if@, @link_to_unless@ because I simply think they should not be used in Rails apps; my strong opinion is that they introduce unnecessary complexity in code and make it less readable. I also skipped @link_to_if_current@ because it's not very thorough implementation - even an extra URI slash make it confused, which is funny. Hate it, or LOVE IT.
264
+ *@link_to_if@ + @link_to_unless@*
265
+
266
+ I didn't extend the behavior for the helpers @link_to_if@ and @link_to_unless@ because I simply think they should not be used in Rails apps; my strong opinion is that they introduce unnecessary complexity in code and make it less readable. I also skipped @link_to_if_current@ because it's not very thorough implementation - even an extra URI slash make it confused, which is funny. Hate it, or LOVE IT.
267
+
268
+ h2. Inspiration
269
+
270
+ I really like the "declarative_authorization":http://github.com/stffn/declarative_authorization view helper DSL for checking permissions on models, and brought the pattern to the link helpers - they are like *ying & yang*:
271
+
272
+ <pre>
273
+ link(:edit, @account) if permitted_to?(:edit, @account)
274
+ </pre>
275
+
276
+ ...but I usually mix it with an alias:
277
+
278
+ <pre>
279
+ link(:edit, @account) if can?(:edit, @account)
280
+ </pre>
281
+
282
+ Nice, huh? =)
283
+
284
+ h2. TODO
285
+
286
+ See "TODO":http://github.com/grimen/link2/blob/master/TODO
258
287
 
259
288
  h2. License
260
289
 
data/Rakefile CHANGED
@@ -13,11 +13,11 @@ require File.join(File.dirname(__FILE__), 'lib', 'link2', 'version')
13
13
  # rake build
14
14
  # sudo rake install
15
15
  #
16
- # == Git tag & push to origin/master
16
+ # == Git tag & push to origin/master and push gem to Rubygems.org:
17
17
  #
18
18
  # rake release
19
19
  #
20
- # == Release to Gemcutter.org:
20
+ # == Release to Rubygems.org:
21
21
  #
22
22
  # rake gemcutter:release
23
23
  #
data/TODO CHANGED
@@ -1,26 +1,27 @@
1
1
  TODO
2
2
 
3
- 1) [ISSUE:] Implement the problematic one: link(:action, [@parent, @resource])
3
+ * [FEATURE:] Fix action mapping procs to handle named scopes. Should work, but do not for some reason. =/
4
4
 
5
- * Fix action mapping procs to handle named scopes. Should work, but do not for some reason. =/
5
+ * [FEATURE:] Fix action mapping procs to receive session, params, etc in a options hash. My mocking/stubbing don't want to work. ='(
6
6
 
7
- * Fix action mapping procs to receive session, params, etc in a options hash. My mocking/stubbing don't want to work. ='(
7
+ * [TEST:] Integration tests for namespaced controllers/models
8
8
 
9
- * Add support: #link_to_function + #button_to_function: I18n, more?
9
+ * [DESIGN:] Link2::Brain#link_to_args MUST be refactored when the DSL is 100% settled. Not far from it, but feedback first.
10
10
 
11
- js_link(:hello, "alert('Clicked :hello.');") => link_to_function(I18n.t(:hello, ...), "alert('Clicked :hello.');")
11
+ * [DESIGN:] Merge #label_and_url_for_resource and #url_for_args
12
12
 
13
- * Add support: #link_to_remote + #button_to_remote: I18n, more? Could probably be cleaned up quite a lot.
13
+ * [TEST:] ORM-tests: DataMapper, MongoMapper
14
14
 
15
- ajax_link(...)
15
+ * [COMPAT:] Check Rails 3 compatibility.
16
16
 
17
- * ORM-tests: DataMapper, MongoMapper
17
+ * [ENHANCEMENT:] Check with Rails routes (RouteSet) if current action is a :member or :collection:
18
18
 
19
- * Check Rails 3 compatibility.
19
+ link(:show) => link(:show, @post)
20
+ link(:index) => link(:index, Post)
20
21
 
21
- * Link2::Brain#link_to_args MUST be refactored when the DSL is 100% settled. Not far from it, but feedback first.
22
+ NOT SURE:
22
23
 
23
- * Integration tests for namespaced controllers/models
24
+ * Make Link2 parse label and title-attribute for the link based on configuration: Link2.label_method = :to_s, Link2.attr_methods = {:title => :description}
24
25
 
25
26
  * This works:
26
27
  link_to x, :terms => link_to x, terms_path
@@ -30,15 +31,8 @@ TODO
30
31
 
31
32
  The middle part (auto-detect route for :fraggle before resource :fraggle) maybe too abstract DSL, not sure.
32
33
 
34
+ * Add support for: #link_to_remote + #button_to_remote in Rails 2.x, with:
33
35
 
34
- NOT SURE:
35
-
36
- * Check with Rails routes (RouteSet) if current action is a :member or :collection, and look for resource vs. collection:
36
+ link(..., :remote => true)
37
37
 
38
- link(:show) => link(@post) => link(:show, @post) => link("Show", @posts)
39
- link(:index) => link(@posts) => link(:index, @posts) => link("Index", @posts)
40
-
41
- Current implementation is a bit dumber but should work okay: resource_or_collection = @post || @posts, where
42
- @post is tried first. Maybe should be developers responsibility like now...
43
-
44
- * Make Link2 parse label and title-attribute for the link based on configuration: Link2.label_method = :to_s, Link2.attr_methods = {:title => :description}
38
+ ...or not: Maybe only support in Rails 3.
@@ -3,7 +3,8 @@
3
3
  module Link2
4
4
  module Brain
5
5
 
6
- AutoDetectionFailed = Class.new(::NotImplementedError)
6
+ AutoDetectionFailed = Class.new(::ArgumentError)
7
+ NilArgument = Class.new(::ArgumentError)
7
8
 
8
9
  URL_PATH_REGEX = /\//
9
10
  CLASS_INSTANCE_STRING = /\#\<.*\:0x.*\>/
@@ -29,6 +30,8 @@ module Link2
29
30
  html_options = args.pop if args.last.is_a?(Hash)
30
31
  url_options = args.pop if args.last.is_a?(Hash)
31
32
 
33
+ raise NilArgument, "Passed argument is nil: #{args.inspect}." if args.any? { |arg| arg.nil? }
34
+
32
35
  case args.size
33
36
  when 0
34
37
  raise ArgumentError, "No arguments specified. A least specify action or url."
@@ -46,8 +49,8 @@ module Link2
46
49
  # link :new => link_to I18n.t(:new, ...), new_{auto_detected_resource}_path
47
50
  # link :back => link_to I18n.t(:back, ...), (session[:return_to] || :back)
48
51
  action = args.shift
49
- resource = nil # TODO: auto-detect resource.
50
- label = self.localized_label(action, resource, url_options)
52
+ label = self.localized_label(action, resource = nil, url_options)
53
+ resource = false if html_options && html_options.key?(:onclick) # false => do not auto-detect
51
54
  url = self.url_for_args(action, resource, url_options)
52
55
  elsif args.first.is_a?(Object)
53
56
  # link @user => link_to I18n.t(:show, ...), user_path(@user)
@@ -66,36 +69,26 @@ module Link2
66
69
  # link "Start", :home => link_to I18n.t(:start, ...), root_path
67
70
  # link "Cancel", :back => link_to I18n.t(:cancel, ...), :back
68
71
  label, action = args.slice!(0..1)
69
- resource = nil
70
- url = self.url_for_args(action, resource, url_options)
72
+ url = self.url_for_args(action, resource = nil, url_options)
71
73
  elsif ::Link2::Support.resource_identifier_class?(args.second)
72
74
  # link "New", :new => link_to "New", new_{auto_detected_resource}_path
73
75
  # link "<<", :back => link_to "<<", (session[:return_to] || :back)
74
76
  label, action = args.slice!(0..1)
75
- resource = nil # TODO: auto-detect resource.
76
- url = self.url_for_args(action, resource, url_options)
77
+ url = self.url_for_args(action, resource = nil, url_options)
77
78
  else
78
79
  raise ArgumentError, "Invalid 2nd argument: #{args.inspect}"
79
80
  end
80
81
  elsif args.first.is_a?(Symbol)
81
- # TODO: Implement support for aray of nested resources.
82
- if args.second.is_a?(Array)
83
- raise ::Link2::NotImplementedYetError, "case link(:action, [...]) not yet supported. Need to refactor some stuff."
84
- end
85
-
86
82
  if args.second.is_a?(String)
87
83
  # link :new, new_post_path => link_to I18n.t(:new, ...), new_post_path
88
84
  # link :back, root_path => link_to I18n.t(:back, ...), (session[:return_to] || :back)
89
85
  action, url = args.slice!(0..1)
90
- resource = nil
91
- label = self.localized_label(action, resource, url_options)
86
+ label = self.localized_label(action, resource = nil, url_options)
92
87
  elsif args.second.is_a?(Symbol) && ::Link2.url_for_mapping(args.second)
93
88
  # link :start, :home => link_to I18n.t(:start, ...), root_path
94
89
  # link :cancel, :back => link_to I18n.t(:cancel, ...), :back
95
90
  key, action = args.slice!(0..1)
96
- resource = nil
97
- resource = self.auto_detect_resource || self.auto_detect_resource
98
- label = self.localized_label(key, resource, url_options)
91
+ label = self.localized_label(key, resource = nil, url_options)
99
92
  url = self.url_for_args(action, resource, url_options)
100
93
  elsif ::Link2::Support.resource_identifier_class?(args.second)
101
94
  # link :new, Post => link_to I18n.t(:new, ...), new_post_path
@@ -104,6 +97,11 @@ module Link2
104
97
  action, resource = args.slice!(0..1)
105
98
  label = self.localized_label(action, resource, url_options)
106
99
  url = self.url_for_args(action, resource, url_options)
100
+ elsif args.second.is_a?(Array)
101
+ # link :kick, [:admin, @user] => link_to I18n.t(:show, ...), admin_user_path(@user)
102
+ action, resource = args.slice!(0..1)
103
+ url_options_with_action = url_options.present? ? url_options.merge(:action => action) : {:action => action}
104
+ label, url = self.label_and_url_for_resource(resource, url_options_with_action)
107
105
  else
108
106
  raise ArgumentError, "Invalid 2nd argument: #{args.inspect}"
109
107
  end
@@ -113,16 +111,16 @@ module Link2
113
111
  when 3
114
112
  if args.first.is_a?(String)
115
113
  if args.second.is_a?(Symbol)
116
- # TODO: Implement support for aray of nested resources.
117
- if args.third.is_a?(Array)
118
- raise ::Link2::NotImplementedYetError, 'case link("Label", :action, [...]) not yet supported. Need to refactor some stuff.'
119
- end
120
-
121
114
  if ::Link2::Support.resource_identifier_class?(args.third)
122
115
  # link "New", :new, Post => link_to "New", new_post_path
123
116
  # link "Edit", :edit, @post => link_to "Edit", edit_post_path(@post)
124
117
  label, action, resource = args.slice!(0..2)
125
118
  url = self.url_for_args(action, resource, url_options)
119
+ elsif args.third.is_a?(Array)
120
+ # link "Kick", :kick, [:admin, @user] => link_to I18n.t(:show, ...), admin_user_path(@user)
121
+ label, action, resource = args.slice!(0..2)
122
+ url_options_with_action = url_options.present? ? url_options.merge(:action => action) : {:action => action}
123
+ url = self.label_and_url_for_resource(resource, url_options_with_action).last
126
124
  else
127
125
  raise ArgumentError, "Invalid 3rd argument: #{args.inspect}"
128
126
  end
@@ -164,13 +162,18 @@ module Link2
164
162
  #
165
163
  # See documentation on +polymorphic_url+ for available core options.
166
164
  #
167
- def label_and_url_for_resource(resource, options = {})
168
- options ||= {}
165
+ def label_and_url_for_resource(*args)
166
+ options = args.extract_options!.dup
167
+ resource = args.first
168
+
169
169
  url_for_options = options.slice(*POLYMORPHIC_OPTION_KEYS).reverse_merge(:routing_type => :path)
170
170
  i18n_options = options.except(url_for_options.keys)
171
+
171
172
  last_resource = ::Link2::Support.extract_resource(resource)
173
+ action = options.delete(:action)
174
+ url_for_options[:action] = [:show, :index].include?(action) ? nil : action
172
175
 
173
- label = self.localized_label(:show, last_resource, i18n_options)
176
+ label = self.localized_label(action || :show, last_resource, i18n_options)
174
177
  url = polymorphic_url(resource, url_for_options)
175
178
 
176
179
  [label, url]
@@ -200,7 +203,9 @@ module Link2
200
203
  options = args.extract_options!.dup
201
204
  action, resource = args
202
205
 
203
- if resource.is_a?(String) # url
206
+ if resource == false # javascript onclick; disable resource auto-detection
207
+ ::Link2::DEFAULT_LINK
208
+ elsif resource.is_a?(String) # url
204
209
  resource
205
210
  elsif resource.nil? && url = ::Link2.url_for_mapping(action, resource) # mapping
206
211
  url
@@ -5,10 +5,6 @@ module Link2
5
5
 
6
6
  include ::Link2::Brain
7
7
 
8
- def self.included(base)
9
- include JavascriptLinkHelpers
10
- end
11
-
12
8
  # Enhanced +link_to+ helper.
13
9
  #
14
10
  # TODO: Documentation for this helper. For now the README should be sufficient.
@@ -31,27 +27,5 @@ module Link2
31
27
  end
32
28
  alias :button2 :button
33
29
 
34
- # Rails 3-deprecations - unless +prototype_legacy_helper+-plugin.
35
-
36
- module JavascriptLinkHelpers
37
- def js_link(*args)
38
- raise ::Link2::NotImplementedYetError
39
- end
40
-
41
- def js_button(*args)
42
- raise ::Link2::NotImplementedYetError
43
- end
44
-
45
- def ajax_link(*args)
46
- raise ::Link2::NotImplementedYetError
47
- end
48
- alias :remote_link :ajax_link
49
-
50
- def ajax_button(*args)
51
- raise ::Link2::NotImplementedYetError
52
- end
53
- alias :remote_button :ajax_button
54
- end
55
-
56
30
  end
57
31
  end
@@ -71,7 +71,7 @@ module Link2
71
71
  # # => true
72
72
  #
73
73
  def resource_identifier_class?(object)
74
- (object.is_a?(NilClass) || object.is_a?(Symbol) || self.record_class?(object))
74
+ (object.is_a?(Symbol) || self.record_class?(object))
75
75
  end
76
76
 
77
77
  # Check if passed object is an array for record instances, i.e. "collection".
@@ -1,5 +1,5 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  module Link2
4
- VERSION = '0.1.6'.freeze
4
+ VERSION = '0.1.7'.freeze
5
5
  end
@@ -35,6 +35,16 @@ class HelpersTest < ActionView::TestCase
35
35
  assert_equal link_to('Hello', '#'), link('Hello')
36
36
  end
37
37
 
38
+ test "link(label_or_action, :onclick => '...') should render link_to_function(label, 'javascript: ...')" do
39
+ if self.respond_to?(:link_to_function) # Note: Deprecated in Rails 3
40
+ assert_equal link_to_function('Hello', 'alert("hello")'), link('Hello', :onclick => 'alert("hello"); return false;')
41
+ assert_equal link_to_function('Hello', 'alert("hello")'), link(:hello, :onclick => 'alert("hello"); return false;')
42
+ end
43
+
44
+ assert_equal link_to('Hello', '#', :onclick => 'alert("hello"); return false;'), link('Hello', :onclick => 'alert("hello"); return false;')
45
+ assert_equal link_to('Hello', '#', :onclick => 'alert("hello"); return false;'), link(:hello, :onclick => 'alert("hello"); return false;')
46
+ end
47
+
38
48
  test "auto-detecting resource: link(:action) should render link_to(t(:action, ...), @resource)" do
39
49
  self.expects(:current_controller_name).with(nil).returns('fraggles').at_least_once
40
50
 
@@ -90,7 +100,7 @@ class HelpersTest < ActionView::TestCase
90
100
  # assert_equal link_to("Show", "?"), link(::Fraggle) # test this stupid case?
91
101
  end
92
102
 
93
- test "link([@parent, @resource]) should render link_to(t(:show, ...), polymorphic_path([@parent, @resource]))" do
103
+ test "link([@parent_resource, @resource]) should render link_to(t(:show, ...), polymorphic_path([@parent_resource, @resource]))" do
94
104
  assert_equal link_to("Show", "/fraggles/#{@mookey.id}/cool_aids/#{@mookeys_cool_aid.id}"), link([@mookey, @mookeys_cool_aid])
95
105
  assert_equal link_to("Show", "/fraggles/#{@mookey.id}/cool_aids/#{@mookeys_cool_aid.id}", ONE_HASH), link([@mookey, @mookeys_cool_aid], ONE_HASH)
96
106
  assert_equal link_to("Show", "/fraggles/#{@mookey.id}/cool_aids/#{@mookeys_cool_aid.id}", *TWO_HASHES), link([@mookey, @mookeys_cool_aid], *TWO_HASHES)
@@ -159,11 +169,37 @@ class HelpersTest < ActionView::TestCase
159
169
  assert_equal link_to("Edit", "/fraggles/#{@mookey.id}/edit", *TWO_HASHES), link(:edit, @mookey, *TWO_HASHES)
160
170
  end
161
171
 
162
- test "link(:action, [@parent, @resource]) should render link_to(t(:action, ...), polymorphic_path([@parent, @resource]), :action => :action)" do
163
- # assert_equal link_to("Edit", "/fraggles/#{@mookey.id}/cool_aids/#{@mookeys_cool_aid.id}/edit"), link(:edit, [@mookey, @mookeys_cool_aid])
164
- assert_raise(::Link2::NotImplementedYetError) { link(:edit, [@mookey, @mookeys_cool_aid]) }
165
- assert_raise(::Link2::NotImplementedYetError) { link(:edit, [@mookey, @mookeys_cool_aid], ONE_HASH) }
166
- assert_raise(::Link2::NotImplementedYetError) { link(:edit, [@mookey, @mookeys_cool_aid], *TWO_HASHES) }
172
+ test "should accept polymorphic resource array for valid actions" do
173
+ [:edit, :show, :kick, :index].each do |action|
174
+ assert_nothing_raised { link(action, [@mookey, @mookeys_cool_aid]) }
175
+ assert_nothing_raised { link(action, [@mookey, @mookeys_cool_aid], ONE_HASH) }
176
+ assert_nothing_raised { link(action, [@mookey, @mookeys_cool_aid], *TWO_HASHES) }
177
+ end
178
+ end
179
+
180
+ test "should raise the traditional error if generated route don't exists" do
181
+ assert_raise(NoMethodError) { link(:hug, [@mookey, @mookeys_cool_aid]) }
182
+ assert_raise(NoMethodError) { link(:hug, [@mookey, @mookeys_cool_aid], ONE_HASH) }
183
+ assert_raise(NoMethodError) { link(:hug, [@mookey, @mookeys_cool_aid], *TWO_HASHES) }
184
+ end
185
+
186
+ test "link(:action, [@a, @b]) should render link_to(t(:action, ...), polymorphic_path([@a, @b]), :action => :action)" do
187
+ assert_equal link_to("Edit", "/fraggles/#{@mookey.id}/cool_aids/#{@mookeys_cool_aid.id}/edit"), link(:edit, [@mookey, @mookeys_cool_aid])
188
+ assert_equal link_to("Edit", "/fraggles/#{@mookey.id}/cool_aids/#{@mookeys_cool_aid.id}/edit", ONE_HASH), link(:edit, [@mookey, @mookeys_cool_aid], ONE_HASH)
189
+ assert_equal link_to("Edit", "/fraggles/#{@mookey.id}/cool_aids/#{@mookeys_cool_aid.id}/edit", *TWO_HASHES), link(:edit, [@mookey, @mookeys_cool_aid], *TWO_HASHES)
190
+
191
+ assert_equal link_to("Show", "/fraggles/#{@mookey.id}/cool_aids/#{@mookeys_cool_aid.id}"), link(:show, [@mookey, @mookeys_cool_aid])
192
+ assert_equal link_to("Show", "/fraggles/#{@mookey.id}/cool_aids/#{@mookeys_cool_aid.id}", ONE_HASH), link(:show, [@mookey, @mookeys_cool_aid], ONE_HASH)
193
+ assert_equal link_to("Show", "/fraggles/#{@mookey.id}/cool_aids/#{@mookeys_cool_aid.id}", *TWO_HASHES), link(:show, [@mookey, @mookeys_cool_aid], *TWO_HASHES)
194
+
195
+ assert_equal link_to("Kick", "/fraggles/#{@mookey.id}/cool_aids/#{@mookeys_cool_aid.id}/kick"), link(:kick, [@mookey, @mookeys_cool_aid])
196
+ assert_equal link_to("Kick", "/fraggles/#{@mookey.id}/cool_aids/#{@mookeys_cool_aid.id}/kick", ONE_HASH), link(:kick, [@mookey, @mookeys_cool_aid], ONE_HASH)
197
+ assert_equal link_to("Kick", "/fraggles/#{@mookey.id}/cool_aids/#{@mookeys_cool_aid.id}/kick", *TWO_HASHES), link(:kick, [@mookey, @mookeys_cool_aid], *TWO_HASHES)
198
+
199
+ # QUESTION: Hum...should be a case?
200
+ # assert_equal link_to("Index", "/fraggles/#{@mookey.id}/cool_aids"), link(:index, [@mookey, @mookeys_cool_aid])
201
+ # assert_equal link_to("Index", "/fraggles/#{@mookey.id}/cool_aids", ONE_HASH), link(:index, [@mookey, @mookeys_cool_aid], ONE_HASH)
202
+ # assert_equal link_to("Index", "/fraggles/#{@mookey.id}/cool_aids", *TWO_HASHES), link(:index, [@mookey, @mookeys_cool_aid], *TWO_HASHES)
167
203
  end
168
204
 
169
205
  test "auto-detecting resource: link(label, :action) should render link_to(label, @resource, :action => :action)" do
@@ -214,14 +250,56 @@ class HelpersTest < ActionView::TestCase
214
250
  assert_equal link_to("Editish", "/fraggles/#{@mookey.id}/edit", *TWO_HASHES), link("Editish", :edit, @mookey, *TWO_HASHES)
215
251
  end
216
252
 
217
- test "js_link should not be implemented (yet)" do
218
- assert_raise(::Link2::NotImplementedYetError) { js_link(:alert, 'alert("New");', {}, {}) }
219
- assert_raise(::Link2::NotImplementedYetError) { js_button(:alert, 'alert("New");', {}, {}) }
253
+ test "three arguments: should accept polymorphic resource array for valid actions" do
254
+ [:edit, :show, :kick, :index].each do |action|
255
+ assert_nothing_raised { link("", action, [@mookey, @mookeys_cool_aid]) }
256
+ assert_nothing_raised { link("", action, [@mookey, @mookeys_cool_aid], ONE_HASH) }
257
+ assert_nothing_raised { link("", action, [@mookey, @mookeys_cool_aid], *TWO_HASHES) }
258
+ end
220
259
  end
221
260
 
222
- test "ajax_link should not be implemented (yet)" do
223
- assert_raise(::Link2::NotImplementedYetError) { ajax_link(:home, {}, {}) }
224
- assert_raise(::Link2::NotImplementedYetError) { ajax_button(:home, {}, {}) }
261
+ test "three arguments: should raise the traditional error if generated route don't exists" do
262
+ assert_raise(NoMethodError) { link("", :hug, [@mookey, @mookeys_cool_aid]) }
263
+ assert_raise(NoMethodError) { link("", :hug, [@mookey, @mookeys_cool_aid], ONE_HASH) }
264
+ assert_raise(NoMethodError) { link("", :hug, [@mookey, @mookeys_cool_aid], *TWO_HASHES) }
265
+ end
266
+
267
+ test "three arguments: link(:action, [@a, @b]) should render link_to(t(:action, ...), polymorphic_path([@a, @b]), :action => :action)" do
268
+ assert_equal link_to("", "/fraggles/#{@mookey.id}/cool_aids/#{@mookeys_cool_aid.id}/edit"), link("", :edit, [@mookey, @mookeys_cool_aid])
269
+ assert_equal link_to("", "/fraggles/#{@mookey.id}/cool_aids/#{@mookeys_cool_aid.id}/edit", ONE_HASH), link("", :edit, [@mookey, @mookeys_cool_aid], ONE_HASH)
270
+ assert_equal link_to("", "/fraggles/#{@mookey.id}/cool_aids/#{@mookeys_cool_aid.id}/edit", *TWO_HASHES), link("", :edit, [@mookey, @mookeys_cool_aid], *TWO_HASHES)
271
+
272
+ assert_equal link_to("", "/fraggles/#{@mookey.id}/cool_aids/#{@mookeys_cool_aid.id}"), link("", :show, [@mookey, @mookeys_cool_aid])
273
+ assert_equal link_to("", "/fraggles/#{@mookey.id}/cool_aids/#{@mookeys_cool_aid.id}", ONE_HASH), link("", :show, [@mookey, @mookeys_cool_aid], ONE_HASH)
274
+ assert_equal link_to("", "/fraggles/#{@mookey.id}/cool_aids/#{@mookeys_cool_aid.id}", *TWO_HASHES), link("", :show, [@mookey, @mookeys_cool_aid], *TWO_HASHES)
275
+
276
+ assert_equal link_to("", "/fraggles/#{@mookey.id}/cool_aids/#{@mookeys_cool_aid.id}/kick"), link("", :kick, [@mookey, @mookeys_cool_aid])
277
+ assert_equal link_to("", "/fraggles/#{@mookey.id}/cool_aids/#{@mookeys_cool_aid.id}/kick", ONE_HASH), link("", :kick, [@mookey, @mookeys_cool_aid], ONE_HASH)
278
+ assert_equal link_to("", "/fraggles/#{@mookey.id}/cool_aids/#{@mookeys_cool_aid.id}/kick", *TWO_HASHES), link("", :kick, [@mookey, @mookeys_cool_aid], *TWO_HASHES)
279
+
280
+ # QUESTION: Hum...should be a case?
281
+ # assert_equal link_to("", "/fraggles/#{@mookey.id}/cool_aids"), link("", :index, [@mookey, @mookeys_cool_aid])
282
+ # assert_equal link_to("", "/fraggles/#{@mookey.id}/cool_aids", ONE_HASH), link("", :index, [@mookey, @mookeys_cool_aid], ONE_HASH)
283
+ # assert_equal link_to("", "/fraggles/#{@mookey.id}/cool_aids", *TWO_HASHES), link("", :index, [@mookey, @mookeys_cool_aid], *TWO_HASHES)
284
+ end
285
+
286
+ # Nil
287
+
288
+ test "should throw error on any nil argument (excluding options)" do
289
+ self.stubs(:current_controller_name).returns('fraggles')
290
+ @fraggle = @mookey
291
+
292
+ assert_raise(::Link2::Brain::NilArgument) { link(nil) }
293
+
294
+ assert_raise(::Link2::Brain::NilArgument) { link(Object.new, nil) }
295
+ assert_raise(::Link2::Brain::NilArgument) { link(nil, Object.new) }
296
+
297
+ assert_raise(::Link2::Brain::NilArgument) { link(nil, nil, nil) }
298
+ assert_raise(::Link2::Brain::NilArgument) { link(Object.new, nil, nil) }
299
+ assert_raise(::Link2::Brain::NilArgument) { link(nil, Object.new, nil) }
300
+ assert_raise(::Link2::Brain::NilArgument) { link(nil, nil, Object.new) }
301
+ assert_raise(::Link2::Brain::NilArgument) { link(Object.new, Object.new, nil) }
302
+ assert_raise(::Link2::Brain::NilArgument) { link(Object.new, nil, Object.new) }
225
303
  end
226
304
 
227
305
  # DOM selectors
@@ -19,10 +19,11 @@ class SupportTest < ActiveSupport::TestCase
19
19
  end
20
20
 
21
21
  test "#resource_identifier_class?: should only be true for valid classes" do
22
- assert ::Link2::Support.resource_identifier_class?(nil)
23
22
  assert ::Link2::Support.resource_identifier_class?(:hello)
24
23
  assert ::Link2::Support.resource_identifier_class?(::Fraggle)
24
+ assert ::Link2::Support.resource_identifier_class?(::Fraggle.new)
25
25
 
26
+ assert_not ::Link2::Support.resource_identifier_class?(nil)
26
27
  assert_not ::Link2::Support.resource_identifier_class?(::Unicorn)
27
28
  end
28
29
 
@@ -44,7 +44,9 @@ end
44
44
 
45
45
  # Routes.
46
46
  ActionController::Routing::Routes.draw do |map|
47
- map.resources :fraggles, :has_many => :cool_aids
47
+ map.resources :fraggles, :member => {:kick => :post} do |fraggles|
48
+ fraggles.resources :cool_aids, :member => {:kick => :post}
49
+ end
48
50
  map.resource :cool_aid
49
51
 
50
52
  map.root :controller => 'fraggles'
metadata CHANGED
@@ -1,7 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: link2
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 7
9
+ version: 0.1.7
5
10
  platform: ruby
6
11
  authors:
7
12
  - Jonas Grimfelt
@@ -9,79 +14,107 @@ autorequire:
9
14
  bindir: bin
10
15
  cert_chain: []
11
16
 
12
- date: 2010-03-04 00:00:00 +01:00
17
+ date: 2010-03-21 00:00:00 +01:00
13
18
  default_executable:
14
19
  dependencies:
15
20
  - !ruby/object:Gem::Dependency
16
21
  name: activesupport
17
- type: :runtime
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
20
24
  requirements:
21
25
  - - ">="
22
26
  - !ruby/object:Gem::Version
27
+ segments:
28
+ - 2
29
+ - 3
30
+ - 0
23
31
  version: 2.3.0
24
- version:
32
+ type: :runtime
33
+ version_requirements: *id001
25
34
  - !ruby/object:Gem::Dependency
26
35
  name: actionpack
27
- type: :runtime
28
- version_requirement:
29
- version_requirements: !ruby/object:Gem::Requirement
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
30
38
  requirements:
31
39
  - - ">="
32
40
  - !ruby/object:Gem::Version
41
+ segments:
42
+ - 2
43
+ - 3
44
+ - 0
33
45
  version: 2.3.0
34
- version:
46
+ type: :runtime
47
+ version_requirements: *id002
35
48
  - !ruby/object:Gem::Dependency
36
49
  name: test-unit
37
- type: :development
38
- version_requirement:
39
- version_requirements: !ruby/object:Gem::Requirement
50
+ prerelease: false
51
+ requirement: &id003 !ruby/object:Gem::Requirement
40
52
  requirements:
41
53
  - - "="
42
54
  - !ruby/object:Gem::Version
55
+ segments:
56
+ - 1
57
+ - 2
58
+ - 3
43
59
  version: 1.2.3
44
- version:
60
+ type: :development
61
+ version_requirements: *id003
45
62
  - !ruby/object:Gem::Dependency
46
63
  name: mocha
47
- type: :development
48
- version_requirement:
49
- version_requirements: !ruby/object:Gem::Requirement
64
+ prerelease: false
65
+ requirement: &id004 !ruby/object:Gem::Requirement
50
66
  requirements:
51
67
  - - ">="
52
68
  - !ruby/object:Gem::Version
69
+ segments:
70
+ - 0
71
+ - 9
72
+ - 8
53
73
  version: 0.9.8
54
- version:
74
+ type: :development
75
+ version_requirements: *id004
55
76
  - !ruby/object:Gem::Dependency
56
77
  name: webrat
57
- type: :development
58
- version_requirement:
59
- version_requirements: !ruby/object:Gem::Requirement
78
+ prerelease: false
79
+ requirement: &id005 !ruby/object:Gem::Requirement
60
80
  requirements:
61
81
  - - ">="
62
82
  - !ruby/object:Gem::Version
83
+ segments:
84
+ - 0
85
+ - 7
86
+ - 0
63
87
  version: 0.7.0
64
- version:
88
+ type: :development
89
+ version_requirements: *id005
65
90
  - !ruby/object:Gem::Dependency
66
91
  name: leftright
67
- type: :development
68
- version_requirement:
69
- version_requirements: !ruby/object:Gem::Requirement
92
+ prerelease: false
93
+ requirement: &id006 !ruby/object:Gem::Requirement
70
94
  requirements:
71
95
  - - ">="
72
96
  - !ruby/object:Gem::Version
97
+ segments:
98
+ - 0
99
+ - 0
100
+ - 3
73
101
  version: 0.0.3
74
- version:
102
+ type: :development
103
+ version_requirements: *id006
75
104
  - !ruby/object:Gem::Dependency
76
105
  name: activerecord
77
- type: :development
78
- version_requirement:
79
- version_requirements: !ruby/object:Gem::Requirement
106
+ prerelease: false
107
+ requirement: &id007 !ruby/object:Gem::Requirement
80
108
  requirements:
81
109
  - - ">="
82
110
  - !ruby/object:Gem::Version
111
+ segments:
112
+ - 2
113
+ - 3
114
+ - 0
83
115
  version: 2.3.0
84
- version:
116
+ type: :development
117
+ version_requirements: *id007
85
118
  description: "Generation next link_to-helper for Rails: Spiced with intelligence, and semantic beauty."
86
119
  email: grimen@gmail.com
87
120
  executables: []
@@ -119,18 +152,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
119
152
  requirements:
120
153
  - - ">="
121
154
  - !ruby/object:Gem::Version
155
+ segments:
156
+ - 0
122
157
  version: "0"
123
- version:
124
158
  required_rubygems_version: !ruby/object:Gem::Requirement
125
159
  requirements:
126
160
  - - ">="
127
161
  - !ruby/object:Gem::Version
162
+ segments:
163
+ - 0
128
164
  version: "0"
129
- version:
130
165
  requirements: []
131
166
 
132
167
  rubyforge_project:
133
- rubygems_version: 1.3.5
168
+ rubygems_version: 1.3.6
134
169
  signing_key:
135
170
  specification_version: 3
136
171
  summary: "Generation next link_to-helper for Rails: Spiced with intelligence, and semantic beauty."