hobo 1.1.0 → 1.3.0.RC

Sign up to get free protection for your applications and to get access to all the features.
Files changed (278) hide show
  1. data/CHANGES.txt +238 -287
  2. data/Rakefile +14 -31
  3. data/VERSION +1 -0
  4. data/{lib/hobo → app/controllers}/dev_controller.rb +6 -6
  5. data/app/controllers/dryml_support_controller.rb +13 -0
  6. data/{rails_generators/hobo_front_controller/templates → app/views/dev}/summary.dryml +7 -10
  7. data/bin/hobo +12 -144
  8. data/config/initializers/inflections.rb +3 -0
  9. data/config/routes.rb +6 -0
  10. data/{doctest → doctests}/hobo/lifecycles.rdoctest +40 -59
  11. data/{doctest → doctests/hobo}/model.rdoctest +33 -40
  12. data/{doctest → doctests/hobo}/multi_model_forms.rdoctest +15 -19
  13. data/{doctest → doctests/hobo}/scopes.rdoctest +27 -41
  14. data/doctests/prepare_testapp.rb +8 -0
  15. data/hobo.gemspec +26 -199
  16. data/lib/generators/hobo/activation_email.rb +11 -0
  17. data/lib/generators/hobo/admin_subsite/admin_subsite_generator.rb +35 -0
  18. data/lib/generators/hobo/admin_subsite/templates/admin.css +20 -0
  19. data/lib/generators/hobo/admin_subsite/templates/admin_tag_injection.erb +10 -0
  20. data/{rails_generators/hobo_admin_site → lib/generators/hobo/admin_subsite}/templates/application.dryml +0 -0
  21. data/{rails_generators/hobo_admin_site/templates/controller.rb → lib/generators/hobo/admin_subsite/templates/controller.rb.erb} +5 -5
  22. data/lib/generators/hobo/admin_subsite/templates/users_index.dryml +7 -0
  23. data/lib/generators/hobo/assets/assets_generator.rb +18 -0
  24. data/{rails_generators/hobo → lib/generators/hobo/assets}/templates/application.css +0 -0
  25. data/lib/generators/hobo/assets/templates/application.dryml.erb +9 -0
  26. data/{rails_generators/hobo → lib/generators/hobo/assets}/templates/dryml-support.js +2 -2
  27. data/lib/generators/hobo/assets/templates/dryml_taglibs_initializer.rb +1 -0
  28. data/lib/generators/hobo/assets/templates/en_injection.yml +19 -0
  29. data/{rails_generators/hobo → lib/generators/hobo/assets}/templates/guest.rb +1 -1
  30. data/lib/generators/hobo/controller.rb +35 -0
  31. data/lib/generators/hobo/controller/controller_generator.rb +6 -0
  32. data/lib/generators/hobo/controller/templates/controller.rb.erb +7 -0
  33. data/{rails_generators/hobo_front_controller → lib/generators/hobo/front_controller}/USAGE +0 -0
  34. data/lib/generators/hobo/front_controller/front_controller_generator.rb +55 -0
  35. data/{rails_generators/hobo_front_controller/templates/controller.rb → lib/generators/hobo/front_controller/templates/controller.rb.erb} +0 -0
  36. data/{rails_generators/hobo_front_controller → lib/generators/hobo/front_controller}/templates/index.dryml +6 -12
  37. data/lib/generators/hobo/i18n/i18n_generator.rb +35 -0
  38. data/lib/generators/hobo/i18n/templates/app.de.yml +30 -0
  39. data/lib/generators/hobo/i18n/templates/app.en.yml +25 -0
  40. data/lib/generators/hobo/i18n/templates/app.es.yml +31 -0
  41. data/lib/generators/hobo/i18n/templates/app.fr.yml +26 -0
  42. data/lib/generators/hobo/i18n/templates/app.it.yml +28 -0
  43. data/lib/generators/hobo/i18n/templates/app.pt-PT.yml +25 -0
  44. data/lib/generators/hobo/i18n/templates/app.ru.yml +24 -0
  45. data/lib/generators/hobo/i18n/templates/hobo.de.yml +204 -0
  46. data/lib/generators/hobo/i18n/templates/hobo.en.yml +195 -0
  47. data/lib/generators/hobo/i18n/templates/hobo.es.yml +202 -0
  48. data/lib/generators/hobo/i18n/templates/hobo.fr.yml +195 -0
  49. data/lib/generators/hobo/i18n/templates/hobo.it.yml +202 -0
  50. data/lib/generators/hobo/i18n/templates/hobo.pt-PT.yml +196 -0
  51. data/lib/generators/hobo/i18n/templates/hobo.ru.yml +200 -0
  52. data/lib/generators/hobo/invite_only.rb +18 -0
  53. data/lib/generators/hobo/model/USAGE +19 -0
  54. data/lib/generators/hobo/model/model_generator.rb +11 -0
  55. data/{rails_generators/hobo_model/templates/model.rb → lib/generators/hobo/model/templates/model_injection.rb.erb} +0 -2
  56. data/{rails_generators/hobo_rapid → lib/generators/hobo/rapid}/USAGE +0 -0
  57. data/lib/generators/hobo/rapid/rapid_generator.rb +24 -0
  58. data/{rails_generators/hobo_rapid → lib/generators/hobo/rapid}/templates/IE7.js +0 -0
  59. data/{rails_generators/hobo_rapid → lib/generators/hobo/rapid}/templates/blank.gif +0 -0
  60. data/{rails_generators/hobo_rapid → lib/generators/hobo/rapid}/templates/hobo-rapid.css +0 -0
  61. data/{rails_generators/hobo_rapid → lib/generators/hobo/rapid}/templates/hobo-rapid.js +65 -65
  62. data/{rails_generators/hobo_rapid → lib/generators/hobo/rapid}/templates/ie7-recalc.js +21 -21
  63. data/{rails_generators/hobo_rapid → lib/generators/hobo/rapid}/templates/lowpro.js +31 -31
  64. data/{rails_generators/hobo_rapid → lib/generators/hobo/rapid}/templates/reset.css +1 -1
  65. data/lib/generators/hobo/rapid/templates/themes/clean-sidemenu/public/images/100-ACD3E6-DBE1E5-H.png +0 -0
  66. data/lib/generators/hobo/rapid/templates/themes/clean-sidemenu/public/images/100-DBE1E5-FCFEF5-H.png +0 -0
  67. data/lib/generators/hobo/rapid/templates/themes/clean-sidemenu/public/images/300-3B5F87-ACD3E6-H.png +0 -0
  68. data/{rails_generators/hobo_rapid/templates/themes/clean → lib/generators/hobo/rapid/templates/themes/clean-sidemenu}/public/images/spinner.gif +0 -0
  69. data/lib/generators/hobo/rapid/templates/themes/clean-sidemenu/public/stylesheets/clean-sidemenu.css +81 -0
  70. data/lib/generators/hobo/rapid/templates/themes/clean-sidemenu/views/clean-sidemenu.dryml +30 -0
  71. data/{rails_generators/hobo_rapid → lib/generators/hobo/rapid}/templates/themes/clean/public/images/101-3B5F87-ACD3E6.png +0 -0
  72. data/{rails_generators/hobo_rapid → lib/generators/hobo/rapid}/templates/themes/clean/public/images/30-3E547A-242E42.png +0 -0
  73. data/{rails_generators/hobo_rapid → lib/generators/hobo/rapid}/templates/themes/clean/public/images/30-DBE1E5-FCFEF5.png +0 -0
  74. data/{rails_generators/hobo_rapid → lib/generators/hobo/rapid}/templates/themes/clean/public/images/300-ACD3E6-fff.png +0 -0
  75. data/{rails_generators/hobo_rapid → lib/generators/hobo/rapid}/templates/themes/clean/public/images/50-ACD3E6-fff.png +0 -0
  76. data/{rails_generators/hobo_rapid → lib/generators/hobo/rapid}/templates/themes/clean/public/images/fieldbg.gif +0 -0
  77. data/{rails_generators/hobo_rapid → lib/generators/hobo/rapid}/templates/themes/clean/public/images/pencil.png +0 -0
  78. data/{rails_generators/hobo_rapid → lib/generators/hobo/rapid}/templates/themes/clean/public/images/small_close.png +0 -0
  79. data/lib/generators/hobo/rapid/templates/themes/clean/public/images/spinner.gif +0 -0
  80. data/{rails_generators/hobo_rapid → lib/generators/hobo/rapid}/templates/themes/clean/public/stylesheets/clean.css +16 -16
  81. data/{rails_generators/hobo_rapid → lib/generators/hobo/rapid}/templates/themes/clean/public/stylesheets/rapid-ui.css +3 -3
  82. data/{rails_generators/hobo_rapid → lib/generators/hobo/rapid}/templates/themes/clean/views/clean.dryml +1 -1
  83. data/lib/generators/hobo/resource/resource_generator.rb +23 -0
  84. data/lib/generators/hobo/routes/USAGE +9 -0
  85. data/lib/generators/hobo/routes/router.rb +115 -0
  86. data/lib/generators/hobo/routes/routes_generator.rb +40 -0
  87. data/lib/generators/hobo/routes/templates/hobo_routes.rb.erb +33 -0
  88. data/lib/generators/hobo/setup_wizard/USAGE +4 -0
  89. data/lib/generators/hobo/setup_wizard/setup_wizard_generator.rb +274 -0
  90. data/lib/generators/hobo/subsite.rb +48 -0
  91. data/lib/generators/hobo/subsite/subsite_generator.rb +16 -0
  92. data/{rails_generators/hobo_subsite → lib/generators/hobo/subsite}/templates/application.dryml +0 -0
  93. data/{rails_generators/hobo_subsite/templates/controller.rb → lib/generators/hobo/subsite/templates/controller.rb.erb} +1 -1
  94. data/lib/generators/hobo/subsite_taglib/subsite_taglib_generator.rb +16 -0
  95. data/{rails_generators/hobo_admin_site/templates/site_taglib.dryml → lib/generators/hobo/subsite_taglib/templates/taglib.dryml.erb} +9 -10
  96. data/lib/generators/hobo/taglib.rb +12 -0
  97. data/lib/generators/hobo/test_framework/test_framework_generator.rb +72 -0
  98. data/lib/generators/hobo/test_options.rb +19 -0
  99. data/{rails_generators/hobo_user_controller → lib/generators/hobo/user_controller}/templates/accept_invitation.dryml +0 -0
  100. data/lib/generators/hobo/user_controller/templates/controller.rb.erb +31 -0
  101. data/lib/generators/hobo/user_controller/user_controller_generator.rb +25 -0
  102. data/lib/generators/hobo/user_mailer/templates/activation.erb +9 -0
  103. data/{rails_generators/hobo_user_model → lib/generators/hobo/user_mailer}/templates/forgot_password.erb +2 -2
  104. data/{rails_generators/hobo_user_model → lib/generators/hobo/user_mailer}/templates/invite.erb +2 -2
  105. data/lib/generators/hobo/user_mailer/templates/mailer.rb.erb +25 -0
  106. data/lib/generators/hobo/user_mailer/user_mailer_generator.rb +33 -0
  107. data/lib/generators/hobo/user_model/USAGE +12 -0
  108. data/{rails_generators/hobo_user_model/templates/model.rb → lib/generators/hobo/user_model/templates/model_injection.rb.erb} +40 -20
  109. data/lib/generators/hobo/user_model/user_model_generator.rb +23 -0
  110. data/lib/generators/hobo/user_resource/user_resource_generator.rb +27 -0
  111. data/lib/hobo.rb +29 -129
  112. data/lib/hobo/controller.rb +37 -47
  113. data/lib/hobo/controller/authentication_support.rb +109 -0
  114. data/lib/hobo/{model_controller.rb → controller/model.rb} +71 -79
  115. data/lib/hobo/{user_controller.rb → controller/user.rb} +59 -49
  116. data/lib/hobo/engine.rb +80 -0
  117. data/lib/hobo/extensions/action_controller/hobo_methods.rb +44 -0
  118. data/lib/hobo/extensions/action_mailer/helper.rb +38 -0
  119. data/lib/{action_view_extensions/helpers → hobo/extensions/action_view}/tag_helper.rb +5 -2
  120. data/lib/hobo/extensions/action_view/translation_helper.rb +25 -0
  121. data/lib/hobo/extensions/active_model/name.rb +16 -0
  122. data/lib/hobo/extensions/active_model/translation.rb +35 -0
  123. data/lib/{active_record/association_collection.rb → hobo/extensions/active_record/associations/collection.rb} +8 -17
  124. data/lib/{active_record/association_proxy.rb → hobo/extensions/active_record/associations/proxy.rb} +6 -7
  125. data/lib/hobo/extensions/active_record/associations/reflection.rb +23 -0
  126. data/lib/hobo/extensions/active_record/associations/scope.rb +35 -0
  127. data/lib/hobo/extensions/active_record/hobo_methods.rb +11 -0
  128. data/lib/hobo/extensions/active_record/permissions.rb +159 -0
  129. data/lib/hobo/extensions/active_record/relation_with_origin.rb +28 -0
  130. data/lib/hobo/extensions/array.rb +27 -0
  131. data/lib/hobo/extensions/enumerable.rb +12 -0
  132. data/lib/hobo/extensions/i18n.rb +17 -0
  133. data/lib/hobo/{hobo_helper.rb → helper.rb} +58 -31
  134. data/lib/hobo/helper/translations.rb +54 -0
  135. data/lib/hobo/helper/translations/normalizer.rb +39 -0
  136. data/lib/hobo/model.rb +61 -95
  137. data/lib/hobo/model/accessible_associations.rb +178 -0
  138. data/lib/hobo/{find_for.rb → model/find_for.rb} +17 -17
  139. data/lib/hobo/model/guest.rb +25 -0
  140. data/lib/hobo/model/include_in_save.rb +55 -0
  141. data/lib/hobo/model/lifecycles.rb +119 -0
  142. data/lib/hobo/model/lifecycles/actions.rb +146 -0
  143. data/lib/hobo/model/lifecycles/creator.rb +74 -0
  144. data/lib/hobo/model/lifecycles/lifecycle.rb +243 -0
  145. data/lib/hobo/model/lifecycles/state.rb +22 -0
  146. data/lib/hobo/model/lifecycles/transition.rb +70 -0
  147. data/lib/hobo/model/permissions.rb +448 -0
  148. data/lib/hobo/model/scopes.rb +38 -0
  149. data/lib/hobo/model/scopes/apply_scopes.rb +21 -0
  150. data/lib/hobo/model/scopes/automatic_scopes.rb +428 -0
  151. data/lib/hobo/model/user_base.rb +184 -0
  152. data/lib/hobo/model/view_hints.rb +123 -0
  153. data/{rapid_generators → lib/hobo/rapid/generators}/rapid/cards.dryml.erb +2 -2
  154. data/{rapid_generators → lib/hobo/rapid/generators}/rapid/forms.dryml.erb +3 -3
  155. data/{rapid_generators → lib/hobo/rapid/generators}/rapid/pages.dryml.erb +38 -51
  156. data/lib/hobo/rapid/helper.rb +166 -0
  157. data/{taglibs → lib/hobo/rapid/taglibs}/rapid.dryml +6 -5
  158. data/{taglibs → lib/hobo/rapid/taglibs}/rapid_core.dryml +286 -118
  159. data/{taglibs → lib/hobo/rapid/taglibs}/rapid_document_tags.dryml +2 -2
  160. data/{taglibs → lib/hobo/rapid/taglibs}/rapid_editing.dryml +45 -45
  161. data/{taglibs → lib/hobo/rapid/taglibs}/rapid_forms.dryml +190 -158
  162. data/{taglibs → lib/hobo/rapid/taglibs}/rapid_generics.dryml +12 -9
  163. data/lib/hobo/rapid/taglibs/rapid_i18n.dryml +107 -0
  164. data/{taglibs → lib/hobo/rapid/taglibs}/rapid_lifecycles.dryml +7 -7
  165. data/{taglibs → lib/hobo/rapid/taglibs}/rapid_navigation.dryml +15 -15
  166. data/{taglibs → lib/hobo/rapid/taglibs}/rapid_pages.dryml +37 -36
  167. data/{taglibs → lib/hobo/rapid/taglibs}/rapid_plus.dryml +107 -43
  168. data/{taglibs → lib/hobo/rapid/taglibs}/rapid_summary.dryml +28 -57
  169. data/{taglibs → lib/hobo/rapid/taglibs}/rapid_support.dryml +9 -9
  170. data/{taglibs → lib/hobo/rapid/taglibs}/rapid_user_pages.dryml +41 -40
  171. data/lib/hobo/routes.rb +31 -0
  172. data/{doctest → test/doctest}/hobo/hobo_helper.rdoctest +11 -11
  173. data/test/irt/generators/admin_subsite.irt +27 -0
  174. data/test/irt/generators/assets.irt +16 -0
  175. data/test/irt/generators/controller.irt +6 -0
  176. data/test/irt/generators/front_controller.irt +30 -0
  177. data/test/irt/generators/helper.rb +31 -0
  178. data/test/irt/generators/model.irt +28 -0
  179. data/test/irt/generators/partials/_account_user_model_tests.rb +21 -0
  180. data/test/irt/generators/partials/_accounts_users_controller_tests.rb +15 -0
  181. data/test/irt/generators/partials/_default_user_model_tests.rb +21 -0
  182. data/test/irt/generators/partials/_default_users_controller_tests.rb +16 -0
  183. data/test/irt/generators/partials/_house_controller_tests.rb +15 -0
  184. data/test/irt/generators/partials/_house_model_tests.rb +18 -0
  185. data/test/irt/generators/partials/_subsite_taglib_admin.rb +4 -0
  186. data/test/irt/generators/partials/_subsite_taglib_admin_invite_only.rb +3 -0
  187. data/test/irt/generators/partials/_subsite_taglib_noopt.rb +4 -0
  188. data/test/irt/generators/partials/_subsite_taglib_variables.rb +27 -0
  189. data/test/irt/generators/partials/_user_mailer_tests.rb +19 -0
  190. data/test/irt/generators/rapid.irt +29 -0
  191. data/test/irt/generators/resource.irt +8 -0
  192. data/test/irt/generators/subsite.irt +52 -0
  193. data/test/irt/generators/subsite_taglib.irt +23 -0
  194. data/test/irt/generators/test_framework.irt +62 -0
  195. data/test/irt/generators/user_controller.irt +10 -0
  196. data/test/irt/generators/user_mailer.irt +20 -0
  197. data/test/irt/generators/user_model.irt +10 -0
  198. data/test/irt/generators/user_resource.irt +14 -0
  199. data/test/irt/readme.txt +7 -0
  200. data/test/permissions/models/models.rb +27 -24
  201. data/test/permissions/test_permissions.rb +104 -104
  202. metadata +239 -217
  203. data/lib/active_record/association_reflection.rb +0 -20
  204. data/lib/active_record/viewhints_validations_interceptor.rb +0 -9
  205. data/lib/hobo/accessible_associations.rb +0 -183
  206. data/lib/hobo/authentication_support.rb +0 -131
  207. data/lib/hobo/generator.rb +0 -26
  208. data/lib/hobo/guest.rb +0 -25
  209. data/lib/hobo/include_in_save.rb +0 -55
  210. data/lib/hobo/lifecycles.rb +0 -137
  211. data/lib/hobo/lifecycles/actions.rb +0 -142
  212. data/lib/hobo/lifecycles/creator.rb +0 -72
  213. data/lib/hobo/lifecycles/lifecycle.rb +0 -249
  214. data/lib/hobo/lifecycles/state.rb +0 -22
  215. data/lib/hobo/lifecycles/transition.rb +0 -70
  216. data/lib/hobo/model_router.rb +0 -290
  217. data/lib/hobo/permissions.rb +0 -451
  218. data/lib/hobo/permissions/associations.rb +0 -175
  219. data/lib/hobo/rapid_helper.rb +0 -157
  220. data/lib/hobo/scopes.rb +0 -43
  221. data/lib/hobo/scopes/apply_scopes.rb +0 -23
  222. data/lib/hobo/scopes/association_proxy_extensions.rb +0 -62
  223. data/lib/hobo/scopes/automatic_scopes.rb +0 -421
  224. data/lib/hobo/scopes/named_scope_extensions.rb +0 -39
  225. data/lib/hobo/tasks/rails.rb +0 -4
  226. data/lib/hobo/translations.rb +0 -93
  227. data/lib/hobo/undefined_access_error.rb +0 -5
  228. data/lib/hobo/user.rb +0 -182
  229. data/lib/hobo/view_hints.rb +0 -115
  230. data/rails/init.rb +0 -10
  231. data/rails_generators/hobo/USAGE +0 -4
  232. data/rails_generators/hobo/hobo_generator.rb +0 -53
  233. data/rails_generators/hobo/templates/application.dryml +0 -1
  234. data/rails_generators/hobo/templates/initializer.rb +0 -2
  235. data/rails_generators/hobo_admin_site/USAGE +0 -16
  236. data/rails_generators/hobo_admin_site/hobo_admin_site_generator.rb +0 -45
  237. data/rails_generators/hobo_admin_site/templates/admin.css +0 -2
  238. data/rails_generators/hobo_admin_site/templates/users_index.dryml +0 -5
  239. data/rails_generators/hobo_front_controller/hobo_front_controller_generator.rb +0 -95
  240. data/rails_generators/hobo_front_controller/templates/functional_test.rb +0 -8
  241. data/rails_generators/hobo_front_controller/templates/helper.rb +0 -2
  242. data/rails_generators/hobo_model/USAGE +0 -25
  243. data/rails_generators/hobo_model/hobo_model_generator.rb +0 -43
  244. data/rails_generators/hobo_model/templates/fixtures.yml +0 -6
  245. data/rails_generators/hobo_model/templates/hints.rb +0 -7
  246. data/rails_generators/hobo_model/templates/unit_test.rb +0 -8
  247. data/rails_generators/hobo_model_controller/USAGE +0 -30
  248. data/rails_generators/hobo_model_controller/hobo_model_controller_generator.rb +0 -46
  249. data/rails_generators/hobo_model_controller/templates/controller.rb +0 -7
  250. data/rails_generators/hobo_model_controller/templates/functional_test.rb +0 -8
  251. data/rails_generators/hobo_model_controller/templates/helper.rb +0 -2
  252. data/rails_generators/hobo_model_resource/USAGE +0 -38
  253. data/rails_generators/hobo_model_resource/hobo_model_resource_generator.rb +0 -73
  254. data/rails_generators/hobo_model_resource/templates/controller.rb +0 -7
  255. data/rails_generators/hobo_model_resource/templates/functional_test.rb +0 -8
  256. data/rails_generators/hobo_model_resource/templates/helper.rb +0 -2
  257. data/rails_generators/hobo_rapid/hobo_rapid_generator.rb +0 -94
  258. data/rails_generators/hobo_subsite/USAGE +0 -16
  259. data/rails_generators/hobo_subsite/hobo_subsite_generator.rb +0 -73
  260. data/rails_generators/hobo_subsite/templates/site_taglib.dryml +0 -13
  261. data/rails_generators/hobo_user_controller/USAGE +0 -34
  262. data/rails_generators/hobo_user_controller/hobo_user_controller_generator.rb +0 -65
  263. data/rails_generators/hobo_user_controller/templates/controller.rb +0 -29
  264. data/rails_generators/hobo_user_controller/templates/functional_test.rb +0 -8
  265. data/rails_generators/hobo_user_controller/templates/helper.rb +0 -2
  266. data/rails_generators/hobo_user_model/USAGE +0 -16
  267. data/rails_generators/hobo_user_model/hobo_user_model_generator.rb +0 -46
  268. data/rails_generators/hobo_user_model/templates/fixtures.yml +0 -19
  269. data/rails_generators/hobo_user_model/templates/mailer.rb +0 -29
  270. data/rails_generators/hobo_user_model/templates/unit_test.rb +0 -8
  271. data/script/destroy +0 -14
  272. data/script/generate +0 -14
  273. data/taglibs/rapid_translations.dryml +0 -36
  274. data/tasks/environments.rake +0 -19
  275. data/tasks/hobo_tasks.rake +0 -58
  276. data/test/generators/test_generator_helper.rb +0 -29
  277. data/test/generators/test_helper.rb +0 -1
  278. data/test/generators/test_hobo_model_controller_generator.rb +0 -56
@@ -1,22 +0,0 @@
1
- module Hobo
2
-
3
- module Lifecycles
4
-
5
- class State < Struct.new(:name, :on_enter, :transitions_out)
6
-
7
- include Actions
8
-
9
- def initialize(*args)
10
- super
11
- self.transitions_out = []
12
- end
13
-
14
-
15
- def activate!(record)
16
- fire_event(record, on_enter)
17
- end
18
-
19
- end
20
-
21
- end
22
- end
@@ -1,70 +0,0 @@
1
- module Hobo
2
-
3
- module Lifecycles
4
-
5
- class Transition < Struct.new(:lifecycle, :name, :start_states, :end_state, :on_transition, :options)
6
-
7
- include Actions
8
-
9
-
10
- def initialize(*args)
11
- super
12
- self.name = name.to_sym
13
- start_states.each do |from|
14
- state = lifecycle.states[from]
15
- raise ArgumentError, "No such state '#{from}' in #{name} transition (#{lifecycle.model.name})" unless state
16
- state.transitions_out << self
17
- end
18
- lifecycle.transitions << self
19
- end
20
-
21
-
22
- def extract_attributes(attributes)
23
- model = lifecycle.model
24
- params = options.fetch(:params, [])
25
- allowed = params.dup
26
- params.each do |p|
27
- if (refl = model.reflections[p]) && refl.macro == :belongs_to
28
- allowed << refl.primary_key_name.to_s
29
- allowed << refl.options[:foreign_type] if refl.options[:polymorphic]
30
- end
31
- end
32
- attributes & allowed
33
- end
34
-
35
-
36
- def change_state(record)
37
- record.lifecycle.clear_key unless options[:new_key] || options[:keep_key]
38
- return record.lifecycle.become(get_state(record, end_state))
39
- end
40
-
41
-
42
- def run!(record, user, attributes)
43
- current_state = record.lifecycle.state_name
44
- unless start_states.include?(current_state)
45
- raise Hobo::Lifecycles::LifecycleError, "Transition #{record.class}##{name} cannot be run from the '#{current_state}' state"
46
- end
47
- record.lifecycle.active_step = self
48
- record.with_acting_user(user) do
49
- prepare!(record, attributes)
50
- if can_run?(record)
51
- if change_state(record)
52
- fire_event(record, on_transition)
53
- end
54
- else
55
- raise Hobo::PermissionDeniedError
56
- end
57
- end
58
- end
59
-
60
-
61
- def parameters
62
- options[:params] || []
63
- end
64
-
65
-
66
- end
67
-
68
- end
69
-
70
- end
@@ -1,290 +0,0 @@
1
- if defined? ActionController::Routing::RouteSet
2
-
3
- class ActionController::Routing::RouteSet
4
- # Monkey patch this method so routes are reloaded on *every*
5
- # request. Without this Rails checks the mtime of config/routes.rb
6
- # which doesn't take into account Hobo's auto routing
7
-
8
- def reload_with_hobo_routes
9
- if Hobo::ModelRouter.reload_routes_on_every_request
10
- load!
11
- else
12
- reload_without_hobo_routes
13
- end
14
- end
15
- alias_method_chain :reload, :hobo_routes
16
-
17
- # temporay hack -- reload assemble.rb whenever routes need reloading
18
- def reload_with_hobo_assemble
19
- if defined? ::ApplicationController
20
- load "#{RAILS_ROOT}/app/assemble.rb" if File.exists? "#{RAILS_ROOT}/app/assemble.rb"
21
- end
22
- reload_without_hobo_assemble
23
- end
24
- alias_method_chain :reload, :hobo_assemble
25
-
26
- end
27
-
28
- end
29
-
30
- module Hobo
31
-
32
- class ModelRouter
33
-
34
- class << self
35
-
36
- attr_accessor :reload_routes_on_every_request
37
-
38
- def reset_linkables
39
- @linkable = Set.new
40
- end
41
-
42
- def linkable_key(klass, action, options)
43
- subsite = options[:subsite] || options['subsite']
44
- method = options[:method] || options['method']
45
- opts = options.map { |k, v| "#{k}=#{v}" unless v.blank? }.compact.join(',')
46
- "#{subsite}/#{klass.name}/#{action}/#{method}"
47
- end
48
-
49
- def linkable!(klass, action, options={})
50
- options[:method] ||= :get
51
- @linkable << linkable_key(klass, action, options)
52
- end
53
-
54
- def linkable?(klass, action, options={})
55
- options[:method] ||= :get
56
- @linkable.member? linkable_key(klass, action, options)
57
- end
58
-
59
- def called_from_generator?
60
- caller[-1] =~ /script[\/\\]generate:\d+$/ || caller[-1] =~ /script[\/\\]destroy:\d+$/
61
- end
62
-
63
- def add_routes(map)
64
- # Don't create routes if it's a generator that's running
65
- return if called_from_generator?
66
-
67
- reset_linkables
68
-
69
- begin
70
- ActiveRecord::Base.connection.reconnect! unless ActiveRecord::Base.connection.active?
71
- rescue
72
- # No database, no routes
73
- return
74
- end
75
-
76
- begin
77
- require "#{RAILS_ROOT}/app/controllers/application" unless Object.const_defined? :ApplicationController
78
- rescue MissingSourceFile => ex
79
- # must be on Rails 2.3. Yay!
80
- end
81
-
82
- # Add non-subsite, and all subsite routes
83
- [nil, *Hobo.subsites].each { |subsite| add_routes_for(map, subsite) }
84
-
85
- add_developer_routes(map) if Hobo.developer_features?
86
-
87
- rescue ActiveRecord::StatementInvalid => e
88
- # Database problem? Just continue without routes
89
- if ActiveRecord::Base.logger
90
- ActiveRecord::Base.logger.warn "!! Database exception during Hobo routing -- continuing without routes"
91
- ActiveRecord::Base.logger.warn "!! #{e.to_s}"
92
- end
93
- end
94
-
95
-
96
- def add_routes_for(map, subsite)
97
- Hobo::ModelController.all_controllers(subsite, :force).each { |controller| ModelRouter.new(map, controller, subsite) }
98
- end
99
-
100
-
101
- def add_developer_routes(map)
102
- map.dryml_support "dryml/:action", :controller => "hobo/dryml/dryml_support"
103
- map.dev_support "dev/:action", :controller => "hobo/dev"
104
- end
105
-
106
- end
107
-
108
- # specify that an id CANNOT be null - needed to disambiguate /models from /models/[nil] - see #251
109
- ID_REQUIREMENT = { :id => /[^#{ActionController::Routing::SEPARATORS.join}]+/ }
110
-
111
- def initialize(map, controller, subsite)
112
- @map = map
113
- @controller = controller
114
- @subsite = subsite
115
- add_routes
116
- end
117
-
118
-
119
- attr_reader :map, :model, :controller, :subsite
120
-
121
-
122
- def model
123
- controller.model
124
- end
125
-
126
-
127
- def plural
128
- model.name.underscore.pluralize
129
- end
130
-
131
-
132
- def singular
133
- model.name.underscore
134
- end
135
-
136
-
137
- def add_routes
138
- if controller < Hobo::ModelController
139
- # index routes need to be first so the index names don't get
140
- # taken as IDs
141
- index_action_routes
142
- lifecycle_routes if defined? model::Lifecycle
143
- resource_routes
144
- owner_routes
145
- web_method_routes
146
- show_action_routes
147
-
148
- reorder_route
149
- user_routes if controller < Hobo::UserController
150
- end
151
- end
152
-
153
-
154
- def resource_routes
155
- # We re-implement resource routing - routes are not created for
156
- # actions that the controller does not provide
157
-
158
- # FIX ME -- what about routes with formats (e.g. .xml)?
159
-
160
- linkable_route(plural, plural, :index, :conditions => { :method => :get })
161
-
162
- linkable_route("new_#{singular}", "#{plural}/new", :new, :conditions => { :method => :get })
163
- linkable_route("edit_#{singular}", "#{plural}/:id/edit", :edit, :conditions => { :method => :get })
164
-
165
- linkable_route(singular, "#{plural}/:id", :show, :conditions => { :method => :get }, :requirements => ID_REQUIREMENT)
166
-
167
- linkable_route("create_#{singular}", plural, :create, :conditions => { :method => :post })
168
- linkable_route("update_#{singular}", "#{plural}/:id", :update, :conditions => { :method => :put }, :requirements => ID_REQUIREMENT)
169
- linkable_route("destroy_#{singular}", "#{plural}/:id", :destroy, :conditions => { :method => :delete }, :requirements => ID_REQUIREMENT)
170
- end
171
-
172
-
173
- def owner_routes
174
- controller.owner_actions.each_pair do |owner, actions|
175
- collection_refl = model.reverse_reflection(owner)
176
- raise HoboError, "Hob routing error -- can't find reverse association for #{model}##{owner} " +
177
- "(e.g. the :has_many that corresponds to a :belongs_to)" if collection_refl.nil?
178
- collection = collection_refl.name
179
- owner_class = model.reflections[owner].klass.name.underscore
180
-
181
- owner = owner.to_s.singularize if model.reflections[owner].macro == :has_many
182
-
183
- collection_path = "#{owner_class.pluralize}/:#{owner}_id/#{collection}"
184
-
185
- actions.each do |action|
186
- case action
187
- when :index
188
- linkable_route("#{plural}_for_#{owner}", collection_path, "index_for_#{owner}",
189
- :conditions => { :method => :get })
190
- when :new
191
- linkable_route("new_#{singular}_for_#{owner}", "#{collection_path}/new", "new_for_#{owner}",
192
- :conditions => { :method => :get })
193
- when :create
194
- linkable_route("create_#{singular}_for_#{owner}", collection_path, "create_for_#{owner}",
195
- :conditions => { :method => :post })
196
- end
197
- end
198
- end
199
- end
200
-
201
- def web_method_routes
202
- controller.web_methods.each do |method|
203
- linkable_route("#{plural.singularize}_#{method}", "#{plural}/:id/#{method}", method.to_s, :conditions => { :method => :post })
204
- end
205
- end
206
-
207
-
208
- def index_action_routes
209
- controller.index_actions.each do |view|
210
- linkable_route("#{view}_#{plural}", "#{plural}/#{view}", view.to_s, :conditions => { :method => :get })
211
- end
212
- end
213
-
214
-
215
- def show_action_routes
216
- controller.show_actions.each do |view|
217
- linkable_route("#{plural.singularize}_#{view}", "#{plural}/:id/#{view}", view.to_s, :conditions => { :method => :get })
218
- end
219
- end
220
-
221
-
222
- def reorder_route
223
- linkable_route("reorder_#{plural}", "#{plural}/reorder", 'reorder', :conditions => { :method => :post })
224
- end
225
-
226
-
227
- def lifecycle_routes
228
- model::Lifecycle.creators.values.where.publishable?.*.name.each do |creator|
229
- linkable_route("do_#{singular}_#{creator}", "#{plural}/#{creator}", "do_#{creator}",
230
- :conditions => { :method => :post }, :linkable_action => creator)
231
- linkable_route("#{singular}_#{creator}", "#{plural}/#{creator}", creator, :conditions => { :method => :get })
232
- end
233
- model::Lifecycle.transitions.where.publishable?.*.name.each do |transition|
234
- linkable_route("do_#{singular}_#{transition}", "#{plural}/:id/#{transition}", "do_#{transition}",
235
- :conditions => { :method => :put }, :linkable_action => transition)
236
- linkable_route("#{singular}_#{transition}", "#{plural}/:id/#{transition}", transition,
237
- :conditions => { :method => :get })
238
- end
239
- end
240
-
241
-
242
- def user_routes
243
- prefix = plural == "users" ? "" : "#{singular}_"
244
- linkable_route("#{singular}_login", "#{prefix}login", 'login')
245
- linkable_route("#{singular}_logout", "#{prefix}logout", 'logout')
246
- linkable_route("#{singular}_forgot_password", "#{prefix}forgot_password", 'forgot_password')
247
- end
248
-
249
-
250
- def named_route(name, route, options={})
251
- if controller.public_method_defined?(options[:action])
252
- options.reverse_merge!(:controller => route_with_subsite(plural))
253
- name = name_with_subsite(name)
254
- route = route_with_subsite(route)
255
- if HoboSupport::RAILS_AT_LEAST_23
256
- map.named_route(name, "#{route}.:format", options)
257
- else
258
- # kick it old-skool
259
- map.named_route(name, route, options)
260
- map.named_route("formatted_#{name}", "#{route}.:format", options)
261
- end
262
- true
263
- else
264
- false
265
- end
266
- end
267
-
268
-
269
- def linkable_route(name, route, action, options={})
270
- linkable_action = options.delete(:linkable_action) || action
271
- named_route(name, route, options.merge(:action => action.to_s)) and
272
- begin
273
- linkable_options = { :method => options[:conditions]._?[:method], :subsite => subsite }
274
- self.class.linkable!(model, linkable_action, linkable_options)
275
- end
276
- end
277
-
278
-
279
- def name_with_subsite(name)
280
- subsite ? "#{subsite}_#{name}" : name
281
- end
282
-
283
-
284
- def route_with_subsite(route)
285
- subsite ? "#{subsite}/#{route}" : route
286
- end
287
-
288
- end
289
-
290
- end
@@ -1,451 +0,0 @@
1
- module Hobo
2
-
3
- module Permissions
4
-
5
- def self.enable
6
- Hobo::Permissions::Associations.enable
7
- end
8
-
9
- def self.included(klass)
10
- klass.class_eval do
11
- extend ClassMethods
12
-
13
- alias_method_chain :create, :hobo_permission_check
14
- alias_method_chain :update, :hobo_permission_check
15
- alias_method_chain :destroy, :hobo_permission_check
16
- class << self
17
- alias_method_chain :has_many, :hobo_permission_check
18
- alias_method_chain :has_one, :hobo_permission_check
19
- alias_method_chain :belongs_to, :hobo_permission_check
20
- end
21
-
22
- attr_accessor :acting_user, :origin, :origin_attribute
23
-
24
- bool_attr_accessor :exempt_from_edit_checks
25
-
26
- define_callbacks :after_user_new
27
- end
28
- end
29
-
30
- def self.find_aliased_name(klass, method_name)
31
- # The method +method_name+ will have been aliased. We jump through some hoops to figure out
32
- # what it's new name is
33
- method_name = method_name.to_sym
34
- method = klass.instance_method method_name
35
- methods = (klass.private_instance_methods + klass.instance_methods).*.to_sym
36
- new_name = methods.select {|m| klass.instance_method(m) == method }.find { |m| m != method_name }
37
- end
38
-
39
- module ClassMethods
40
-
41
- def user_find(user, *args)
42
- record = find(*args)
43
- yield(record) if block_given?
44
- record.user_view user
45
- record
46
- end
47
-
48
-
49
- def user_new(user, attributes={})
50
- new(attributes) do |r|
51
- r.set_creator user
52
- yield r if block_given?
53
- r.user_view(user)
54
- r.with_acting_user(user) { r.send :callback, :after_user_new }
55
- end
56
- end
57
-
58
-
59
- def user_create(user, attributes={}, &block)
60
- if attributes.is_a?(Array)
61
- attributes.map { |attrs| user_create(user, attrs) }
62
- else
63
- record = user_new(user, attributes, &block)
64
- record.user_save(user)
65
- record
66
- end
67
- end
68
-
69
-
70
- def user_create!(user, attributes={}, &block)
71
- if attributes.is_a?(Array)
72
- attributes.map { |attrs| user_create(user, attrs) }
73
- else
74
- record = user_new(user, attributes, &block)
75
- record.user_save!(user)
76
- record
77
- end
78
- end
79
-
80
- def viewable_by?(user, attribute=nil)
81
- new.viewable_by?(user, attribute)
82
- end
83
-
84
- # ensure active_user gets passed down to :dependent => destroy
85
- # associations (Ticket #528)
86
-
87
- def has_many_with_hobo_permission_check(association_id, options = {}, &extension)
88
- has_many_without_hobo_permission_check(association_id, options, &extension)
89
- reflection = reflections[association_id]
90
- if reflection.options[:dependent]==:destroy
91
- #overriding dynamic method created in ActiveRecord::Associations#configure_dependency_for_has_many
92
- method_name = "has_many_dependent_destroy_for_#{reflection.name}".to_sym
93
- define_method(method_name) do
94
- send(reflection.name).each { |r| r.is_a?(Hobo::Model) ? r.user_destroy(acting_user) : r.destroy }
95
- end
96
- end
97
- end
98
-
99
- def has_one_with_hobo_permission_check(association_id, options = {}, &extension)
100
- has_one_without_hobo_permission_check(association_id, options, &extension)
101
- reflection = reflections[association_id]
102
- if reflection.options[:dependent]==:destroy
103
- #overriding dynamic method created in ActiveRecord::Associations#configure_dependency_for_has_one
104
- method_name = "has_one_dependent_destroy_for_#{reflection.name}".to_sym
105
- define_method(method_name) do
106
- association = send(reflection.name)
107
- unless association.nil?
108
- association.is_a?(Hobo::Model) ? association.user_destroy(acting_user) : association.destroy
109
- end
110
- end
111
- end
112
- end
113
-
114
- def belongs_to_with_hobo_permission_check(association_id, options = {}, &extension)
115
- belongs_to_without_hobo_permission_check(association_id, options, &extension)
116
- reflection = reflections[association_id]
117
- if reflection.options[:dependent]==:destroy
118
- #overriding dynamic method created in ActiveRecord::Associations#configure_dependency_for_belongs_to
119
- method_name = "belongs_to_dependent_destroy_for_#{reflection.name}".to_sym
120
- define_method(method_name) do
121
- association = send(reflection.name)
122
- unless association.nil?
123
- association.is_a?(Hobo::Model) ? association.user_destroy(acting_user) : association.destroy
124
- end
125
- end
126
- end
127
- end
128
-
129
- end
130
-
131
-
132
- # --- Hook ActiveRecord CRUD actions --- #
133
-
134
-
135
- def permission_check_required?
136
- # Lifecycle steps are exempt from permission checks
137
- acting_user && !(self.class.has_lifecycle? && lifecycle.active_step)
138
- end
139
-
140
- def create_with_hobo_permission_check(*args, &b)
141
- if permission_check_required?
142
- create_permitted? or raise PermissionDeniedError, "#{self.class.name}#create"
143
- end
144
- create_without_hobo_permission_check(*args, &b)
145
- end
146
-
147
- def update_with_hobo_permission_check(*args)
148
- if permission_check_required?
149
- update_permitted? or raise PermissionDeniedError, "#{self.class.name}#update"
150
- end
151
- update_without_hobo_permission_check(*args)
152
- end
153
-
154
- def destroy_with_hobo_permission_check
155
- if permission_check_required?
156
- destroy_permitted? or raise PermissionDeniedError, "#{self.class.name}#.destroy"
157
- end
158
-
159
- destroy_without_hobo_permission_check
160
- end
161
-
162
- # -------------------------------------- #
163
-
164
-
165
- # --- Permissions API --- #
166
-
167
-
168
- def with_acting_user(user)
169
- old = acting_user
170
- self.acting_user = user
171
- result = yield
172
- self.acting_user = old
173
- result
174
- end
175
-
176
- def user_save(user, validate = true)
177
- with_acting_user(user) { save(validate) }
178
- end
179
-
180
- def user_save!(user)
181
- with_acting_user(user) { save! }
182
- end
183
-
184
- def user_destroy(user)
185
- with_acting_user(user) { destroy }
186
- end
187
-
188
- def user_view(user, attribute=nil)
189
- raise PermissionDeniedError unless viewable_by?(user, attribute)
190
- end
191
-
192
- def user_update_attributes(user, attributes)
193
- with_acting_user(user) do
194
- self.attributes = attributes
195
- save
196
- end
197
- end
198
-
199
- def user_update_attributes!(user, attributes)
200
- with_acting_user(user) do
201
- self.attributes = attributes
202
- save!
203
- end
204
- end
205
-
206
- def creatable_by?(user)
207
- with_acting_user(user) { create_permitted? }
208
- end
209
-
210
- def updatable_by?(user)
211
- with_acting_user(user) { update_permitted? }
212
- end
213
-
214
- def destroyable_by?(user)
215
- with_acting_user(user) { destroy_permitted? }
216
- end
217
-
218
- def method_callable_by?(user, method)
219
- permission_method = "#{method}_permitted?"
220
- respond_to?(permission_method) && with_acting_user(user) { send(permission_method) }
221
- end
222
-
223
- def viewable_by?(user, attribute=nil)
224
- if attribute
225
- attribute = attribute.to_s.sub(/\?$/, '').to_sym
226
- return false if attribute && self.class.never_show?(attribute)
227
- end
228
- with_acting_user(user) { view_permitted?(attribute) }
229
- end
230
-
231
-
232
- def editable_by?(user, attribute=nil)
233
- return false if attribute_protected?(attribute)
234
-
235
- return true if exempt_from_edit_checks?
236
-
237
- # Can't view implies can't edit
238
- return false unless viewable_by?(user, attribute)
239
-
240
- if attribute
241
- attribute = attribute.to_s.sub(/\?$/, '').to_sym
242
-
243
- # Try the attribute-specific edit-permission method if there is one
244
- if has_hobo_method?(meth = "#{attribute}_edit_permitted?")
245
- return with_acting_user(user) { send(meth) }
246
- end
247
-
248
- # No setter = no edit permission
249
- return false if !respond_to?("#{attribute}=")
250
-
251
- refl = self.class.reflections[attribute.to_sym]
252
- if refl && refl.macro != :belongs_to # a belongs_to is handled the same as a regular attribute
253
- return association_editable_by?(user, refl)
254
- end
255
- end
256
-
257
- with_acting_user(user) { edit_permitted?(attribute) }
258
- end
259
-
260
-
261
- def attribute_protected?(attribute)
262
- attribute = attribute.to_s
263
-
264
- return true if attributes_protected_by_default.include? attribute
265
-
266
- if self.class.accessible_attributes
267
- return true if !self.class.accessible_attributes.include?(attribute)
268
- elsif self.class.protected_attributes
269
- return true if self.class.protected_attributes.include?(attribute)
270
- end
271
-
272
- # Readonly attributes can be set on creation but not thereafter
273
- return self.class.readonly_attributes.include?(attribute) if !new_record? && self.class.readonly_attributes
274
-
275
- false
276
- end
277
-
278
-
279
- def association_editable_by?(user, reflection)
280
- # has_one and polymorphic associations are not editable (for now)
281
- return false if reflection.macro == :has_one || reflection.options[:polymorphic]
282
-
283
- return false unless reflection.options[:accessible]
284
-
285
- record = if (through = reflection.through_reflection)
286
- # For edit permission on a has_many :through,
287
- # the user needs create+destroy permission on the join model
288
- send(through.name).new_candidate
289
- else
290
- # For edit permission on a regular has_many,
291
- # the user needs create/destroy permission on the member model
292
- send(reflection.name).new_candidate
293
- end
294
- record.creatable_by?(user) && record.destroyable_by?(user)
295
- end
296
-
297
- # ----------------------- #
298
-
299
-
300
- # --- Permission Declaration Helpers --- #
301
-
302
- def only_changed?(*attributes)
303
- attributes = attributes.map do |attr|
304
- with_attribute_or_belongs_to_keys(attr) { |a, ftype| ftype ? [a, ftype] : a }
305
- end.flatten
306
-
307
- changed.all? { |attr| attributes.include?(attr) }
308
- end
309
-
310
- def none_changed?(*attributes)
311
- attributes = attributes.map do |attr|
312
- with_attribute_or_belongs_to_keys(attr) { |a, ftype| ftype ? [a, ftype] : a }
313
- end.flatten
314
-
315
- attributes.all? { |attr| !changed.include?(attr) }
316
- end
317
-
318
- def any_changed?(*attributes)
319
- attributes.any? do |attr|
320
- with_attribute_or_belongs_to_keys(attr) do |a, ftype|
321
- if ftype
322
- changed.include?(a) || changed.include?(ftype)
323
- else
324
- changed.include?(a)
325
- end
326
- end
327
- end
328
- end
329
-
330
- def all_changed?(*attributes)
331
- attributes = prepare_attributes_for_change_helpers(attributes)
332
- attributes.all? do |attr|
333
- with_attribute_or_belongs_to_keys(attr) do |a, ftype|
334
- if ftype
335
- changed.include?(a) || changed.include?(ftype)
336
- else
337
- changed.include?(a)
338
- end
339
- end
340
- end
341
- end
342
-
343
- def with_attribute_or_belongs_to_keys(attribute)
344
- if (refl = self.class.reflections[attribute.to_sym]) && refl.macro == :belongs_to
345
- if refl.options[:polymorphic]
346
- yield refl.primary_key_name, refl.options[:foreign_type]
347
- else
348
- yield refl.primary_key_name, nil
349
- end
350
- else
351
- yield attribute.to_s, nil
352
- end
353
- end
354
-
355
-
356
-
357
- # -------------------------------------- #
358
-
359
-
360
- # --- Default *_permitted? methods --- #
361
-
362
- # Conservative default permissions #
363
- def create_permitted?; false end
364
- def update_permitted?; false end
365
- def destroy_permitted?; false end
366
-
367
- # Allow viewing by default
368
- def view_permitted?(attribute) true end
369
-
370
- # By default, attempt to derive edit permission from create/update permission
371
- def edit_permitted?(attribute)
372
- unknownify_attribute(attribute) if attribute
373
- new_record? ? create_permitted? : update_permitted?
374
- rescue Hobo::UndefinedAccessError
375
- # The permission is dependent on the unknown value
376
- # so this attribute is not editable
377
- false
378
- ensure
379
- deunknownify_attribute(attribute) if attribute
380
- end
381
-
382
-
383
- # Add some singleton methods to +record+ to give the effect that +attribute+ is unknown. That is,
384
- # attempts to access the attribute will result in a Hobo::UndefinedAccessError
385
- def unknownify_attribute(attr)
386
- metaclass.class_eval do
387
- define_method attr do
388
- raise Hobo::UndefinedAccessError
389
- end
390
- end
391
-
392
- if (refl = self.class.reflections[attr.to_sym]) && refl.macro == :belongs_to
393
- # A belongs_to -- also unknownify the underlying fields
394
- unknownify_attribute refl.primary_key_name
395
- unknownify_attribute refl.options[:foreign_type] if refl.options[:polymorphic]
396
- else
397
- # A regular field -- hack the dirty tracking methods
398
-
399
- metaclass.class_eval do
400
-
401
- define_method "#{attr}_change" do
402
- raise Hobo::UndefinedAccessError
403
- end
404
-
405
- define_method "#{attr}_was" do
406
- read_attribute attr
407
- end
408
-
409
- define_method "#{attr}_changed?" do
410
- true
411
- end
412
-
413
- def changed?
414
- true
415
- end
416
-
417
- define_method :changed do
418
- changed_attributes.keys | [attr.to_s]
419
- end
420
-
421
- def changes
422
- raise Hobo::UndefinedAccessError
423
- end
424
-
425
- end
426
- end
427
- end
428
-
429
- # Best. Name. Ever
430
- def deunknownify_attribute(attr, remove_globals = true)
431
- attr = attr.to_sym
432
-
433
- metaclass.send :remove_method, attr
434
-
435
- if (refl = self.class.reflections[attr]) && refl.macro == :belongs_to
436
- # A belongs_to -- restore the underlying fields
437
- deunknownify_attribute refl.primary_key_name
438
- deunknownify_attribute(refl.options[:foreign_type], false) if refl.options[:polymorphic]
439
- else
440
- # A regular field -- restore the dirty tracking methods
441
- # if remove_globals is false, skip the top-level methods, as we have already removed them
442
- to_remove = remove_globals ? [:changed?, :changed, :changes] : []
443
- (["#{attr}_change", "#{attr}_was", "#{attr}_changed?"] + to_remove).each do |m|
444
- metaclass.send :remove_method, m.to_sym
445
- end
446
- end
447
- end
448
- end
449
-
450
- end
451
-