resource_controller_views 0.6.6.views2
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +22 -0
- data/README.rdoc +361 -0
- data/TODO +0 -0
- data/VERSION.yml +5 -0
- data/generators/scaffold_resource/USAGE +29 -0
- data/generators/scaffold_resource/scaffold_resource_generator.rb +179 -0
- data/generators/scaffold_resource/templates/controller.rb +2 -0
- data/generators/scaffold_resource/templates/fixtures.yml +10 -0
- data/generators/scaffold_resource/templates/functional_test.rb +57 -0
- data/generators/scaffold_resource/templates/helper.rb +2 -0
- data/generators/scaffold_resource/templates/migration.rb +15 -0
- data/generators/scaffold_resource/templates/model.rb +2 -0
- data/generators/scaffold_resource/templates/old_migration.rb +13 -0
- data/generators/scaffold_resource/templates/rspec/functional_spec.rb +255 -0
- data/generators/scaffold_resource/templates/rspec/helper_spec.rb +11 -0
- data/generators/scaffold_resource/templates/rspec/routing_spec.rb +61 -0
- data/generators/scaffold_resource/templates/rspec/unit_spec.rb +11 -0
- data/generators/scaffold_resource/templates/rspec/views/edit_spec.rb +28 -0
- data/generators/scaffold_resource/templates/rspec/views/index_spec.rb +26 -0
- data/generators/scaffold_resource/templates/rspec/views/new_spec.rb +30 -0
- data/generators/scaffold_resource/templates/rspec/views/show_spec.rb +25 -0
- data/generators/scaffold_resource/templates/shoulda_functional_test.rb +19 -0
- data/generators/scaffold_resource/templates/unit_test.rb +7 -0
- data/generators/scaffold_resource/templates/view__form.erb +6 -0
- data/generators/scaffold_resource/templates/view__form.haml +5 -0
- data/generators/scaffold_resource/templates/view_edit.erb +16 -0
- data/generators/scaffold_resource/templates/view_edit.haml +11 -0
- data/generators/scaffold_resource/templates/view_index.erb +22 -0
- data/generators/scaffold_resource/templates/view_index.haml +19 -0
- data/generators/scaffold_resource/templates/view_new.erb +12 -0
- data/generators/scaffold_resource/templates/view_new.haml +9 -0
- data/generators/scaffold_resource/templates/view_show.erb +9 -0
- data/generators/scaffold_resource/templates/view_show.haml +9 -0
- data/lib/resource_controller.rb +43 -0
- data/lib/resource_controller/accessors.rb +77 -0
- data/lib/resource_controller/action_options.rb +40 -0
- data/lib/resource_controller/actions.rb +75 -0
- data/lib/resource_controller/base.rb +15 -0
- data/lib/resource_controller/class_methods.rb +24 -0
- data/lib/resource_controller/controller.rb +76 -0
- data/lib/resource_controller/failable_action_options.rb +25 -0
- data/lib/resource_controller/helpers.rb +29 -0
- data/lib/resource_controller/helpers/current_objects.rb +75 -0
- data/lib/resource_controller/helpers/internal.rb +80 -0
- data/lib/resource_controller/helpers/nested.rb +67 -0
- data/lib/resource_controller/helpers/pagination.rb +13 -0
- data/lib/resource_controller/helpers/searchlogic.rb +20 -0
- data/lib/resource_controller/helpers/singleton_customizations.rb +64 -0
- data/lib/resource_controller/helpers/urls.rb +132 -0
- data/lib/resource_controller/helpers/views.rb +21 -0
- data/lib/resource_controller/partials.rb +25 -0
- data/lib/resource_controller/response_collector.rb +27 -0
- data/lib/resource_controller/singleton.rb +15 -0
- data/lib/resource_controller/views.rb +19 -0
- data/lib/urligence.rb +50 -0
- data/rails/init.rb +6 -0
- data/test/Rakefile +10 -0
- data/test/app/controllers/accounts_controller.rb +6 -0
- data/test/app/controllers/application_controller.rb +5 -0
- data/test/app/controllers/cms/options_controller.rb +3 -0
- data/test/app/controllers/cms/personnel_controller.rb +2 -0
- data/test/app/controllers/cms/photos_controller.rb +6 -0
- data/test/app/controllers/cms/products_controller.rb +3 -0
- data/test/app/controllers/comments_controller.rb +3 -0
- data/test/app/controllers/images_controller.rb +4 -0
- data/test/app/controllers/options_controller.rb +8 -0
- data/test/app/controllers/people_controller.rb +11 -0
- data/test/app/controllers/photos_controller.rb +14 -0
- data/test/app/controllers/posts_controller.rb +10 -0
- data/test/app/controllers/projects_controller.rb +3 -0
- data/test/app/controllers/somethings_controller.rb +3 -0
- data/test/app/controllers/tags_controller.rb +13 -0
- data/test/app/controllers/users_controller.rb +12 -0
- data/test/app/helpers/accounts_helper.rb +2 -0
- data/test/app/helpers/application_helper.rb +3 -0
- data/test/app/helpers/cms/products_helper.rb +2 -0
- data/test/app/helpers/comments_helper.rb +2 -0
- data/test/app/helpers/images_helper.rb +2 -0
- data/test/app/helpers/options_helper.rb +2 -0
- data/test/app/helpers/people_helper.rb +2 -0
- data/test/app/helpers/photos_helper.rb +2 -0
- data/test/app/helpers/posts_helper.rb +2 -0
- data/test/app/helpers/projects_helper.rb +2 -0
- data/test/app/helpers/somethings_helper.rb +2 -0
- data/test/app/helpers/tags_helper.rb +2 -0
- data/test/app/helpers/users_helper.rb +2 -0
- data/test/app/models/account.rb +4 -0
- data/test/app/models/comment.rb +3 -0
- data/test/app/models/image.rb +3 -0
- data/test/app/models/option.rb +3 -0
- data/test/app/models/personnel.rb +3 -0
- data/test/app/models/photo.rb +5 -0
- data/test/app/models/post.rb +3 -0
- data/test/app/models/product.rb +3 -0
- data/test/app/models/project.rb +2 -0
- data/test/app/models/something.rb +2 -0
- data/test/app/models/tag.rb +3 -0
- data/test/app/models/user.rb +3 -0
- data/test/app/views/accounts/_form.html.erb +4 -0
- data/test/app/views/accounts/edit.html.erb +14 -0
- data/test/app/views/accounts/new.html.erb +12 -0
- data/test/app/views/accounts/show.html.erb +5 -0
- data/test/app/views/cms/options/edit.rhtml +17 -0
- data/test/app/views/cms/options/index.rhtml +20 -0
- data/test/app/views/cms/options/new.rhtml +16 -0
- data/test/app/views/cms/options/show.rhtml +8 -0
- data/test/app/views/cms/photos/edit.rhtml +17 -0
- data/test/app/views/cms/photos/index.rhtml +20 -0
- data/test/app/views/cms/photos/new.rhtml +16 -0
- data/test/app/views/cms/photos/show.rhtml +8 -0
- data/test/app/views/cms/products/edit.rhtml +17 -0
- data/test/app/views/cms/products/index.rhtml +20 -0
- data/test/app/views/cms/products/new.rhtml +16 -0
- data/test/app/views/cms/products/show.rhtml +8 -0
- data/test/app/views/comments/edit.rhtml +27 -0
- data/test/app/views/comments/index.rhtml +24 -0
- data/test/app/views/comments/new.rhtml +26 -0
- data/test/app/views/comments/show.rhtml +18 -0
- data/test/app/views/images/_form.html.erb +4 -0
- data/test/app/views/images/edit.html.erb +14 -0
- data/test/app/views/images/new.html.erb +12 -0
- data/test/app/views/layouts/application.rhtml +17 -0
- data/test/app/views/layouts/comments.rhtml +17 -0
- data/test/app/views/layouts/options.rhtml +17 -0
- data/test/app/views/layouts/people.rhtml +17 -0
- data/test/app/views/layouts/photos.rhtml +17 -0
- data/test/app/views/layouts/projects.rhtml +17 -0
- data/test/app/views/layouts/somethings.rhtml +17 -0
- data/test/app/views/layouts/tags.rhtml +17 -0
- data/test/app/views/options/_form.html.erb +8 -0
- data/test/app/views/options/edit.html.erb +16 -0
- data/test/app/views/options/index.html.erb +21 -0
- data/test/app/views/options/new.html.erb +12 -0
- data/test/app/views/options/show.html.erb +10 -0
- data/test/app/views/people/_form.rhtml +4 -0
- data/test/app/views/people/index.rhtml +20 -0
- data/test/app/views/people/show.rhtml +8 -0
- data/test/app/views/photos/_form.rhtml +4 -0
- data/test/app/views/photos/_info.rhtml +1 -0
- data/test/app/views/photos/index.rhtml +20 -0
- data/test/app/views/photos/show.rhtml +8 -0
- data/test/app/views/posts/edit.rhtml +22 -0
- data/test/app/views/posts/index.rhtml +22 -0
- data/test/app/views/posts/new.rhtml +21 -0
- data/test/app/views/posts/show.rhtml +13 -0
- data/test/app/views/projects/edit.rhtml +17 -0
- data/test/app/views/projects/index.rhtml +20 -0
- data/test/app/views/projects/new.rhtml +16 -0
- data/test/app/views/projects/show.rhtml +8 -0
- data/test/app/views/scaffold/edit.rhtml +18 -0
- data/test/app/views/scaffold/new.rhtml +13 -0
- data/test/app/views/somethings/edit.rhtml +17 -0
- data/test/app/views/somethings/index.rhtml +20 -0
- data/test/app/views/somethings/new.rhtml +16 -0
- data/test/app/views/somethings/show.rhtml +8 -0
- data/test/app/views/tags/edit.rhtml +17 -0
- data/test/app/views/tags/index.rhtml +20 -0
- data/test/app/views/tags/index.rjs +0 -0
- data/test/app/views/tags/new.rhtml +16 -0
- data/test/app/views/tags/show.rhtml +8 -0
- data/test/app/views/users/edit.rhtml +17 -0
- data/test/app/views/users/index.rhtml +20 -0
- data/test/app/views/users/new.rhtml +16 -0
- data/test/app/views/users/show.rhtml +8 -0
- data/test/config/boot.rb +110 -0
- data/test/config/database.yml +9 -0
- data/test/config/environment.rb +49 -0
- data/test/config/environments/development.rb +17 -0
- data/test/config/environments/test.rb +19 -0
- data/test/config/initializers/inflections.rb +14 -0
- data/test/config/routes.rb +61 -0
- data/test/db/migrate/001_create_posts.rb +12 -0
- data/test/db/migrate/002_create_products.rb +11 -0
- data/test/db/migrate/003_create_comments.rb +13 -0
- data/test/db/migrate/004_create_options.rb +13 -0
- data/test/db/migrate/005_create_photos.rb +11 -0
- data/test/db/migrate/006_create_tags.rb +17 -0
- data/test/db/migrate/007_create_somethings.rb +11 -0
- data/test/db/migrate/008_create_accounts.rb +11 -0
- data/test/db/migrate/009_add_account_id_to_photos.rb +9 -0
- data/test/db/migrate/010_create_projects.rb +11 -0
- data/test/db/migrate/011_create_images.rb +12 -0
- data/test/db/migrate/012_create_users.rb +11 -0
- data/test/db/migrate/013_create_personnel.rb +11 -0
- data/test/db/migrate/014_add_personnel_id_to_photos.rb +9 -0
- data/test/db/schema.rb +78 -0
- data/test/script/console +3 -0
- data/test/script/destroy +3 -0
- data/test/script/generate +3 -0
- data/test/script/server +3 -0
- data/test/test/fixtures/accounts.yml +7 -0
- data/test/test/fixtures/comments.yml +11 -0
- data/test/test/fixtures/images.yml +6 -0
- data/test/test/fixtures/options.yml +9 -0
- data/test/test/fixtures/personnel.yml +5 -0
- data/test/test/fixtures/photos.yml +9 -0
- data/test/test/fixtures/photos_tags.yml +3 -0
- data/test/test/fixtures/posts.yml +9 -0
- data/test/test/fixtures/products.yml +7 -0
- data/test/test/fixtures/projects.yml +7 -0
- data/test/test/fixtures/somethings.yml +7 -0
- data/test/test/fixtures/tags.yml +7 -0
- data/test/test/fixtures/users.yml +5 -0
- data/test/test/functional/cms/options_controller_test.rb +23 -0
- data/test/test/functional/cms/photos_controller_test.rb +43 -0
- data/test/test/functional/cms/products_controller_test.rb +23 -0
- data/test/test/functional/comments_controller_test.rb +19 -0
- data/test/test/functional/images_controller_test.rb +30 -0
- data/test/test/functional/people_controller_test.rb +35 -0
- data/test/test/functional/photos_controller_test.rb +123 -0
- data/test/test/functional/posts_controller_test.rb +27 -0
- data/test/test/functional/projects_controller_test.rb +21 -0
- data/test/test/functional/somethings_controller_test.rb +21 -0
- data/test/test/functional/tags_controller_test.rb +57 -0
- data/test/test/functional/users_controller_test.rb +17 -0
- data/test/test/test_helper.rb +13 -0
- data/test/test/unit/accessors_test.rb +110 -0
- data/test/test/unit/account_test.rb +7 -0
- data/test/test/unit/action_options_test.rb +109 -0
- data/test/test/unit/base_test.rb +11 -0
- data/test/test/unit/comment_test.rb +10 -0
- data/test/test/unit/failable_action_options_test.rb +77 -0
- data/test/test/unit/helpers/current_objects_test.rb +133 -0
- data/test/test/unit/helpers/internal_test.rb +106 -0
- data/test/test/unit/helpers/nested_test.rb +86 -0
- data/test/test/unit/helpers/singleton_current_objects_test.rb +68 -0
- data/test/test/unit/helpers/singleton_nested_test.rb +77 -0
- data/test/test/unit/helpers/singleton_urls_test.rb +67 -0
- data/test/test/unit/helpers/urls_test.rb +75 -0
- data/test/test/unit/helpers_test.rb +25 -0
- data/test/test/unit/image_test.rb +7 -0
- data/test/test/unit/option_test.rb +10 -0
- data/test/test/unit/photo_test.rb +10 -0
- data/test/test/unit/post_test.rb +10 -0
- data/test/test/unit/project_test.rb +10 -0
- data/test/test/unit/response_collector_test.rb +49 -0
- data/test/test/unit/something_test.rb +10 -0
- data/test/test/unit/tag_test.rb +10 -0
- data/test/test/unit/urligence_test.rb +203 -0
- metadata +394 -0
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2008 James Golick
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person
|
4
|
+
obtaining a copy of this software and associated documentation
|
5
|
+
files (the "Software"), to deal in the Software without
|
6
|
+
restriction, including without limitation the rights to use,
|
7
|
+
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
8
|
+
copies of the Software, and to permit persons to whom the
|
9
|
+
Software is furnished to do so, subject to the following
|
10
|
+
conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be
|
13
|
+
included in all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
16
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
17
|
+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
18
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
19
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
20
|
+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
21
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
22
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,361 @@
|
|
1
|
+
= Resource Controller
|
2
|
+
|
3
|
+
resource_controller makes RESTful controllers easier, more maintainable, and super readable. With the RESTful controller pattern hidden away, you can focus on what makes your controller special.
|
4
|
+
|
5
|
+
= Views Extension
|
6
|
+
|
7
|
+
resource_controller_views expands upon resource_controller, allowing you to specify a view "fallback" directory, so you to define standard views used for your standard CRUD operations (as well as any others). Then within your controllers main view directory, you only need to provide views that differ from your application's standard views.
|
8
|
+
|
9
|
+
See towards the bottom of this document for examples.
|
10
|
+
|
11
|
+
== Get It
|
12
|
+
|
13
|
+
Install it as a plugin:
|
14
|
+
|
15
|
+
script/plugin install git://github.com/giraffesoft/resource_controller.git
|
16
|
+
|
17
|
+
Or grab the source
|
18
|
+
|
19
|
+
git clone git://github.com/giraffesoft/resource_controller.git
|
20
|
+
|
21
|
+
= Usage
|
22
|
+
|
23
|
+
Creating a basic RESTful controller is as easy as...
|
24
|
+
|
25
|
+
class PostsController < ResourceController::Base
|
26
|
+
end
|
27
|
+
|
28
|
+
...or if you prefer, you can use the method-call syntax. If you need to inherit from some other class, this syntax is definitely for you:
|
29
|
+
|
30
|
+
class PostsController < ApplicationController
|
31
|
+
resource_controller
|
32
|
+
end
|
33
|
+
|
34
|
+
Both syntaxes are identical in their behavior. Just make sure you call resource_controller before you use any other r_c functionality in your controller.
|
35
|
+
|
36
|
+
|
37
|
+
Nobody just uses the default RESTful controller, though. resource_controller provides a simple API for customizations.
|
38
|
+
|
39
|
+
== Action Lifecycle
|
40
|
+
|
41
|
+
It's really easy to make changes to the lifecycle of your actions.
|
42
|
+
|
43
|
+
Note: We had to call the new accessor "new_action", since new is somewhat reserved in ruby.
|
44
|
+
|
45
|
+
=== Before and After
|
46
|
+
|
47
|
+
class ProjectsController < ResourceController::Base
|
48
|
+
|
49
|
+
new_action.before do
|
50
|
+
3.times { current_object.tasks.build }
|
51
|
+
end
|
52
|
+
|
53
|
+
create.after do
|
54
|
+
current_object.creator = current_user
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
|
59
|
+
=== Flash
|
60
|
+
|
61
|
+
class ProjectsController < ResourceController::Base
|
62
|
+
create.flash "Can you believe how easy it is to use resource_controller? Neither could I!"
|
63
|
+
end
|
64
|
+
|
65
|
+
=== respond_to
|
66
|
+
|
67
|
+
You can add to what's already there...
|
68
|
+
|
69
|
+
class ProjectsController < ResourceController::Base
|
70
|
+
create.wants.js { render :template => "show.rjs" }
|
71
|
+
end
|
72
|
+
|
73
|
+
Or you can create a whole new block. This syntax destroys everything that's there, and starts again...
|
74
|
+
|
75
|
+
class ProjectsController < ResourceController::Base
|
76
|
+
create.response do |wants|
|
77
|
+
wants.html
|
78
|
+
wants.js { render :template => "show.rjs" }
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
If you have a nested resource and want to redirect to the parent after create/update and destroy you can do this in the object controller
|
83
|
+
|
84
|
+
class CommentsController < ResourceController::Base
|
85
|
+
belongs_to :post
|
86
|
+
|
87
|
+
create.wants.html { redirect_to smart_url(parent_url_options) }
|
88
|
+
update.wants.html { redirect_to smart_url(parent_url_options) }
|
89
|
+
destroy.wants.html { redirect_to smart_url(parent_url_options) }
|
90
|
+
end
|
91
|
+
|
92
|
+
=== Scoping
|
93
|
+
|
94
|
+
Because sometimes you want to make a bunch of customizations at once, most of the helpers accept blocks that make grouping calls really easy. Is it a DSL? Maybe; maybe not. But, it's definitely awesome.
|
95
|
+
|
96
|
+
With actions that can fail, the scoping defaults to success. That means that create.flash == create.success.flash.
|
97
|
+
|
98
|
+
class ProjectsController < ResourceController::Base
|
99
|
+
|
100
|
+
create do
|
101
|
+
flash "Object successfully created!"
|
102
|
+
wants.js { render :template => "show.rjs" }
|
103
|
+
|
104
|
+
failure.wants.js { render :template => "display_errors.rjs" }
|
105
|
+
end
|
106
|
+
|
107
|
+
destroy do
|
108
|
+
flash "You destroyed your project. Good work."
|
109
|
+
|
110
|
+
failure do
|
111
|
+
flash "You cannot destroy that project. Stop trying!"
|
112
|
+
wants.js { render :template => "display_errors.rjs" }
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
end
|
117
|
+
|
118
|
+
== Singleton Resource
|
119
|
+
|
120
|
+
If you want to create a singleton RESTful controller inherit from ResourceController::Singleton.
|
121
|
+
|
122
|
+
class AccountsController < ResourceController::Singleton
|
123
|
+
end
|
124
|
+
|
125
|
+
...or if need to inherit from some other class:
|
126
|
+
|
127
|
+
class AccountsController < ApplicationController
|
128
|
+
resource_controller :singleton
|
129
|
+
end
|
130
|
+
|
131
|
+
*Note:* This type of controllers handle a single resource only so the index action and all the collection helpers (collection_url, collection_path...) are not available for them.
|
132
|
+
|
133
|
+
Loading objects in singletons is similar to plural controllers with one exception. For non-nested singleton controllers you should override the object method as it defaults to nil for them.
|
134
|
+
|
135
|
+
class AccountsController < ResourceController::Singleton
|
136
|
+
private
|
137
|
+
def object
|
138
|
+
@_rc_object ||= Account.find(session[:account_id])
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
In other cases you can use the default logic and override it only if you use permalinks or anything special.
|
143
|
+
|
144
|
+
Singleton nesting with both :has_many and :has_one associations is provided...
|
145
|
+
|
146
|
+
map.resource :account, :has_many => :options # /account/options, account is a singleton parent
|
147
|
+
map.resources :users, :has_one => :image # /users/1/image, image is a singleton child
|
148
|
+
|
149
|
+
If you have the :has_many association with a singleton parent remember to override parent_object for your :has_many controller as it returns nil by default in this case.
|
150
|
+
|
151
|
+
class OptionsController < ResourceController::Base
|
152
|
+
belongs_to :account
|
153
|
+
|
154
|
+
protected
|
155
|
+
def parent_object
|
156
|
+
Account.find(session[:account_id])
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
== Helpers (ResourceController::Helpers)
|
161
|
+
|
162
|
+
=== Loading objects
|
163
|
+
|
164
|
+
You want to add something like pagination to your controller...
|
165
|
+
|
166
|
+
class PostsController < ResourceController::Base
|
167
|
+
private
|
168
|
+
def collection
|
169
|
+
@collection ||= end_of_association_chain.find(:all, :page => {:size => 10, :current => params[:page]})
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
Or maybe you used a permalink...
|
174
|
+
|
175
|
+
class PostsController < ResourceController::Base
|
176
|
+
private
|
177
|
+
def object
|
178
|
+
@_rc_object ||= end_of_association_chain.find_by_permalink(param)
|
179
|
+
end
|
180
|
+
end
|
181
|
+
|
182
|
+
=== Building objects
|
183
|
+
|
184
|
+
Maybe you have some alternative way of building objects...
|
185
|
+
|
186
|
+
class PostsController < ResourceController::Base
|
187
|
+
private
|
188
|
+
def build_object
|
189
|
+
@_rc_object ||= end_of_association_chain.build_my_object_some_funky_way object_params
|
190
|
+
end
|
191
|
+
end
|
192
|
+
|
193
|
+
...and there are tons more helpers in the ResourceController::Helpers
|
194
|
+
|
195
|
+
== Nested Resources
|
196
|
+
|
197
|
+
Nested controllers can be a pain, especially if routing is such that you may or may not have a parent. Not so with Resource Controller.
|
198
|
+
|
199
|
+
class CommentsController < ResourceController::Base
|
200
|
+
belongs_to :post
|
201
|
+
end
|
202
|
+
|
203
|
+
All of the finding, and creation, and everything will be done at the scope of the post automatically.
|
204
|
+
|
205
|
+
== Namespaced Resources
|
206
|
+
|
207
|
+
...are handled automatically, and any namespaces are always available, symbolized, in array form @ ResourceController::Helpers#namespaces
|
208
|
+
|
209
|
+
== Polymorphic Resources
|
210
|
+
|
211
|
+
Everything, including url generation is handled completely automatically. Take this example...
|
212
|
+
|
213
|
+
## comment.rb
|
214
|
+
class Comment
|
215
|
+
belongs_to :commentable, :polymorphic => true
|
216
|
+
end
|
217
|
+
|
218
|
+
## comments_controller.rb
|
219
|
+
class CommentsController < ResourceController::Base
|
220
|
+
belongs_to :post, :product, :user
|
221
|
+
end
|
222
|
+
*Note:* Your model doesn't have to be polymorphic in the ActiveRecord sense. It can be associated in whichever way you want.
|
223
|
+
|
224
|
+
## routes.rb
|
225
|
+
map.resources :posts, :has_many => :comments
|
226
|
+
map.resources :products, :has_many => :comments
|
227
|
+
map.resources :users, :has_many => :comments
|
228
|
+
|
229
|
+
All you have to do is that, and r_c will infer whichever relationship is present, and perform all the actions at the scope of the parent current_object.
|
230
|
+
|
231
|
+
=== Parent Helpers
|
232
|
+
|
233
|
+
You also get some helpers for reflecting on your parent.
|
234
|
+
|
235
|
+
parent? # => true/false is there a parent present?
|
236
|
+
parent_type # => :post
|
237
|
+
parent_model # => Post
|
238
|
+
parent_object # => @post
|
239
|
+
|
240
|
+
=== Non-standard resource names
|
241
|
+
|
242
|
+
resource_controller supports overrides for every non-standard configuration of resources.
|
243
|
+
|
244
|
+
The most common example is where the resource has a different name than the associated model. Simply overriding the model_name helper will get resource_controller working with your model.
|
245
|
+
|
246
|
+
map.resources :tags
|
247
|
+
...
|
248
|
+
class PhotoTag < ActiveRecord::Base
|
249
|
+
...
|
250
|
+
class TagsController < ResourceController::Base
|
251
|
+
private
|
252
|
+
def model_name
|
253
|
+
'photo_tag'
|
254
|
+
end
|
255
|
+
end
|
256
|
+
|
257
|
+
In the above example, the variable, and params will be set to @tag, @tags, and params[:tag]. If you'd like to change that, override object_name.
|
258
|
+
|
259
|
+
def object_name
|
260
|
+
'photo_tag'
|
261
|
+
end
|
262
|
+
|
263
|
+
If you're using a non-standard controller name, but everything else is standard, overriding resource_name will propagate through all of the other helpers.
|
264
|
+
|
265
|
+
map.resources :tags, :controller => "somethings"
|
266
|
+
...
|
267
|
+
class Tag < ActiveRecord::Base
|
268
|
+
...
|
269
|
+
class SomethingsController < ResourceController::Base
|
270
|
+
private
|
271
|
+
def resource_name
|
272
|
+
'tag'
|
273
|
+
end
|
274
|
+
end
|
275
|
+
|
276
|
+
Finally, the route_name helper is used by Urligence to determine which url helper to call, so if you have non-standard route names, override it.
|
277
|
+
|
278
|
+
map.resources :tags, :controller => "taggings"
|
279
|
+
...
|
280
|
+
class Taggings < ActiveRecord::Base
|
281
|
+
...
|
282
|
+
class TaggingsController < ResourceController::Base
|
283
|
+
private
|
284
|
+
def route_name
|
285
|
+
'tag'
|
286
|
+
end
|
287
|
+
end
|
288
|
+
|
289
|
+
== Url Helpers
|
290
|
+
|
291
|
+
Thanks to Urligence, you also get some free url helpers.
|
292
|
+
|
293
|
+
No matter what your controller looks like...
|
294
|
+
|
295
|
+
[edit_|new_]object_url # is the equivalent of saying [edit_|new_]post_url(@post)
|
296
|
+
[edit_|new_]object_url(some_other_object) # allows you to specify an object, but still maintain any paths or namespaces that are present
|
297
|
+
|
298
|
+
collection_url # is like saying posts_url
|
299
|
+
|
300
|
+
Url helpers are especially useful when working with polymorphic controllers.
|
301
|
+
|
302
|
+
# /posts/1/comments
|
303
|
+
object_url # => /posts/1/comments/#{@comment.to_param}
|
304
|
+
object_url(comment) # => /posts/1/comments/#{comment.to_param}
|
305
|
+
edit_object_url # => /posts/1/comments/#{@comment.to_param}/edit
|
306
|
+
collection_url # => /posts/1/comments
|
307
|
+
|
308
|
+
# /products/1/comments
|
309
|
+
object_url # => /products/1/comments/#{@comment.to_param}
|
310
|
+
object_url(comment) # => /products/1/comments/#{comment.to_param}
|
311
|
+
edit_object_url # => /products/1/comments/#{@comment.to_param}/edit
|
312
|
+
collection_url # => /products/1/comments
|
313
|
+
|
314
|
+
# /comments
|
315
|
+
object_url # => /comments/#{@comment.to_param}
|
316
|
+
object_url(comment) # => /comments/#{comment.to_param}
|
317
|
+
edit_object_url # => /comments/#{@comment.to_param}/edit
|
318
|
+
collection_url # => /comments
|
319
|
+
|
320
|
+
Or with namespaced, nested controllers...
|
321
|
+
|
322
|
+
# /admin/products/1/options
|
323
|
+
object_url # => /admin/products/1/options/#{@option.to_param}
|
324
|
+
object_url(option) # => /admin/products/1/options/#{option.to_param}
|
325
|
+
edit_object_url # => /admin/products/1/options/#{@option.to_param}/edit
|
326
|
+
collection_url # => /admin/products/1/options
|
327
|
+
|
328
|
+
You get the idea. Everything is automagical! All parameters are inferred.
|
329
|
+
|
330
|
+
== View Fallback
|
331
|
+
|
332
|
+
With the view extension, you're able to provide default views for all your standard actions, so in effect you really only need to define your CRUD views once, and just have a different form partial in each controller's view directory. For example:
|
333
|
+
|
334
|
+
/app/views/common/edit.html.erb:
|
335
|
+
|
336
|
+
<h1>Editing <%= resource_controller_options[:english_name] %></h1>
|
337
|
+
|
338
|
+
<% form_for :object, object_url do |form| %>
|
339
|
+
<%= render "form", :form => form %>
|
340
|
+
|
341
|
+
<%= form.submit "Update" %>
|
342
|
+
<% end %>
|
343
|
+
|
344
|
+
/app/views/posts/_form.html.erb:
|
345
|
+
|
346
|
+
<%= form.label :name %>
|
347
|
+
<%= form.text_field :name %>
|
348
|
+
|
349
|
+
/app/controllers/posts_controller.rb:
|
350
|
+
|
351
|
+
resource_controller :scaffold_root => "/app/views/common", :english_name => "Post"
|
352
|
+
|
353
|
+
With the extensions installed, a GET to /posts/1/edit would cause Rails to attempt to render /app/views/posts/edit.html.erb. If that cannot be found, resource_controller_views will make Rails attempt to render /app/views/common/edit.html.erb. The common edit helper will then attempt to render the "form" partial, which again uses the same mechanism as for standard views. In the second case however it finds the _form.html.erb partial in the controllers own directory, and so renders that.
|
354
|
+
|
355
|
+
== Credits
|
356
|
+
|
357
|
+
resource_controller was created, and is maintained by {James Golick}[http://jamesgolick.com].
|
358
|
+
|
359
|
+
== License
|
360
|
+
|
361
|
+
resource_controller is available under the {MIT License}[http://en.wikipedia.org/wiki/MIT_License]
|
data/TODO
ADDED
File without changes
|
data/VERSION.yml
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
Description:
|
2
|
+
The scaffold resource generator creates a model, a controller, and a set of templates that's ready to use as the
|
3
|
+
starting point for your REST-like, resource-oriented application. This basically means that it follows a set of
|
4
|
+
conventions to exploit the full set of HTTP verbs (GET/POST/PUT/DELETE) and is prepared for multi-client access
|
5
|
+
(like one view for HTML, one for an XML API, one for ATOM, etc). Everything comes with sample unit and functional
|
6
|
+
tests as well.
|
7
|
+
|
8
|
+
The generator takes the name of the model as its first argument. This model name is then pluralized to get the
|
9
|
+
controller name. So "scaffold_resource post" will generate a Post model and a PostsController and will be intended
|
10
|
+
for URLs like /posts and /posts/45.
|
11
|
+
|
12
|
+
As additional parameters, the generator will take attribute pairs described by name and type. These attributes will
|
13
|
+
be used to prepopulate the migration to create the table for the model and to give you a set of templates for the
|
14
|
+
view. For example, "scaffold_resource post title:string created_on:date body:text published:boolean" will give
|
15
|
+
you a model with those four attributes, forms to create and edit those models from, and an index that'll list them
|
16
|
+
all.
|
17
|
+
|
18
|
+
You don't have to think up all attributes up front, but it's a good idea of adding just the baseline of what's
|
19
|
+
needed to start really working with the resource.
|
20
|
+
|
21
|
+
Once the generator has run, you'll need to add a declaration to your config/routes.rb file to hook up the rules
|
22
|
+
that'll point URLs to this new resource. If you create a resource like "scaffold_resource post", you'll need to
|
23
|
+
add "map.resources :posts" (notice the plural form) in the routes file. Then your new resource is accessible from
|
24
|
+
/posts.
|
25
|
+
|
26
|
+
Examples:
|
27
|
+
./script/generate scaffold_resource post # no attributes, view will be anemic
|
28
|
+
./script/generate scaffold_resource post title:string created_on:date body:text published:boolean
|
29
|
+
./script/generate scaffold_resource purchase order_id:integer created_at:datetime amount:decimal
|
@@ -0,0 +1,179 @@
|
|
1
|
+
class ScaffoldResourceGenerator < Rails::Generator::NamedBase
|
2
|
+
attr_reader :controller_name,
|
3
|
+
:controller_class_path,
|
4
|
+
:controller_file_path,
|
5
|
+
:controller_class_nesting,
|
6
|
+
:controller_class_nesting_depth,
|
7
|
+
:controller_class_name,
|
8
|
+
:controller_singular_name,
|
9
|
+
:controller_plural_name,
|
10
|
+
:resource_edit_path,
|
11
|
+
:default_file_extension,
|
12
|
+
:generator_default_file_extension
|
13
|
+
alias_method :controller_file_name, :controller_singular_name
|
14
|
+
alias_method :controller_table_name, :controller_plural_name
|
15
|
+
|
16
|
+
def initialize(runtime_args, runtime_options = {})
|
17
|
+
super
|
18
|
+
|
19
|
+
if @rspec = has_rspec?
|
20
|
+
if ActionController::Base.respond_to?(:resource_action_separator)
|
21
|
+
@resource_edit_path = "/edit"
|
22
|
+
else
|
23
|
+
@resource_edit_path = ";edit"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
@generator_default_file_extension = (defined? Haml )? "haml" : "erb"
|
28
|
+
|
29
|
+
# we want to call erb templates .rhtml or .haml if this is rails 1
|
30
|
+
if RAILS_GEM_VERSION.to_i == 1
|
31
|
+
@default_file_extension = @generator_default_file_extension == 'erb' ? 'rhtml' : @generator_default_file_extension
|
32
|
+
else
|
33
|
+
@default_file_extension = "html.#{@generator_default_file_extension}"
|
34
|
+
end
|
35
|
+
|
36
|
+
@controller_name = @name.pluralize
|
37
|
+
|
38
|
+
base_name, @controller_class_path, @controller_file_path, @controller_class_nesting, @controller_class_nesting_depth = extract_modules(@controller_name)
|
39
|
+
@controller_class_name_without_nesting, @controller_singular_name, @controller_plural_name = inflect_names(base_name)
|
40
|
+
|
41
|
+
if @controller_class_nesting.empty?
|
42
|
+
@controller_class_name = @controller_class_name_without_nesting
|
43
|
+
else
|
44
|
+
@controller_class_name = "#{@controller_class_nesting}::#{@controller_class_name_without_nesting}"
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def manifest
|
49
|
+
record do |m|
|
50
|
+
# Check for class naming collisions.
|
51
|
+
m.class_collisions(controller_class_path, "#{controller_class_name}Controller", "#{controller_class_name}Helper")
|
52
|
+
m.class_collisions(class_path, "#{class_name}")
|
53
|
+
|
54
|
+
# Controller, helper, views, and test directories.
|
55
|
+
m.directory(File.join('app/models', class_path))
|
56
|
+
m.directory(File.join('app/controllers', controller_class_path))
|
57
|
+
m.directory(File.join('app/helpers', controller_class_path))
|
58
|
+
m.directory(File.join('app/views', controller_class_path, controller_file_name))
|
59
|
+
|
60
|
+
if @rspec
|
61
|
+
m.directory(File.join('spec/controllers', controller_class_path))
|
62
|
+
m.directory(File.join('spec/helpers', class_path))
|
63
|
+
m.directory(File.join('spec/models', class_path))
|
64
|
+
m.directory File.join('spec/views', controller_class_path, controller_file_name)
|
65
|
+
m.directory(File.join('spec/fixtures', class_path))
|
66
|
+
else
|
67
|
+
m.directory(File.join('test/functional', controller_class_path))
|
68
|
+
m.directory(File.join('test/unit', class_path))
|
69
|
+
end
|
70
|
+
|
71
|
+
scaffold_views.each do |action|
|
72
|
+
m.template(
|
73
|
+
"view_#{action}.#{generator_default_file_extension}",
|
74
|
+
File.join('app/views', controller_class_path, controller_file_name, "#{action}.#{default_file_extension}")
|
75
|
+
)
|
76
|
+
end
|
77
|
+
|
78
|
+
m.template('model.rb', File.join('app/models', class_path, "#{file_name}.rb"))
|
79
|
+
m.template('controller.rb', File.join('app/controllers', controller_class_path, "#{controller_file_name}_controller.rb"))
|
80
|
+
m.template('helper.rb', File.join('app/helpers', controller_class_path, "#{controller_file_name}_helper.rb"))
|
81
|
+
|
82
|
+
if @rspec
|
83
|
+
m.template('rspec/functional_spec.rb', File.join('spec/controllers', controller_class_path, "#{controller_file_name}_controller_spec.rb"))
|
84
|
+
m.template('rspec/routing_spec.rb', File.join('spec/controllers', controller_class_path, "#{controller_file_name}_routing_spec.rb"))
|
85
|
+
m.template('rspec/helper_spec.rb', File.join('spec/helpers', class_path, "#{controller_file_name}_helper_spec.rb"))
|
86
|
+
m.template('rspec/unit_spec.rb', File.join('spec/models', class_path, "#{file_name}_spec.rb"))
|
87
|
+
m.template('fixtures.yml', File.join('spec/fixtures', "#{table_name}.yml"))
|
88
|
+
|
89
|
+
rspec_views.each do |action|
|
90
|
+
m.template(
|
91
|
+
"rspec/views/#{action}_spec.rb",
|
92
|
+
File.join('spec/views', controller_class_path, controller_file_name, "#{action}_spec.rb")
|
93
|
+
)
|
94
|
+
end
|
95
|
+
|
96
|
+
else
|
97
|
+
functional_test = (defined? ThoughtBot::Shoulda) ? "shoulda_functional_test.rb" : "functional_test.rb"
|
98
|
+
|
99
|
+
m.template("#{functional_test}", File.join('test/functional', controller_class_path, "#{controller_file_name}_controller_test.rb"))
|
100
|
+
m.template('unit_test.rb', File.join('test/unit', class_path, "#{file_name}_test.rb"))
|
101
|
+
m.template('fixtures.yml', File.join('test/fixtures', "#{table_name}.yml"))
|
102
|
+
end
|
103
|
+
|
104
|
+
|
105
|
+
unless options[:skip_migration]
|
106
|
+
migration_template = RAILS_GEM_VERSION.to_i == 1 ? 'old_migration.rb' : 'migration.rb'
|
107
|
+
|
108
|
+
m.migration_template(
|
109
|
+
migration_template, 'db/migrate',
|
110
|
+
:assigns => {
|
111
|
+
:migration_name => "Create#{class_name.pluralize.gsub(/::/, '')}",
|
112
|
+
:attributes => attributes
|
113
|
+
},
|
114
|
+
:migration_file_name => "create_#{file_path.gsub(/\//, '_').pluralize}"
|
115
|
+
)
|
116
|
+
end
|
117
|
+
|
118
|
+
m.route_resources controller_file_name
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
# Lifted from Rick Olson's restful_authentication
|
123
|
+
def has_rspec?
|
124
|
+
options[:rspec] || (File.exist?('spec') && File.directory?('spec'))
|
125
|
+
end
|
126
|
+
|
127
|
+
protected
|
128
|
+
# Override with your own usage banner.
|
129
|
+
def banner
|
130
|
+
"Usage: #{$0} scaffold_resource ModelName [field:type, field:type]"
|
131
|
+
end
|
132
|
+
|
133
|
+
def rspec_views
|
134
|
+
%w[ index show new edit ]
|
135
|
+
end
|
136
|
+
|
137
|
+
def scaffold_views
|
138
|
+
rspec_views + %w[ _form ]
|
139
|
+
end
|
140
|
+
|
141
|
+
def model_name
|
142
|
+
class_name.demodulize
|
143
|
+
end
|
144
|
+
|
145
|
+
def add_options!(opt)
|
146
|
+
opt.separator ''
|
147
|
+
opt.separator 'Options:'
|
148
|
+
opt.on("--rspec", "Force rspec mode (checks for RAILS_ROOT/spec by default)") { |v| options[:rspec] = true }
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
module Rails
|
153
|
+
module Generator
|
154
|
+
class GeneratedAttribute
|
155
|
+
def default_value
|
156
|
+
@default_value ||= case type
|
157
|
+
when :int, :integer then "\"1\""
|
158
|
+
when :float then "\"1.5\""
|
159
|
+
when :decimal then "\"9.99\""
|
160
|
+
when :datetime, :timestamp, :time then "Time.now"
|
161
|
+
when :date then "Date.today"
|
162
|
+
when :string then "\"MyString\""
|
163
|
+
when :text then "\"MyText\""
|
164
|
+
when :boolean then "false"
|
165
|
+
else
|
166
|
+
""
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
def input_type
|
171
|
+
@input_type ||= case type
|
172
|
+
when :text then "textarea"
|
173
|
+
else
|
174
|
+
"input"
|
175
|
+
end
|
176
|
+
end
|
177
|
+
end
|
178
|
+
end
|
179
|
+
end
|