josevalim-inherited_resources 0.4.3 → 0.4.4
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/CHANGELOG +6 -0
- data/README +11 -3
- data/lib/inherited_resources/base.rb +5 -5
- data/lib/inherited_resources/base_helpers.rb +38 -11
- data/lib/inherited_resources/belongs_to_helpers.rb +3 -0
- data/lib/inherited_resources/dumb_responder.rb +1 -1
- data/lib/inherited_resources/respond_to.rb +4 -2
- data/test/base_helpers_test.rb +16 -0
- data/test/fixtures/en.yml +5 -1
- metadata +2 -2
data/CHANGELOG
CHANGED
@@ -1,3 +1,9 @@
|
|
1
|
+
# Version 0.4.4
|
2
|
+
|
3
|
+
* Added better handling for namespaced controllers;
|
4
|
+
* Added flash messages scoped by namespaced controllers;
|
5
|
+
* Deprecated {{resource}} in I18n, use {{resource_name}} instead.
|
6
|
+
|
1
7
|
# Version 0.4.3
|
2
8
|
|
3
9
|
* rspec bug fix is not automatically required anymore. User has to do it
|
data/README
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
Inherited Resources
|
2
2
|
License: MIT
|
3
|
-
Version: 0.4.
|
3
|
+
Version: 0.4.4
|
4
4
|
|
5
5
|
You can also read this README in pretty html at the GitHub project Wiki page:
|
6
6
|
|
@@ -265,9 +265,10 @@ is also localized and it means you can set:
|
|
265
265
|
flash:
|
266
266
|
actions:
|
267
267
|
create:
|
268
|
-
notice: "Hooray! {{
|
268
|
+
notice: "Hooray! {{resource_name}} was successfully created!"
|
269
269
|
|
270
|
-
It will replace {{
|
270
|
+
It will replace {{resource_name}} by the human name of the resource class,
|
271
|
+
which is "Car" in this case.
|
271
272
|
|
272
273
|
But sometimes, flash messages are not that simple. Going back
|
273
274
|
to cars example, you might want to say the brand of the car when it's
|
@@ -289,6 +290,13 @@ Then you will finally have:
|
|
289
290
|
|
290
291
|
'Hooray! You just tuned your Aston Martin!'
|
291
292
|
|
293
|
+
If your controller is namespaced, for example Deluxe::CarsController, the
|
294
|
+
the messages will be checked in the following order:
|
295
|
+
|
296
|
+
flash.deluxe.cars.create.status
|
297
|
+
flash.cars.create.status
|
298
|
+
flash.actions.create.status
|
299
|
+
|
292
300
|
Belongs to
|
293
301
|
----------
|
294
302
|
|
@@ -117,9 +117,9 @@
|
|
117
117
|
# flash:
|
118
118
|
# actions:
|
119
119
|
# update:
|
120
|
-
# notice: "Hooray! {{
|
120
|
+
# notice: "Hooray! {{resource_name}} was updated with success!"
|
121
121
|
#
|
122
|
-
# It will replace {{
|
122
|
+
# It will replace {{resource_name}} by Project.human_name, which is also localized
|
123
123
|
# (check http://rails-i18n.org/wiki/pages/i18n-rails-guide for more info).
|
124
124
|
#
|
125
125
|
# But sometimes, flash messages are not that simple. You might want to say the
|
@@ -225,7 +225,7 @@ module InheritedResources
|
|
225
225
|
object = build_resource(params[resource_instance_name])
|
226
226
|
|
227
227
|
if object.save
|
228
|
-
set_flash_message!(:notice, '{{
|
228
|
+
set_flash_message!(:notice, '{{resource_name}} was successfully created.')
|
229
229
|
|
230
230
|
respond_to(:with => object, :status => :created, :location => resource_url) do |format|
|
231
231
|
block.call args_for_block(block, format, true) if block_given?
|
@@ -247,7 +247,7 @@ module InheritedResources
|
|
247
247
|
object = resource
|
248
248
|
|
249
249
|
if object.update_attributes(params[resource_instance_name])
|
250
|
-
set_flash_message!(:notice, '{{
|
250
|
+
set_flash_message!(:notice, '{{resource_name}} was successfully updated.')
|
251
251
|
|
252
252
|
respond_to do |format|
|
253
253
|
block.call args_for_block(block, format, true) if block_given?
|
@@ -269,7 +269,7 @@ module InheritedResources
|
|
269
269
|
def destroy
|
270
270
|
resource.destroy
|
271
271
|
|
272
|
-
set_flash_message!(:notice, '{{
|
272
|
+
set_flash_message!(:notice, '{{resource_name}} was successfully destroyed.')
|
273
273
|
|
274
274
|
respond_to do |format|
|
275
275
|
yield(format) if block_given?
|
@@ -58,11 +58,11 @@ module InheritedResources #:nodoc:
|
|
58
58
|
# @current_user.projects.find(params[:id])
|
59
59
|
#
|
60
60
|
# The variable set in begin_of_association_chain is not sent when building
|
61
|
-
# urls, so this is never going to happen:
|
61
|
+
# urls, so this is never going to happen when calling resource_url:
|
62
62
|
#
|
63
63
|
# project_url(@current_user, @project)
|
64
64
|
#
|
65
|
-
# If the user actually scopes the url, you should
|
65
|
+
# If the user actually scopes the url, you should use belongs_to method
|
66
66
|
# and declare that projects belong to user.
|
67
67
|
#
|
68
68
|
def begin_of_association_chain
|
@@ -154,7 +154,7 @@ module InheritedResources #:nodoc:
|
|
154
154
|
# flash:
|
155
155
|
# actions:
|
156
156
|
# create:
|
157
|
-
# notice: "Hooray! {{
|
157
|
+
# notice: "Hooray! {{resource_name}} was successfully created!"
|
158
158
|
#
|
159
159
|
# But sometimes, flash messages are not that simple. Going back
|
160
160
|
# to cars example, you might want to say the brand of the car when it's
|
@@ -176,20 +176,47 @@ module InheritedResources #:nodoc:
|
|
176
176
|
#
|
177
177
|
# 'Hooray! You just tuned your Aston Martin!'
|
178
178
|
#
|
179
|
+
# If your controller is namespaced, for example Deluxe::CarsController, the
|
180
|
+
# the messages will be checked in the following order:
|
181
|
+
#
|
182
|
+
# flash.deluxe.cars.create.status
|
183
|
+
# flash.cars.create.status
|
184
|
+
# flash.actions.create.status
|
185
|
+
#
|
179
186
|
def set_flash_message!(status, default_message = '')
|
180
|
-
options = {
|
181
|
-
:default => [ :"flash
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
187
|
+
options = interpolation_options.merge({
|
188
|
+
:default => [ :"flash.#{controller_name}.#{action_name}.#{status}",
|
189
|
+
:"flash.actions.#{action_name}.#{status}",
|
190
|
+
default_message ],
|
191
|
+
:resource_name => resource_class.human_name,
|
192
|
+
})
|
193
|
+
|
194
|
+
unless controller_path == controller_name
|
195
|
+
options[:default].unshift(:"flash.#{controller_path.gsub('/','.')}.#{action_name}.#{status}")
|
196
|
+
end
|
197
|
+
|
198
|
+
# TODO Deprecate this whole begin/rescue block and replace it for:
|
199
|
+
#
|
200
|
+
# message = I18n.t options[:default].shift, options
|
201
|
+
#
|
202
|
+
first = options[:default].shift
|
203
|
+
begin
|
204
|
+
message = I18n.t first, options
|
205
|
+
rescue Exception => e
|
206
|
+
options[:resource] = options[:resource_name]
|
207
|
+
warn "[DEPRECATION] {{resource}} is deprecated as interpolation option " <<
|
208
|
+
"in InheritedResources I18n. Please use {{resource_name}} instead."
|
209
|
+
message = I18n.t first, options
|
210
|
+
end
|
186
211
|
|
187
212
|
flash[status] = message unless message.blank?
|
188
213
|
end
|
189
214
|
|
190
215
|
# Overwrite this method to provide other interpolation options when
|
191
|
-
# the flash message is going to be set.
|
192
|
-
#
|
216
|
+
# the flash message is going to be set.
|
217
|
+
#
|
218
|
+
# You cannot overwrite :resource_name and :default options using this
|
219
|
+
# method. Check <tt>set_flash_message!</tt> for more information.
|
193
220
|
#
|
194
221
|
def interpolation_options
|
195
222
|
{ }
|
@@ -12,6 +12,9 @@ module InheritedResources #:nodoc:
|
|
12
12
|
# association chain.
|
13
13
|
#
|
14
14
|
def evaluate_parent(parent_config, chain = nil)
|
15
|
+
instantiated_object = instance_variable_get("@#{parent_config[:instance_name]}")
|
16
|
+
return instantiated_object if instantiated_object
|
17
|
+
|
15
18
|
scoped_parent = if chain
|
16
19
|
chain.send(parent_config[:collection_name])
|
17
20
|
else
|
@@ -259,9 +259,11 @@ module ActionController #:nodoc:
|
|
259
259
|
private
|
260
260
|
|
261
261
|
# Define template_exists? for Rails 2.3
|
262
|
-
unless ActionController::Base.private_instance_methods.include?
|
262
|
+
unless ActionController::Base.private_instance_methods.include?('template_exists?') ||
|
263
|
+
ActionController::Base.private_instance_methods.include?(:template_exists?)
|
264
|
+
|
263
265
|
def template_exists?
|
264
|
-
|
266
|
+
default_template ? true : false
|
265
267
|
rescue ActionView::MissingTemplate
|
266
268
|
false
|
267
269
|
end
|
data/test/base_helpers_test.rb
CHANGED
@@ -11,6 +11,14 @@ class AddressesController < InheritedResources::Base
|
|
11
11
|
end
|
12
12
|
end
|
13
13
|
|
14
|
+
module Admin; end
|
15
|
+
class Admin::AddressesController < InheritedResources::Base
|
16
|
+
protected
|
17
|
+
def interpolation_options
|
18
|
+
{ :reference => 'Ocean Avenue' }
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
14
22
|
class FlashBaseHelpersTest < TEST_CLASS
|
15
23
|
|
16
24
|
def setup
|
@@ -27,6 +35,14 @@ class FlashBaseHelpersTest < TEST_CLASS
|
|
27
35
|
assert_equal 'You created a new address close to <b>Ocean Avenue</b>.', flash[:notice]
|
28
36
|
end
|
29
37
|
|
38
|
+
def test_default_success_flash_message_on_create_for_namespaced_controller
|
39
|
+
@controller = Admin::AddressesController.new
|
40
|
+
Address.stubs(:new).returns(mock_address(:save => true))
|
41
|
+
@controller.stubs(:address_url)
|
42
|
+
post :create
|
43
|
+
assert_equal 'Admin, you created a new address close to <b>Ocean Avenue</b>.', flash[:notice]
|
44
|
+
end
|
45
|
+
|
30
46
|
def test_set_success_flash_message_on_update
|
31
47
|
Address.stubs(:find).returns(mock_address(:update_attributes => true))
|
32
48
|
put :update
|
data/test/fixtures/en.yml
CHANGED
@@ -4,6 +4,10 @@ en:
|
|
4
4
|
create:
|
5
5
|
notice: "You created a new address close to <b>{{reference}}</b>."
|
6
6
|
update:
|
7
|
-
notice: "Nice! {{
|
7
|
+
notice: "Nice! {{resource_name}} was updated with success!"
|
8
8
|
error: "Oh no! We could not update your address!"
|
9
|
+
admin:
|
10
|
+
addresses:
|
11
|
+
create:
|
12
|
+
notice: "Admin, you created a new address close to <b>{{reference}}</b>."
|
9
13
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: josevalim-inherited_resources
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- "Jos\xC3\xA9 Valim"
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-02-
|
12
|
+
date: 2009-02-27 00:00:00 -08:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|