link2 0.1.5 → 0.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.textile +23 -10
- data/TODO +19 -11
- data/lib/link2.rb +2 -0
- data/lib/link2/brain.rb +73 -25
- data/lib/link2/support.rb +64 -1
- data/lib/link2/version.rb +1 -1
- data/test/brain_test.rb +20 -2
- data/test/helpers_test.rb +92 -12
- data/test/support_test.rb +16 -0
- metadata +2 -2
data/README.textile
CHANGED
@@ -16,9 +16,10 @@ This is *not* a re-implementation of these helpers; rather it wraps these but pa
|
|
16
16
|
*Goals/Features:*
|
17
17
|
|
18
18
|
* *Rapid & flexible* - prototyping-friendly link helpers - with no trade-offs, really.
|
19
|
-
* *DRY* - based on specified arguments: Use what's know to make smart assumptions
|
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
23
|
* *Maintainable* - Well-documented code.
|
23
24
|
|
24
25
|
h2. Installation
|
@@ -52,12 +53,12 @@ h2. Usage
|
|
52
53
|
link "No operation"
|
53
54
|
# => link_to 'No operation', '#'
|
54
55
|
|
55
|
-
link "Hilarious", 'http://bash.org'
|
56
|
-
# => link_to 'No operation', 'http://bash.org'
|
57
|
-
|
58
56
|
link 'http://bash.org'
|
59
57
|
# => link_to 'http://bash.org', 'http://bash.org'
|
60
58
|
|
59
|
+
link "Hilarious", 'http://bash.org'
|
60
|
+
# => link_to 'Hilarious', 'http://bash.org'
|
61
|
+
|
61
62
|
link :home, '/intro'
|
62
63
|
# => link_to I18n.t(:home, ...), '/intro'
|
63
64
|
|
@@ -67,12 +68,24 @@ h2. Usage
|
|
67
68
|
link :back
|
68
69
|
# => link_to I18n.t(:home, ...), :back
|
69
70
|
|
71
|
+
link :index
|
72
|
+
# => link_to I18n.t(:index, ...), posts_path # Note: auto-detected
|
73
|
+
|
74
|
+
link :trash
|
75
|
+
# => link_to I18n.t(:trash, ...), trash_post_path(@post) # Note: auto-detected
|
76
|
+
|
77
|
+
link :new
|
78
|
+
# => link_to I18n.t(:new, ...), new_post_path(:id => @post.id) # Note: auto-detected, id for current resource - if found - passed, makes the implementation more generic
|
79
|
+
|
70
80
|
link @post
|
71
81
|
# => link_to I18n.t(:show, ...), post_path(@post)
|
72
82
|
|
73
83
|
link [@post, @comment]
|
74
84
|
# => link_to I18n.t(:show, ...), post_comment_path(@post, @comment)
|
75
85
|
|
86
|
+
link :go_back, :back
|
87
|
+
# => link_to I18n.t(:go_back, ...), :back
|
88
|
+
|
76
89
|
link :new, :post
|
77
90
|
# => link_to I18n.t(:new, ...), new_post_path
|
78
91
|
|
@@ -80,7 +93,7 @@ h2. Usage
|
|
80
93
|
# => link_to I18n.t(:new, ...), new_post_path
|
81
94
|
|
82
95
|
link :new, @post
|
83
|
-
# => link_to I18n.t(:new, ...), new_post_path(:id => @post.id)
|
96
|
+
# => link_to I18n.t(:new, ...), new_post_path(:id => @post.id) # ...if you think about it; useful for cloning.
|
84
97
|
|
85
98
|
link :edit, @post
|
86
99
|
# => link_to I18n.t(:edit, ...), edit_post_path(@post)
|
@@ -91,6 +104,9 @@ h2. Usage
|
|
91
104
|
link :kick, @post
|
92
105
|
# => link_to I18n.t(:kick, ...), kick_post_path(@post)
|
93
106
|
|
107
|
+
link "All items", :index
|
108
|
+
# => link_to "All items", posts_path # Note: auto-detected, see above
|
109
|
+
|
94
110
|
link "New one!", :new, Post
|
95
111
|
# => link_to "New one!", new_post_path
|
96
112
|
|
@@ -105,14 +121,11 @@ h2. Usage
|
|
105
121
|
<pre>
|
106
122
|
link :edit, [@post, @comment]
|
107
123
|
# => ::Link2::NotImplementedYetError, "case link(:action, [...]) not yet supported. Need to refactor some stuff."
|
108
|
-
|
109
|
-
link :new
|
110
|
-
# => ::Link2::NotImplementedYetError, "Auto-detection of resource is not supported yet."
|
111
124
|
</pre>
|
112
125
|
|
113
126
|
Same works for @button_to@, and you also can optionally use the branded aliases: @link2@ and @button2@.
|
114
127
|
|
115
|
-
h2. Options hashes: URL
|
128
|
+
h2. Options hashes: URL + HTML Options
|
116
129
|
|
117
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.
|
118
131
|
|
data/TODO
CHANGED
@@ -1,16 +1,6 @@
|
|
1
1
|
TODO
|
2
2
|
|
3
|
-
1)
|
4
|
-
|
5
|
-
link(:edit) => link(:edit, @post)
|
6
|
-
|
7
|
-
Detect using "self.controller_name.singularize", or use "resource" if InheritedResources is used.
|
8
|
-
|
9
|
-
2) [ISSUE:] Implement the problematic one: link(:action, [@parent, @resource])
|
10
|
-
|
11
|
-
* Collections detection:
|
12
|
-
|
13
|
-
link(:index) => link(@posts) => link(:index, @posts) => link("Index", @posts)
|
3
|
+
1) [ISSUE:] Implement the problematic one: link(:action, [@parent, @resource])
|
14
4
|
|
15
5
|
* Fix action mapping procs to handle named scopes. Should work, but do not for some reason. =/
|
16
6
|
|
@@ -28,9 +18,27 @@ TODO
|
|
28
18
|
|
29
19
|
* Check Rails 3 compatibility.
|
30
20
|
|
21
|
+
* Link2::Brain#link_to_args MUST be refactored when the DSL is 100% settled. Not far from it, but feedback first.
|
31
22
|
|
32
23
|
* Integration tests for namespaced controllers/models
|
33
24
|
|
25
|
+
* This works:
|
26
|
+
link_to x, :terms => link_to x, terms_path
|
27
|
+
|
28
|
+
So maybe this should work the same?:
|
29
|
+
link :terms, :terms <=> link :terms => link :terms, terms_path
|
30
|
+
|
31
|
+
The middle part (auto-detect route for :fraggle before resource :fraggle) maybe too abstract DSL, not sure.
|
32
|
+
|
33
|
+
|
34
34
|
NOT SURE:
|
35
35
|
|
36
|
+
* Check with Rails routes (RouteSet) if current action is a :member or :collection, and look for resource vs. collection:
|
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
|
+
|
36
44
|
* Make Link2 parse label and title-attribute for the link based on configuration: Link2.label_method = :to_s, Link2.attr_methods = {:title => :description}
|
data/lib/link2.rb
CHANGED
data/lib/link2/brain.rb
CHANGED
@@ -3,6 +3,8 @@
|
|
3
3
|
module Link2
|
4
4
|
module Brain
|
5
5
|
|
6
|
+
AutoDetectionFailed = Class.new(::NotImplementedError)
|
7
|
+
|
6
8
|
URL_PATH_REGEX = /\//
|
7
9
|
CLASS_INSTANCE_STRING = /\#\<.*\:0x.*\>/
|
8
10
|
|
@@ -29,7 +31,7 @@ module Link2
|
|
29
31
|
|
30
32
|
case args.size
|
31
33
|
when 0
|
32
|
-
raise "No arguments specified. A least specify action or url."
|
34
|
+
raise ArgumentError, "No arguments specified. A least specify action or url."
|
33
35
|
when 1
|
34
36
|
if args.first.is_a?(String)
|
35
37
|
if args.first =~ URL_PATH_REGEX
|
@@ -53,13 +55,19 @@ module Link2
|
|
53
55
|
resource = args.shift
|
54
56
|
label, url = self.label_and_url_for_resource(resource, url_options)
|
55
57
|
else
|
56
|
-
raise "Invalid 1st argument: #{args.inspect}"
|
58
|
+
raise ArgumentError, "Invalid 1st argument: #{args.inspect}"
|
57
59
|
end
|
58
60
|
when 2
|
59
61
|
if args.first.is_a?(String)
|
60
62
|
if args.second.is_a?(String)
|
61
63
|
# link "Hello", hello_path => link_to "Hello", hello_path
|
62
64
|
label, url = args.slice!(0..1)
|
65
|
+
elsif args.second.is_a?(Symbol) && ::Link2.url_for_mapping(args.second)
|
66
|
+
# link "Start", :home => link_to I18n.t(:start, ...), root_path
|
67
|
+
# link "Cancel", :back => link_to I18n.t(:cancel, ...), :back
|
68
|
+
label, action = args.slice!(0..1)
|
69
|
+
resource = nil
|
70
|
+
url = self.url_for_args(action, resource, url_options)
|
63
71
|
elsif ::Link2::Support.resource_identifier_class?(args.second)
|
64
72
|
# link "New", :new => link_to "New", new_{auto_detected_resource}_path
|
65
73
|
# link "<<", :back => link_to "<<", (session[:return_to] || :back)
|
@@ -67,7 +75,7 @@ module Link2
|
|
67
75
|
resource = nil # TODO: auto-detect resource.
|
68
76
|
url = self.url_for_args(action, resource, url_options)
|
69
77
|
else
|
70
|
-
raise "Invalid 2nd argument: #{args.inspect}"
|
78
|
+
raise ArgumentError, "Invalid 2nd argument: #{args.inspect}"
|
71
79
|
end
|
72
80
|
elsif args.first.is_a?(Symbol)
|
73
81
|
# TODO: Implement support for aray of nested resources.
|
@@ -79,7 +87,16 @@ module Link2
|
|
79
87
|
# link :new, new_post_path => link_to I18n.t(:new, ...), new_post_path
|
80
88
|
# link :back, root_path => link_to I18n.t(:back, ...), (session[:return_to] || :back)
|
81
89
|
action, url = args.slice!(0..1)
|
82
|
-
|
90
|
+
resource = nil
|
91
|
+
label = self.localized_label(action, resource, url_options)
|
92
|
+
elsif args.second.is_a?(Symbol) && ::Link2.url_for_mapping(args.second)
|
93
|
+
# link :start, :home => link_to I18n.t(:start, ...), root_path
|
94
|
+
# link :cancel, :back => link_to I18n.t(:cancel, ...), :back
|
95
|
+
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)
|
99
|
+
url = self.url_for_args(action, resource, url_options)
|
83
100
|
elsif ::Link2::Support.resource_identifier_class?(args.second)
|
84
101
|
# link :new, Post => link_to I18n.t(:new, ...), new_post_path
|
85
102
|
# link :edit, @post => link_to I18n.t(:edit, ...), edit_post_path(@post)
|
@@ -88,10 +105,10 @@ module Link2
|
|
88
105
|
label = self.localized_label(action, resource, url_options)
|
89
106
|
url = self.url_for_args(action, resource, url_options)
|
90
107
|
else
|
91
|
-
raise "Invalid 2nd argument: #{args.inspect}"
|
108
|
+
raise ArgumentError, "Invalid 2nd argument: #{args.inspect}"
|
92
109
|
end
|
93
110
|
else
|
94
|
-
raise "Invalid 1st argument: #{args.inspect}"
|
111
|
+
raise ArgumentError, "Invalid 1st argument: #{args.inspect}"
|
95
112
|
end
|
96
113
|
when 3
|
97
114
|
if args.first.is_a?(String)
|
@@ -107,16 +124,16 @@ module Link2
|
|
107
124
|
label, action, resource = args.slice!(0..2)
|
108
125
|
url = self.url_for_args(action, resource, url_options)
|
109
126
|
else
|
110
|
-
raise "Invalid 3rd argument: #{args.inspect}"
|
127
|
+
raise ArgumentError, "Invalid 3rd argument: #{args.inspect}"
|
111
128
|
end
|
112
129
|
else
|
113
|
-
raise "Invalid 2nd argument: #{args.inspect}"
|
130
|
+
raise ArgumentError, "Invalid 2nd argument: #{args.inspect}"
|
114
131
|
end
|
115
132
|
else
|
116
|
-
raise "Invalid 1st argument: #{args.inspect}"
|
133
|
+
raise ArgumentError, "Invalid 1st argument: #{args.inspect}"
|
117
134
|
end
|
118
135
|
else # when else
|
119
|
-
raise "Invalid number of arguments: #{args.inspect}."
|
136
|
+
raise ArgumentError, "Invalid number of arguments: #{args.inspect}."
|
120
137
|
end
|
121
138
|
|
122
139
|
if ::Link2.dom_selectors == true
|
@@ -127,8 +144,6 @@ module Link2
|
|
127
144
|
args << html_options if html_options
|
128
145
|
|
129
146
|
[label, url, *args]
|
130
|
-
rescue => e
|
131
|
-
raise ::ArgumentError, e
|
132
147
|
end
|
133
148
|
|
134
149
|
# Extracts a label and a url for a (polymorphic) resource.
|
@@ -185,17 +200,17 @@ module Link2
|
|
185
200
|
options = args.extract_options!.dup
|
186
201
|
action, resource = args
|
187
202
|
|
188
|
-
if resource.
|
189
|
-
url = ::Link2.url_for_mapping(action, resource) rescue nil
|
190
|
-
if url
|
191
|
-
url
|
192
|
-
else
|
193
|
-
raise ::Link2::NotImplementedYetError,
|
194
|
-
"Resource needs to be specified for non-mapped actions; auto-detection of resource(s) not implemented yet."
|
195
|
-
end
|
196
|
-
elsif resource.is_a?(String)
|
203
|
+
if resource.is_a?(String) # url
|
197
204
|
resource
|
205
|
+
elsif resource.nil? && url = ::Link2.url_for_mapping(action, resource) # mapping
|
206
|
+
url
|
198
207
|
else
|
208
|
+
resource ||= self.auto_detect_resource || self.auto_detect_collection
|
209
|
+
if resource.nil?
|
210
|
+
raise ::Link2::Brain::AutoDetectionFailed,
|
211
|
+
"Auto-detection of resource or collection failed for args: #{[action, resource].inspect}."
|
212
|
+
end
|
213
|
+
|
199
214
|
options[:controller] ||= "/%s" % self.controller_name_for_resource(resource)
|
200
215
|
options[:action] = action
|
201
216
|
options[:id] = resource.id if !resource.is_a?(Class) && ::Link2::Support.record_class?(resource)
|
@@ -231,12 +246,21 @@ module Link2
|
|
231
246
|
# controller_name_for_resource(@post)
|
232
247
|
# # => "posts"
|
233
248
|
#
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
249
|
+
# controller_name_for_resource([@post_1, @post_2])
|
250
|
+
# # => "posts"
|
251
|
+
#
|
252
|
+
def controller_name_for_resource(resource_or_collection = nil)
|
253
|
+
if resource_or_collection
|
254
|
+
if ::Link2::Support.record_collection?(resource_or_collection)
|
255
|
+
resource_or_collection.first.class.name.tableize
|
256
|
+
else
|
257
|
+
resource_class = ::Link2::Support.find_resource_class(resource_or_collection)
|
258
|
+
resource_class.name.tableize if ::Link2::Support.record_class?(resource_class)
|
259
|
+
end
|
238
260
|
end || self.controller.controller_name
|
239
261
|
end
|
262
|
+
# When no resource is passed +current_controller_name+ makes more sense.
|
263
|
+
alias :current_controller_name :controller_name_for_resource
|
240
264
|
|
241
265
|
# Parse human resource name for a specified resource.
|
242
266
|
#
|
@@ -269,6 +293,30 @@ module Link2
|
|
269
293
|
custom_name
|
270
294
|
end
|
271
295
|
|
296
|
+
# Auto-detect current view resource instance based on expected ivar-name.
|
297
|
+
#
|
298
|
+
def auto_detect_resource
|
299
|
+
if self.respond_to?(:resource) # InheritedResources pattern
|
300
|
+
self.resource
|
301
|
+
else
|
302
|
+
self.instance_variable_get(:"@#{self.current_controller_name.singularize}") # @post
|
303
|
+
end
|
304
|
+
rescue
|
305
|
+
nil
|
306
|
+
end
|
307
|
+
|
308
|
+
# Auto-detect current view resource collection instance based on expected ivar-name.
|
309
|
+
#
|
310
|
+
def auto_detect_collection
|
311
|
+
if self.respond_to?(:collection) # InheritedResources pattern
|
312
|
+
self.collection
|
313
|
+
else
|
314
|
+
self.instance_variable_get(:"@#{self.current_controller_name.pluralize}") # @posts
|
315
|
+
end
|
316
|
+
rescue
|
317
|
+
nil
|
318
|
+
end
|
319
|
+
|
272
320
|
# Attach Link2 semantic DOM selector attributes (attributes "id" and "class") based on
|
273
321
|
# action and resource - if any can be parsed.
|
274
322
|
#
|
data/lib/link2/support.rb
CHANGED
@@ -28,6 +28,22 @@ module Link2
|
|
28
28
|
raise "No such class: #{resource_class_name}"
|
29
29
|
end
|
30
30
|
|
31
|
+
# Extract resource based on resource or collecton of resources.
|
32
|
+
#
|
33
|
+
# == Usage/Examples:
|
34
|
+
#
|
35
|
+
# extract_resource?(nil)
|
36
|
+
# # => nil
|
37
|
+
#
|
38
|
+
# extract_resource?([])
|
39
|
+
# # => nil
|
40
|
+
#
|
41
|
+
# extract_resource?(@post)
|
42
|
+
# # => @post
|
43
|
+
#
|
44
|
+
# extract_resource?([@post_1, @post_2])
|
45
|
+
# # => @post_2
|
46
|
+
#
|
31
47
|
def extract_resource(resource)
|
32
48
|
resource.compact! if resource.is_a?(Array)
|
33
49
|
[resource].flatten.last
|
@@ -36,8 +52,55 @@ module Link2
|
|
36
52
|
# Check if the specified object is a valid resource identifier class. Used
|
37
53
|
# for detecting current resource based on controller, action, etc.
|
38
54
|
#
|
55
|
+
# resource_identifier_class?(:string)
|
56
|
+
# # => false
|
57
|
+
#
|
58
|
+
# resource_identifier_class?(:post)
|
59
|
+
# # => true
|
60
|
+
#
|
61
|
+
# resource_identifier_class?(String)
|
62
|
+
# # => false
|
63
|
+
#
|
64
|
+
# resource_identifier_class?(Post)
|
65
|
+
# # => true
|
66
|
+
#
|
67
|
+
# resource_identifier_class?("")
|
68
|
+
# # => false
|
69
|
+
#
|
70
|
+
# resource_identifier_class?(@post)
|
71
|
+
# # => true
|
72
|
+
#
|
39
73
|
def resource_identifier_class?(object)
|
40
|
-
object.is_a?(NilClass) || object.is_a?(Symbol) || self.record_class?(object)
|
74
|
+
(object.is_a?(NilClass) || object.is_a?(Symbol) || self.record_class?(object))
|
75
|
+
end
|
76
|
+
|
77
|
+
# Check if passed object is an array for record instances, i.e. "collection".
|
78
|
+
# THis assumes all objects in the array is of same kind; makes little sense with mixing
|
79
|
+
# different records kinds in a collection, and in such case any auto-detection is hard.
|
80
|
+
#
|
81
|
+
# == Usage/Examples:
|
82
|
+
#
|
83
|
+
# record_collection?([])
|
84
|
+
# # => false
|
85
|
+
#
|
86
|
+
# record_collection?(@post)
|
87
|
+
# # => false
|
88
|
+
#
|
89
|
+
# record_collection?([@post])
|
90
|
+
# # => true
|
91
|
+
#
|
92
|
+
# record_collection?([@post, @article])
|
93
|
+
# # => false
|
94
|
+
#
|
95
|
+
# record_collection?([@post, @post])
|
96
|
+
# # => true
|
97
|
+
#
|
98
|
+
def record_collection?(collection_maybe)
|
99
|
+
if collection_maybe.present? && collection_maybe.is_a?(Array)
|
100
|
+
collection_maybe.compact.all? { |object| record_object?(object) }
|
101
|
+
else
|
102
|
+
false
|
103
|
+
end
|
41
104
|
end
|
42
105
|
|
43
106
|
# Check if a specified objec is a record class type.
|
data/lib/link2/version.rb
CHANGED
data/test/brain_test.rb
CHANGED
@@ -9,6 +9,7 @@ class BrainTest < ActionView::TestCase
|
|
9
9
|
::I18n.locale = :en
|
10
10
|
|
11
11
|
@mookey = ::Fraggle.create(:name => 'Mookey')
|
12
|
+
@wembley = ::Fraggle.create(:name => 'Wembley')
|
12
13
|
@tweaked_mookey = ::Fraggle.create(:name => 'Mookey 2.0')
|
13
14
|
@tweaked_mookey.class_eval do
|
14
15
|
def to_s
|
@@ -49,8 +50,25 @@ class BrainTest < ActionView::TestCase
|
|
49
50
|
end
|
50
51
|
|
51
52
|
test "#controller_name_for_resource: should return current controller if no resource is specified or is nil" do
|
52
|
-
|
53
|
-
assert_equal '
|
53
|
+
self.expects(:controller_name_for_resource).with(nil).returns('fraggles').at_least_once
|
54
|
+
assert_equal 'fraggles', controller_name_for_resource(nil)
|
55
|
+
end
|
56
|
+
|
57
|
+
test "#current_controller_name: should return current controller" do
|
58
|
+
self.expects(:current_controller_name).returns('fraggles').at_least_once
|
59
|
+
assert_equal 'fraggles', current_controller_name
|
60
|
+
end
|
61
|
+
|
62
|
+
test "#auto_detect_resource: " do
|
63
|
+
self.expects(:current_controller_name).returns('fraggles').at_least_once
|
64
|
+
@fraggle = @mookey
|
65
|
+
assert_equal @fraggle, auto_detect_resource
|
66
|
+
end
|
67
|
+
|
68
|
+
test "#auto_detect_collection: should " do
|
69
|
+
self.expects(:current_controller_name).returns('fraggles').at_least_once
|
70
|
+
@fraggles = [@mookey, @wembley]
|
71
|
+
assert_equal @fraggles, auto_detect_collection
|
54
72
|
end
|
55
73
|
|
56
74
|
test "#human_name_for_resource: should return reosurce name based on resource using #to_s or #human_name" do
|
data/test/helpers_test.rb
CHANGED
@@ -14,6 +14,7 @@ class HelpersTest < ActionView::TestCase
|
|
14
14
|
::Link2.i18n_scopes = [] # Keep it simple - no scoped lookups by default.
|
15
15
|
|
16
16
|
@mookey = ::Fraggle.create(:name => 'Mookey')
|
17
|
+
@wembley = ::Fraggle.create(:name => 'Wembley')
|
17
18
|
@mookeys_cool_aid = ::CoolAid.create(:name => 'Super-tasty')
|
18
19
|
end
|
19
20
|
|
@@ -30,22 +31,49 @@ class HelpersTest < ActionView::TestCase
|
|
30
31
|
assert_equal link_to('/posts?by=date', '/posts?by=date'), link('/posts?by=date')
|
31
32
|
end
|
32
33
|
|
33
|
-
test "link(label) should render link_to(
|
34
|
+
test "link(label) should render link_to(label, '#')" do
|
34
35
|
assert_equal link_to('Hello', '#'), link('Hello')
|
35
36
|
end
|
36
37
|
|
37
|
-
test "link(:action
|
38
|
-
|
38
|
+
test "auto-detecting resource: link(:action) should render link_to(t(:action, ...), @resource)" do
|
39
|
+
self.expects(:current_controller_name).with(nil).returns('fraggles').at_least_once
|
40
|
+
|
41
|
+
assert_raise(::Link2::Brain::AutoDetectionFailed) do
|
42
|
+
link(:show)
|
43
|
+
end
|
44
|
+
|
45
|
+
@fraggle = @mookey
|
46
|
+
|
47
|
+
assert_nothing_raised(::Link2::Brain::AutoDetectionFailed) do
|
48
|
+
link(:show)
|
49
|
+
end
|
50
|
+
|
51
|
+
assert_equal link_to("Show", "/fraggles/#{@fraggle.id}"), link(:show)
|
52
|
+
assert_equal link_to("Show", "/fraggles/#{@fraggle.id}", ONE_HASH), link(:show, ONE_HASH)
|
53
|
+
assert_equal link_to("Show", "/fraggles/#{@fraggle.id}", *TWO_HASHES), link(:show, *TWO_HASHES)
|
39
54
|
end
|
40
55
|
|
41
|
-
test "link(:action) should render link_to(t(:action, ...),
|
42
|
-
|
43
|
-
|
56
|
+
test "auto-detecting collection: link(:action) should render link_to(t(:action, ...), @collection)" do
|
57
|
+
self.expects(:current_controller_name).with(nil).returns('fraggles').at_least_once
|
58
|
+
|
59
|
+
assert_raise(::Link2::Brain::AutoDetectionFailed) do
|
60
|
+
link(:index)
|
61
|
+
end
|
62
|
+
|
63
|
+
@fraggles = [@mookey, @wembley]
|
64
|
+
|
65
|
+
assert_nothing_raised(::Link2::Brain::AutoDetectionFailed) do
|
66
|
+
link(:index)
|
67
|
+
end
|
68
|
+
|
69
|
+
assert_equal link_to("Index", "/fraggles"), link(:index)
|
70
|
+
assert_equal link_to("Index", "/fraggles", ONE_HASH), link(:index, ONE_HASH)
|
71
|
+
assert_equal link_to("Index", "/fraggles", *TWO_HASHES), link(:index, *TWO_HASHES)
|
44
72
|
end
|
45
73
|
|
46
|
-
test "link(:mapping) should render link_to(t(:mapping, ...), url_for_mapping(:mapping, ...)),
|
47
|
-
assert_equal link_to("Home", '/'),
|
48
|
-
assert_equal link_to("Home", '/', ONE_HASH),
|
74
|
+
test "link(:mapping) should render link_to(t(:mapping, ...), url_for_mapping(:mapping, ...)), lookup mapping" do
|
75
|
+
assert_equal link_to("Home", '/'), link(:home)
|
76
|
+
assert_equal link_to("Home", '/', ONE_HASH), link(:home, ONE_HASH)
|
49
77
|
assert_equal link_to("Home", '/', *TWO_HASHES), link(:home, *TWO_HASHES)
|
50
78
|
|
51
79
|
swap ::Link2, :action_mappings => {:secret => '/secret'} do
|
@@ -70,6 +98,22 @@ class HelpersTest < ActionView::TestCase
|
|
70
98
|
|
71
99
|
# link(x, y, {}, {})
|
72
100
|
|
101
|
+
test "link(:action, url) should render link_to(t(:action, ...), url)" do
|
102
|
+
assert_equal link_to('Home', '/custom-home'), link(:home, '/custom-home')
|
103
|
+
end
|
104
|
+
|
105
|
+
test "link(..., :mapping) should render link_to(..., url_for_mapping(:mapping, ...))" do
|
106
|
+
assert_nothing_raised(Exception) { link("Home", :home) }
|
107
|
+
assert_equal link_to("Home", root_path), link("Home", :home)
|
108
|
+
assert_nothing_raised(Exception) { link(:home, :home) }
|
109
|
+
assert_equal link_to("Home", root_path), link(:home, :home)
|
110
|
+
|
111
|
+
assert_nothing_raised(Exception) { link("Back", :back) }
|
112
|
+
assert_equal link_to("Back", :back), link("Back", :back)
|
113
|
+
assert_nothing_raised(Exception) { link(:back, :back) }
|
114
|
+
assert_equal link_to("Back", :back), link(:back, :back)
|
115
|
+
end
|
116
|
+
|
73
117
|
test "link(label, url) should render link_to(label, url)" do
|
74
118
|
assert_equal link_to('New', '/posts/new'), link('New', '/posts/new')
|
75
119
|
assert_equal link_to('New', '/posts/new', ONE_HASH), link('New', '/posts/new', ONE_HASH)
|
@@ -82,9 +126,9 @@ class HelpersTest < ActionView::TestCase
|
|
82
126
|
|
83
127
|
test "link(:action) should render link_to(label, url_for(:action => :action, ...)), auto-detecting resource" do
|
84
128
|
# assert_equal link_to("New Fraggle!!"), link("New Fraggle!!", :new)
|
85
|
-
assert_raise(::Link2::
|
86
|
-
assert_raise(::Link2::
|
87
|
-
assert_raise(::Link2::
|
129
|
+
assert_raise(::Link2::Brain::AutoDetectionFailed) { link("New Fraggle!!", :new) }
|
130
|
+
assert_raise(::Link2::Brain::AutoDetectionFailed) { link("New Fraggle!!", :new, ONE_HASH) }
|
131
|
+
assert_raise(::Link2::Brain::AutoDetectionFailed) { link("New Fraggle!!", :new, *TWO_HASHES) }
|
88
132
|
end
|
89
133
|
|
90
134
|
test "link(label, action) should render link_to(label, url_for_mapping(:mapping, ...)), auto-detecting resource" do
|
@@ -122,6 +166,42 @@ class HelpersTest < ActionView::TestCase
|
|
122
166
|
assert_raise(::Link2::NotImplementedYetError) { link(:edit, [@mookey, @mookeys_cool_aid], *TWO_HASHES) }
|
123
167
|
end
|
124
168
|
|
169
|
+
test "auto-detecting resource: link(label, :action) should render link_to(label, @resource, :action => :action)" do
|
170
|
+
self.expects(:current_controller_name).with(nil).returns('fraggles').at_least_once
|
171
|
+
|
172
|
+
assert_raise(::Link2::Brain::AutoDetectionFailed) do
|
173
|
+
link("Show it", :show)
|
174
|
+
end
|
175
|
+
|
176
|
+
@fraggle = @mookey
|
177
|
+
|
178
|
+
assert_nothing_raised(::Link2::Brain::AutoDetectionFailed) do
|
179
|
+
link("Show it", :show)
|
180
|
+
end
|
181
|
+
|
182
|
+
assert_equal link_to("Show it", "/fraggles/#{@fraggle.id}"), link("Show it", :show)
|
183
|
+
assert_equal link_to("Show it", "/fraggles/#{@fraggle.id}", ONE_HASH), link("Show it", :show, ONE_HASH)
|
184
|
+
assert_equal link_to("Show it", "/fraggles/#{@fraggle.id}", *TWO_HASHES), link("Show it", :show, *TWO_HASHES)
|
185
|
+
end
|
186
|
+
|
187
|
+
test "auto-detecting collection: link(label, :action) should render link_to(label, @collection, :action => :action)" do
|
188
|
+
self.expects(:current_controller_name).with(nil).returns('fraggles').at_least_once
|
189
|
+
|
190
|
+
assert_raise(::Link2::Brain::AutoDetectionFailed) do
|
191
|
+
link("All", :index)
|
192
|
+
end
|
193
|
+
|
194
|
+
@fraggles = [@mookey, @wembley]
|
195
|
+
|
196
|
+
assert_nothing_raised(::Link2::Brain::AutoDetectionFailed) do
|
197
|
+
link("All", :index)
|
198
|
+
end
|
199
|
+
|
200
|
+
assert_equal link_to("All", "/fraggles"), link("All", :index)
|
201
|
+
assert_equal link_to("All", "/fraggles", ONE_HASH), link("All", :index, ONE_HASH)
|
202
|
+
assert_equal link_to("All", "/fraggles", *TWO_HASHES), link("All", :index, *TWO_HASHES)
|
203
|
+
end
|
204
|
+
|
125
205
|
# link(x, y, z, {}, {})
|
126
206
|
|
127
207
|
test "link(label, action, resource)" do
|
data/test/support_test.rb
CHANGED
@@ -26,10 +26,26 @@ class SupportTest < ActiveSupport::TestCase
|
|
26
26
|
assert_not ::Link2::Support.resource_identifier_class?(::Unicorn)
|
27
27
|
end
|
28
28
|
|
29
|
+
test "#record_collection?: should be true only i passed value is array of record objects of same kind" do
|
30
|
+
assert_not ::Link2::Support.record_collection?([])
|
31
|
+
assert_not ::Link2::Support.record_collection?(::Fraggle.new)
|
32
|
+
assert_not ::Link2::Support.record_collection?([::Unicorn.new])
|
33
|
+
assert_not ::Link2::Support.record_collection?([::Unicorn.new, ::Fraggle.new])
|
34
|
+
|
35
|
+
assert ::Link2::Support.record_collection?([::Fraggle.new])
|
36
|
+
assert ::Link2::Support.record_collection?([::Fraggle.new, ::Fraggle.new])
|
37
|
+
end
|
38
|
+
|
29
39
|
test "#record_class?: should only be true for record classes" do
|
30
40
|
assert ::Link2::Support.record_class?(::Fraggle)
|
31
41
|
|
32
42
|
assert_not ::Link2::Support.record_class?(::Unicorn)
|
33
43
|
end
|
34
44
|
|
45
|
+
test "#record_object?: should only be true for record objects" do
|
46
|
+
assert ::Link2::Support.record_class?(::Fraggle.new)
|
47
|
+
|
48
|
+
assert_not ::Link2::Support.record_class?(::Unicorn.new)
|
49
|
+
end
|
50
|
+
|
35
51
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: link2
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jonas Grimfelt
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2010-03-
|
12
|
+
date: 2010-03-04 00:00:00 +01:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|