karsthammer-inherited_resources 1.1.2.1 → 1.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/CHANGELOG +11 -0
- data/Gemfile +12 -0
- data/Gemfile.lock +86 -0
- data/README.rdoc +78 -50
- data/Rakefile +0 -22
- data/karsthammer-inherited_resources.gemspec +24 -0
- data/lib/inherited_resources/actions.rb +4 -4
- data/lib/inherited_resources/base.rb +11 -10
- data/lib/inherited_resources/base_helpers.rb +40 -3
- data/lib/inherited_resources/class_methods.rb +100 -9
- data/lib/inherited_resources/shallow_helpers.rb +87 -0
- data/lib/inherited_resources/url_helpers.rb +44 -4
- data/lib/inherited_resources/version.rb +1 -1
- data/lib/karsthammer-inherited_resources.rb +7 -2
- data/test/base_test.rb +33 -13
- data/test/belongs_to_test.rb +29 -4
- data/test/belongs_to_with_shallow_test.rb +93 -0
- data/test/class_methods_test.rb +13 -11
- data/test/customized_belongs_to_test.rb +1 -1
- data/test/customized_redirect_to_test.rb +32 -0
- data/test/defaults_test.rb +137 -10
- data/test/locales/en.yml +17 -0
- data/test/nested_belongs_to_with_shallow_test.rb +123 -0
- data/test/nested_model_with_shallow_test.rb +147 -0
- data/test/optional_belongs_to_test.rb +3 -3
- data/test/polymorphic_test.rb +4 -4
- data/test/singleton_test.rb +4 -1
- data/test/test_helper.rb +4 -3
- data/test/url_helpers_test.rb +146 -2
- data/test/views/cars/edit.html.erb +1 -0
- data/test/views/cars/index.html.erb +1 -0
- data/test/views/cars/new.html.erb +1 -0
- data/test/views/cars/show.html.erb +1 -0
- data/test/views/cities/edit.html.erb +1 -0
- data/test/views/cities/index.html.erb +1 -0
- data/test/views/cities/new.html.erb +1 -0
- data/test/views/cities/show.html.erb +1 -0
- data/test/views/comments/edit.html.erb +1 -0
- data/test/views/comments/index.html.erb +1 -0
- data/test/views/comments/new.html.erb +1 -0
- data/test/views/comments/show.html.erb +1 -0
- data/test/views/educations/new.html.erb +0 -0
- data/test/views/employees/edit.html.erb +1 -0
- data/test/views/employees/index.html.erb +1 -0
- data/test/views/employees/new.html.erb +1 -0
- data/test/views/employees/show.html.erb +1 -0
- data/test/views/groups/edit.html.erb +0 -0
- data/test/views/managers/edit.html.erb +1 -0
- data/test/views/managers/new.html.erb +1 -0
- data/test/views/managers/show.html.erb +1 -0
- data/test/views/painters/edit.html.erb +1 -0
- data/test/views/painters/index.html.erb +1 -0
- data/test/views/painters/new.html.erb +1 -0
- data/test/views/painters/show.html.erb +1 -0
- data/test/views/pets/edit.html.erb +1 -0
- data/test/views/pets/index.html.erb +1 -0
- data/test/views/pets/new.html.erb +1 -0
- data/test/views/pets/show.html.erb +1 -0
- data/test/views/plates/edit.html.erb +1 -0
- data/test/views/plates/index.html.erb +1 -0
- data/test/views/plates/new.html.erb +1 -0
- data/test/views/plates/show.html.erb +1 -0
- data/test/views/products/edit.html.erb +1 -0
- data/test/views/products/index.html.erb +1 -0
- data/test/views/products/new.html.erb +1 -0
- data/test/views/products/show.html.erb +1 -0
- data/test/views/professors/edit.html.erb +1 -0
- data/test/views/professors/index.html.erb +1 -0
- data/test/views/professors/new.html.erb +1 -0
- data/test/views/professors/show.html.erb +1 -0
- data/test/views/projects/index.html.erb +1 -0
- data/test/views/projects/index.json.erb +1 -0
- data/test/views/projects/respond_to_skip_default_template.html.erb +1 -0
- data/test/views/projects/respond_with_resource.html.erb +1 -0
- data/test/views/students/edit.html.erb +1 -0
- data/test/views/students/new.html.erb +1 -0
- data/test/views/tags/edit.html.erb +1 -0
- data/test/views/tags/index.html.erb +1 -0
- data/test/views/tags/new.html.erb +1 -0
- data/test/views/tags/show.html.erb +1 -0
- data/test/views/trees/edit.html.erb +1 -0
- data/test/views/trees/index.html.erb +1 -0
- data/test/views/trees/new.html.erb +1 -0
- data/test/views/trees/show.html.erb +1 -0
- data/test/views/university/professors/edit.html.erb +1 -0
- data/test/views/university/professors/index.html.erb +1 -0
- data/test/views/university/professors/new.html.erb +1 -0
- data/test/views/university/professors/show.html.erb +1 -0
- data/test/views/users/create.js.erb +1 -0
- data/test/views/users/destroy.js.erb +1 -0
- data/test/views/users/edit.html.erb +1 -0
- data/test/views/users/index.html.erb +1 -0
- data/test/views/users/new.html.erb +1 -0
- data/test/views/users/show.html.erb +1 -0
- data/test/views/users/update.js.erb +1 -0
- metadata +178 -29
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
.bundle
|
data/CHANGELOG
CHANGED
@@ -1,3 +1,14 @@
|
|
1
|
+
# Version 1.2.1
|
2
|
+
|
3
|
+
* Fix a bug with namespaces
|
4
|
+
* Use Post.scoped instead of Post.all in collection
|
5
|
+
|
6
|
+
# Version 1.2
|
7
|
+
|
8
|
+
* Improved lookup for namespaces (by github.com/Sirupsen)
|
9
|
+
* Support to custom actions (by github.com/lda)
|
10
|
+
* Rails 3.1 compatibility (by github.com/etehtsea)
|
11
|
+
|
1
12
|
# Version 1.1
|
2
13
|
|
3
14
|
* Rails 3 compatible
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
abstract (1.0.0)
|
5
|
+
actionmailer (3.0.3)
|
6
|
+
actionpack (= 3.0.3)
|
7
|
+
mail (~> 2.2.9)
|
8
|
+
actionpack (3.0.3)
|
9
|
+
activemodel (= 3.0.3)
|
10
|
+
activesupport (= 3.0.3)
|
11
|
+
builder (~> 2.1.2)
|
12
|
+
erubis (~> 2.6.6)
|
13
|
+
i18n (~> 0.4)
|
14
|
+
rack (~> 1.2.1)
|
15
|
+
rack-mount (~> 0.6.13)
|
16
|
+
rack-test (~> 0.5.6)
|
17
|
+
tzinfo (~> 0.3.23)
|
18
|
+
activemodel (3.0.3)
|
19
|
+
activesupport (= 3.0.3)
|
20
|
+
builder (~> 2.1.2)
|
21
|
+
i18n (~> 0.4)
|
22
|
+
activerecord (3.0.3)
|
23
|
+
activemodel (= 3.0.3)
|
24
|
+
activesupport (= 3.0.3)
|
25
|
+
arel (~> 2.0.2)
|
26
|
+
tzinfo (~> 0.3.23)
|
27
|
+
activeresource (3.0.3)
|
28
|
+
activemodel (= 3.0.3)
|
29
|
+
activesupport (= 3.0.3)
|
30
|
+
activesupport (3.0.3)
|
31
|
+
arel (2.0.6)
|
32
|
+
builder (2.1.2)
|
33
|
+
columnize (0.3.1)
|
34
|
+
erubis (2.6.6)
|
35
|
+
abstract (>= 1.0.0)
|
36
|
+
has_scope (0.5.0)
|
37
|
+
i18n (0.5.0)
|
38
|
+
linecache (0.43)
|
39
|
+
mail (2.2.14)
|
40
|
+
activesupport (>= 2.3.6)
|
41
|
+
i18n (>= 0.4.0)
|
42
|
+
mime-types (~> 1.16)
|
43
|
+
treetop (~> 1.4.8)
|
44
|
+
mime-types (1.16)
|
45
|
+
mocha (0.9.8)
|
46
|
+
rake
|
47
|
+
polyglot (0.3.1)
|
48
|
+
rack (1.2.1)
|
49
|
+
rack-mount (0.6.13)
|
50
|
+
rack (>= 1.0.0)
|
51
|
+
rack-test (0.5.7)
|
52
|
+
rack (>= 1.0)
|
53
|
+
rails (3.0.3)
|
54
|
+
actionmailer (= 3.0.3)
|
55
|
+
actionpack (= 3.0.3)
|
56
|
+
activerecord (= 3.0.3)
|
57
|
+
activeresource (= 3.0.3)
|
58
|
+
activesupport (= 3.0.3)
|
59
|
+
bundler (~> 1.0)
|
60
|
+
railties (= 3.0.3)
|
61
|
+
railties (3.0.3)
|
62
|
+
actionpack (= 3.0.3)
|
63
|
+
activesupport (= 3.0.3)
|
64
|
+
rake (>= 0.8.7)
|
65
|
+
thor (~> 0.14.4)
|
66
|
+
rake (0.8.7)
|
67
|
+
responders (0.6.2)
|
68
|
+
ruby-debug (0.10.3)
|
69
|
+
columnize (>= 0.1)
|
70
|
+
ruby-debug-base (~> 0.10.3.0)
|
71
|
+
ruby-debug-base (0.10.3)
|
72
|
+
linecache (>= 0.3)
|
73
|
+
thor (0.14.6)
|
74
|
+
treetop (1.4.9)
|
75
|
+
polyglot (>= 0.3.1)
|
76
|
+
tzinfo (0.3.24)
|
77
|
+
|
78
|
+
PLATFORMS
|
79
|
+
ruby
|
80
|
+
|
81
|
+
DEPENDENCIES
|
82
|
+
has_scope (~> 0.5.0)
|
83
|
+
mocha
|
84
|
+
rails (= 3.0.3)
|
85
|
+
responders (~> 0.6.0)
|
86
|
+
ruby-debug
|
data/README.rdoc
CHANGED
@@ -5,27 +5,12 @@ 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
|
Plus, making your controllers follow a pattern, it helps you to write better
|
8
|
-
code by following fat models and skinny controllers convention. There
|
9
|
-
|
8
|
+
code by following fat models and skinny controllers convention. There are
|
9
|
+
two screencasts available besides this README:
|
10
10
|
|
11
|
+
http://railscasts.com/episodes/230-inherited-resources
|
11
12
|
http://akitaonrails.com/2009/09/01/screencast-real-thin-restful-controllers-with-inherited-resources
|
12
13
|
|
13
|
-
=== What about views?
|
14
|
-
|
15
|
-
Sometimes just DRY the controllers is not enough, a lot of the resources often share the same views, wouldn't it be nice to have view files DRY'ed too? You can! There are currently two projects do just that.
|
16
|
-
|
17
|
-
==== Inherited Resources Views
|
18
|
-
|
19
|
-
Inherited Resources Views is an extremely easy to use plugin to DRY the views associated with your inherited resources controllers. It is library-agnostic (it only depends on Inherited Resources) and it supports generating both erb and haml templates.
|
20
|
-
|
21
|
-
Source: http://github.com/fredwu/inherited_resources_views
|
22
|
-
|
23
|
-
==== Inherited Views
|
24
|
-
|
25
|
-
Inherited Views is a thin addition to the Inherited Resources project adding default html views which can be later customized on a per controller basis or globally for your rails app. It depends on Inherited Resources, Formtastic and WillPaginate.
|
26
|
-
|
27
|
-
Source: http://github.com/gregbell/inherited_views
|
28
|
-
|
29
14
|
== Installation
|
30
15
|
|
31
16
|
=== Rails 3
|
@@ -34,7 +19,7 @@ Inherited Resources master branch is now supports Rails 3 and is NOT backward co
|
|
34
19
|
|
35
20
|
You can let bundler to install Inherited Resources by adding this line to your application's Gemfile:
|
36
21
|
|
37
|
-
gem 'inherited_resources', '1.1
|
22
|
+
gem 'inherited_resources', '~> 1.2.1'
|
38
23
|
|
39
24
|
And then execute:
|
40
25
|
|
@@ -42,7 +27,7 @@ And then execute:
|
|
42
27
|
|
43
28
|
Or install it yourself as:
|
44
29
|
|
45
|
-
gem install inherited_resources --version=1.1
|
30
|
+
gem install inherited_resources --version=1.2.1
|
46
31
|
|
47
32
|
=== Rails 2.3.x
|
48
33
|
|
@@ -66,7 +51,7 @@ has_scope gem is available at:
|
|
66
51
|
|
67
52
|
And can be installed as:
|
68
53
|
|
69
|
-
|
54
|
+
gem install has_scope
|
70
55
|
|
71
56
|
== Responders
|
72
57
|
|
@@ -79,23 +64,14 @@ how it will change your application:
|
|
79
64
|
|
80
65
|
And it can be installed as:
|
81
66
|
|
82
|
-
|
67
|
+
gem install responders
|
83
68
|
|
84
69
|
Using responders will set the flash message to :notice and :alert. You can change
|
85
70
|
that through the following configuration value:
|
86
71
|
|
87
72
|
InheritedResources.flash_keys = [ :success, :failure ]
|
88
73
|
|
89
|
-
|
90
|
-
|
91
|
-
Rspec monkey patches a couple of controller methods in a way that Controller specs
|
92
|
-
(with integrate views true or false) and Inherited Resources are not compatible.
|
93
|
-
|
94
|
-
However, since your controllers inherit from InheritedResources::Base, they are
|
95
|
-
already unit-tested in the plugin, so there is no need to test them again in Rspec.
|
96
|
-
|
97
|
-
You should test things like url redirection and associations in your integration
|
98
|
-
specs.
|
74
|
+
Notice the CollectionResponder won't work with InheritedResources, as InheritedResources hardcodes the redirect path based on the current scope (like belongs to, polymorphic associations, etc).
|
99
75
|
|
100
76
|
== Basic Usage
|
101
77
|
|
@@ -307,6 +283,25 @@ give a block that expects two arguments, the first will be executed only in
|
|
307
283
|
success scenarios and the second in failure scenarios. You keep everything
|
308
284
|
clean and organized inside the same action.
|
309
285
|
|
286
|
+
== Smart redirects
|
287
|
+
|
288
|
+
Although the syntax above is a nice shortcut, you won't need to do it frequently
|
289
|
+
because (since version 1.2) Inherited Resources has smart redirects. Redirects
|
290
|
+
in actions calculates depending on the existent controller methods.
|
291
|
+
|
292
|
+
Redirects in create and update actions calculates in following order resource_url,
|
293
|
+
collection_url, parent_url (which we are going to see later), root_url. Redirect
|
294
|
+
in destroy action calculate in following order collection_url, parent_url, root_url.
|
295
|
+
|
296
|
+
Example:
|
297
|
+
|
298
|
+
class ButtonsConntroller < InheritedResources::Base
|
299
|
+
belongs_to :window
|
300
|
+
actions :all, :except => [:show, :index]
|
301
|
+
end
|
302
|
+
|
303
|
+
This controller redirect to parent window after all CUD actions.
|
304
|
+
|
310
305
|
== Success and failure scenarios on destroy
|
311
306
|
|
312
307
|
The destroy action can also fail, this usually happens when you have a
|
@@ -322,24 +317,6 @@ be something like this:
|
|
322
317
|
end
|
323
318
|
end
|
324
319
|
|
325
|
-
== Some DSL
|
326
|
-
|
327
|
-
For those DSL lovers, InheritedResources won't leave you alone. You can overwrite
|
328
|
-
your success/failure blocks straight from your class binding. For it, you just
|
329
|
-
need to add a DSL module to your application controller:
|
330
|
-
|
331
|
-
class ApplicationController < ActionController::Base
|
332
|
-
include InheritedResources::DSL
|
333
|
-
end
|
334
|
-
|
335
|
-
And then you can rewrite the last example as:
|
336
|
-
|
337
|
-
class ProjectsController < InheritedResources::Base
|
338
|
-
update! do |success, failure|
|
339
|
-
failure.html { redirect_to project_url(@project) }
|
340
|
-
end
|
341
|
-
end
|
342
|
-
|
343
320
|
== Belongs to
|
344
321
|
|
345
322
|
Finally, our Projects are going to get some Tasks. Then you create a
|
@@ -463,6 +440,20 @@ option.
|
|
463
440
|
|
464
441
|
It will deal with everything again and hide the action :index from you.
|
465
442
|
|
443
|
+
== Namespaced Controllers
|
444
|
+
|
445
|
+
Namespaced controllers works out the box.
|
446
|
+
|
447
|
+
class Forum::PostsController < InheritedResources::Base
|
448
|
+
end
|
449
|
+
|
450
|
+
Inherited Resources prioritizes the default resource class for the namespaced controller in
|
451
|
+
this order:
|
452
|
+
|
453
|
+
Forum::Post
|
454
|
+
ForumPost
|
455
|
+
Post
|
456
|
+
|
466
457
|
== URL Helpers
|
467
458
|
|
468
459
|
When you use InheritedResources it creates some URL helpers.
|
@@ -506,6 +497,43 @@ Another nice thing is that those urls are not guessed during runtime. They are
|
|
506
497
|
all created when your application is loaded (except for polymorphic
|
507
498
|
associations, that relies on Rails polymorphic_url).
|
508
499
|
|
500
|
+
== Custom actions
|
501
|
+
|
502
|
+
Since version 1.2, Inherited Resources allows you to define custom actions in controller:
|
503
|
+
|
504
|
+
class ButtonsController < InheritedResources::Base
|
505
|
+
custom_actions :resource => :delete, :collection => :search
|
506
|
+
end
|
507
|
+
|
508
|
+
This code creates delete and search actions in controller (they behaves like show and
|
509
|
+
index actions accordingly). Also, it will produce delete_resource_{path,url} and
|
510
|
+
search_resources_{path,url} url helpers.
|
511
|
+
|
512
|
+
== What about views?
|
513
|
+
|
514
|
+
Sometimes just DRY the controllers is not enough, if you need to DRY up your views,
|
515
|
+
check this Wiki page:
|
516
|
+
|
517
|
+
https://github.com/josevalim/inherited_resources/wiki/Views-Inheritance
|
518
|
+
|
519
|
+
== Some DSL
|
520
|
+
|
521
|
+
For those DSL lovers, InheritedResources won't leave you alone. You can overwrite
|
522
|
+
your success/failure blocks straight from your class binding. For it, you just
|
523
|
+
need to add a DSL module to your application controller:
|
524
|
+
|
525
|
+
class ApplicationController < ActionController::Base
|
526
|
+
include InheritedResources::DSL
|
527
|
+
end
|
528
|
+
|
529
|
+
And then you can rewrite the last example as:
|
530
|
+
|
531
|
+
class ProjectsController < InheritedResources::Base
|
532
|
+
update! do |success, failure|
|
533
|
+
failure.html { redirect_to project_url(@project) }
|
534
|
+
end
|
535
|
+
end
|
536
|
+
|
509
537
|
== Bugs and Feedback
|
510
538
|
|
511
539
|
If you discover any bugs or want to drop a line, join us in the mailing list:
|
data/Rakefile
CHANGED
@@ -3,28 +3,6 @@
|
|
3
3
|
require 'rake'
|
4
4
|
require 'rake/testtask'
|
5
5
|
require 'rake/rdoctask'
|
6
|
-
require File.join(File.dirname(__FILE__), 'lib', 'inherited_resources', 'version')
|
7
|
-
|
8
|
-
begin
|
9
|
-
require 'jeweler'
|
10
|
-
Jeweler::Tasks.new do |s|
|
11
|
-
s.name = "inherited_resources"
|
12
|
-
s.version = InheritedResources::VERSION.dup
|
13
|
-
s.rubyforge_project = "inherited_resources"
|
14
|
-
s.summary = "Inherited Resources speeds up development by making your controllers inherit all restful actions so you just have to focus on what is important."
|
15
|
-
s.email = "jose.valim@gmail.com"
|
16
|
-
s.homepage = "http://github.com/josevalim/inherited_resources"
|
17
|
-
s.description = "Inherited Resources speeds up development by making your controllers inherit all restful actions so you just have to focus on what is important."
|
18
|
-
s.authors = ['José Valim']
|
19
|
-
s.files = FileList["[A-Z]*", "init.rb", "{lib}/**/*"]
|
20
|
-
s.add_dependency("responders", "~> 0.6.0")
|
21
|
-
s.add_dependency("has_scope", "~> 0.5.0")
|
22
|
-
end
|
23
|
-
|
24
|
-
Jeweler::GemcutterTasks.new
|
25
|
-
rescue LoadError
|
26
|
-
puts "Jeweler, or one of its dependencies, is not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
|
27
|
-
end
|
28
6
|
|
29
7
|
desc 'Run tests for InheritedResources.'
|
30
8
|
Rake::TestTask.new(:test) do |t|
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "inherited_resources/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "karsthammer-inherited_resources"
|
7
|
+
s.version = InheritedResources::VERSION.dup
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.summary = "Inherited Resources speeds up development by making your controllers inherit all restful actions so you just have to focus on what is important."
|
10
|
+
s.email = "developers@plataformatec.com.br"
|
11
|
+
s.homepage = "http://github.com/josevalim/inherited_resources"
|
12
|
+
s.description = "Inherited Resources speeds up development by making your controllers inherit all restful actions so you just have to focus on what is important."
|
13
|
+
s.authors = ['Karst Hammer', 'José Valim']
|
14
|
+
|
15
|
+
s.rubyforge_project = "inherited_resources"
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
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
|
+
s.require_paths = ["lib"]
|
21
|
+
|
22
|
+
s.add_dependency("responders", "~> 0.6.0")
|
23
|
+
s.add_dependency("has_scope", "~> 0.5.0")
|
24
|
+
end
|
@@ -31,7 +31,7 @@ module InheritedResources
|
|
31
31
|
object = build_resource
|
32
32
|
|
33
33
|
if create_resource(object)
|
34
|
-
options[:location] ||=
|
34
|
+
options[:location] ||= smart_resource_url
|
35
35
|
end
|
36
36
|
|
37
37
|
respond_with_dual_blocks(object, options, &block)
|
@@ -42,8 +42,8 @@ module InheritedResources
|
|
42
42
|
def update(options={}, &block)
|
43
43
|
object = resource
|
44
44
|
|
45
|
-
if update_resource(object,
|
46
|
-
options[:location] ||=
|
45
|
+
if update_resource(object, resource_params)
|
46
|
+
options[:location] ||= smart_resource_url
|
47
47
|
end
|
48
48
|
|
49
49
|
respond_with_dual_blocks(object, options, &block)
|
@@ -53,7 +53,7 @@ module InheritedResources
|
|
53
53
|
# DELETE /resources/1
|
54
54
|
def destroy(options={}, &block)
|
55
55
|
object = resource
|
56
|
-
options[:location] ||=
|
56
|
+
options[:location] ||= smart_collection_url
|
57
57
|
|
58
58
|
destroy_resource(object)
|
59
59
|
respond_with_dual_blocks(object, options, &block)
|
@@ -24,18 +24,19 @@ module InheritedResources
|
|
24
24
|
respond_to :html
|
25
25
|
self.responder = InheritedResources::Responder
|
26
26
|
|
27
|
-
helper_method :
|
28
|
-
:
|
29
|
-
:
|
30
|
-
:
|
27
|
+
helper_method :resource, :collection, :resource_class, :association_chain,
|
28
|
+
:resource_instance_name, :resource_collection_name,
|
29
|
+
:resource_url, :resource_path,
|
30
|
+
:collection_url, :collection_path,
|
31
|
+
:new_resource_url, :new_resource_path,
|
32
|
+
:edit_resource_url, :edit_resource_path,
|
33
|
+
:parent_url, :parent_path,
|
34
|
+
:smart_resource_url, :smart_collection_url
|
31
35
|
|
32
|
-
|
33
|
-
c.class_inheritable_accessor :resource_class
|
34
|
-
c.class_inheritable_array :parents_symbols
|
35
|
-
c.class_inheritable_hash :resources_configuration
|
36
|
-
end
|
36
|
+
self.class_attribute :resource_class, :parents_symbols, :resources_configuration, :instance_writer => false
|
37
37
|
|
38
|
-
protected :resource_class, :parents_symbols, :resources_configuration
|
38
|
+
protected :resource_class, :parents_symbols, :resources_configuration,
|
39
|
+
:resource_class?, :parents_symbols?, :resources_configuration?
|
39
40
|
end
|
40
41
|
end
|
41
42
|
|
@@ -20,7 +20,10 @@ module InheritedResources
|
|
20
20
|
# end
|
21
21
|
#
|
22
22
|
def collection
|
23
|
-
get_collection_ivar ||
|
23
|
+
get_collection_ivar || begin
|
24
|
+
c = end_of_association_chain
|
25
|
+
set_collection_ivar(c.respond_to?(:scoped) ? c.scoped : c.all)
|
26
|
+
end
|
24
27
|
end
|
25
28
|
|
26
29
|
# This is how the resource is loaded.
|
@@ -38,7 +41,7 @@ module InheritedResources
|
|
38
41
|
# probably render a 500 error message.
|
39
42
|
#
|
40
43
|
def resource
|
41
|
-
get_resource_ivar || set_resource_ivar(end_of_association_chain.
|
44
|
+
get_resource_ivar || set_resource_ivar(end_of_association_chain.send(method_for_find, params[:id]))
|
42
45
|
end
|
43
46
|
|
44
47
|
# This method is responsable for building the object on :new and :create
|
@@ -46,7 +49,7 @@ module InheritedResources
|
|
46
49
|
# instance variable.
|
47
50
|
#
|
48
51
|
def build_resource
|
49
|
-
get_resource_ivar || set_resource_ivar(end_of_association_chain.send(method_for_build,
|
52
|
+
get_resource_ivar || set_resource_ivar(end_of_association_chain.send(method_for_build, resource_params))
|
50
53
|
end
|
51
54
|
|
52
55
|
# Responsible for saving the resource on :create method. Overwriting this
|
@@ -155,6 +158,10 @@ module InheritedResources
|
|
155
158
|
self.resources_configuration[:self][:instance_name]
|
156
159
|
end
|
157
160
|
|
161
|
+
def resource_request_name
|
162
|
+
self.resources_configuration[:self][:request_name]
|
163
|
+
end
|
164
|
+
|
158
165
|
# This methods gets your begin_of_association_chain, join it with your
|
159
166
|
# parents chain and returns the scoped association.
|
160
167
|
#
|
@@ -195,6 +202,11 @@ module InheritedResources
|
|
195
202
|
resource_collection_name
|
196
203
|
end
|
197
204
|
|
205
|
+
# Returns finder method for instantiate resource by params[:id]
|
206
|
+
def method_for_find
|
207
|
+
resources_configuration[:self][:finder] || :find
|
208
|
+
end
|
209
|
+
|
198
210
|
# Get resource ivar based on the current resource controller.
|
199
211
|
#
|
200
212
|
def get_resource_ivar #:nodoc:
|
@@ -265,6 +277,31 @@ module InheritedResources
|
|
265
277
|
[]
|
266
278
|
end
|
267
279
|
|
280
|
+
# URL to redirect to when redirect implies resource url.
|
281
|
+
def smart_resource_url
|
282
|
+
url = nil
|
283
|
+
if respond_to? :show
|
284
|
+
url = resource_url rescue nil
|
285
|
+
end
|
286
|
+
url ||= smart_collection_url
|
287
|
+
end
|
288
|
+
|
289
|
+
# URL to redirect to when redirect implies collection url.
|
290
|
+
def smart_collection_url
|
291
|
+
url = nil
|
292
|
+
if respond_to? :index
|
293
|
+
url ||= collection_url rescue nil
|
294
|
+
end
|
295
|
+
if respond_to? :parent
|
296
|
+
url ||= parent_url rescue nil
|
297
|
+
end
|
298
|
+
url ||= root_url rescue nil
|
299
|
+
end
|
300
|
+
|
301
|
+
# extract attributes from params
|
302
|
+
def resource_params
|
303
|
+
params[resource_request_name] || params[resource_instance_name] || {}
|
304
|
+
end
|
268
305
|
end
|
269
306
|
end
|
270
307
|
|