link2 0.1.0 → 0.1.1

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 CHANGED
@@ -16,12 +16,6 @@ This is *not* a re-implementation of @link_to@/@button_to@-helpers; rather it wr
16
16
  * Full test-coverage for stability.
17
17
  * Well-documented code.
18
18
 
19
- h2. Dependencies
20
-
21
- * "rails 2.3.x":http://github.com/rails/rails only. Might as well work with Rails 3 already, but I didn't write that.
22
-
23
- For testing: test-unit, and "left-right":http://github.com/jordi/leftright.
24
-
25
19
  h2. Installation
26
20
 
27
21
  "Drop a *Gem* on 'em":http://open.spotify.com/track/2pqKuHtn8ZKMMMqbJrA2e7:
@@ -30,12 +24,19 @@ h2. Installation
30
24
  $ sudo gem install link2
31
25
  </pre>
32
26
 
27
+ *Dependencies:*
28
+
29
+ * *"rails 2.3.x":http://github.com/rails/rails* only. Might as well work with Rails 3 already, but I didn't write that.
30
+
31
+ For testing: test-unit, "mocha":http://github.com/floehopper/mocha, and "webrat":http://github.com/brynary/webrat. Optional: "left-right":http://github.com/jordi/leftright
32
+
33
33
  h2. Setup
34
34
 
35
- Generate initializer (optional):
35
+ Generate *initializer* (optional):
36
36
 
37
37
  <pre>
38
38
  $ ./script/generate link2
39
+ create config/initializers/link2.rb
39
40
  </pre>
40
41
 
41
42
  h2. Usage
data/TODO CHANGED
@@ -1,10 +1,8 @@
1
1
  TODO
2
2
 
3
- * Implement the problematic one: link(:action, [@parent, @resource])
3
+ 1) [BUG:] I18n lookup scope interpolations should not be same as I18n.t interpolations.
4
4
 
5
- * Write tests for options hashes as well: url_options vs. html_options
6
-
7
- * Generate proper DOM ID selectors, e.g. class="new post"
5
+ 2) Generate proper DOM ID selectors, e.g. class="new post"
8
6
 
9
7
  DOM Selectors
10
8
 
@@ -21,20 +19,23 @@ TODO
21
19
  # => <a class="back" ...>...<a/>
22
20
  </pre>
23
21
 
24
- * Fix action mapping procs to handle named scopes. Should work, but do not for some reason. =/
22
+ 3) Auto-detect current resource instance for member views (non-collection)
25
23
 
26
- * Fix action mapping procs to receive session, params, etc in a options hash. My mocking/stubbing don't want to work. ='(
24
+ link(:edit) => link(:edit, @post)
25
+
26
+ Detect using "self.controller_name.singularize", or use "resource" if InheritedResources is used.
27
+
28
+ * [ISSUE:] Implement the problematic one: link(:action, [@parent, @resource])
27
29
 
28
- * Allow customization of alternatives to Object#to_s for parsing object label, i.e. {{name}} interpolation.
29
30
  * Collections detection:
30
31
 
31
32
  link(:index) => link(@posts) => link(:index, @posts) => link("Index", @posts)
32
33
 
33
- * Auto-detect current resource instance for member views (non-collection)
34
+ * Allow customization of alternatives to Object#to_s for parsing object label, i.e. {{name}} interpolation.
34
35
 
35
- link(:edit) => link(:edit, @post)
36
+ * Fix action mapping procs to handle named scopes. Should work, but do not for some reason. =/
36
37
 
37
- Detect using "self.controller_name.singularize", or use "resource" if InheritedResources is used.
38
+ * Fix action mapping procs to receive session, params, etc in a options hash. My mocking/stubbing don't want to work. ='(
38
39
 
39
40
  * Add support: #link_to_function + #button_to_function: I18n, more?
40
41
 
@@ -44,6 +45,8 @@ TODO
44
45
 
45
46
  ajax_link(...)
46
47
 
48
+ * ORM-tests: DataMapper, MongoMapper
49
+
47
50
  Consider:
48
51
 
49
- * Possible parse TITLE-attribute from the model, say Object#link_title or similar.
52
+ * Possible parse TITLE-attribute from the model, say Object#link_title or similar.
@@ -3,7 +3,7 @@
3
3
  if defined?(::Link2)
4
4
  ::Link2.setup do |config|
5
5
  # Configure how - and in what order - link labels should be looked up.
6
- # config.i18n_scopes = ['links.{{action}}']
6
+ # config.i18n_scopes = ['{{model}}.links.{{action}}', 'links.{{action}}']
7
7
  #
8
8
  # Configure any custom action mappings.
9
9
  # config.action_mappings = {
data/lib/link2.rb CHANGED
@@ -31,7 +31,7 @@ module Link2
31
31
 
32
32
  # Default I18n lookup scopes if none are set.
33
33
  DEFAULT_I18N_SCOPES = [
34
- '{{resource}}.links.{{action}}',
34
+ '{{model}}.links.{{action}}',
35
35
  'links.{{action}}'
36
36
  ]
37
37
 
data/lib/link2/brain.rb CHANGED
@@ -6,10 +6,9 @@ module Link2
6
6
  URL_PATH_REGEX = /\//
7
7
  CLASS_INSTANCE_STRING = /\#\<.*\:0x.*\>/
8
8
 
9
- LINK2_OPTION_KEYS = [:scope, :strong].freeze
10
9
  LINK_TO_OPTION_KEYS = [:method, :confirm, :popup, :html_options].freeze
11
10
  BUTTON_TO_OPTION_KEYS = [:method, :confirm, :disabled].freeze
12
- IGNORED_OPTION_KEYS = (LINK2_OPTION_KEYS + LINK_TO_OPTION_KEYS + BUTTON_TO_OPTION_KEYS).uniq
11
+ IGNORED_OPTION_KEYS = (LINK_TO_OPTION_KEYS + BUTTON_TO_OPTION_KEYS).uniq
13
12
 
14
13
  POLYMORPHIC_OPTION_KEYS = [:action, :routing_type]
15
14
 
@@ -23,10 +22,13 @@ module Link2
23
22
  # The comments give guidelines on what assumptions is being made in each case.
24
23
  #
25
24
  def link_to_args(*args)
26
- html_options = args.extract_options!
27
- url_options = args.extract_options!
28
25
  args.unshift(capture(&block)) if block_given?
29
26
 
27
+ html_options = args.pop if args.last.is_a?(Hash)
28
+ url_options = args.pop if args.last.is_a?(Hash)
29
+
30
+ #puts [url_options, html_options].compact.inspect
31
+
30
32
  case args.size
31
33
  when 0
32
34
  raise "No arguments specified. A least specify action or url."
@@ -71,7 +73,9 @@ module Link2
71
73
  end
72
74
  elsif args.first.is_a?(Symbol)
73
75
  # TODO: Implement this:
74
- raise ::Link2::NotImplementedYetError, "case link(:action, [...]) not yet supported. Need to refactor some stuff." if args.second.is_a?(Array)
76
+ if args.second.is_a?(Array)
77
+ raise ::Link2::NotImplementedYetError, "case link(:action, [...]) not yet supported. Need to refactor some stuff."
78
+ end
75
79
  # TODO: Cleanup.
76
80
  if self.resource_identifier_class?(args.second)
77
81
  # link :new, Post => link_to I18n.t(:new, ...), new_post_path
@@ -107,8 +111,8 @@ module Link2
107
111
  else
108
112
  raise "Invalid number of arguments: #{args.inspect}."
109
113
  end
110
- #[label, url, *((args << options) << html_options)]
111
- #puts url_options.inspect
114
+ args << url_options if url_options
115
+ args << html_options if html_options
112
116
  [label, url, *args]
113
117
  rescue => e
114
118
  raise ::ArgumentError, e
@@ -133,18 +137,17 @@ module Link2
133
137
  # See documentation on +polymorphic_url+ for available core options.
134
138
  #
135
139
  def label_and_url_for_resource(resource, options = {})
140
+ options ||= {}
136
141
  resource.compact! if resource.is_a?(Array)
137
142
  last_resource = [resource].flatten.last
138
- url_for_options = options.slice!(*POLYMORPHIC_OPTION_KEYS).reverse_merge(:routing_type => :path)
139
- i18n_options = options
143
+ url_for_options = options.slice(*POLYMORPHIC_OPTION_KEYS).reverse_merge(:routing_type => :path)
144
+ i18n_options = options.except(url_for_options.keys)
140
145
 
141
146
  # Skip any ugly default to_s-value.
142
147
  custom_name = last_resource.to_s =~ CLASS_INSTANCE_STRING ? last_resource.class.human_name.downcase : last_resource.to_s
143
148
  custom_name = last_resource.class.human_name.downcase if custom_name.blank?
144
149
 
145
- i18n_options.merge!(:name => custom_name)
146
-
147
- label = self.localized_label(:show, last_resource.class, i18n_options)
150
+ label = self.localized_label(:show, last_resource.class, i18n_options.merge(:name => custom_name))
148
151
  url = polymorphic_url(resource, url_for_options)
149
152
 
150
153
  [label, url]
@@ -171,7 +174,7 @@ module Link2
171
174
  # See documentation on +url_for+ for available core options.
172
175
  #
173
176
  def url_for_args(*args)
174
- options = args.extract_options!
177
+ options = args.extract_options!.dup
175
178
  action, resource = args
176
179
 
177
180
  if resource.nil?
@@ -189,7 +192,7 @@ module Link2
189
192
  options[:action] = action
190
193
  options[:id] = resource.id if !resource.is_a?(Class) && self.record_class?(resource)
191
194
 
192
- url_for(options.except(IGNORED_OPTION_KEYS))
195
+ url_for(options.except(*IGNORED_OPTION_KEYS))
193
196
  end
194
197
  end
195
198
 
@@ -197,8 +200,9 @@ module Link2
197
200
  # for the current template instance.
198
201
  #
199
202
  def localized_label(action, resource, options = {})
200
- options.merge!(:controller => self.controller_name_for_resource(resource))
201
- ::Link2::I18n.t(action, resource, options)
203
+ options ||= {}
204
+ i18n_options = options.merge(:controller => self.controller_name_for_resource(resource))
205
+ ::Link2::I18n.t(action, resource, i18n_options)
202
206
  end
203
207
 
204
208
  # Get controller name based for a specified resource.
data/lib/link2/i18n.rb CHANGED
@@ -22,6 +22,12 @@ module Link2
22
22
  # 2. links.{{action}}
23
23
  # ...
24
24
  #
25
+ # == Valid value interpolations:
26
+ #
27
+ # * +resource+ - resource humanized name (parsed with I18n if possible), e.g. CaptainMorgan / @captain_morgan => "captain morgan"
28
+ # * +resources+ - pluralized resource humanized name (parsed with I18n if possible), e.g. CaptainMorgan / @captain_morgan => "captain morgans"
29
+ # * +name+ - current resource name to_s-value, e.g. @captain_morgan.to_s => "Captain Morgan with Cola and lime #4"
30
+ #
25
31
  def translate_with_scoping(action, resource, options = {})
26
32
  raise ArgumentError, "At least action must be specified." unless action.present?
27
33
  resource_name = self.localized_resource_class_name(resource)
@@ -45,7 +51,7 @@ module Link2
45
51
  #
46
52
  # == Usage/Examples:
47
53
  #
48
- # ::Link2.i18n_scopes = ['{{resources}}.links.{{action}}', '{{controller}}.links.{{action}}', 'links.{{action}}']
54
+ # ::Link2.i18n_scopes = ['{{models}}.links.{{action}}', '{{controller}}.links.{{action}}', 'links.{{action}}']
49
55
  #
50
56
  # substituted_scopes_for(:new, Post.new)
51
57
  # # => Link2::I18n::ScopeInterpolationError
@@ -53,12 +59,19 @@ module Link2
53
59
  # substituted_scopes_for(:new, Post.new, :controller => 'admin')
54
60
  # # => ['posts.links.new', 'admin.links.{new', 'links.new']
55
61
  #
62
+ # == Valid lookup scope interpolations:
63
+ #
64
+ # * +model+ - link model name, e.g. CaptainMorgan / @captain_morgan => "captain_morgan"
65
+ # * +models+ - pluralized link model name, e.g. CaptainMorgan / @captain_morgan => "captain_morgans"
66
+ # * +controller+ - current controller name
67
+ # * +action+ - the link action name
68
+ #
56
69
  def substituted_scopes_for(action, resource, options = {})
57
- resource_name = self.localized_resource_class_name(resource) # TODO: Should not be localized. Maybe use "model"/"models" to avoid confusion?
70
+ model_name = self.localized_resource_class_name(resource) # TODO: Should not be localized. Maybe use "model"/"models" to avoid confusion?
58
71
  substitutions = options.merge(
59
72
  :action => action.to_s.underscore,
60
- :resource => resource_name,
61
- :resources => resource_name.pluralize
73
+ :model => model_name,
74
+ :models => model_name.pluralize
62
75
  )
63
76
 
64
77
  scopes = ::Link2::i18n_scopes.collect do |i18n_scope|
data/lib/link2/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  module Link2
4
- VERSION = '0.1.0'.freeze
4
+ VERSION = '0.1.1'.freeze
5
5
  end
data/test/brain_test.rb CHANGED
@@ -95,14 +95,15 @@ class BrainTest < ActionView::TestCase
95
95
  test "#url_for_args: should generate a url based on action and resource" do
96
96
  # FIXME: stub(:session).returns({:return_to => '/'})
97
97
 
98
- self.expects(:controller_name_for_resource).with(@mookey).returns('fraggles').twice
99
98
  self.expects(:controller_name_for_resource).with(::Fraggle).returns('fraggles')
100
99
  self.expects(:controller_name_for_resource).with(:fraggle).returns('fraggles')
101
100
 
102
- assert_equal "/fraggles/new?id=#{@mookey.id}", url_for_args(:new, @mookey)
103
- assert_equal '/fraggles/new', url_for_args(:new, ::Fraggle)
104
101
  assert_equal '/fraggles/new', url_for_args(:new, :fraggle)
102
+ assert_equal '/fraggles/new', url_for_args(:new, ::Fraggle)
105
103
 
104
+ self.expects(:controller_name_for_resource).with(@mookey).returns('fraggles').twice
105
+
106
+ assert_equal "/fraggles/new?id=#{@mookey.id}", url_for_args(:new, @mookey)
106
107
  assert_equal "/fraggles/#{@mookey.id}/edit", url_for_args(:edit, @mookey)
107
108
  end
108
109
 
data/test/helpers_test.rb CHANGED
@@ -5,6 +5,9 @@ class HelpersTest < ActionView::TestCase
5
5
 
6
6
  include Link2::Helpers
7
7
 
8
+ ONE_HASH = {:confirm => 'Really?'}
9
+ TWO_HASHES = [{:confirm => 'Really?'}, {:class => 'funny'}]
10
+
8
11
  def setup
9
12
  ::I18n.locale = :en
10
13
 
@@ -22,7 +25,7 @@ class HelpersTest < ActionView::TestCase
22
25
 
23
26
  test "link(url) should render link_to(url, url)" do
24
27
  assert_equal link_to('http://example.com', 'http://example.com'), link('http://example.com')
25
- assert_equal link_to('/posts?by=date', '/posts?by=date'), link('/posts?by=date')
28
+ assert_equal link_to('/posts?by=date', '/posts?by=date'), link('/posts?by=date')
26
29
  end
27
30
 
28
31
  test "link(label) should render link_to(str_label, '#')" do
@@ -39,69 +42,95 @@ class HelpersTest < ActionView::TestCase
39
42
  test "link(:mapping) should render link_to(t(:mapping, ...), url_for_mapping(:mapping, ...)), auto-detecting resource" do
40
43
  swap ::Link2, :i18n_scopes => ['link.{{action}}'] do
41
44
  assert_equal link_to("Home", '/'), link(:home)
45
+ assert_equal link_to("Home", '/', ONE_HASH), link(:home, ONE_HASH)
46
+ assert_equal link_to("Home", '/', *TWO_HASHES), link(:home, *TWO_HASHES)
42
47
 
43
48
  swap ::Link2, :action_mappings => {:secret => '/secret'} do
44
- assert_equal link_to("Secret", '/secret'), link(:secret)
49
+ assert_equal link_to("Secret", '/secret'), link(:secret)
50
+ assert_equal link_to("Secret", '/secret', ONE_HASH), link(:secret, ONE_HASH)
51
+ assert_equal link_to("Secret", '/secret', *TWO_HASHES), link(:secret, *TWO_HASHES)
45
52
  end
46
53
  end
47
54
  end
48
55
 
49
56
  test "link(@resource) should render link_to(t(:show, ...), @object)" do
50
57
  swap ::Link2, :i18n_scopes => ['link.{{action}}'] do
51
- assert_equal link_to("Show", "/fraggles/#{@mookey.id}"), link(@mookey)
58
+ assert_equal link_to("Show", "/fraggles/#{@mookey.id}"), link(@mookey)
59
+ assert_equal link_to("Show", "/fraggles/#{@mookey.id}", ONE_HASH), link(@mookey, ONE_HASH)
60
+ assert_equal link_to("Show", "/fraggles/#{@mookey.id}", *TWO_HASHES), link(@mookey, *TWO_HASHES)
52
61
  # assert_equal link_to("Show", "?"), link(::Fraggle) # test this stupid case?
53
62
  end
54
63
  end
55
64
 
56
65
  test "link([@parent, @resource]) should render link_to(t(:show, ...), polymorphic_path([@parent, @resource]))" do
57
66
  swap ::Link2, :i18n_scopes => ['link.{{action}}'] do
58
- assert_equal link_to("Show", "/fraggles/#{@mookey.id}/cool_aids/#{@mookeys_cool_aid.id}"), link([@mookey, @mookeys_cool_aid])
67
+ assert_equal link_to("Show", "/fraggles/#{@mookey.id}/cool_aids/#{@mookeys_cool_aid.id}"), link([@mookey, @mookeys_cool_aid])
68
+ assert_equal link_to("Show", "/fraggles/#{@mookey.id}/cool_aids/#{@mookeys_cool_aid.id}", ONE_HASH), link([@mookey, @mookeys_cool_aid], ONE_HASH)
69
+ assert_equal link_to("Show", "/fraggles/#{@mookey.id}/cool_aids/#{@mookeys_cool_aid.id}", *TWO_HASHES), link([@mookey, @mookeys_cool_aid], *TWO_HASHES)
59
70
  end
60
71
  end
61
72
 
62
- # FINALIZE: link(x, y, {}, {})
73
+ # link(x, y, {}, {})
63
74
 
64
75
  test "link(label, url) should render link_to(label, url)" do
65
- assert_equal link_to('http://example.com', 'http://example.com'), link('http://example.com', 'http://example.com')
66
- assert_equal link_to('New', '/posts/new'), link('New', '/posts/new')
76
+ assert_equal link_to('New', '/posts/new'), link('New', '/posts/new')
77
+ assert_equal link_to('New', '/posts/new', ONE_HASH), link('New', '/posts/new', ONE_HASH)
78
+ assert_equal link_to('New', '/posts/new', *TWO_HASHES), link('New', '/posts/new', *TWO_HASHES)
79
+
80
+ assert_equal link_to('http://example.com', 'http://example.com'), link('http://example.com', 'http://example.com')
81
+ assert_equal link_to('http://example.com', 'http://example.com', ONE_HASH), link('http://example.com', 'http://example.com', ONE_HASH)
82
+ assert_equal link_to('http://example.com', 'http://example.com', *TWO_HASHES), link('http://example.com', 'http://example.com', *TWO_HASHES)
67
83
  end
68
84
 
69
85
  test "link(:action) should render link_to(label, url_for(:action => :action, ...)), auto-detecting resource" do
70
86
  swap ::Link2, :i18n_scopes => ['link.{{action}}'] do
71
87
  # assert_equal link_to("New Fraggle!!"), link("New Fraggle!!", :new)
72
88
  assert_raise(::Link2::NotImplementedYetError) { link("New Fraggle!!", :new) }
89
+ assert_raise(::Link2::NotImplementedYetError) { link("New Fraggle!!", :new, ONE_HASH) }
90
+ assert_raise(::Link2::NotImplementedYetError) { link("New Fraggle!!", :new, *TWO_HASHES) }
73
91
  end
74
92
  end
75
93
 
76
94
  test "link(label, action) should render link_to(label, url_for_mapping(:mapping, ...)), auto-detecting resource" do
77
95
  swap ::Link2, :i18n_scopes => ['link.{{action}}'] do
78
- assert_equal link_to("Home!!", '/'), link("Home!!", :home)
96
+ assert_equal link_to("Home!!", '/'), link("Home!!", :home)
97
+ assert_equal link_to("Home!!", '/', ONE_HASH), link("Home!!", :home, ONE_HASH)
98
+ assert_equal link_to("Home!!", '/', *TWO_HASHES), link("Home!!", :home, *TWO_HASHES)
79
99
 
80
100
  swap ::Link2, :action_mappings => {:secret => '/secret'} do
81
- assert_equal link_to("Damn you!!", '/secret'), link("Damn you!!", :secret)
101
+ assert_equal link_to("Damn you!!", '/secret'), link("Damn you!!", :secret)
102
+ assert_equal link_to("Damn you!!", '/secret', ONE_HASH), link("Damn you!!", :secret, ONE_HASH)
103
+ assert_equal link_to("Damn you!!", '/secret', *TWO_HASHES), link("Damn you!!", :secret, *TWO_HASHES)
82
104
  end
83
105
  end
84
106
  end
85
107
 
86
108
  test "link(:action, Resource) should render link_to(t(:action, ...), url_for(:action => :action, ...))" do
87
109
  swap ::Link2, :i18n_scopes => ['link.{{action}}'] do
88
- assert_equal link_to("New", "/fraggles/new"), link(:new, ::Fraggle)
110
+ assert_equal link_to("New", "/fraggles/new"), link(:new, ::Fraggle)
111
+ assert_equal link_to("New", "/fraggles/new", ONE_HASH), link(:new, ::Fraggle, ONE_HASH)
112
+ assert_equal link_to("New", "/fraggles/new", *TWO_HASHES), link(:new, ::Fraggle, *TWO_HASHES)
89
113
  end
90
114
  end
91
115
 
92
- test "link(:action, @resource) should render link_to(t(:action, ...), url_for(:action => :action, ...))" do
116
+ test "link(:action, @resource) should render link_to(t(:action, ...), url_for(:action => :action, ...)), non-RESTful vs. RESTful routes" do
93
117
  swap ::Link2, :i18n_scopes => ['link.{{action}}'] do
94
- # Non-RESTful-route, basic case.
95
- assert_equal link_to("New", "/fraggles/new?id=#{@mookey.id}"), link(:new, @mookey)
96
- # REST-case.
97
- assert_equal link_to("Edit", "/fraggles/#{@mookey.id}/edit"), link(:edit, @mookey)
118
+ assert_equal link_to("New", "/fraggles/new?id=#{@mookey.id}"), link(:new, @mookey)
119
+ assert_equal link_to("New", "/fraggles/new?id=#{@mookey.id}", ONE_HASH), link(:new, @mookey, ONE_HASH)
120
+ assert_equal link_to("New", "/fraggles/new?id=#{@mookey.id}", *TWO_HASHES), link(:new, @mookey, *TWO_HASHES)
121
+
122
+ assert_equal link_to("Edit", "/fraggles/#{@mookey.id}/edit"), link(:edit, @mookey)
123
+ assert_equal link_to("Edit", "/fraggles/#{@mookey.id}/edit", ONE_HASH), link(:edit, @mookey, ONE_HASH)
124
+ assert_equal link_to("Edit", "/fraggles/#{@mookey.id}/edit", *TWO_HASHES), link(:edit, @mookey, *TWO_HASHES)
98
125
  end
99
126
  end
100
127
 
101
128
  test "link(:action, [@parent, @resource]) should render link_to(t(:action, ...), polymorphic_path([@parent, @resource]), :action => :action)" do
102
129
  swap ::Link2, :i18n_scopes => ['link.{{action}}'] do
103
130
  # assert_equal link_to("Edit", "/fraggles/#{@mookey.id}/cool_aids/#{@mookeys_cool_aid.id}/edit"), link(:edit, [@mookey, @mookeys_cool_aid])
104
- assert_raise(::Link2::NotImplementedYetError) { link(:edit, [@mookey, @mookeys_cool_aid]) }
131
+ assert_raise(::Link2::NotImplementedYetError) { link(:edit, [@mookey, @mookeys_cool_aid]) }
132
+ assert_raise(::Link2::NotImplementedYetError) { link(:edit, [@mookey, @mookeys_cool_aid], ONE_HASH) }
133
+ assert_raise(::Link2::NotImplementedYetError) { link(:edit, [@mookey, @mookeys_cool_aid], *TWO_HASHES) }
105
134
  end
106
135
  end
107
136
 
@@ -109,7 +138,12 @@ class HelpersTest < ActionView::TestCase
109
138
 
110
139
  test "link(label, action, resource)" do
111
140
  assert_equal link_to("Newish", "/fraggles/new"), link("Newish", :new, ::Fraggle)
141
+ assert_equal link_to("Newish", "/fraggles/new", ONE_HASH), link("Newish", :new, ::Fraggle, ONE_HASH)
142
+ assert_equal link_to("Newish", "/fraggles/new", *TWO_HASHES), link("Newish", :new, ::Fraggle, *TWO_HASHES)
143
+
112
144
  assert_equal link_to("Editish", "/fraggles/#{@mookey.id}/edit"), link("Editish", :edit, @mookey)
145
+ assert_equal link_to("Editish", "/fraggles/#{@mookey.id}/edit", ONE_HASH), link("Editish", :edit, @mookey, ONE_HASH)
146
+ assert_equal link_to("Editish", "/fraggles/#{@mookey.id}/edit", *TWO_HASHES), link("Editish", :edit, @mookey, *TWO_HASHES)
113
147
  end
114
148
 
115
149
  test "js_link should not be implemented (yet)" do
data/test/i18n_test.rb CHANGED
@@ -23,7 +23,7 @@ class I18nTest < ActiveSupport::TestCase
23
23
  end
24
24
 
25
25
  test "i18n: should substitute scopes with parsed values for: controller, action, resource, resources" do
26
- dummie_scopes = ['{{controller}}.{{resources}}.{{resource}}.{{action}}.label', 'links.{{action}}']
26
+ dummie_scopes = ['{{controller}}.{{models}}.{{model}}.{{action}}.label', 'links.{{action}}']
27
27
  expected_substitution = [:'fraggles.fraggles.fraggle.new.label', :'links.new']
28
28
 
29
29
  swap ::Link2, :i18n_scopes => dummie_scopes do
@@ -0,0 +1,14 @@
1
+ # encoding: utf-8
2
+ require 'test/test_helper'
3
+
4
+ class HelpersIntegrationTest < ActionController::IntegrationTest
5
+
6
+ test 'Link2 + Rails = ♥' do
7
+ visit '/'
8
+ assert_response :success
9
+ assert_template 'home/index'
10
+
11
+ assert_contain "WIN"
12
+ end
13
+
14
+ end
@@ -0,0 +1,12 @@
1
+ # encoding: utf-8
2
+
3
+ # Filters added to this controller apply to all controllers in the application.
4
+ # Likewise, all the methods added will be available for all controllers.
5
+
6
+ class ApplicationController < ActionController::Base
7
+ helper :all # include all helpers, all the time
8
+ protect_from_forgery # See ActionController::RequestForgeryProtection for details
9
+
10
+ # Scrub sensitive parameters from your log
11
+ filter_parameter_logging :password
12
+ end
@@ -0,0 +1,11 @@
1
+ # encoding: utf-8
2
+
3
+ class HomeController < ApplicationController
4
+
5
+ def index
6
+ @blue_pony = Pony.create
7
+ @pink_pony = Pony.create
8
+ @blue_pony.ponies << @pink_pony
9
+ end
10
+
11
+ end
@@ -0,0 +1,12 @@
1
+ # encoding: utf-8
2
+
3
+ class PoniesController < ApplicationController
4
+
5
+ def index
6
+ end
7
+
8
+ def shut_up
9
+ render :text => "Shut up Blue Pony; you are not as blue as you think!"
10
+ end
11
+
12
+ end
@@ -0,0 +1,5 @@
1
+ # encoding: utf-8
2
+
3
+ # Methods added to this helper will be available to all templates in the application.
4
+ module ApplicationHelper
5
+ end
@@ -0,0 +1,5 @@
1
+ # encoding: utf-8
2
+
3
+ class Pony < ActiveRecord::Base
4
+ has_many :ponies # yea...
5
+ end
@@ -0,0 +1,110 @@
1
+ # Don't change this file!
2
+ # Configure your app in config/environment.rb and config/environments/*.rb
3
+
4
+ RAILS_ROOT = "#{File.dirname(__FILE__)}/.." unless defined?(RAILS_ROOT)
5
+
6
+ module Rails
7
+ class << self
8
+ def boot!
9
+ unless booted?
10
+ preinitialize
11
+ pick_boot.run
12
+ end
13
+ end
14
+
15
+ def booted?
16
+ defined? Rails::Initializer
17
+ end
18
+
19
+ def pick_boot
20
+ (vendor_rails? ? VendorBoot : GemBoot).new
21
+ end
22
+
23
+ def vendor_rails?
24
+ File.exist?("#{RAILS_ROOT}/vendor/rails")
25
+ end
26
+
27
+ def preinitialize
28
+ load(preinitializer_path) if File.exist?(preinitializer_path)
29
+ end
30
+
31
+ def preinitializer_path
32
+ "#{RAILS_ROOT}/config/preinitializer.rb"
33
+ end
34
+ end
35
+
36
+ class Boot
37
+ def run
38
+ load_initializer
39
+ Rails::Initializer.run(:set_load_path)
40
+ end
41
+ end
42
+
43
+ class VendorBoot < Boot
44
+ def load_initializer
45
+ require "#{RAILS_ROOT}/vendor/rails/railties/lib/initializer"
46
+ Rails::Initializer.run(:install_gem_spec_stubs)
47
+ Rails::GemDependency.add_frozen_gem_path
48
+ end
49
+ end
50
+
51
+ class GemBoot < Boot
52
+ def load_initializer
53
+ self.class.load_rubygems
54
+ load_rails_gem
55
+ require 'initializer'
56
+ end
57
+
58
+ def load_rails_gem
59
+ if version = self.class.gem_version
60
+ gem 'rails', version
61
+ else
62
+ gem 'rails'
63
+ end
64
+ rescue Gem::LoadError => load_error
65
+ $stderr.puts %(Missing the Rails #{version} gem. Please `gem install -v=#{version} rails`, update your RAILS_GEM_VERSION setting in config/environment.rb for the Rails version you do have installed, or comment out RAILS_GEM_VERSION to use the latest version installed.)
66
+ exit 1
67
+ end
68
+
69
+ class << self
70
+ def rubygems_version
71
+ Gem::RubyGemsVersion rescue nil
72
+ end
73
+
74
+ def gem_version
75
+ if defined? RAILS_GEM_VERSION
76
+ RAILS_GEM_VERSION
77
+ elsif ENV.include?('RAILS_GEM_VERSION')
78
+ ENV['RAILS_GEM_VERSION']
79
+ else
80
+ parse_gem_version(read_environment_rb)
81
+ end
82
+ end
83
+
84
+ def load_rubygems
85
+ min_version = '1.3.2'
86
+ require 'rubygems'
87
+ unless rubygems_version >= min_version
88
+ $stderr.puts %Q(Rails requires RubyGems >= #{min_version} (you have #{rubygems_version}). Please `gem update --system` and try again.)
89
+ exit 1
90
+ end
91
+
92
+ rescue LoadError
93
+ $stderr.puts %Q(Rails requires RubyGems >= #{min_version}. Please install RubyGems and try again: http://rubygems.rubyforge.org)
94
+ exit 1
95
+ end
96
+
97
+ def parse_gem_version(text)
98
+ $1 if text =~ /^[^#]*RAILS_GEM_VERSION\s*=\s*["']([!~<>=]*\s*[\d.]+)["']/
99
+ end
100
+
101
+ private
102
+ def read_environment_rb
103
+ File.read("#{RAILS_ROOT}/config/environment.rb")
104
+ end
105
+ end
106
+ end
107
+ end
108
+
109
+ # All that for this:
110
+ Rails.boot!
@@ -0,0 +1,42 @@
1
+ # Be sure to restart your server when you modify this file
2
+
3
+ # Specifies gem version of Rails to use when vendor/rails is not present
4
+ RAILS_GEM_VERSION = '2.3.5' unless defined? RAILS_GEM_VERSION
5
+ TEST_ORM = :active_record unless defined? TEST_ORM
6
+
7
+ # Bootstrap the Rails environment, frameworks, and default configuration
8
+ require File.join(File.dirname(__FILE__), 'boot')
9
+
10
+ Rails::Initializer.run do |config|
11
+ # Settings in config/environments/* take precedence over those specified here.
12
+ # Application configuration should go into files in config/initializers
13
+ # -- all .rb files in that directory are automatically loaded.
14
+
15
+ # Add additional load paths for your own custom dirs
16
+ # config.load_paths += [ "#{RAILS_ROOT}/app/models/#{TEST_ORM}/" ]
17
+
18
+ # Specify gems that this application depends on and have them installed with rake gems:install
19
+ # config.gem "bj"
20
+ # config.gem "hpricot", :version => '0.6', :source => "http://code.whytheluckystiff.net"
21
+ # config.gem "sqlite3-ruby", :lib => "sqlite3"
22
+ # config.gem "aws-s3", :lib => "aws/s3"
23
+
24
+ # Only load the plugins named here, in the order given (default is alphabetical).
25
+ # :all can be used as a placeholder for all plugins not explicitly named
26
+ # config.plugins = [ :exception_notification, :ssl_requirement, :all ]
27
+
28
+ # Skip frameworks you're not going to use. To use Rails without a database,
29
+ # you must remove the Active Record framework.
30
+ config.frameworks -= [ :active_record ] unless TEST_ORM == :active_record
31
+
32
+ # Activate observers that should always be running
33
+ # config.active_record.observers = :cacher, :garbage_collector, :forum_observer
34
+
35
+ # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
36
+ # Run "rake -D time" for a list of tasks for finding time zone names.
37
+ config.time_zone = 'UTC'
38
+
39
+ # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
40
+ # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}')]
41
+ # config.i18n.default_locale = :en
42
+ end
@@ -0,0 +1,28 @@
1
+ # Settings specified here will take precedence over those in config/environment.rb
2
+
3
+ # The test environment is used exclusively to run your application's
4
+ # test suite. You never need to work with it otherwise. Remember that
5
+ # your test database is "scratch space" for the test suite and is wiped
6
+ # and recreated between test runs. Don't rely on the data there!
7
+ config.cache_classes = true
8
+
9
+ # Log error messages when you accidentally call methods on nil.
10
+ config.whiny_nils = true
11
+
12
+ # Show full error reports and disable caching
13
+ config.action_controller.consider_all_requests_local = true
14
+ config.action_controller.perform_caching = false
15
+ config.action_view.cache_template_loading = true
16
+
17
+ # Disable request forgery protection in test environment
18
+ config.action_controller.allow_forgery_protection = false
19
+
20
+ # Tell Action Mailer not to deliver emails to the real world.
21
+ # The :test delivery method accumulates sent emails in the
22
+ # ActionMailer::Base.deliveries array.
23
+ config.action_mailer.delivery_method = :test
24
+
25
+ # Use SQL instead of Active Record's schema dumper when creating the test database.
26
+ # This is necessary if your schema can't be completely dumped by the schema dumper,
27
+ # like if you have constraints or database-specific column types
28
+ # config.active_record.schema_format = :sql
@@ -0,0 +1,2 @@
1
+ ActiveSupport::Inflector.inflections do |inflect|
2
+ end
@@ -0,0 +1,20 @@
1
+ # encoding: utf-8
2
+
3
+ Link2.setup do |config|
4
+ # Configure how - and in what order - link labels should be looked up.
5
+ config.i18n_scopes = [
6
+ '{{model}}.links.{{action}}',
7
+ 'links.{{action}}'
8
+ ]
9
+
10
+ # Configure any custom action mappings.
11
+ config.action_mappings = {
12
+ :home => lambda { '/' },
13
+ :back => lambda { |url| url || :back }
14
+ }
15
+ # TODO: Make procs containing routes work.
16
+ # config.action_mappings = {
17
+ # :home => lambda { root_path },
18
+ # :back => lambda { |url| url || session[:return_to] || :back }
19
+ # }
20
+ end
@@ -0,0 +1,24 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # These settings change the behavior of Rails 2 apps and will be defaults
4
+ # for Rails 3. You can remove this initializer when Rails 3 is released.
5
+
6
+ if defined?(ActiveRecord)
7
+ # Include Active Record class name as root for JSON serialized output.
8
+ ActiveRecord::Base.include_root_in_json = true
9
+
10
+ # Store the full class name (including module namespace) in STI type column.
11
+ ActiveRecord::Base.store_full_sti_class = true
12
+ end
13
+
14
+ ActionController::Routing.generate_best_match = false
15
+
16
+ # Use ISO 8601 format for JSON serialized times and dates.
17
+ ActiveSupport.use_standard_json_time_format = true
18
+
19
+ # Don't escape HTML entities in JSON, leave that for the #json_escape helper.
20
+ # if you're including raw json in an HTML page.
21
+ ActiveSupport.escape_html_entities_in_json = false
22
+
23
+ # Clean up silencers
24
+ Rails.backtrace_cleaner.remove_silencers!
@@ -0,0 +1,15 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # Your secret key for verifying cookie session data integrity.
4
+ # If you change this key, all old sessions will become invalid!
5
+ # Make sure the secret is at least 30 characters and all random,
6
+ # no regular words or you'll be exposed to dictionary attacks.
7
+ ActionController::Base.session = {
8
+ :key => '_rails_app_session',
9
+ :secret => '73d6541260e27f6651d2a0e444468482151f867aa40f8306b1dbf1e468e27a434e8e072c74498757e6091c6b92aa962df1e64163cc12d318d7b59b41758db165'
10
+ }
11
+
12
+ # Use the database for sessions instead of the cookie-based default,
13
+ # which shouldn't be used to store highly confidential information
14
+ # (create the session table with "rake db:sessions:create")
15
+ # ActionController::Base.session_store = :active_record_store
@@ -0,0 +1,15 @@
1
+ # encoding: utf-8
2
+
3
+ ActionController::Routing::Routes.draw do |map|
4
+
5
+ map.root :controller => 'home'
6
+
7
+ map.resources :ponies, :member => {:shut_up => :get} do |ponies|
8
+ ponies.resources :ponies
9
+ end
10
+
11
+ # WHY?: Tests fails when these are enabled.
12
+ # map.connect ':controller/:action/:id'
13
+ # map.connect ':controller/:action/:id.:format'
14
+
15
+ end
@@ -0,0 +1,29 @@
1
+ # encoding: utf-8
2
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'integration', 'rails_app', 'config', 'environment'))
3
+ require 'test_help'
4
+
5
+ ActiveRecord::Migration.verbose = false
6
+ ActiveRecord::Base.logger = Logger.new(nil)
7
+ ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
8
+
9
+ ActiveRecord::Schema.define(:version => 1) do
10
+ create_table :fraggles do |t|
11
+ t.string :name
12
+ t.integer :craziness
13
+ t.string :hair_color
14
+ end
15
+
16
+ create_table :cool_aids do |t|
17
+ t.string :name
18
+ t.decimal :strength
19
+ end
20
+
21
+ create_table :ponies do |t|
22
+ t.string :color
23
+ end
24
+ end
25
+
26
+ class ActiveSupport::TestCase
27
+ self.use_transactional_fixtures = true
28
+ self.use_instantiated_fixtures = false
29
+ end
@@ -0,0 +1,6 @@
1
+ # encoding: utf-8
2
+
3
+ Webrat.configure do |config|
4
+ config.mode = :rails
5
+ config.open_error_files = false
6
+ end
data/test/test_helper.rb CHANGED
@@ -1,13 +1,26 @@
1
1
  # encoding: utf-8
2
2
  require 'rubygems'
3
3
 
4
+ ENV['RAILS_ENV'] = 'test'
5
+ TEST_ORM = (ENV['ORM'] || :active_record).to_sym
6
+
7
+ # ORM / Schema.
8
+ require File.join(File.dirname(__FILE__), 'orm', TEST_ORM.to_s)
9
+
4
10
  gem 'test-unit', '1.2.3'
5
11
  require 'test/unit'
6
- require 'leftright'
7
12
 
8
- require 'active_support'
9
- require 'action_controller'
10
- require 'active_record'
13
+ begin
14
+ require 'leftright'
15
+ rescue LoadError
16
+ end
17
+
18
+ # require 'active_support'
19
+ # require 'action_controller'
20
+ # require 'active_record'
21
+
22
+ require 'mocha'
23
+ require 'webrat'
11
24
 
12
25
  require 'active_support/test_case'
13
26
  require 'action_view/test_case'
@@ -15,20 +28,6 @@ require 'action_view/test_case'
15
28
  # Support.
16
29
  Dir[File.join(File.dirname(__FILE__), *%w[support ** *.rb]).to_s].each { |f| require f }
17
30
 
18
- # Schema.
19
- ActiveRecord::Schema.define(:version => 1) do
20
- create_table :fraggles do |t|
21
- t.string :name
22
- t.integer :craziness
23
- t.string :hair_color
24
- end
25
-
26
- create_table :cool_aids do |t|
27
- t.string :name
28
- t.decimal :strength
29
- end
30
- end
31
-
32
31
  # Models.
33
32
  class Fraggle < ActiveRecord::Base
34
33
  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.0
4
+ version: 0.1.1
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-02-25 00:00:00 +01:00
12
+ date: 2010-02-28 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -108,10 +108,25 @@ test_files:
108
108
  - test/brain_test.rb
109
109
  - test/helpers_test.rb
110
110
  - test/i18n_test.rb
111
+ - test/integration/helpers_integration_test.rb
112
+ - test/integration/rails_app/app/controllers/application_controller.rb
113
+ - test/integration/rails_app/app/controllers/home_controller.rb
114
+ - test/integration/rails_app/app/controllers/ponies_controller.rb
115
+ - test/integration/rails_app/app/helpers/application_helper.rb
116
+ - test/integration/rails_app/app/models/active_record/pony.rb
117
+ - test/integration/rails_app/config/boot.rb
118
+ - test/integration/rails_app/config/environment.rb
119
+ - test/integration/rails_app/config/environments/test.rb
120
+ - test/integration/rails_app/config/initializers/inflections.rb
121
+ - test/integration/rails_app/config/initializers/link2.rb
122
+ - test/integration/rails_app/config/initializers/new_rails_defaults.rb
123
+ - test/integration/rails_app/config/initializers/session_store.rb
124
+ - test/integration/rails_app/config/routes.rb
111
125
  - test/link2_test.rb
126
+ - test/orm/active_record.rb
112
127
  - test/support/assertions_helper.rb
113
- - test/support/db_setup.rb
114
128
  - test/support/debug_helper.rb
115
129
  - test/support/substitutions_helper.rb
130
+ - test/support/webrat_setup.rb
116
131
  - test/support_test.rb
117
132
  - test/test_helper.rb
@@ -1,10 +0,0 @@
1
- # encoding: utf-8
2
-
3
- ActiveRecord::Migration.verbose = false
4
- ActiveRecord::Base.logger = Logger.new(nil)
5
- ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
6
-
7
- ActiveSupport::TestCase.class_eval do
8
- # self.use_transactional_fixtures = true
9
- # self.use_instantiated_fixtures = false
10
- end