emmanuel-inherited_resources 0.9.1 → 0.9.2

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 CHANGED
@@ -1,3 +1,10 @@
1
+ # Version 0.9
2
+
3
+ * Added #association_chain and #context_chain to provide visibility into the
4
+ instances scoping the current resource/collection
5
+ * Added several configuration hooks (#lood_collection, #load_resource,
6
+ #build_resource_instance, #translate_flash_lookup_defaults)
7
+
1
8
  # Version 0.8
2
9
 
3
10
  * Fixed a small bug on optional belongs to with namespaced controllers.
@@ -9,44 +9,22 @@ module InheritedResources
9
9
 
10
10
  protected
11
11
 
12
- # This is how the collection is loaded.
13
- #
14
- # You might want to overwrite this method if you want to add pagination
15
- # for example. When you do that, don't forget to cache the result in an
16
- # instance_variable:
17
- #
18
- # def collection
19
- # @projects ||= end_of_association_chain.paginate(params[:page]).all
20
- # end
12
+ # If you overwrite this, don't forget to cache the result in an instance variable.
21
13
  #
22
14
  def collection
23
- get_collection_ivar || set_collection_ivar(end_of_association_chain.find(:all))
15
+ get_collection_ivar || set_collection_ivar(load_collection)
24
16
  end
25
17
 
26
- # This is how the resource is loaded.
27
- #
28
- # You might want to overwrite this method when you are using permalink.
29
- # When you do that, don't forget to cache the result in an
30
- # instance_variable:
31
- #
32
- # def resource
33
- # @project ||= end_of_association_chain.find_by_permalink!(params[:id])
34
- # end
35
- #
36
- # You also might want to add the exclamation mark at the end of the method
37
- # because it will raise a 404 if nothing can be found. Otherwise it will
38
- # probably render a 500 error message.
18
+ # If you overwrite this, don't forget to cache the result in an instance variable.
39
19
  #
40
20
  def resource
41
- get_resource_ivar || set_resource_ivar(end_of_association_chain.find(params[:id]))
21
+ get_resource_ivar || set_resource_ivar(load_resource)
42
22
  end
43
23
 
44
- # This method is responsable for building the object on :new and :create
45
- # methods. If you overwrite it, don't forget to cache the result in an
46
- # instance variable.
24
+ # If you overwrite this, don't forget to cache the result in an instance variable.
47
25
  #
48
26
  def build_resource
49
- get_resource_ivar || set_resource_ivar(end_of_association_chain.send(method_for_build, params[resource_instance_name] || {}))
27
+ get_resource_ivar || set_resource_ivar(build_resource_instance)
50
28
  end
51
29
 
52
30
  # This class allows you to set a instance variable to begin your
@@ -82,12 +60,12 @@ module InheritedResources
82
60
  false
83
61
  end
84
62
 
85
- # Overwrite this method to provide other interpolation options when
86
- # the flash message is going to be set.
63
+ # Overwrite this method to provide interpolation options for
64
+ # flash messages, and page titles
87
65
  #
88
- # def interpolation_options
89
- # { }
90
- # end
66
+ def interpolation_options
67
+ { }
68
+ end
91
69
 
92
70
  private
93
71
 
@@ -103,6 +81,18 @@ module InheritedResources
103
81
  self.resources_configuration[:self][:instance_name]
104
82
  end
105
83
 
84
+ def resource_name
85
+ if resource_class
86
+ if resource_class.respond_to?(:human_name)
87
+ resource_class.human_name
88
+ else
89
+ resource_class.name.humanize
90
+ end
91
+ else
92
+ "Resource"
93
+ end
94
+ end
95
+
106
96
  def context_chain
107
97
  @context_chain ||= begin
108
98
  chain = []
@@ -140,6 +130,41 @@ module InheritedResources
140
130
  end
141
131
  end
142
132
 
133
+ # This is how the collection is loaded.
134
+ #
135
+ # You might want to overwrite this method if you want to add pagination,
136
+ # for example:
137
+ #
138
+ # def load_collection
139
+ # end_of_association_chain.paginate(params[:page]).all
140
+ # end
141
+ #
142
+ def load_collection
143
+ end_of_association_chain.find(:all)
144
+ end
145
+
146
+ # This is how the resource is loaded.
147
+ #
148
+ # You might want to overwrite this method when you are using permalink.
149
+ #
150
+ # def load_resource
151
+ # end_of_association_chain.find_by_permalink!(params[:id])
152
+ # end
153
+ #
154
+ # You also might want to add the exclamation mark at the end of the method
155
+ # because it will raise a 404 if nothing can be found. Otherwise it will
156
+ # probably render a 500 error message.
157
+ #
158
+ def load_resource
159
+ end_of_association_chain.find(params[:id])
160
+ end
161
+
162
+ # This method is responsible for building the object on :new and :create methods.
163
+ #
164
+ def build_resource_instance
165
+ end_of_association_chain.send(method_for_build, params[resource_instance_name] || {})
166
+ end
167
+
143
168
  # Returns the appropriated method to build the resource.
144
169
  #
145
170
  def method_for_build #:nodoc:
@@ -240,37 +265,9 @@ module InheritedResources
240
265
  def set_flash_message!(status, default_message=nil)
241
266
  return flash[status] = default_message unless defined?(::I18n)
242
267
 
243
- resource_name = if resource_class
244
- if resource_class.respond_to?(:human_name)
245
- resource_class.human_name
246
- else
247
- resource_class.name.humanize
248
- end
249
- else
250
- "Resource"
251
- end
252
-
253
- given_options = if self.respond_to?(:interpolation_options)
254
- interpolation_options
255
- else
256
- {}
257
- end
258
-
259
- options = {
260
- :default => default_message || '',
261
- :resource_name => resource_name
262
- }.merge(given_options)
263
-
264
- defaults = []
265
- slices = controller_path.split('/')
266
-
267
- while slices.size > 0
268
- defaults << :"flash.#{slices.fill(controller_name, -1).join('.')}.#{action_name}.#{status}"
269
- defaults << :"flash.#{slices.fill(:actions, -1).join('.')}.#{action_name}.#{status}"
270
- slices.shift
271
- end
272
-
273
- options[:default] = defaults.push(options[:default])
268
+ options = { :resource_name => resource_name }.merge(interpolation_options)
269
+ options[:default] = translate_flash_lookup_defaults(status)
270
+ options[:default] << (default_message || '')
274
271
  options[:default].flatten!
275
272
 
276
273
  message = ::I18n.t options[:default].shift, options
@@ -315,6 +312,17 @@ module InheritedResources
315
312
  end
316
313
  end
317
314
 
315
+ # Hook to control flash message lookup strategy
316
+ def translate_flash_lookup_defaults(status, slices=controller_path.split('/'))
317
+ defaults = []
318
+ while slices.size > 0
319
+ defaults << :"flash.#{slices.fill(controller_name, -1).join('.')}.#{action_name}.#{status}"
320
+ defaults << :"flash.#{slices.fill(:actions, -1).join('.')}.#{action_name}.#{status}"
321
+ slices.shift
322
+ end
323
+ defaults
324
+ end
325
+
318
326
  # Hook to apply scopes. By default returns only the target_object given.
319
327
  # It's extend by HasScopeHelpers.
320
328
  #
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: emmanuel-inherited_resources
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.1
4
+ version: 0.9.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - "Jos\xC3\xA9 Valim"
@@ -42,6 +42,7 @@ files:
42
42
  - lib/inherited_resources/url_helpers.rb
43
43
  has_rdoc: true
44
44
  homepage: http://github.com/josevalim/inherited_resources
45
+ licenses:
45
46
  post_install_message:
46
47
  rdoc_options:
47
48
  - --charset=UTF-8
@@ -62,7 +63,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
62
63
  requirements: []
63
64
 
64
65
  rubyforge_project: inherited_resources
65
- rubygems_version: 1.2.0
66
+ rubygems_version: 1.3.5
66
67
  signing_key:
67
68
  specification_version: 3
68
69
  summary: Inherited Resources speeds up development by making your controllers inherit all restful actions so you just have to focus on what is important.