inherited_resources 1.3.0 → 1.4.0
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 +15 -1
- data/Gemfile +5 -10
- data/Gemfile.lock +79 -78
- data/MIT-LICENSE +1 -1
- data/README.rdoc +41 -23
- data/Rakefile +5 -2
- data/inherited_resources.gemspec +2 -3
- data/lib/inherited_resources/base_helpers.rb +38 -12
- data/lib/inherited_resources/belongs_to_helpers.rb +13 -5
- data/lib/inherited_resources/class_methods.rb +7 -4
- data/lib/inherited_resources/polymorphic_helpers.rb +11 -4
- data/lib/inherited_resources/url_helpers.rb +12 -6
- data/lib/inherited_resources/version.rb +1 -1
- data/test/base_test.rb +19 -2
- data/test/class_methods_test.rb +1 -1
- data/test/nested_model_with_shallow_test.rb +4 -4
- data/test/nested_singleton_test.rb +167 -0
- data/test/polymorphic_test.rb +29 -0
- data/test/singleton_test.rb +2 -1
- data/test/strong_parameters_test.rb +34 -0
- data/test/test_helper.rb +2 -1
- data/test/url_helpers_test.rb +180 -4
- data/test/views/address/edit.html.erb +1 -0
- data/test/views/address/new.html.erb +1 -0
- data/test/views/address/show.html.erb +1 -0
- data/test/views/geolocation/show.html.erb +1 -0
- data/test/views/photos/index.html.erb +1 -0
- data/test/views/venue/show.html.erb +1 -0
- data/test/views/widgets/new.html.erb +0 -0
- metadata +66 -66
data/CHANGELOG
CHANGED
|
@@ -1,3 +1,17 @@
|
|
|
1
|
+
# Version 1.4.0
|
|
2
|
+
|
|
3
|
+
* Supports Ruby 2.0.0
|
|
4
|
+
* Added support for the strong_parameters gem. See the README for more
|
|
5
|
+
* Added the ability to pass without_protection when creating/updating
|
|
6
|
+
* Fixed multi-level nested singletons
|
|
7
|
+
* Correct paths now generated for uncountable shallow resources
|
|
8
|
+
|
|
9
|
+
# Version 1.3.1
|
|
10
|
+
|
|
11
|
+
* Fix polymorphic_belongs_to to get the parent
|
|
12
|
+
* Added support for Rails 3.2
|
|
13
|
+
* Added support to responders >= 0.6.0
|
|
14
|
+
|
|
1
15
|
# Version 1.3.0
|
|
2
16
|
|
|
3
17
|
* Added support for multiple polymorphic optional nesting
|
|
@@ -35,7 +49,7 @@
|
|
|
35
49
|
To install it, please do:
|
|
36
50
|
|
|
37
51
|
sudo gem install has_scope
|
|
38
|
-
|
|
52
|
+
|
|
39
53
|
# Version 0.9
|
|
40
54
|
|
|
41
55
|
* Allow dual blocks in destroy;
|
data/Gemfile
CHANGED
|
@@ -1,12 +1,7 @@
|
|
|
1
|
-
source
|
|
1
|
+
source 'https://rubygems.org'
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
gem "responders", "~> 0.6.0"
|
|
5
|
-
gem "has_scope", "~> 0.5.0"
|
|
6
|
-
gem "mocha"
|
|
3
|
+
gemspec
|
|
7
4
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
gem "test-unit"
|
|
12
|
-
end
|
|
5
|
+
gem 'rails', '~> 3.2'
|
|
6
|
+
gem 'mocha'
|
|
7
|
+
gem 'turn'
|
data/Gemfile.lock
CHANGED
|
@@ -1,104 +1,105 @@
|
|
|
1
|
+
PATH
|
|
2
|
+
remote: .
|
|
3
|
+
specs:
|
|
4
|
+
inherited_resources (1.3.1)
|
|
5
|
+
has_scope (~> 0.5.0)
|
|
6
|
+
responders (~> 0.9)
|
|
7
|
+
|
|
1
8
|
GEM
|
|
2
|
-
remote:
|
|
9
|
+
remote: https://rubygems.org/
|
|
3
10
|
specs:
|
|
4
|
-
actionmailer (3.
|
|
5
|
-
actionpack (= 3.
|
|
6
|
-
mail (~> 2.3
|
|
7
|
-
actionpack (3.
|
|
8
|
-
activemodel (= 3.
|
|
9
|
-
activesupport (= 3.
|
|
11
|
+
actionmailer (3.2.13)
|
|
12
|
+
actionpack (= 3.2.13)
|
|
13
|
+
mail (~> 2.5.3)
|
|
14
|
+
actionpack (3.2.13)
|
|
15
|
+
activemodel (= 3.2.13)
|
|
16
|
+
activesupport (= 3.2.13)
|
|
10
17
|
builder (~> 3.0.0)
|
|
11
18
|
erubis (~> 2.7.0)
|
|
12
|
-
|
|
13
|
-
rack (~> 1.
|
|
14
|
-
rack-cache (~> 1.
|
|
15
|
-
rack-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
activemodel (3.1.0.rc4)
|
|
20
|
-
activesupport (= 3.1.0.rc4)
|
|
21
|
-
bcrypt-ruby (~> 2.1.4)
|
|
19
|
+
journey (~> 1.0.4)
|
|
20
|
+
rack (~> 1.4.5)
|
|
21
|
+
rack-cache (~> 1.2)
|
|
22
|
+
rack-test (~> 0.6.1)
|
|
23
|
+
sprockets (~> 2.2.1)
|
|
24
|
+
activemodel (3.2.13)
|
|
25
|
+
activesupport (= 3.2.13)
|
|
22
26
|
builder (~> 3.0.0)
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
27
|
+
activerecord (3.2.13)
|
|
28
|
+
activemodel (= 3.2.13)
|
|
29
|
+
activesupport (= 3.2.13)
|
|
30
|
+
arel (~> 3.0.2)
|
|
31
|
+
tzinfo (~> 0.3.29)
|
|
32
|
+
activeresource (3.2.13)
|
|
33
|
+
activemodel (= 3.2.13)
|
|
34
|
+
activesupport (= 3.2.13)
|
|
35
|
+
activesupport (3.2.13)
|
|
36
|
+
i18n (= 0.6.1)
|
|
33
37
|
multi_json (~> 1.0)
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
builder (3.0.
|
|
37
|
-
columnize (0.3.4)
|
|
38
|
+
ansi (1.4.3)
|
|
39
|
+
arel (3.0.2)
|
|
40
|
+
builder (3.0.4)
|
|
38
41
|
erubis (2.7.0)
|
|
39
|
-
has_scope (0.5.
|
|
40
|
-
hike (1.1
|
|
41
|
-
i18n (0.6.
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
mail (2.3
|
|
42
|
+
has_scope (0.5.1)
|
|
43
|
+
hike (1.2.1)
|
|
44
|
+
i18n (0.6.1)
|
|
45
|
+
journey (1.0.4)
|
|
46
|
+
json (1.7.7)
|
|
47
|
+
mail (2.5.3)
|
|
45
48
|
i18n (>= 0.4.0)
|
|
46
49
|
mime-types (~> 1.16)
|
|
47
50
|
treetop (~> 1.4.8)
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
rack
|
|
51
|
+
metaclass (0.0.1)
|
|
52
|
+
mime-types (1.21)
|
|
53
|
+
mocha (0.13.3)
|
|
54
|
+
metaclass (~> 0.0.1)
|
|
55
|
+
multi_json (1.7.2)
|
|
56
|
+
polyglot (0.3.3)
|
|
57
|
+
rack (1.4.5)
|
|
58
|
+
rack-cache (1.2)
|
|
55
59
|
rack (>= 0.4)
|
|
56
|
-
rack-
|
|
57
|
-
rack (>= 1.0.0)
|
|
58
|
-
rack-ssl (1.3.2)
|
|
60
|
+
rack-ssl (1.3.3)
|
|
59
61
|
rack
|
|
60
|
-
rack-test (0.6.
|
|
62
|
+
rack-test (0.6.2)
|
|
61
63
|
rack (>= 1.0)
|
|
62
|
-
rails (3.
|
|
63
|
-
actionmailer (= 3.
|
|
64
|
-
actionpack (= 3.
|
|
65
|
-
activerecord (= 3.
|
|
66
|
-
activeresource (= 3.
|
|
67
|
-
activesupport (= 3.
|
|
64
|
+
rails (3.2.13)
|
|
65
|
+
actionmailer (= 3.2.13)
|
|
66
|
+
actionpack (= 3.2.13)
|
|
67
|
+
activerecord (= 3.2.13)
|
|
68
|
+
activeresource (= 3.2.13)
|
|
69
|
+
activesupport (= 3.2.13)
|
|
68
70
|
bundler (~> 1.0)
|
|
69
|
-
railties (= 3.
|
|
70
|
-
railties (3.
|
|
71
|
-
actionpack (= 3.
|
|
72
|
-
activesupport (= 3.
|
|
71
|
+
railties (= 3.2.13)
|
|
72
|
+
railties (3.2.13)
|
|
73
|
+
actionpack (= 3.2.13)
|
|
74
|
+
activesupport (= 3.2.13)
|
|
73
75
|
rack-ssl (~> 1.3.2)
|
|
74
76
|
rake (>= 0.8.7)
|
|
75
77
|
rdoc (~> 3.4)
|
|
76
|
-
thor (
|
|
77
|
-
rake (0.
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
responders (0.
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
linecache (>= 0.3)
|
|
86
|
-
sprockets (2.0.0.beta.10)
|
|
87
|
-
hike (~> 1.0)
|
|
78
|
+
thor (>= 0.14.6, < 2.0)
|
|
79
|
+
rake (10.0.4)
|
|
80
|
+
rdoc (3.12.2)
|
|
81
|
+
json (~> 1.4)
|
|
82
|
+
responders (0.9.3)
|
|
83
|
+
railties (~> 3.1)
|
|
84
|
+
sprockets (2.2.2)
|
|
85
|
+
hike (~> 1.2)
|
|
86
|
+
multi_json (~> 1.0)
|
|
88
87
|
rack (~> 1.0)
|
|
89
88
|
tilt (~> 1.1, != 1.3.0)
|
|
90
|
-
thor (0.
|
|
91
|
-
tilt (1.3.
|
|
92
|
-
treetop (1.4.
|
|
89
|
+
thor (0.17.0)
|
|
90
|
+
tilt (1.3.6)
|
|
91
|
+
treetop (1.4.12)
|
|
92
|
+
polyglot
|
|
93
93
|
polyglot (>= 0.3.1)
|
|
94
|
-
|
|
94
|
+
turn (0.9.6)
|
|
95
|
+
ansi
|
|
96
|
+
tzinfo (0.3.37)
|
|
95
97
|
|
|
96
98
|
PLATFORMS
|
|
97
99
|
ruby
|
|
98
100
|
|
|
99
101
|
DEPENDENCIES
|
|
100
|
-
|
|
102
|
+
inherited_resources!
|
|
101
103
|
mocha
|
|
102
|
-
rails (
|
|
103
|
-
|
|
104
|
-
ruby-debug
|
|
104
|
+
rails (~> 3.2)
|
|
105
|
+
turn
|
data/MIT-LICENSE
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
Copyright (c) 2009 José Valim http://blog.plataformatec.com.br
|
|
1
|
+
Copyright (c) 2009-2012 José Valim http://blog.plataformatec.com.br
|
|
2
2
|
|
|
3
3
|
Permission is hereby granted, free of charge, to any person obtaining
|
|
4
4
|
a copy of this software and associated documentation files (the
|
data/README.rdoc
CHANGED
|
@@ -4,7 +4,7 @@ Inherited Resources speeds up development by making your controllers inherit
|
|
|
4
4
|
all restful actions so you just have to focus on what is important. It makes
|
|
5
5
|
your controllers more powerful and cleaner at the same time.
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
In addition to making your controllers follow a pattern, it helps you to write better
|
|
8
8
|
code by following fat models and skinny controllers convention. There are
|
|
9
9
|
two screencasts available besides this README:
|
|
10
10
|
|
|
@@ -23,7 +23,7 @@ And then execute:
|
|
|
23
23
|
|
|
24
24
|
bundle install
|
|
25
25
|
|
|
26
|
-
Or install it yourself
|
|
26
|
+
Or install it yourself with:
|
|
27
27
|
|
|
28
28
|
gem install inherited_resources
|
|
29
29
|
|
|
@@ -57,7 +57,7 @@ how it will change your application:
|
|
|
57
57
|
|
|
58
58
|
http://github.com/plataformatec/responders
|
|
59
59
|
|
|
60
|
-
And it can be installed
|
|
60
|
+
And it can be installed with:
|
|
61
61
|
|
|
62
62
|
gem install responders
|
|
63
63
|
|
|
@@ -85,7 +85,7 @@ The next step is to define which mime types this controller provides:
|
|
|
85
85
|
respond_to :html, :xml, :json
|
|
86
86
|
end
|
|
87
87
|
|
|
88
|
-
You can also specify them
|
|
88
|
+
You can also specify them per action:
|
|
89
89
|
|
|
90
90
|
class ProjectsController < InheritedResources::Base
|
|
91
91
|
respond_to :html, :xml, :json
|
|
@@ -93,7 +93,7 @@ You can also specify them based per action:
|
|
|
93
93
|
respond_to :iphone, :except => [ :edit, :update ]
|
|
94
94
|
end
|
|
95
95
|
|
|
96
|
-
For each request, it first
|
|
96
|
+
For each request, it first checks if the "controller/action.format" file is
|
|
97
97
|
available (for example "projects/create.xml") and if it's not, it checks if
|
|
98
98
|
the resource respond to :to_format (in this case, :to_xml). Otherwise returns 404.
|
|
99
99
|
|
|
@@ -126,11 +126,19 @@ call inherit_resources in your controller class scope:
|
|
|
126
126
|
inherit_resources
|
|
127
127
|
end
|
|
128
128
|
|
|
129
|
+
One reason to use the "inherit_resources" macro would be to ensure that your controller never responds with the html mime-type. InheritedResources::Base already responds_to :html, and the respond_to macro is strictly additive. Therefore, if you want to create a controller that, for example, responds ONLY via :js, you will have write it this way:
|
|
130
|
+
|
|
131
|
+
class AccountsController < ApplicationController
|
|
132
|
+
respond_to :js
|
|
133
|
+
inherit_resources
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
|
|
129
137
|
== Overwriting defaults
|
|
130
138
|
|
|
131
139
|
Whenever you inherit from InheritedResources, several defaults are assumed.
|
|
132
|
-
For example you can have an AccountsController
|
|
133
|
-
resource is
|
|
140
|
+
For example you can have an AccountsController for account management while the
|
|
141
|
+
resource is a User:
|
|
134
142
|
|
|
135
143
|
class AccountsController < InheritedResources::Base
|
|
136
144
|
defaults :resource_class => User, :collection_name => 'users', :instance_name => 'user'
|
|
@@ -141,7 +149,7 @@ the routes used will still be accounts_url and account_url. If you plan also to
|
|
|
141
149
|
change the routes, you can use :route_collection_name and :route_instance_name.
|
|
142
150
|
|
|
143
151
|
Namespaced controllers work out of the box, but if you need to specify a
|
|
144
|
-
different route prefix
|
|
152
|
+
different route prefix you can do the following:
|
|
145
153
|
|
|
146
154
|
class Administrators::PeopleController < InheritedResources::Base
|
|
147
155
|
defaults :route_prefix => 'admin'
|
|
@@ -167,10 +175,10 @@ and scopes (more about this below).
|
|
|
167
175
|
|
|
168
176
|
InheritedResources also introduces another method called begin_of_association_chain.
|
|
169
177
|
It's mostly used when you want to create resources based on the @current_user and
|
|
170
|
-
you have urls like "account/projects". In such cases
|
|
178
|
+
you have urls like "account/projects". In such cases you have to do
|
|
171
179
|
@current_user.projects.find or @current_user.projects.build in your actions.
|
|
172
180
|
|
|
173
|
-
You can deal with it just doing:
|
|
181
|
+
You can deal with it just by doing:
|
|
174
182
|
|
|
175
183
|
class ProjectsController < InheritedResources::Base
|
|
176
184
|
protected
|
|
@@ -204,13 +212,13 @@ why all methods have aliases. So this is equivalent:
|
|
|
204
212
|
end
|
|
205
213
|
end
|
|
206
214
|
|
|
207
|
-
|
|
208
|
-
action
|
|
215
|
+
Since most of the time when you change a create, update or destroy
|
|
216
|
+
action you do so because you want to to change its redirect url, a shortcut is
|
|
209
217
|
provided. So you can do:
|
|
210
218
|
|
|
211
219
|
class ProjectsController < InheritedResources::Base
|
|
212
220
|
def destroy
|
|
213
|
-
destroy!{ root_url }
|
|
221
|
+
destroy! { root_url }
|
|
214
222
|
end
|
|
215
223
|
end
|
|
216
224
|
|
|
@@ -243,7 +251,7 @@ but you don't want to create a before filter for it:
|
|
|
243
251
|
end
|
|
244
252
|
end
|
|
245
253
|
|
|
246
|
-
Yes, that simple! The nice part is since you already set the instance variable
|
|
254
|
+
Yes, it's that simple! The nice part is since you already set the instance variable
|
|
247
255
|
@project, it will not build a project again.
|
|
248
256
|
|
|
249
257
|
Before we finish this topic, we should talk about one more thing: "success/failure
|
|
@@ -262,7 +270,7 @@ Our first attempt to do this would be:
|
|
|
262
270
|
end
|
|
263
271
|
end
|
|
264
272
|
|
|
265
|
-
Looks
|
|
273
|
+
Looks too verbose, right? We can actually do:
|
|
266
274
|
|
|
267
275
|
class ProjectsController < InheritedResources::Base
|
|
268
276
|
def update
|
|
@@ -273,7 +281,7 @@ Looks to verbose, right? We can actually do:
|
|
|
273
281
|
end
|
|
274
282
|
|
|
275
283
|
Much better! So explaining everything: when you give a block which expects one
|
|
276
|
-
argument it will be executed in both scenarios: success and failure. But
|
|
284
|
+
argument it will be executed in both scenarios: success and failure. But if you
|
|
277
285
|
give a block that expects two arguments, the first will be executed only in
|
|
278
286
|
success scenarios and the second in failure scenarios. You keep everything
|
|
279
287
|
clean and organized inside the same action.
|
|
@@ -284,13 +292,13 @@ Although the syntax above is a nice shortcut, you won't need to do it frequently
|
|
|
284
292
|
because (since version 1.2) Inherited Resources has smart redirects. Redirects
|
|
285
293
|
in actions calculates depending on the existent controller methods.
|
|
286
294
|
|
|
287
|
-
Redirects in create and update actions calculates in following order resource_url,
|
|
288
|
-
collection_url, parent_url (which we are going to see later), root_url. Redirect
|
|
295
|
+
Redirects in create and update actions calculates in the following order resource_url,
|
|
296
|
+
collection_url, parent_url (which we are going to see later), and root_url. Redirect
|
|
289
297
|
in destroy action calculate in following order collection_url, parent_url, root_url.
|
|
290
298
|
|
|
291
299
|
Example:
|
|
292
300
|
|
|
293
|
-
class
|
|
301
|
+
class ButtonsController < InheritedResources::Base
|
|
294
302
|
belongs_to :window
|
|
295
303
|
actions :all, :except => [:show, :index]
|
|
296
304
|
end
|
|
@@ -352,7 +360,7 @@ If you need to configure any of these belongs to, you can nest them using blocks
|
|
|
352
360
|
|
|
353
361
|
Warning: calling several belongs_to is the same as nesting them:
|
|
354
362
|
|
|
355
|
-
class
|
|
363
|
+
class CommentsController < InheritedResources::Base
|
|
356
364
|
belongs_to :project
|
|
357
365
|
belongs_to :task
|
|
358
366
|
end
|
|
@@ -459,7 +467,7 @@ And they handle everything for you. :)
|
|
|
459
467
|
resource_url(comment) # => /posts/1/comments/#{comment.to_param}
|
|
460
468
|
new_resource_url # => /posts/1/comments/new
|
|
461
469
|
edit_resource_url # => /posts/1/comments/#{@comment.to_param}/edit
|
|
462
|
-
edit_resource_url(comment)
|
|
470
|
+
edit_resource_url(comment) # => /posts/1/comments/#{comment.to_param}/edit
|
|
463
471
|
collection_url # => /posts/1/comments
|
|
464
472
|
parent_url # => /posts/1
|
|
465
473
|
|
|
@@ -506,7 +514,7 @@ search_resources_{path,url} url helpers.
|
|
|
506
514
|
|
|
507
515
|
== What about views?
|
|
508
516
|
|
|
509
|
-
Sometimes just
|
|
517
|
+
Sometimes just DRYing up the controllers is not enough. If you need to DRY up your views,
|
|
510
518
|
check this Wiki page:
|
|
511
519
|
|
|
512
520
|
https://github.com/josevalim/inherited_resources/wiki/Views-Inheritance
|
|
@@ -531,11 +539,21 @@ And then you can rewrite the last example as:
|
|
|
531
539
|
end
|
|
532
540
|
end
|
|
533
541
|
|
|
542
|
+
== Strong Parameters
|
|
543
|
+
|
|
544
|
+
If your controller defines a method named permitted_params, Inherited Resources will call it where it would normally call params. This allows for easy integration with the strong_parameters gem:
|
|
545
|
+
|
|
546
|
+
def permitted_params
|
|
547
|
+
params.permit(:widget => [:permitted_field, :other_permitted_field])
|
|
548
|
+
end
|
|
549
|
+
|
|
550
|
+
Note that this doesn't work if you use strong_parameters' require method instead of permit, because whereas permit returns the entire sanitized parameter hash, require returns only the sanitized params below the parameter you required.
|
|
551
|
+
|
|
534
552
|
== Bugs and Feedback
|
|
535
553
|
|
|
536
554
|
If you discover any bugs, please describe it in the issues tracker, including Rails and Inherited Resources versions.
|
|
537
555
|
|
|
538
556
|
Questions are better handled on StackOverflow.
|
|
539
557
|
|
|
540
|
-
Copyright (c)
|
|
558
|
+
Copyright (c) 2009-2012 José Valim http://blog.plataformatec.com.br
|
|
541
559
|
See the attached MIT License.
|
data/Rakefile
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
# encoding: UTF-8
|
|
2
2
|
|
|
3
|
-
require '
|
|
3
|
+
require 'bundler/gem_tasks'
|
|
4
4
|
require 'rake/testtask'
|
|
5
|
-
require '
|
|
5
|
+
require 'rdoc/task'
|
|
6
6
|
|
|
7
7
|
desc 'Run tests for InheritedResources.'
|
|
8
8
|
Rake::TestTask.new(:test) do |t|
|
|
@@ -19,3 +19,6 @@ Rake::RDocTask.new(:rdoc) do |rdoc|
|
|
|
19
19
|
rdoc.rdoc_files.include('MIT-LICENSE')
|
|
20
20
|
rdoc.rdoc_files.include('lib/**/*.rb')
|
|
21
21
|
end
|
|
22
|
+
|
|
23
|
+
# Make test the default task.
|
|
24
|
+
task :default => :test
|
data/inherited_resources.gemspec
CHANGED
|
@@ -16,9 +16,8 @@ Gem::Specification.new do |s|
|
|
|
16
16
|
|
|
17
17
|
s.files = `git ls-files`.split("\n")
|
|
18
18
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
|
19
|
-
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
|
20
19
|
s.require_paths = ["lib"]
|
|
21
20
|
|
|
22
|
-
s.add_dependency("responders", "~> 0.
|
|
21
|
+
s.add_dependency("responders", "~> 0.9")
|
|
23
22
|
s.add_dependency("has_scope", "~> 0.5.0")
|
|
24
|
-
end
|
|
23
|
+
end
|
|
@@ -67,7 +67,7 @@ module InheritedResources
|
|
|
67
67
|
end
|
|
68
68
|
|
|
69
69
|
# Responsible for updating the resource in :update method. This allow you
|
|
70
|
-
# to handle how the resource is
|
|
70
|
+
# to handle how the resource is going to be updated, let's say in a different
|
|
71
71
|
# way then the usual :update_attributes:
|
|
72
72
|
#
|
|
73
73
|
# def update_resource(object, attributes)
|
|
@@ -126,8 +126,16 @@ module InheritedResources
|
|
|
126
126
|
# current resource).
|
|
127
127
|
#
|
|
128
128
|
def association_chain
|
|
129
|
-
@association_chain
|
|
130
|
-
|
|
129
|
+
return @association_chain if @association_chain
|
|
130
|
+
|
|
131
|
+
symbol_chain = if resources_configuration[:self][:singleton]
|
|
132
|
+
symbols_for_association_chain.reverse
|
|
133
|
+
else
|
|
134
|
+
symbols_for_association_chain
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
@association_chain =
|
|
138
|
+
symbol_chain.inject([begin_of_association_chain]) do |chain, symbol|
|
|
131
139
|
chain << evaluate_parent(symbol, resources_configuration[symbol], chain.last)
|
|
132
140
|
end.compact.freeze
|
|
133
141
|
end
|
|
@@ -164,7 +172,6 @@ module InheritedResources
|
|
|
164
172
|
|
|
165
173
|
# This methods gets your begin_of_association_chain, join it with your
|
|
166
174
|
# parents chain and returns the scoped association.
|
|
167
|
-
#
|
|
168
175
|
def end_of_association_chain #:nodoc:
|
|
169
176
|
if chain = association_chain.last
|
|
170
177
|
if method_for_association_chain
|
|
@@ -267,7 +274,7 @@ module InheritedResources
|
|
|
267
274
|
# It's extend by HasScopeHelpers.
|
|
268
275
|
#
|
|
269
276
|
def apply_scopes_if_available(target_object) #:nodoc:
|
|
270
|
-
respond_to?(:apply_scopes) ? apply_scopes(target_object) : target_object
|
|
277
|
+
respond_to?(:apply_scopes, true) ? apply_scopes(target_object) : target_object
|
|
271
278
|
end
|
|
272
279
|
|
|
273
280
|
# Symbols chain in base helpers return nothing. This is later overwriten
|
|
@@ -292,27 +299,46 @@ module InheritedResources
|
|
|
292
299
|
if respond_to? :index
|
|
293
300
|
url ||= collection_url rescue nil
|
|
294
301
|
end
|
|
295
|
-
if respond_to? :parent
|
|
302
|
+
if respond_to? :parent, true
|
|
296
303
|
url ||= parent_url rescue nil
|
|
297
304
|
end
|
|
298
305
|
url ||= root_url rescue nil
|
|
299
306
|
end
|
|
300
307
|
|
|
301
|
-
#
|
|
308
|
+
# memoize the extraction of attributes from params
|
|
302
309
|
def resource_params
|
|
303
|
-
|
|
304
|
-
|
|
310
|
+
@resource_params ||= build_resource_params
|
|
311
|
+
end
|
|
312
|
+
|
|
313
|
+
# extract attributes from params
|
|
314
|
+
def build_resource_params
|
|
315
|
+
parameters = respond_to?(:permitted_params) ? permitted_params : params
|
|
316
|
+
rparams = [parameters[resource_request_name] || parameters[resource_instance_name] || {}]
|
|
317
|
+
if without_protection_given?
|
|
318
|
+
rparams << without_protection
|
|
319
|
+
else
|
|
320
|
+
rparams << as_role if role_given?
|
|
321
|
+
end
|
|
322
|
+
|
|
305
323
|
rparams
|
|
306
324
|
end
|
|
307
|
-
|
|
325
|
+
|
|
308
326
|
# checking if role given
|
|
309
327
|
def role_given?
|
|
310
328
|
self.resources_configuration[:self][:role].present?
|
|
311
329
|
end
|
|
312
|
-
|
|
330
|
+
|
|
313
331
|
# getting role for mass-asignment
|
|
314
332
|
def as_role
|
|
315
|
-
|
|
333
|
+
{ :as => self.resources_configuration[:self][:role] }
|
|
334
|
+
end
|
|
335
|
+
|
|
336
|
+
def without_protection_given?
|
|
337
|
+
self.resources_configuration[:self][:without_protection].present?
|
|
338
|
+
end
|
|
339
|
+
|
|
340
|
+
def without_protection
|
|
341
|
+
{ :without_protection => self.resources_configuration[:self][:without_protection] }
|
|
316
342
|
end
|
|
317
343
|
end
|
|
318
344
|
end
|
|
@@ -74,13 +74,21 @@ module InheritedResources
|
|
|
74
74
|
instantiated_object = instance_variable_get("@#{parent_config[:instance_name]}")
|
|
75
75
|
return instantiated_object if instantiated_object
|
|
76
76
|
|
|
77
|
-
|
|
78
|
-
chain
|
|
77
|
+
if parent_config[:singleton]
|
|
78
|
+
parent = if chain
|
|
79
|
+
chain.send(parent_config[:instance_name])
|
|
80
|
+
else
|
|
81
|
+
nil
|
|
82
|
+
end
|
|
79
83
|
else
|
|
80
|
-
|
|
81
|
-
|
|
84
|
+
parent = if chain
|
|
85
|
+
chain.send(parent_config[:collection_name])
|
|
86
|
+
else
|
|
87
|
+
parent_config[:parent_class]
|
|
88
|
+
end
|
|
82
89
|
|
|
83
|
-
|
|
90
|
+
parent = parent.send(parent_config[:finder], params[parent_config[:param]])
|
|
91
|
+
end
|
|
84
92
|
|
|
85
93
|
instance_variable_set("@#{parent_config[:instance_name]}", parent)
|
|
86
94
|
end
|
|
@@ -62,7 +62,7 @@ module InheritedResources
|
|
|
62
62
|
create_resources_url_helpers!
|
|
63
63
|
end
|
|
64
64
|
|
|
65
|
-
# Defines wich actions
|
|
65
|
+
# Defines wich actions will be inherited from the inherited controller.
|
|
66
66
|
# Syntax is borrowed from resource_controller.
|
|
67
67
|
#
|
|
68
68
|
# actions :index, :show, :edit
|
|
@@ -145,13 +145,11 @@ module InheritedResources
|
|
|
145
145
|
|
|
146
146
|
optional = options.delete(:optional)
|
|
147
147
|
shallow = options.delete(:shallow)
|
|
148
|
-
singleton = options.delete(:singleton)
|
|
149
148
|
polymorphic = options.delete(:polymorphic)
|
|
150
149
|
finder = options.delete(:finder)
|
|
151
150
|
|
|
152
151
|
include BelongsToHelpers if self.parents_symbols.empty?
|
|
153
152
|
|
|
154
|
-
acts_as_singleton! if singleton
|
|
155
153
|
acts_as_polymorphic! if polymorphic || optional
|
|
156
154
|
acts_as_shallow! if shallow
|
|
157
155
|
|
|
@@ -181,6 +179,7 @@ module InheritedResources
|
|
|
181
179
|
nil
|
|
182
180
|
end
|
|
183
181
|
|
|
182
|
+
config[:singleton] = options.delete(:singleton) || false
|
|
184
183
|
config[:collection_name] = options.delete(:collection_name) || symbol.to_s.pluralize.to_sym
|
|
185
184
|
config[:instance_name] = options.delete(:instance_name) || symbol
|
|
186
185
|
config[:param] = options.delete(:param) || :"#{symbol}_id"
|
|
@@ -259,7 +258,11 @@ module InheritedResources
|
|
|
259
258
|
# Makes sense when using rails 3.1 mass assignment conventions
|
|
260
259
|
def with_role(role)
|
|
261
260
|
self.resources_configuration[:self][:role] = role.try(:to_sym)
|
|
262
|
-
end
|
|
261
|
+
end
|
|
262
|
+
|
|
263
|
+
def without_protection(flag)
|
|
264
|
+
self.resources_configuration[:self][:without_protection] = flag
|
|
265
|
+
end
|
|
263
266
|
|
|
264
267
|
private
|
|
265
268
|
|