typus 1.0.0.pre8 → 1.0.0.pre9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (247) hide show
  1. data/CHANGELOG +4 -6
  2. data/Gemfile +16 -13
  3. data/Gemfile.lock +26 -2
  4. data/Rakefile +2 -3
  5. data/app/controllers/admin/base_controller.rb +3 -5
  6. data/app/controllers/admin/resources_controller.rb +24 -26
  7. data/app/helpers/admin/base_helper.rb +14 -22
  8. data/app/helpers/admin/dashboard_helper.rb +2 -5
  9. data/app/helpers/admin/filters_helper.rb +6 -8
  10. data/app/helpers/admin/form_helper.rb +3 -9
  11. data/app/helpers/admin/preview_helper.rb +4 -6
  12. data/app/helpers/admin/relationships_helper.rb +9 -11
  13. data/app/helpers/admin/resources_helper.rb +6 -24
  14. data/app/helpers/admin/search_helper.rb +1 -1
  15. data/app/helpers/admin/sidebar_helper.rb +12 -15
  16. data/app/helpers/admin/table_helper.rb +13 -13
  17. data/app/views/admin/dashboard/show.html.erb +1 -1
  18. data/app/views/admin/helpers/_apps.html.erb +4 -3
  19. data/app/views/admin/helpers/_header.html.erb +2 -2
  20. data/app/views/admin/helpers/dashboard/_applications.html.erb +3 -3
  21. data/app/views/admin/helpers/dashboard/_resources.html.erb +17 -13
  22. data/app/views/admin/helpers/filters/_filters.html.erb +9 -27
  23. data/app/views/admin/helpers/resources/_pagination.html.erb +8 -9
  24. data/app/views/admin/helpers/search/_search.html.erb +1 -1
  25. data/app/views/admin/mailer/reset_password_link.text.erb +3 -0
  26. data/app/views/admin/resources/_form.html.erb +4 -4
  27. data/app/views/admin/resources/edit.html.erb +2 -3
  28. data/app/views/admin/resources/index.html.erb +15 -13
  29. data/app/views/admin/resources/new.html.erb +2 -2
  30. data/app/views/admin/resources/show.html.erb +1 -2
  31. data/app/views/admin/templates/_boolean.html.erb +1 -1
  32. data/app/views/admin/templates/_has_many.html.erb +2 -4
  33. data/app/views/admin/templates/_has_n.html.erb +1 -1
  34. data/app/views/admin/templates/_selector.html.erb +0 -1
  35. data/app/views/admin/templates/_string.html.erb +1 -1
  36. data/app/views/layouts/admin/session.html.erb +1 -1
  37. data/config/available_locales/ca.yml +3 -5
  38. data/config/available_locales/de.yml +2 -4
  39. data/config/available_locales/es.yml +2 -4
  40. data/config/available_locales/fr.yml +40 -39
  41. data/config/available_locales/fr_models.yml +2 -2
  42. data/config/available_locales/hu.yml +3 -5
  43. data/config/available_locales/it.yml +0 -4
  44. data/config/available_locales/language.yml.template +2 -4
  45. data/config/available_locales/pt-BR.yml +2 -4
  46. data/config/available_locales/ru.yml +2 -4
  47. data/config/routes.rb +1 -0
  48. data/lib/generators/templates/config/initializers/typus.rb +5 -5
  49. data/lib/generators/templates/config/initializers/typus_authentication.rb +5 -5
  50. data/lib/generators/templates/config/initializers/typus_resources.rb +7 -7
  51. data/lib/generators/templates/config/typus/README +11 -13
  52. data/lib/generators/templates/controller.rb +0 -2
  53. data/lib/generators/templates/functional_test.rb +4 -15
  54. data/lib/generators/typus/migration_generator.rb +0 -1
  55. data/lib/support/active_record.rb +18 -9
  56. data/lib/support/fake_user.rb +42 -0
  57. data/lib/support/object.rb +1 -0
  58. data/lib/support/string.rb +0 -32
  59. data/lib/tasks/typus.rake +0 -8
  60. data/lib/typus.rb +34 -18
  61. data/lib/typus/authentication/base.rb +26 -0
  62. data/lib/typus/authentication/http_basic.rb +22 -0
  63. data/lib/typus/authentication/none.rb +19 -0
  64. data/lib/typus/authentication/session.rb +190 -0
  65. data/lib/typus/format.rb +12 -19
  66. data/lib/typus/orm/active_record.rb +13 -51
  67. data/lib/typus/resources.rb +0 -9
  68. data/lib/typus/version.rb +1 -1
  69. data/test/factories.rb +43 -0
  70. data/test/fixtures/rails_app/app/controllers/application_controller.rb +0 -1
  71. data/test/fixtures/rails_app/app/models/page.rb +0 -4
  72. data/test/fixtures/rails_app/config/application.rb +6 -3
  73. data/test/fixtures/rails_app/config/database.yml +12 -20
  74. data/test/fixtures/rails_app/config/typus/application.yml +4 -8
  75. data/test/fixtures/rails_app/db/test.sqlite3 +0 -0
  76. data/test/fixtures/rails_app/log/test.log +227115 -0
  77. data/test/fixtures/rails_app/tmp/export-posts-20101012213057.csv +2 -0
  78. data/test/fixtures/rails_app/tmp/export-posts-20101012213132.csv +2 -0
  79. data/test/fixtures/rails_app/tmp/export-posts-20101012213234.csv +2 -0
  80. data/test/fixtures/rails_app/tmp/export-posts-20101012213506.csv +2 -0
  81. data/test/fixtures/rails_app/tmp/export-posts-20101012215057.csv +2 -0
  82. data/test/fixtures/rails_app/tmp/export-posts-20101012215217.csv +2 -0
  83. data/test/fixtures/rails_app/tmp/export-posts-20101012215415.csv +2 -0
  84. data/test/fixtures/rails_app/tmp/export-posts-20101012215443.csv +2 -0
  85. data/test/fixtures/rails_app/tmp/export-posts-20101012215509.csv +2 -0
  86. data/test/fixtures/rails_app/tmp/export-posts-20101012215528.csv +2 -0
  87. data/test/fixtures/rails_app/tmp/export-posts-20101012215551.csv +2 -0
  88. data/test/fixtures/rails_app/tmp/export-posts-20101012215615.csv +2 -0
  89. data/test/fixtures/rails_app/tmp/export-posts-20101012215640.csv +2 -0
  90. data/test/fixtures/rails_app/tmp/export-posts-20101012215714.csv +2 -0
  91. data/test/fixtures/rails_app/tmp/export-posts-20101012215744.csv +2 -0
  92. data/test/fixtures/rails_app/tmp/export-posts-20101012215814.csv +2 -0
  93. data/test/fixtures/rails_app/tmp/export-posts-20101012215836.csv +2 -0
  94. data/test/fixtures/rails_app/tmp/export-posts-20101012215906.csv +2 -0
  95. data/test/fixtures/rails_app/tmp/export-posts-20101012220659.csv +2 -0
  96. data/test/fixtures/rails_app/tmp/export-posts-20101012221043.csv +2 -0
  97. data/test/fixtures/rails_app/tmp/export-posts-20101012221258.csv +2 -0
  98. data/test/fixtures/rails_app/tmp/export-posts-20101012222558.csv +2 -0
  99. data/test/fixtures/rails_app/tmp/export-posts-20101012222752.csv +2 -0
  100. data/test/fixtures/rails_app/tmp/export-posts-20101012222848.csv +2 -0
  101. data/test/fixtures/rails_app/tmp/export-posts-20101012223433.csv +2 -0
  102. data/test/fixtures/rails_app/tmp/export-posts-20101012223535.csv +2 -0
  103. data/test/fixtures/rails_app/tmp/export-posts-20101012224003.csv +2 -0
  104. data/test/fixtures/rails_app/tmp/export-posts-20101012224052.csv +2 -0
  105. data/test/fixtures/rails_app/tmp/export-posts-20101012224109.csv +2 -0
  106. data/test/fixtures/rails_app/tmp/export-posts-20101012224139.csv +2 -0
  107. data/test/fixtures/rails_app/tmp/export-posts-20101012224205.csv +2 -0
  108. data/test/fixtures/rails_app/tmp/export-posts-20101012224243.csv +2 -0
  109. data/test/fixtures/rails_app/tmp/export-posts-20101012225119.csv +2 -0
  110. data/test/fixtures/rails_app/tmp/export-posts-20101013063215.csv +2 -0
  111. data/test/fixtures/rails_app/tmp/export-posts-20101013063300.csv +2 -0
  112. data/test/fixtures/rails_app/tmp/export-posts-20101013063352.csv +2 -0
  113. data/test/fixtures/rails_app/tmp/export-posts-20101013063427.csv +2 -0
  114. data/test/fixtures/rails_app/tmp/export-posts-20101013063448.csv +2 -0
  115. data/test/fixtures/rails_app/tmp/export-posts-20101013063530.csv +2 -0
  116. data/test/fixtures/rails_app/tmp/export-posts-20101013063559.csv +2 -0
  117. data/test/fixtures/rails_app/tmp/export-posts-20101013063646.csv +2 -0
  118. data/test/fixtures/rails_app/tmp/export-posts-20101013063737.csv +2 -0
  119. data/test/fixtures/rails_app/tmp/export-posts-20101013063822.csv +2 -0
  120. data/test/fixtures/rails_app/tmp/export-posts-20101013063837.csv +2 -0
  121. data/test/fixtures/rails_app/tmp/export-posts-20101013063919.csv +2 -0
  122. data/test/fixtures/rails_app/tmp/export-posts-20101013063939.csv +2 -0
  123. data/test/fixtures/rails_app/tmp/export-posts-20101013063952.csv +2 -0
  124. data/test/fixtures/rails_app/tmp/export-posts-20101013064220.csv +2 -0
  125. data/test/fixtures/rails_app/tmp/export-posts-20101013064253.csv +2 -0
  126. data/test/fixtures/rails_app/tmp/export-posts-20101013064318.csv +2 -0
  127. data/test/fixtures/rails_app/tmp/export-posts-20101013064404.csv +2 -0
  128. data/test/fixtures/rails_app/tmp/export-posts-20101013064452.csv +2 -0
  129. data/test/fixtures/rails_app/tmp/export-posts-20101013064536.csv +2 -0
  130. data/test/fixtures/rails_app/tmp/export-posts-20101013064652.csv +2 -0
  131. data/test/fixtures/rails_app/tmp/export-posts-20101013064845.csv +2 -0
  132. data/test/fixtures/rails_app/tmp/export-posts-20101013065023.csv +2 -0
  133. data/test/fixtures/rails_app/tmp/export-posts-20101013065137.csv +2 -0
  134. data/test/fixtures/rails_app/tmp/export-posts-20101013065713.csv +2 -0
  135. data/test/fixtures/rails_app/tmp/export-posts-20101013065954.csv +2 -0
  136. data/test/fixtures/rails_app/tmp/export-posts-20101013070057.csv +2 -0
  137. data/test/fixtures/rails_app/tmp/export-posts-20101013070116.csv +2 -0
  138. data/test/fixtures/rails_app/tmp/export-posts-20101013070156.csv +2 -0
  139. data/test/fixtures/rails_app/tmp/export-posts-20101013070349.csv +2 -0
  140. data/test/fixtures/rails_app/tmp/export-posts-20101013070429.csv +2 -0
  141. data/test/fixtures/rails_app/tmp/export-posts-20101013070448.csv +2 -0
  142. data/test/fixtures/rails_app/tmp/export-posts-20101013070514.csv +2 -0
  143. data/test/fixtures/rails_app/tmp/export-posts-20101013070709.csv +2 -0
  144. data/test/fixtures/rails_app/tmp/export-posts-20101013070745.csv +2 -0
  145. data/test/fixtures/rails_app/tmp/export-posts-20101013070809.csv +2 -0
  146. data/test/fixtures/rails_app/tmp/export-posts-20101013070905.csv +2 -0
  147. data/test/fixtures/rails_app/tmp/export-posts-20101013070955.csv +2 -0
  148. data/test/fixtures/rails_app/tmp/export-posts-20101013071116.csv +2 -0
  149. data/test/fixtures/rails_app/tmp/export-posts-20101013071209.csv +2 -0
  150. data/test/fixtures/rails_app/tmp/export-posts-20101013071335.csv +2 -0
  151. data/test/fixtures/rails_app/tmp/export-posts-20101013071632.csv +2 -0
  152. data/test/fixtures/rails_app/tmp/export-posts-20101013071643.csv +2 -0
  153. data/test/fixtures/rails_app/tmp/export-posts-20101013071735.csv +2 -0
  154. data/test/fixtures/rails_app/tmp/export-posts-20101013071749.csv +2 -0
  155. data/test/fixtures/rails_app/tmp/export-posts-20101013072030.csv +2 -0
  156. data/test/fixtures/rails_app/tmp/export-posts-20101013072050.csv +2 -0
  157. data/test/fixtures/rails_app/tmp/export-posts-20101013072108.csv +2 -0
  158. data/test/fixtures/rails_app/tmp/export-posts-20101013072713.csv +2 -0
  159. data/test/fixtures/rails_app/tmp/export-posts-20101013072751.csv +2 -0
  160. data/test/fixtures/rails_app/tmp/export-posts-20101013072912.csv +2 -0
  161. data/test/fixtures/rails_app/tmp/export-posts-20101013074546.csv +2 -0
  162. data/test/fixtures/rails_app/tmp/export-posts-20101018180512.csv +2 -0
  163. data/test/fixtures/rails_app/tmp/export-posts-20101018183359.csv +2 -0
  164. data/test/fixtures/rails_app/tmp/export-posts-20101018183608.csv +2 -0
  165. data/test/fixtures/rails_app/tmp/export-posts-20101018183759.csv +2 -0
  166. data/test/functional/admin/account_controller_test.rb +1 -1
  167. data/test/functional/admin/{resources_controller_assets_relationships.rb → assets_controller_test.rb} +12 -16
  168. data/test/functional/admin/categories_controller_test.rb +62 -0
  169. data/test/functional/admin/{resources_controller_comments_toggle_test.rb → comments_controller_test.rb} +0 -0
  170. data/test/functional/admin/dashboard_controller_test.rb +34 -2
  171. data/test/functional/admin/posts_controller_test.rb +548 -0
  172. data/test/functional/admin/session_controller_test.rb +2 -2
  173. data/test/functional/admin/{resources_controller_tableless_resource_test.rb → status_controller_test.rb} +0 -0
  174. data/test/functional/admin/{resources_controller_typus_users_test.rb → typus_users_controller_test.rb} +2 -2
  175. data/test/helpers/admin/base_helper_test.rb +35 -37
  176. data/test/helpers/admin/dashboard_helper_test.rb +4 -8
  177. data/test/helpers/admin/form_helper_test.rb +9 -34
  178. data/test/helpers/admin/resources_helper_test.rb +28 -16
  179. data/test/helpers/admin/sidebar_helper_test.rb +14 -27
  180. data/test/helpers/admin/table_helper_test.rb +24 -68
  181. data/test/integration/login_test.rb +13 -0
  182. data/test/integration_test_helper.rb +21 -0
  183. data/test/lib/support/active_record_test.rb +72 -21
  184. data/test/lib/support/array_test.rb +2 -6
  185. data/test/lib/support/string_test.rb +21 -30
  186. data/test/lib/typus/active_record_test.rb +162 -100
  187. data/test/lib/typus/resource_test.rb +2 -3
  188. data/test/lib/typus_test.rb +25 -22
  189. data/test/test_helper.rb +4 -17
  190. data/test/unit/admin/mailer_test.rb +2 -2
  191. data/test/unit/typus_user_roles_test.rb +23 -23
  192. data/test/unit/typus_user_test.rb +30 -24
  193. data/typus.gemspec +0 -1
  194. metadata +111 -60
  195. data/app/models/admin/fake_user.rb +0 -46
  196. data/lib/generators/templates/model.yml +0 -10
  197. data/lib/typus/authentication.rb +0 -241
  198. data/lib/vendor/paginator.rb +0 -143
  199. data/test/factories/assets.rb +0 -5
  200. data/test/factories/categories.rb +0 -3
  201. data/test/factories/comments.rb +0 -6
  202. data/test/factories/pages.rb +0 -5
  203. data/test/factories/pictures.rb +0 -7
  204. data/test/factories/posts.rb +0 -6
  205. data/test/factories/typus_users.rb +0 -8
  206. data/test/fixtures/rails_app/README +0 -244
  207. data/test/fixtures/rails_app/app/helpers/application_helper.rb +0 -2
  208. data/test/fixtures/rails_app/app/views/layouts/application.html.erb +0 -14
  209. data/test/fixtures/rails_app/config/environments/development.rb +0 -19
  210. data/test/fixtures/rails_app/config/environments/production.rb +0 -42
  211. data/test/fixtures/rails_app/config/initializers/acts_as_list.rb +0 -258
  212. data/test/fixtures/rails_app/config/initializers/acts_as_tree.rb +0 -98
  213. data/test/fixtures/rails_app/config/initializers/backtrace_silencers.rb +0 -7
  214. data/test/fixtures/rails_app/config/initializers/inflections.rb +0 -10
  215. data/test/fixtures/rails_app/config/initializers/mime_types.rb +0 -5
  216. data/test/fixtures/rails_app/config/locales/en.yml +0 -5
  217. data/test/fixtures/rails_app/db/seeds.rb +0 -7
  218. data/test/fixtures/rails_app/public/404.html +0 -26
  219. data/test/fixtures/rails_app/public/422.html +0 -26
  220. data/test/fixtures/rails_app/public/500.html +0 -26
  221. data/test/fixtures/rails_app/public/favicon.ico +0 -0
  222. data/test/fixtures/rails_app/public/images/rails.png +0 -0
  223. data/test/fixtures/rails_app/public/index.html +0 -279
  224. data/test/fixtures/rails_app/public/javascripts/application.js +0 -2
  225. data/test/fixtures/rails_app/public/javascripts/controls.js +0 -965
  226. data/test/fixtures/rails_app/public/javascripts/dragdrop.js +0 -974
  227. data/test/fixtures/rails_app/public/javascripts/effects.js +0 -1123
  228. data/test/fixtures/rails_app/public/javascripts/prototype.js +0 -4874
  229. data/test/fixtures/rails_app/public/javascripts/rails.js +0 -118
  230. data/test/fixtures/rails_app/public/robots.txt +0 -5
  231. data/test/fixtures/rails_app/script/rails +0 -9
  232. data/test/functional/admin/dashboard_controller_http_basic_test.rb +0 -24
  233. data/test/functional/admin/dashboard_controller_none_test.rb +0 -18
  234. data/test/functional/admin/resources_controller_categories_before_test.rb +0 -10
  235. data/test/functional/admin/resources_controller_categories_lists_test.rb +0 -44
  236. data/test/functional/admin/resources_controller_categories_views_test.rb +0 -10
  237. data/test/functional/admin/resources_controller_posts_before_test.rb +0 -10
  238. data/test/functional/admin/resources_controller_posts_crud_custom_test.rb +0 -27
  239. data/test/functional/admin/resources_controller_posts_crud_test.rb +0 -44
  240. data/test/functional/admin/resources_controller_posts_formats_test.rb +0 -39
  241. data/test/functional/admin/resources_controller_posts_forms_test.rb +0 -58
  242. data/test/functional/admin/resources_controller_posts_permissions_test.rb +0 -100
  243. data/test/functional/admin/resources_controller_posts_relationships_test.rb +0 -91
  244. data/test/functional/admin/resources_controller_posts_roles.rb +0 -52
  245. data/test/functional/admin/resources_controller_posts_views_test.rb +0 -143
  246. data/test/lib/vendor/paginator_test.rb +0 -141
  247. data/typus-1.0.0.pre7.gem +0 -0
@@ -1,46 +0,0 @@
1
- module Admin
2
-
3
- class FakeUser
4
-
5
- def id
6
- 0
7
- end
8
-
9
- def can?(*args)
10
- true
11
- end
12
-
13
- def cannot?(*args)
14
- !can?(*args)
15
- end
16
-
17
- def is_root?
18
- true
19
- end
20
-
21
- def is_not_root?
22
- !is_root?
23
- end
24
-
25
- def resources
26
- Typus::Configuration.roles[role].compact
27
- end
28
-
29
- def applications
30
- Typus.applications
31
- end
32
-
33
- def application(name)
34
- Typus.application(name)
35
- end
36
-
37
- def role
38
- Typus.master_role
39
- end
40
-
41
- def name
42
- end
43
-
44
- end
45
-
46
- end
@@ -1,10 +0,0 @@
1
- admin:
2
- first_name: Admin
3
- last_name: Example
4
- role: admin
5
- email: admin@example.com
6
- status: true
7
- token: 1A2B3C4D5E6F
8
- salt: admin
9
- crypted_password: <%= Digest::SHA1.hexdigest("--admin--#{12345678}--") %>
10
- preferences: { :locale => :en }
@@ -1,241 +0,0 @@
1
- module Typus
2
-
3
- module Authentication
4
-
5
- protected
6
-
7
- #--
8
- # Require login checks if the user is logged on Typus, otherwise
9
- # is sent to the sign in page with a :back_to param to return where
10
- # she tried to go.
11
- #++
12
- def authenticate
13
- case Typus.authentication
14
- when :none
15
- authenticate_via_none
16
- when :http_basic
17
- authenticate_via_http_basic
18
- when :session
19
- authenticate_via_session
20
- end
21
- end
22
-
23
- def authenticate_via_none
24
- @current_user = Admin::FakeUser.new
25
- end
26
-
27
- def authenticate_via_http_basic
28
- @current_user = Admin::FakeUser.new
29
- authenticate_or_request_with_http_basic(Typus.admin_title) do |user_name, password|
30
- user_name == Typus.username && password == Typus.password
31
- end
32
- end
33
-
34
- def authenticate_via_session
35
- if session[:typus_user_id]
36
- current_user
37
- else
38
- back_to = request.env['PATH_INFO'] unless [admin_dashboard_path, admin_path].include?(request.env['PATH_INFO'])
39
- redirect_to new_admin_session_path(:back_to => back_to)
40
- end
41
- end
42
-
43
- #--
44
- # Return the current user. If role does not longer exist on the
45
- # system @current_user will be signed out from Typus.
46
- #++
47
- def current_user
48
-
49
- @current_user = Typus.user_class.find(session[:typus_user_id])
50
-
51
- unless Typus::Configuration.roles.has_key?(@current_user.role)
52
- raise _t("Role does no longer exists.")
53
- end
54
-
55
- unless @current_user.status
56
- back_to = (request.env['REQUEST_URI'] == admin_dashboard_path) ? nil : request.env['REQUEST_URI']
57
- raise _t("Typus user has been disabled.")
58
- end
59
-
60
- I18n.locale = @current_user.preferences[:locale]
61
-
62
- rescue Exception => error
63
- flash[:notice] = error.message
64
- session[:typus_user_id] = nil
65
- redirect_to new_admin_session_path(:back_to => back_to)
66
- end
67
-
68
- #--
69
- # Action is available on: edit, update, toggle and destroy
70
- #++
71
- def check_if_user_can_perform_action_on_user
72
-
73
- return unless Typus.authentication.eql?(:session)
74
-
75
- return unless @item.kind_of?(Typus.user_class)
76
-
77
- current_user = (@current_user == @item)
78
-
79
- message = case params[:action]
80
- when 'edit'
81
-
82
- # Only admin and owner of Typus User can edit.
83
- if @current_user.is_not_root? && !current_user
84
- _t("As you're not the admin or the owner of this record you cannot edit it.")
85
- end
86
-
87
- when 'update'
88
-
89
- # current_user cannot change her role.
90
- if current_user && !(@item.role == params[@object_name][:role])
91
- _t("You can't change your role.")
92
- end
93
-
94
- when 'toggle'
95
-
96
- # Only admin can toggle typus user status, but not herself.
97
- if @current_user.is_root? && current_user
98
- _t("You can't toggle your status.")
99
- elsif @current_user.is_not_root?
100
- _t("You're not allowed to toggle status.")
101
- end
102
-
103
- when 'destroy'
104
-
105
- # Admin can remove anything except herself.
106
- if @current_user.is_root? && current_user
107
- _t("You can't remove yourself.")
108
- elsif @current_user.is_not_root?
109
- _t("You're not allowed to remove Typus Users.")
110
- end
111
-
112
- end
113
-
114
- if message
115
- flash[:notice] = message
116
- redirect_to set_path
117
- end
118
-
119
- end
120
-
121
- #--
122
- # This method checks if the user can perform the requested action.
123
- # It works on models, so its available on the admin_controller.
124
- #++
125
- def check_if_user_can_perform_action_on_resources
126
-
127
- return unless Typus.authentication.eql?(:session)
128
-
129
- message = case params[:action]
130
- when 'index', 'show'
131
- "%{current_user_role} can't display items."
132
- when 'destroy'
133
- "%{current_user_role} can't delete this item."
134
- else
135
- "%{current_user_role} can't perform action. (%{action})"
136
- end
137
-
138
- message = _t(message,
139
- :current_user_role => @current_user.role.capitalize,
140
- :action => params[:action])
141
-
142
- unless @current_user.can?(params[:action], @resource)
143
- flash[:notice] = message
144
- redirect_to set_path
145
- end
146
-
147
- end
148
-
149
- #--
150
- # This method checks if the user can perform the requested action.
151
- # It works on a resource: git, memcached, syslog ...
152
- #++
153
- def check_if_user_can_perform_action_on_resource
154
- return unless Typus.authentication.eql?(:session)
155
-
156
- controller = params[:controller].extract_resource
157
- action = params[:action]
158
- unless @current_user.can?(action, controller.camelize, { :special => true })
159
- render :text => "Not allowed!", :status => :unprocessable_entity
160
- end
161
- end
162
-
163
- #--
164
- # If item is owned by another user, we only can perform a
165
- # show action on the item. Updated item is also blocked.
166
- #
167
- # before_filter :check_resource_ownership, :only => [ :edit, :update, :destroy,
168
- # :toggle, :position,
169
- # :relate, :unrelate ]
170
- #++
171
- def check_resource_ownership
172
-
173
- return unless Typus.authentication.eql?(:session)
174
-
175
- # By-pass if current_user is root.
176
- return if @current_user.is_root?
177
-
178
- condition_typus_users = @item.respond_to?(Typus.relationship) && !@item.send(Typus.relationship).include?(@current_user)
179
- condition_typus_user_id = @item.respond_to?(Typus.user_fk) && !@item.owned_by?(@current_user)
180
-
181
- if condition_typus_users || condition_typus_user_id
182
- alert = _t("You don't have permission to access this item.")
183
- redirect_to set_path, :alert => alert
184
- end
185
-
186
- end
187
-
188
- def check_resource_ownerships
189
-
190
- return unless Typus.authentication.eql?(:session)
191
-
192
- # By-pass if current_user is root.
193
- return if @current_user.is_root?
194
-
195
- # Show only related items it @resource has a foreign_key (Typus.user_fk)
196
- # related to the logged user.
197
- if @resource.typus_user_id?
198
- condition = { Typus.user_fk => @current_user }
199
- @conditions = @resource.merge_conditions(@conditions, condition)
200
- end
201
-
202
- end
203
-
204
- def check_ownership_of_referal_item
205
- return unless Typus.authentication.eql?(:session)
206
-
207
- return unless params[:resource] && params[:resource_id]
208
- klass = params[:resource].classify.constantize
209
- return if !klass.typus_user_id?
210
- item = klass.find(params[:resource_id])
211
- raise "You're not owner of this record." unless item.owned_by?(@current_user) || @current_user.is_root?
212
- end
213
-
214
- def set_attributes_on_create
215
- return unless Typus.authentication.eql?(:session)
216
- if @resource.typus_user_id?
217
- @item.attributes = { Typus.user_fk => @current_user.id }
218
- end
219
- end
220
-
221
- def set_attributes_on_update
222
- return unless Typus.authentication.eql?(:session)
223
- if @resource.typus_user_id? && @current_user.is_not_root?
224
- @item.update_attributes(Typus.user_fk => @current_user.id)
225
- end
226
- end
227
-
228
- #--
229
- # Reload @current_user when updating to see flash message in the
230
- # correct locale.
231
- #++
232
- def reload_locales
233
- return unless Typus.authentication.eql?(:session)
234
- if @resource.eql?(Typus.user_class)
235
- I18n.locale = @current_user.reload.preferences[:locale]
236
- end
237
- end
238
-
239
- end
240
-
241
- end
@@ -1,143 +0,0 @@
1
- require 'forwardable'
2
-
3
- class Paginator
4
-
5
- VERSION = '1.1.1'
6
-
7
- include Enumerable
8
-
9
- class ArgumentError < ::ArgumentError; end
10
- class MissingCountError < ArgumentError; end
11
- class MissingSelectError < ArgumentError; end
12
-
13
- attr_reader :per_page, :count
14
-
15
- # Instantiate a new Paginator object
16
- #
17
- # Provide:
18
- # * A total count of the number of objects to paginate
19
- # * The number of objects in each page
20
- # * A block that returns the array of items
21
- # * The block is passed the item offset
22
- # (and the number of items to show per page, for
23
- # convenience, if the arity is 2)
24
- def initialize(count, per_page, &select)
25
- @count, @per_page = count, per_page
26
- unless select
27
- raise MissingSelectError, "Must provide block to select data for each page"
28
- end
29
- @select = select
30
- end
31
-
32
- # Total number of pages
33
- def number_of_pages
34
- (@count / @per_page).to_i + (@count % @per_page > 0 ? 1 : 0)
35
- end
36
-
37
- # First page object
38
- def first
39
- page 1
40
- end
41
-
42
- # Last page object
43
- def last
44
- page number_of_pages
45
- end
46
-
47
- def each
48
- 1.upto(number_of_pages) do |number|
49
- yield page(number)
50
- end
51
- end
52
-
53
- # Retrieve page object by number
54
- def page(number)
55
- number = (n = number.to_i) > 0 ? n : 1
56
- Page.new(self, number, lambda {
57
- offset = (number - 1) * @per_page
58
- args = [offset]
59
- args << @per_page if @select.arity == 2
60
- @select.call(*args)
61
- })
62
- end
63
-
64
- # Page object
65
- #
66
- # Retrieves items for a page and provides metadata about the position
67
- # of the page in the paginator
68
- class Page
69
-
70
- include Enumerable
71
-
72
- attr_reader :number, :pager
73
-
74
- def initialize(pager, number, select) #:nodoc:
75
- @pager, @number = pager, number
76
- @offset = (number - 1) * pager.per_page
77
- @select = select
78
- end
79
-
80
- # Retrieve the items for this page
81
- # * Caches
82
- def items
83
- @items ||= @select.call
84
- end
85
-
86
- # Does this page have any items?
87
- def empty?
88
- items.empty?
89
- end
90
-
91
- # Checks to see if there's a page before this one
92
- def prev?
93
- @number > 1
94
- end
95
-
96
- # Get previous page (if possible)
97
- def prev
98
- @pager.page(@number - 1) if prev?
99
- end
100
-
101
- # Checks to see if there's a page after this one
102
- def next?
103
- @number < @pager.number_of_pages
104
- end
105
-
106
- # Get next page (if possible)
107
- def next
108
- @pager.page(@number + 1) if next?
109
- end
110
-
111
- # The "item number" of the first item on this page
112
- def first_item_number
113
- 1 + @offset
114
- end
115
-
116
- # The "item number" of the last item on this page
117
- def last_item_number
118
- if next?
119
- @offset + @pager.per_page
120
- else
121
- @pager.count
122
- end
123
- end
124
-
125
- def ==(other) #:nodoc:
126
- @pager == other.pager && self.number == other.number
127
- end
128
-
129
- def each(&block)
130
- items.each(&block)
131
- end
132
-
133
- def method_missing(meth, *args, &block) #:nodoc:
134
- if @pager.respond_to?(meth)
135
- @pager.__send__(meth, *args, &block)
136
- else
137
- super
138
- end
139
- end
140
-
141
- end
142
-
143
- end
@@ -1,5 +0,0 @@
1
- Factory.define :asset do |f|
2
- f.caption "Caption"
3
- f.resource_type Post
4
- f.resource_id 1
5
- end