base_rails_app 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (194) hide show
  1. data/LICENSE +1 -0
  2. data/README +1 -0
  3. data/lib/app/controllers/application_controller.rb +55 -0
  4. data/lib/app/controllers/password_resets_controller.rb +45 -0
  5. data/lib/app/controllers/user_sessions_controller.rb +25 -0
  6. data/lib/app/controllers/users_controller.rb +37 -0
  7. data/lib/app/helpers/application_helper.rb +4 -0
  8. data/lib/app/helpers/user_sessions_helper.rb +2 -0
  9. data/lib/app/helpers/users_helper.rb +2 -0
  10. data/lib/app/models/postman.rb +20 -0
  11. data/lib/app/models/user.rb +12 -0
  12. data/lib/app/models/user_session.rb +2 -0
  13. data/lib/app/views/application/404_not_found.html.erb +8 -0
  14. data/lib/app/views/layouts/application.html.erb +57 -0
  15. data/lib/app/views/password_resets/_sidebar.html.erb +0 -0
  16. data/lib/app/views/password_resets/edit.html.erb +33 -0
  17. data/lib/app/views/password_resets/new.html.erb +26 -0
  18. data/lib/app/views/postman/password_reset_instructions.html.erb +3 -0
  19. data/lib/app/views/postman/welcome_email.html.erb +3 -0
  20. data/lib/app/views/user_sessions/_form.html.erb +19 -0
  21. data/lib/app/views/user_sessions/_sidebar.html.erb +13 -0
  22. data/lib/app/views/user_sessions/new.html.erb +15 -0
  23. data/lib/app/views/users/_form.html.erb +28 -0
  24. data/lib/app/views/users/_sidebar.html.erb +6 -0
  25. data/lib/app/views/users/edit.html.erb +19 -0
  26. data/lib/app/views/users/new.html.erb +15 -0
  27. data/lib/app/views/users/show.html.erb +46 -0
  28. data/lib/base_rails_app.rb +5 -0
  29. data/lib/base_rails_app/boot.rb +2 -0
  30. data/lib/base_rails_app/tasks.rb +12 -0
  31. data/lib/config/configatron/cucumber.rb +0 -0
  32. data/lib/config/configatron/defaults.rb +4 -0
  33. data/lib/config/configatron/development.rb +1 -0
  34. data/lib/config/configatron/production.rb +0 -0
  35. data/lib/config/configatron/test.rb +0 -0
  36. data/lib/config/database.yml +25 -0
  37. data/lib/config/deploy.rb +15 -0
  38. data/lib/config/environments/cucumber.rb +22 -0
  39. data/lib/config/environments/development.rb +17 -0
  40. data/lib/config/environments/production.rb +28 -0
  41. data/lib/config/environments/test.rb +28 -0
  42. data/lib/config/gemtronics.rb +36 -0
  43. data/lib/config/initializers/backtrace_silencers.rb +7 -0
  44. data/lib/config/initializers/configatron.rb +1 -0
  45. data/lib/config/initializers/hoptoad.rb +3 -0
  46. data/lib/config/initializers/inflections.rb +10 -0
  47. data/lib/config/initializers/mailers.rb +1 -0
  48. data/lib/config/initializers/mime_types.rb +5 -0
  49. data/lib/config/initializers/new_rails_defaults.rb +21 -0
  50. data/lib/config/initializers/session_store.rb +17 -0
  51. data/lib/config/locales/en.yml +5 -0
  52. data/lib/config/routes.rb +51 -0
  53. data/lib/config/schedule.rb +20 -0
  54. data/lib/db/migrate/20090809175903_create_users.rb +30 -0
  55. data/lib/db/migrate/20090809204840_acts_as_taggable_on_migration.rb +29 -0
  56. data/lib/db/migrate/20090912171353_create_delayed_jobs.rb +20 -0
  57. data/lib/db/migrate/20090912171829_add_delayed_job_extras.rb +11 -0
  58. data/lib/db/migrate/20090929201455_add_more_time_columns_to_dj.rb +11 -0
  59. data/lib/db/schema.rb +72 -0
  60. data/lib/db/seeds.rb +7 -0
  61. data/lib/lib/tasks/cucumber.rake +36 -0
  62. data/lib/lib/tasks/rcov.rake +27 -0
  63. data/lib/lib/tasks/rspec.rake +183 -0
  64. data/lib/vendor/plugins/delayed_job/generators/delayed_job/delayed_job_generator.rb +22 -0
  65. data/lib/vendor/plugins/delayed_job/generators/delayed_job/templates/migration.rb +20 -0
  66. data/lib/vendor/plugins/delayed_job/init.rb +2 -0
  67. data/lib/vendor/plugins/delayed_job/lib/delayed/command.rb +65 -0
  68. data/lib/vendor/plugins/delayed_job/lib/delayed/job.rb +271 -0
  69. data/lib/vendor/plugins/delayed_job/lib/delayed/message_sending.rb +18 -0
  70. data/lib/vendor/plugins/delayed_job/lib/delayed/performable_method.rb +55 -0
  71. data/lib/vendor/plugins/delayed_job/lib/delayed/tasks.rb +15 -0
  72. data/lib/vendor/plugins/delayed_job/lib/delayed/worker.rb +54 -0
  73. data/lib/vendor/plugins/delayed_job/lib/delayed_job.rb +13 -0
  74. data/lib/vendor/plugins/delayed_job/recipes/delayed_job.rb +26 -0
  75. data/lib/vendor/plugins/delayed_job/tasks/jobs.rake +1 -0
  76. data/lib/vendor/plugins/hoptoad_notifier/install.rb +1 -0
  77. data/lib/vendor/plugins/hoptoad_notifier/lib/hoptoad_notifier.rb +418 -0
  78. data/lib/vendor/plugins/hoptoad_notifier/lib/hoptoad_tasks.rb +26 -0
  79. data/lib/vendor/plugins/hoptoad_notifier/recipes/hoptoad.rb +22 -0
  80. data/lib/vendor/plugins/hoptoad_notifier/tasks/hoptoad_notifier_tasks.rake +66 -0
  81. data/lib/vendor/plugins/inherited_resources/README.rdoc +524 -0
  82. data/lib/vendor/plugins/inherited_resources/inherited_resources.gemspec +71 -0
  83. data/lib/vendor/plugins/inherited_resources/init.rb +1 -0
  84. data/lib/vendor/plugins/inherited_resources/lib/inherited_resources.rb +23 -0
  85. data/lib/vendor/plugins/inherited_resources/lib/inherited_resources/actions.rb +79 -0
  86. data/lib/vendor/plugins/inherited_resources/lib/inherited_resources/base.rb +42 -0
  87. data/lib/vendor/plugins/inherited_resources/lib/inherited_resources/base_helpers.rb +363 -0
  88. data/lib/vendor/plugins/inherited_resources/lib/inherited_resources/belongs_to_helpers.rb +89 -0
  89. data/lib/vendor/plugins/inherited_resources/lib/inherited_resources/class_methods.rb +338 -0
  90. data/lib/vendor/plugins/inherited_resources/lib/inherited_resources/dsl.rb +26 -0
  91. data/lib/vendor/plugins/inherited_resources/lib/inherited_resources/dumb_responder.rb +20 -0
  92. data/lib/vendor/plugins/inherited_resources/lib/inherited_resources/has_scope_helpers.rb +83 -0
  93. data/lib/vendor/plugins/inherited_resources/lib/inherited_resources/legacy/respond_to.rb +156 -0
  94. data/lib/vendor/plugins/inherited_resources/lib/inherited_resources/legacy/responder.rb +200 -0
  95. data/lib/vendor/plugins/inherited_resources/lib/inherited_resources/polymorphic_helpers.rb +155 -0
  96. data/lib/vendor/plugins/inherited_resources/lib/inherited_resources/singleton_helpers.rb +95 -0
  97. data/lib/vendor/plugins/inherited_resources/lib/inherited_resources/url_helpers.rb +179 -0
  98. data/lib/vendor/plugins/inherited_resources/test/aliases_test.rb +139 -0
  99. data/lib/vendor/plugins/inherited_resources/test/association_chain_test.rb +125 -0
  100. data/lib/vendor/plugins/inherited_resources/test/base_test.rb +225 -0
  101. data/lib/vendor/plugins/inherited_resources/test/belongs_to_test.rb +87 -0
  102. data/lib/vendor/plugins/inherited_resources/test/class_methods_test.rb +138 -0
  103. data/lib/vendor/plugins/inherited_resources/test/customized_base_test.rb +162 -0
  104. data/lib/vendor/plugins/inherited_resources/test/customized_belongs_to_test.rb +76 -0
  105. data/lib/vendor/plugins/inherited_resources/test/defaults_test.rb +70 -0
  106. data/lib/vendor/plugins/inherited_resources/test/flash_test.rb +88 -0
  107. data/lib/vendor/plugins/inherited_resources/test/has_scope_test.rb +139 -0
  108. data/lib/vendor/plugins/inherited_resources/test/locales/en.yml +17 -0
  109. data/lib/vendor/plugins/inherited_resources/test/nested_belongs_to_test.rb +108 -0
  110. data/lib/vendor/plugins/inherited_resources/test/optional_belongs_to_test.rb +164 -0
  111. data/lib/vendor/plugins/inherited_resources/test/polymorphic_test.rb +186 -0
  112. data/lib/vendor/plugins/inherited_resources/test/redirect_to_test.rb +51 -0
  113. data/lib/vendor/plugins/inherited_resources/test/respond_to_test.rb +155 -0
  114. data/lib/vendor/plugins/inherited_resources/test/singleton_test.rb +83 -0
  115. data/lib/vendor/plugins/inherited_resources/test/test_helper.rb +38 -0
  116. data/lib/vendor/plugins/inherited_resources/test/url_helpers_test.rb +537 -0
  117. data/lib/vendor/plugins/inherited_resources/test/views/cars/edit.html.erb +1 -0
  118. data/lib/vendor/plugins/inherited_resources/test/views/cars/index.html.erb +1 -0
  119. data/lib/vendor/plugins/inherited_resources/test/views/cars/new.html.erb +1 -0
  120. data/lib/vendor/plugins/inherited_resources/test/views/cars/show.html.erb +1 -0
  121. data/lib/vendor/plugins/inherited_resources/test/views/cities/edit.html.erb +1 -0
  122. data/lib/vendor/plugins/inherited_resources/test/views/cities/index.html.erb +1 -0
  123. data/lib/vendor/plugins/inherited_resources/test/views/cities/new.html.erb +1 -0
  124. data/lib/vendor/plugins/inherited_resources/test/views/cities/show.html.erb +1 -0
  125. data/lib/vendor/plugins/inherited_resources/test/views/comments/edit.html.erb +1 -0
  126. data/lib/vendor/plugins/inherited_resources/test/views/comments/index.html.erb +1 -0
  127. data/lib/vendor/plugins/inherited_resources/test/views/comments/new.html.erb +1 -0
  128. data/lib/vendor/plugins/inherited_resources/test/views/comments/show.html.erb +1 -0
  129. data/lib/vendor/plugins/inherited_resources/test/views/employees/edit.html.erb +1 -0
  130. data/lib/vendor/plugins/inherited_resources/test/views/employees/index.html.erb +1 -0
  131. data/lib/vendor/plugins/inherited_resources/test/views/employees/new.html.erb +1 -0
  132. data/lib/vendor/plugins/inherited_resources/test/views/employees/show.html.erb +1 -0
  133. data/lib/vendor/plugins/inherited_resources/test/views/managers/edit.html.erb +1 -0
  134. data/lib/vendor/plugins/inherited_resources/test/views/managers/new.html.erb +1 -0
  135. data/lib/vendor/plugins/inherited_resources/test/views/managers/show.html.erb +1 -0
  136. data/lib/vendor/plugins/inherited_resources/test/views/painters/edit.html.erb +1 -0
  137. data/lib/vendor/plugins/inherited_resources/test/views/painters/index.html.erb +1 -0
  138. data/lib/vendor/plugins/inherited_resources/test/views/painters/new.html.erb +1 -0
  139. data/lib/vendor/plugins/inherited_resources/test/views/painters/show.html.erb +1 -0
  140. data/lib/vendor/plugins/inherited_resources/test/views/pets/edit.html.erb +1 -0
  141. data/lib/vendor/plugins/inherited_resources/test/views/pets/index.html.erb +1 -0
  142. data/lib/vendor/plugins/inherited_resources/test/views/pets/new.html.erb +1 -0
  143. data/lib/vendor/plugins/inherited_resources/test/views/pets/show.html.erb +1 -0
  144. data/lib/vendor/plugins/inherited_resources/test/views/products/edit.html.erb +1 -0
  145. data/lib/vendor/plugins/inherited_resources/test/views/products/index.html.erb +1 -0
  146. data/lib/vendor/plugins/inherited_resources/test/views/products/new.html.erb +1 -0
  147. data/lib/vendor/plugins/inherited_resources/test/views/products/show.html.erb +1 -0
  148. data/lib/vendor/plugins/inherited_resources/test/views/professors/edit.html.erb +1 -0
  149. data/lib/vendor/plugins/inherited_resources/test/views/professors/index.html.erb +1 -0
  150. data/lib/vendor/plugins/inherited_resources/test/views/professors/new.html.erb +1 -0
  151. data/lib/vendor/plugins/inherited_resources/test/views/professors/show.html.erb +1 -0
  152. data/lib/vendor/plugins/inherited_resources/test/views/projects/index.html.erb +1 -0
  153. data/lib/vendor/plugins/inherited_resources/test/views/projects/index.json.erb +1 -0
  154. data/lib/vendor/plugins/inherited_resources/test/views/projects/respond_to_skip_default_template.html.erb +1 -0
  155. data/lib/vendor/plugins/inherited_resources/test/views/projects/respond_with_resource.html.erb +1 -0
  156. data/lib/vendor/plugins/inherited_resources/test/views/students/edit.html.erb +1 -0
  157. data/lib/vendor/plugins/inherited_resources/test/views/students/new.html.erb +1 -0
  158. data/lib/vendor/plugins/inherited_resources/test/views/trees/edit.html.erb +1 -0
  159. data/lib/vendor/plugins/inherited_resources/test/views/trees/index.html.erb +1 -0
  160. data/lib/vendor/plugins/inherited_resources/test/views/trees/new.html.erb +1 -0
  161. data/lib/vendor/plugins/inherited_resources/test/views/trees/show.html.erb +1 -0
  162. data/lib/vendor/plugins/inherited_resources/test/views/users/edit.html.erb +1 -0
  163. data/lib/vendor/plugins/inherited_resources/test/views/users/index.html.erb +1 -0
  164. data/lib/vendor/plugins/inherited_resources/test/views/users/new.html.erb +1 -0
  165. data/lib/vendor/plugins/inherited_resources/test/views/users/show.html.erb +1 -0
  166. data/lib/vendor/plugins/web-app-theme/README.md +22 -0
  167. data/lib/vendor/plugins/web-app-theme/images/avatar.png +0 -0
  168. data/lib/vendor/plugins/web-app-theme/index.html +473 -0
  169. data/lib/vendor/plugins/web-app-theme/javascripts/jquery-1.3.min.js +19 -0
  170. data/lib/vendor/plugins/web-app-theme/javascripts/jquery.localscroll.js +104 -0
  171. data/lib/vendor/plugins/web-app-theme/javascripts/jquery.scrollTo.js +150 -0
  172. data/lib/vendor/plugins/web-app-theme/rails_generators/theme/templates/view_layout_administration.html.erb +48 -0
  173. data/lib/vendor/plugins/web-app-theme/rails_generators/theme/templates/view_layout_sign.html.erb +15 -0
  174. data/lib/vendor/plugins/web-app-theme/rails_generators/theme/theme_generator.rb +38 -0
  175. data/lib/vendor/plugins/web-app-theme/rails_generators/themed/templates/view_edit.html.erb +20 -0
  176. data/lib/vendor/plugins/web-app-theme/rails_generators/themed/templates/view_form.html.erb +10 -0
  177. data/lib/vendor/plugins/web-app-theme/rails_generators/themed/templates/view_new.html.erb +19 -0
  178. data/lib/vendor/plugins/web-app-theme/rails_generators/themed/templates/view_show.html.erb +23 -0
  179. data/lib/vendor/plugins/web-app-theme/rails_generators/themed/templates/view_sidebar.html.erb +13 -0
  180. data/lib/vendor/plugins/web-app-theme/rails_generators/themed/templates/view_signin.html.erb +39 -0
  181. data/lib/vendor/plugins/web-app-theme/rails_generators/themed/templates/view_signup.html.erb +56 -0
  182. data/lib/vendor/plugins/web-app-theme/rails_generators/themed/templates/view_tables.html.erb +50 -0
  183. data/lib/vendor/plugins/web-app-theme/rails_generators/themed/themed_generator.rb +89 -0
  184. data/lib/vendor/plugins/web-app-theme/stylesheets/base.css +336 -0
  185. data/lib/vendor/plugins/web-app-theme/stylesheets/themes/bec-green/style.css +290 -0
  186. data/lib/vendor/plugins/web-app-theme/stylesheets/themes/bec/style.css +301 -0
  187. data/lib/vendor/plugins/web-app-theme/stylesheets/themes/blue/style.css +280 -0
  188. data/lib/vendor/plugins/web-app-theme/stylesheets/themes/default/style.css +267 -0
  189. data/lib/vendor/plugins/web-app-theme/stylesheets/themes/djime-cerulean/style.css +298 -0
  190. data/lib/vendor/plugins/web-app-theme/stylesheets/themes/drastic-dark/style.css +373 -0
  191. data/lib/vendor/plugins/web-app-theme/stylesheets/themes/kathleene/style.css +272 -0
  192. data/lib/vendor/plugins/web-app-theme/stylesheets/themes/orange/style.css +263 -0
  193. data/lib/vendor/plugins/web-app-theme/stylesheets/themes/reidb-greenish/style.css +301 -0
  194. metadata +257 -0
@@ -0,0 +1,155 @@
1
+ module InheritedResources
2
+
3
+ # = polymorphic associations
4
+ #
5
+ # In some cases you have a resource that belongs to two different resources
6
+ # but not at the same time. For example, let's suppose you have File, Message
7
+ # and Task as resources and they are all commentable.
8
+ #
9
+ # Polymorphic associations allows you to create just one controller that will
10
+ # deal with each case.
11
+ #
12
+ # class Comment < InheritedResources::Base
13
+ # belongs_to :file, :message, :task, :polymorphic => true
14
+ # end
15
+ #
16
+ # Your routes should be something like:
17
+ #
18
+ # m.resources :files, :has_many => :comments #=> /files/13/comments
19
+ # m.resources :tasks, :has_many => :comments #=> /tasks/17/comments
20
+ # m.resources :messages, :has_many => :comments #=> /messages/11/comments
21
+ #
22
+ # When using polymorphic associations, you get some free helpers:
23
+ #
24
+ # parent? #=> true
25
+ # parent_type #=> :task
26
+ # parent_class #=> Task
27
+ # parent #=> @task
28
+ #
29
+ # This polymorphic controllers thing is a great idea by James Golick and he
30
+ # built it in resource_controller. Here is just a re-implementation.
31
+ #
32
+ # = optional polymorphic associations
33
+ #
34
+ # Let's take another break from ProjectsController. Let's suppose we are
35
+ # building a store, which sell products.
36
+ #
37
+ # On the website, we can show all products, but also products scoped to
38
+ # categories, brands, users. In this case case, the association is optional, and
39
+ # we deal with it in the following way:
40
+ #
41
+ # class ProductsController < InheritedResources::Base
42
+ # belongs_to :category, :brand, :user, :polymorphic => true, :optional => true
43
+ # end
44
+ #
45
+ # This will handle all those urls properly:
46
+ #
47
+ # /products/1
48
+ # /categories/2/products/5
49
+ # /brands/10/products/3
50
+ # /user/13/products/11
51
+ #
52
+ # = nested polymorphic associations
53
+ #
54
+ # You can have polymorphic associations with nested resources. Let's suppose
55
+ # that our File, Task and Message resources in the previous example belongs to
56
+ # a project.
57
+ #
58
+ # This way we can have:
59
+ #
60
+ # class CommentsController < InheritedResources::Base
61
+ # belongs_to :project {
62
+ # belongs_to :file, :message, :task, :polymorphic => true
63
+ # }
64
+ # end
65
+ #
66
+ # Or:
67
+ #
68
+ # class CommentsController < InheritedResources::Base
69
+ # nested_belongs_to :project
70
+ # nested_belongs_to :file, :message, :task, :polymorphic => true
71
+ # end
72
+ #
73
+ # Choose the syntax that makes more sense to you. :)
74
+ #
75
+ # Finally your routes should be something like:
76
+ #
77
+ # map.resources :projects do |m|
78
+ # m.resources :files, :has_many => :comments #=> /projects/1/files/13/comments
79
+ # m.resources :tasks, :has_many => :comments #=> /projects/1/tasks/17/comments
80
+ # m.resources :messages, :has_many => :comments #=> /projects/1/messages/11/comments
81
+ # end
82
+ #
83
+ # The helpers work in the same way as above.
84
+ #
85
+ module PolymorphicHelpers
86
+
87
+ protected
88
+
89
+ # Returns the parent type. A Comments class can have :task, :file, :note
90
+ # as parent types.
91
+ #
92
+ def parent_type
93
+ @parent_type
94
+ end
95
+
96
+ def parent_class
97
+ parent.class if @parent_type
98
+ end
99
+
100
+ # Returns the parent object. They are also available with the instance
101
+ # variable name: @task, @file, @note...
102
+ #
103
+ def parent
104
+ instance_variable_get("@#{@parent_type}") if @parent_type
105
+ end
106
+
107
+ # If the polymorphic association is optional, we might not have a parent.
108
+ #
109
+ def parent?
110
+ if resources_configuration[:polymorphic][:optional]
111
+ parents_symbols.size > 1 || !@parent_type.nil?
112
+ else
113
+ true
114
+ end
115
+ end
116
+
117
+ private
118
+
119
+ # Maps parents_symbols to build association chain.
120
+ #
121
+ # If the parents_symbols find :polymorphic, it goes through the
122
+ # params keys to see which polymorphic parent matches the given params.
123
+ #
124
+ # When optional is given, it does not raise errors if the polymorphic
125
+ # params are missing.
126
+ #
127
+ def symbols_for_association_chain #:nodoc:
128
+ polymorphic_config = resources_configuration[:polymorphic]
129
+
130
+ parents_symbols.map do |symbol|
131
+ if symbol == :polymorphic
132
+ params_keys = params.keys
133
+
134
+ key = polymorphic_config[:symbols].find do |poly|
135
+ params_keys.include? resources_configuration[poly][:param].to_s
136
+ end
137
+
138
+ if key.nil?
139
+ raise ScriptError, "Could not find param for polymorphic association. The request" <<
140
+ "parameters are #{params.keys.inspect} and the polymorphic " <<
141
+ "associations are #{polymorphic_config[:symbols].inspect}." unless polymorphic_config[:optional]
142
+
143
+ nil
144
+ else
145
+ @parent_type = key.to_sym
146
+ end
147
+ else
148
+ symbol
149
+ end
150
+ end.compact
151
+ end
152
+
153
+ end
154
+ end
155
+
@@ -0,0 +1,95 @@
1
+ module InheritedResources
2
+
3
+ # = singleton
4
+ #
5
+ # Singletons are usually used in associations which are related through has_one
6
+ # and belongs_to. You declare those associations like this:
7
+ #
8
+ # class ManagersController < InheritedResources::Base
9
+ # belongs_to :project, :singleton => true
10
+ # end
11
+ #
12
+ # But in some cases, like an AccountsController, you have a singleton object
13
+ # that is not necessarily associated with another:
14
+ #
15
+ # class AccountsController < InheritedResources::Base
16
+ # defaults :singleton => true
17
+ # end
18
+ #
19
+ # Besides that, you should overwrite the methods :resource and :build_resource
20
+ # to make it work properly:
21
+ #
22
+ # class AccountsController < InheritedResources::Base
23
+ # defaults :singleton => true
24
+ #
25
+ # protected
26
+ # def resource
27
+ # @current_user.account
28
+ # end
29
+ #
30
+ # def build_resource(attributes = {})
31
+ # Account.new(attributes)
32
+ # end
33
+ # end
34
+ #
35
+ # When you have a singleton controller, the action index is removed.
36
+ #
37
+ module SingletonHelpers
38
+
39
+ protected
40
+
41
+ # Singleton methods does not deal with collections.
42
+ #
43
+ def collection
44
+ nil
45
+ end
46
+
47
+ # Overwrites how singleton deals with resource.
48
+ #
49
+ # If you are going to overwrite it, you should notice that the
50
+ # end_of_association_chain here is not the same as in default belongs_to.
51
+ #
52
+ # class TasksController < InheritedResources::Base
53
+ # belongs_to :project
54
+ # end
55
+ #
56
+ # In this case, the association chain would be:
57
+ #
58
+ # Project.find(params[:project_id]).tasks
59
+ #
60
+ # So you would just have to call find(:all) at the end of association
61
+ # chain. And this is what happened.
62
+ #
63
+ # In singleton controllers:
64
+ #
65
+ # class ManagersController < InheritedResources::Base
66
+ # belongs_to :project, :singleton => true
67
+ # end
68
+ #
69
+ # The association chain will be:
70
+ #
71
+ # Project.find(params[:project_id])
72
+ #
73
+ # So we have to call manager on it, not find.
74
+ #
75
+ def resource
76
+ get_resource_ivar || set_resource_ivar(end_of_association_chain.send(resource_instance_name))
77
+ end
78
+
79
+ private
80
+
81
+ # Returns the appropriated method to build the resource.
82
+ #
83
+ def method_for_association_build #:nodoc:
84
+ :"build_#{resource_instance_name}"
85
+ end
86
+
87
+ # Sets the method_for_association_chain to nil. See <tt>resource</tt>
88
+ # above for more information.
89
+ #
90
+ def method_for_association_chain #:nodoc:
91
+ nil
92
+ end
93
+
94
+ end
95
+ end
@@ -0,0 +1,179 @@
1
+ module InheritedResources
2
+ # = URLHelpers
3
+ #
4
+ # When you use InheritedResources it creates some UrlHelpers for you.
5
+ # And they handle everything for you.
6
+ #
7
+ # # /posts/1/comments
8
+ # resource_url # => /posts/1/comments/#{@comment.to_param}
9
+ # resource_url(comment) # => /posts/1/comments/#{comment.to_param}
10
+ # new_resource_url # => /posts/1/comments/new
11
+ # edit_resource_url # => /posts/1/comments/#{@comment.to_param}/edit
12
+ # collection_url # => /posts/1/comments
13
+ # parent_url # => /posts/1
14
+ #
15
+ # # /projects/1/tasks
16
+ # resource_url # => /projects/1/tasks/#{@task.to_param}
17
+ # resource_url(task) # => /projects/1/tasks/#{task.to_param}
18
+ # new_resource_url # => /projects/1/tasks/new
19
+ # edit_resource_url # => /projects/1/tasks/#{@task.to_param}/edit
20
+ # collection_url # => /projects/1/tasks
21
+ # parent_url # => /projects/1
22
+ #
23
+ # # /users
24
+ # resource_url # => /users/#{@user.to_param}
25
+ # resource_url(user) # => /users/#{user.to_param}
26
+ # new_resource_url # => /users/new
27
+ # edit_resource_url # => /users/#{@user.to_param}/edit
28
+ # collection_url # => /users
29
+ # parent_url # => /
30
+ #
31
+ # The nice thing is that those urls are not guessed during runtime. They are
32
+ # all created when you inherit.
33
+ #
34
+ module UrlHelpers
35
+
36
+ # This method hard code url helpers in the class.
37
+ #
38
+ # We are doing this because is cheaper than guessing them when our action
39
+ # is being processed (and even more cheaper when we are using nested
40
+ # resources).
41
+ #
42
+ # When we are using polymorphic associations, those helpers rely on
43
+ # polymorphic_url Rails helper.
44
+ #
45
+ def create_resources_url_helpers!
46
+ resource_segments, resource_ivars = [], []
47
+ resource_config = self.resources_configuration[:self]
48
+
49
+ singleton = self.resources_configuration[:self][:singleton]
50
+ polymorphic = self.parents_symbols.include?(:polymorphic)
51
+
52
+ # Add route_prefix if any.
53
+ unless resource_config[:route_prefix].blank?
54
+ if polymorphic
55
+ resource_ivars << resource_config[:route_prefix].to_s.inspect
56
+ else
57
+ resource_segments << resource_config[:route_prefix]
58
+ end
59
+ end
60
+
61
+ # Deal with belongs_to associations and polymorphic associations.
62
+ # Remember that we don't have to build the segments in polymorphic cases,
63
+ # because the url will be polymorphic_url.
64
+ #
65
+ self.parents_symbols.each do |symbol|
66
+ if symbol == :polymorphic
67
+ resource_ivars << :parent
68
+ else
69
+ config = self.resources_configuration[symbol]
70
+ resource_segments << config[:route_name]
71
+ resource_ivars << :"@#{config[:instance_name]}"
72
+ end
73
+ end
74
+
75
+ collection_ivars = resource_ivars.dup
76
+ collection_segments = resource_segments.dup
77
+
78
+ # Generate parent url before we add resource instances.
79
+ generate_url_and_path_helpers nil, :parent, resource_segments, resource_ivars
80
+
81
+ # This is the default route configuration, later we have to deal with
82
+ # exception from polymorphic and singleton cases.
83
+ #
84
+ collection_segments << resource_config[:route_collection_name]
85
+ resource_segments << resource_config[:route_instance_name]
86
+ resource_ivars << :"@#{resource_config[:instance_name]}"
87
+
88
+ # In singleton cases, we do not send the current element instance variable
89
+ # because the id is not in the URL. For example, we should call:
90
+ #
91
+ # project_manager_url(@project)
92
+ #
93
+ # Instead of:
94
+ #
95
+ # project_manager_url(@project, @manager)
96
+ #
97
+ # Another exception in singleton cases is that collection url does not
98
+ # exist. In such cases, we create the parent collection url. So in the
99
+ # manager case above, the collection url will be:
100
+ #
101
+ # project_url(@project)
102
+ #
103
+ # If the singleton does not have a parent, it will default to root_url.
104
+ #
105
+ # Finally, polymorphic cases we have to give hints to the polymorphic url
106
+ # builder. This works by attaching new ivars as symbols or records.
107
+ #
108
+ if singleton
109
+ collection_segments.pop
110
+ resource_ivars.pop
111
+
112
+ if polymorphic
113
+ resource_ivars << resource_config[:instance_name].inspect
114
+ new_ivars = resource_ivars
115
+ end
116
+ elsif polymorphic
117
+ collection_ivars << '(@_resource_class_new ||= resource_class.new)'
118
+ end
119
+
120
+ generate_url_and_path_helpers nil, :collection, collection_segments, collection_ivars
121
+ generate_url_and_path_helpers :new, :resource, resource_segments, new_ivars || collection_ivars
122
+ generate_url_and_path_helpers nil, :resource, resource_segments, resource_ivars
123
+ generate_url_and_path_helpers :edit, :resource, resource_segments, resource_ivars
124
+ end
125
+
126
+ def generate_url_and_path_helpers(prefix, name, resource_segments, resource_ivars) #:nodoc:
127
+ ivars = resource_ivars.dup
128
+
129
+ singleton = self.resources_configuration[:self][:singleton]
130
+ polymorphic = self.parents_symbols.include?(:polymorphic)
131
+
132
+ # If it's not a singleton, ivars are not empty, not a collection or
133
+ # not a "new" named route, we can pass a resource as argument.
134
+ #
135
+ unless (singleton && name != :parent) || ivars.empty? || name == :collection || prefix == :new
136
+ ivars.push "(given_args.first || #{ivars.pop})"
137
+ end
138
+
139
+ # In collection in polymorphic cases, allow an argument to be given as a
140
+ # replacemente for the parent.
141
+ #
142
+ if name == :collection && polymorphic
143
+ index = ivars.index(:parent)
144
+ ivars.insert index, "(given_args.first || parent)"
145
+ ivars.delete(:parent)
146
+ end
147
+
148
+ # When polymorphic is true, the segments must be replace by :polymorphic
149
+ # and ivars should be gathered into an array, which is compacted when
150
+ # optional.
151
+ #
152
+ if polymorphic
153
+ segments = :polymorphic
154
+ ivars = "[#{ivars.join(', ')}]"
155
+ ivars << '.compact' if self.resources_configuration[:polymorphic][:optional]
156
+ else
157
+ segments = resource_segments.empty? ? 'root' : resource_segments.join('_')
158
+ ivars = ivars.join(', ')
159
+ end
160
+
161
+ prefix = prefix ? "#{prefix}_" : ''
162
+ ivars << (ivars.empty? ? 'given_options' : ', given_options')
163
+
164
+ class_eval <<-URL_HELPERS, __FILE__, __LINE__
165
+ protected
166
+ def #{prefix}#{name}_path(*given_args)
167
+ given_options = given_args.extract_options!
168
+ #{prefix}#{segments}_path(#{ivars})
169
+ end
170
+
171
+ def #{prefix}#{name}_url(*given_args)
172
+ given_options = given_args.extract_options!
173
+ #{prefix}#{segments}_url(#{ivars})
174
+ end
175
+ URL_HELPERS
176
+ end
177
+
178
+ end
179
+ end
@@ -0,0 +1,139 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class Student;
4
+ def self.human_name; 'Student'; end
5
+ end
6
+
7
+ class ApplicationController < ActionController::Base
8
+ include InheritedResources::DSL
9
+ end
10
+
11
+ class StudentsController < ApplicationController
12
+ inherit_resources
13
+ respond_to :html, :xml
14
+
15
+ def edit
16
+ edit! do |format|
17
+ format.xml { render :text => 'Render XML' }
18
+ end
19
+ end
20
+
21
+ def new
22
+ @something = 'magical'
23
+ new!
24
+ end
25
+
26
+ create!(:location => "http://test.host/") do |success, failure|
27
+ success.html { render :text => "I won't redirect!" }
28
+ failure.xml { render :text => "I shouldn't be rendered" }
29
+ end
30
+
31
+ update! do |success, failure|
32
+ success.html { redirect_to(resource_url) }
33
+ failure.html { render :text => "I won't render!" }
34
+ end
35
+
36
+ destroy! do |format|
37
+ format.html { render :text => "Destroyed!" }
38
+ end
39
+ end
40
+
41
+ class AliasesTest < ActionController::TestCase
42
+ tests StudentsController
43
+
44
+ def test_assignments_before_calling_alias
45
+ Student.stubs(:new).returns(mock_student)
46
+ get :new
47
+ assert_response :success
48
+ assert_equal 'magical', assigns(:something)
49
+ end
50
+
51
+ def test_controller_should_render_new
52
+ Student.stubs(:new).returns(mock_student)
53
+ get :new
54
+ assert_response :success
55
+ assert_equal 'New HTML', @response.body.strip
56
+ end
57
+
58
+ def test_expose_the_resquested_user_on_edit
59
+ Student.expects(:find).with('42').returns(mock_student)
60
+ get :edit, :id => '42'
61
+ assert_equal mock_student, assigns(:student)
62
+ assert_response :success
63
+ end
64
+
65
+ def test_controller_should_render_edit
66
+ Student.stubs(:find).returns(mock_student)
67
+ get :edit
68
+ assert_response :success
69
+ assert_equal 'Edit HTML', @response.body.strip
70
+ end
71
+
72
+ def test_render_xml_when_it_is_given_as_a_block
73
+ @request.accept = 'application/xml'
74
+ Student.stubs(:find).returns(mock_student)
75
+ get :edit
76
+ assert_response :success
77
+ assert_equal 'Render XML', @response.body
78
+ end
79
+
80
+ def test_is_not_redirected_on_create_with_success_if_success_block_is_given
81
+ Student.stubs(:new).returns(mock_student(:save => true))
82
+ @controller.stubs(:resource_url).returns('http://test.host/')
83
+ post :create
84
+ assert_response :success
85
+ assert_equal "I won't redirect!", @response.body
86
+ end
87
+
88
+ def test_dumb_responder_quietly_receives_everything_on_failure
89
+ @request.accept = 'text/html'
90
+ Student.stubs(:new).returns(mock_student(:save => false, :errors => {:some => :error}))
91
+ @controller.stubs(:resource_url).returns('http://test.host/')
92
+ post :create
93
+ assert_response :success
94
+ assert_equal "New HTML", @response.body.strip
95
+ end
96
+
97
+ def test_html_is_the_default_when_only_xml_is_overwriten
98
+ @request.accept = '*/*'
99
+ Student.stubs(:new).returns(mock_student(:save => false, :errors => {:some => :error}))
100
+ @controller.stubs(:resource_url).returns('http://test.host/')
101
+ post :create
102
+ assert_response :success
103
+ assert_equal "New HTML", @response.body.strip
104
+ end
105
+
106
+ def test_wont_render_edit_template_on_update_with_failure_if_failure_block_is_given
107
+ Student.stubs(:find).returns(mock_student(:update_attributes => false))
108
+ put :update
109
+ assert_response :success
110
+ assert_equal "I won't render!", @response.body
111
+ end
112
+
113
+ def test_dumb_responder_quietly_receives_everything_on_success
114
+ Student.stubs(:find).returns(mock_student(:update_attributes => true))
115
+ @controller.stubs(:resource_url).returns('http://test.host/')
116
+ put :update, :id => '42', :student => {:these => 'params'}
117
+ assert_equal mock_student, assigns(:student)
118
+ end
119
+
120
+ def test_block_is_called_when_student_is_destroyed
121
+ Student.stubs(:find).returns(mock_student(:destroy => true))
122
+ delete :destroy
123
+ assert_response :success
124
+ assert_equal "Destroyed!", @response.body
125
+ end
126
+
127
+ def test_options_are_used_in_respond_with
128
+ @request.accept = "application/xml"
129
+ Student.stubs(:new).returns(mock_student(:save => true, :to_xml => "XML"))
130
+ post :create
131
+ assert_equal "http://test.host/", @response.location
132
+ end
133
+
134
+ protected
135
+ def mock_student(stubs={})
136
+ @mock_student ||= mock(stubs)
137
+ end
138
+ end
139
+