link2 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.textile +4 -1
- data/TODO +2 -0
- data/lib/link2.rb +1 -1
- data/lib/link2/brain.rb +58 -55
- data/lib/link2/version.rb +1 -1
- data/test/helpers_test.rb +5 -0
- metadata +1 -1
data/README.textile
CHANGED
@@ -56,7 +56,10 @@ h2. Usage
|
|
56
56
|
# => link_to 'No operation', 'http://bash.org'
|
57
57
|
|
58
58
|
link 'http://bash.org'
|
59
|
-
|
59
|
+
# => link_to 'http://bash.org', 'http://bash.org'
|
60
|
+
|
61
|
+
link :home, '/intro'
|
62
|
+
# => link_to I18n.t(:home, ...), '/intro'
|
60
63
|
|
61
64
|
link :home
|
62
65
|
# => link_to I18n.t(:home, ...), root_path
|
data/TODO
CHANGED
@@ -26,6 +26,8 @@ TODO
|
|
26
26
|
|
27
27
|
* ORM-tests: DataMapper, MongoMapper
|
28
28
|
|
29
|
+
* Check Rails 3 compatibility.
|
30
|
+
|
29
31
|
NOT SURE:
|
30
32
|
|
31
33
|
* 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
@@ -28,74 +28,77 @@ module Link2
|
|
28
28
|
url_options = args.pop if args.last.is_a?(Hash)
|
29
29
|
|
30
30
|
case args.size
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
31
|
+
when 0
|
32
|
+
raise "No arguments specified. A least specify action or url."
|
33
|
+
when 1
|
34
|
+
if args.first.is_a?(String)
|
35
|
+
if args.first =~ URL_PATH_REGEX
|
36
|
+
# link 'http://example.com' => link_to 'http://example.com', 'http://example.com'
|
37
|
+
label = url = args.shift
|
38
|
+
else
|
39
|
+
# link "Hello" => link_to 'Hello', '#'
|
40
|
+
url = ::Link2::DEFAULT_LINK
|
41
|
+
label = args.shift
|
42
|
+
end
|
43
|
+
elsif args.first.is_a?(Symbol)
|
44
|
+
# link :new => link_to I18n.t(:new, ...), new_{auto_detected_resource}_path
|
45
|
+
# link :back => link_to I18n.t(:back, ...), (session[:return_to] || :back)
|
46
|
+
action = args.shift
|
47
|
+
resource = nil # TODO: auto-detect resource.
|
48
|
+
label = self.localized_label(action, resource, url_options)
|
49
|
+
url = self.url_for_args(action, resource, url_options)
|
50
|
+
elsif args.first.is_a?(Object)
|
51
|
+
# link @user => link_to I18n.t(:show, ...), user_path(@user)
|
52
|
+
# link [:admin, @user] => link_to I18n.t(:show, ...), admin_user_path(@user)
|
53
|
+
resource = args.shift
|
54
|
+
label, url = self.label_and_url_for_resource(resource, url_options)
|
55
|
+
else
|
56
|
+
raise "Invalid 1st argument: #{args.inspect}"
|
57
|
+
end
|
58
|
+
when 2
|
59
|
+
if args.first.is_a?(String)
|
60
|
+
if args.second.is_a?(String)
|
61
|
+
# link "Hello", hello_path => link_to "Hello", hello_path
|
62
|
+
label, url = args.slice!(0..1)
|
63
|
+
elsif ::Link2::Support.resource_identifier_class?(args.second)
|
64
|
+
# link "New", :new => link_to "New", new_{auto_detected_resource}_path
|
65
|
+
# link "<<", :back => link_to "<<", (session[:return_to] || :back)
|
66
|
+
label, action = args.slice!(0..1)
|
47
67
|
resource = nil # TODO: auto-detect resource.
|
48
|
-
label = self.localized_label(action, resource, url_options)
|
49
68
|
url = self.url_for_args(action, resource, url_options)
|
50
|
-
elsif args.first.is_a?(Object)
|
51
|
-
# link @user => link_to I18n.t(:show, ...), user_path(@user)
|
52
|
-
# link [:admin, @user] => link_to I18n.t(:show, ...), admin_user_path(@user)
|
53
|
-
resource = args.shift
|
54
|
-
label, url = self.label_and_url_for_resource(resource, url_options)
|
55
69
|
else
|
56
|
-
raise "Invalid
|
70
|
+
raise "Invalid 2nd argument: #{args.inspect}"
|
57
71
|
end
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
label, url = args.slice!(0..1)
|
63
|
-
elsif ::Link2::Support.resource_identifier_class?(args.second)
|
64
|
-
# link "New", :new => link_to "New", new_{auto_detected_resource}_path
|
65
|
-
# link "<<", :back => link_to "<<", (session[:return_to] || :back)
|
66
|
-
label, action = args.slice!(0..1)
|
67
|
-
resource = nil # TODO: auto-detect resource.
|
68
|
-
url = self.url_for_args(action, resource, url_options)
|
69
|
-
else
|
70
|
-
raise "Invalid 2nd argument: #{args.inspect}"
|
71
|
-
end
|
72
|
-
elsif args.first.is_a?(Symbol)
|
73
|
-
# TODO: Implement support for aray of nested resources.
|
74
|
-
if args.second.is_a?(Array)
|
75
|
-
raise ::Link2::NotImplementedYetError, "case link(:action, [...]) not yet supported. Need to refactor some stuff."
|
72
|
+
elsif args.first.is_a?(Symbol)
|
73
|
+
# TODO: Implement support for aray of nested resources.
|
74
|
+
if args.second.is_a?(Array)
|
75
|
+
raise ::Link2::NotImplementedYetError, "case link(:action, [...]) not yet supported. Need to refactor some stuff."
|
76
76
|
end
|
77
77
|
|
78
|
-
|
79
|
-
|
78
|
+
if args.second.is_a?(String)
|
79
|
+
# link :new, new_post_path => link_to I18n.t(:new, ...), new_post_path
|
80
|
+
# link :back, root_path => link_to I18n.t(:back, ...), (session[:return_to] || :back)
|
81
|
+
action, url = args.slice!(0..1)
|
82
|
+
label = self.localized_label(action, nil, url_options)
|
83
|
+
elsif ::Link2::Support.resource_identifier_class?(args.second)
|
80
84
|
# link :new, Post => link_to I18n.t(:new, ...), new_post_path
|
81
85
|
# link :edit, @post => link_to I18n.t(:edit, ...), edit_post_path(@post)
|
82
86
|
# link :show, [:admin, @user] => link_to I18n.t(:show, ...), admin_user_path(@user)
|
83
|
-
# link :back, root_path => link_to I18n.t(:back, ...), (session[:return_to] || :back)
|
84
87
|
action, resource = args.slice!(0..1)
|
85
88
|
label = self.localized_label(action, resource, url_options)
|
86
89
|
url = self.url_for_args(action, resource, url_options)
|
87
90
|
else
|
88
91
|
raise "Invalid 2nd argument: #{args.inspect}"
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
92
|
+
end
|
93
|
+
else
|
94
|
+
raise "Invalid 1st argument: #{args.inspect}"
|
95
|
+
end
|
96
|
+
when 3
|
97
|
+
if args.first.is_a?(String)
|
98
|
+
if args.second.is_a?(Symbol)
|
99
|
+
# TODO: Implement support for aray of nested resources.
|
100
|
+
if args.third.is_a?(Array)
|
101
|
+
raise ::Link2::NotImplementedYetError, 'case link("Label", :action, [...]) not yet supported. Need to refactor some stuff.'
|
99
102
|
end
|
100
103
|
|
101
104
|
if ::Link2::Support.resource_identifier_class?(args.third)
|
@@ -112,7 +115,7 @@ module Link2
|
|
112
115
|
else
|
113
116
|
raise "Invalid 1st argument: #{args.inspect}"
|
114
117
|
end
|
115
|
-
else
|
118
|
+
else # when else
|
116
119
|
raise "Invalid number of arguments: #{args.inspect}."
|
117
120
|
end
|
118
121
|
|
data/lib/link2/version.rb
CHANGED
data/test/helpers_test.rb
CHANGED
@@ -34,6 +34,10 @@ class HelpersTest < ActionView::TestCase
|
|
34
34
|
assert_equal link_to('Hello', '#'), link('Hello')
|
35
35
|
end
|
36
36
|
|
37
|
+
test "link(:action, url) should render link_to(t(:action, ...), url)" do
|
38
|
+
assert_equal link_to('Home', '/custom-home'), link(:home, '/custom-home')
|
39
|
+
end
|
40
|
+
|
37
41
|
test "link(:action) should render link_to(t(:action, ...), url_for(:action => :action, ...)), auto-detecting resource" do
|
38
42
|
# assert_equal link_to("New Fraggle"), link(:new)
|
39
43
|
assert_raise(::Link2::NotImplementedYetError) { link(:new) }
|
@@ -155,6 +159,7 @@ class HelpersTest < ActionView::TestCase
|
|
155
159
|
# link(:edit, [@mookey, @mookeys_cool_aid])
|
156
160
|
|
157
161
|
assert_equal link_to("Home", '/', :class => 'home'), link(:home)
|
162
|
+
assert_equal link_to("Home", '/', :class => 'home'), link(:home, '/')
|
158
163
|
end
|
159
164
|
end
|
160
165
|
|