brainsome_typus 4.0.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (241) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +488 -0
  3. data/Gemfile +60 -0
  4. data/Gemfile.lock +205 -0
  5. data/MIT-LICENSE +20 -0
  6. data/README.md +94 -0
  7. data/Rakefile +33 -0
  8. data/TODO.md +32 -0
  9. data/app/assets/javascripts/typus/application.js +11 -0
  10. data/app/assets/javascripts/typus/custom.js +2 -0
  11. data/app/assets/javascripts/typus/jquery-1.8.3-min.js +2 -0
  12. data/app/assets/javascripts/typus/jquery.application.js +33 -0
  13. data/app/assets/stylesheets/typus/application.css +5 -0
  14. data/app/assets/stylesheets/typus/custom.css +4 -0
  15. data/app/assets/stylesheets/typus/overrides.css.scss +46 -0
  16. data/app/controllers/admin/account_controller.rb +47 -0
  17. data/app/controllers/admin/base_controller.rb +44 -0
  18. data/app/controllers/admin/dashboard_controller.rb +5 -0
  19. data/app/controllers/admin/resource_controller.rb +7 -0
  20. data/app/controllers/admin/resources_controller.rb +256 -0
  21. data/app/controllers/admin/session_controller.rb +42 -0
  22. data/app/controllers/concerns/admin/actions.rb +88 -0
  23. data/app/controllers/concerns/admin/acts_as_list.rb +26 -0
  24. data/app/controllers/concerns/admin/ancestry.rb +26 -0
  25. data/app/controllers/concerns/admin/autocomplete.rb +11 -0
  26. data/app/controllers/concerns/admin/bulk.rb +76 -0
  27. data/app/controllers/concerns/admin/featured_image.rb +22 -0
  28. data/app/controllers/concerns/admin/filters.rb +57 -0
  29. data/app/controllers/concerns/admin/format.rb +48 -0
  30. data/app/controllers/concerns/admin/headless.rb +23 -0
  31. data/app/controllers/concerns/admin/hooks.rb +4 -0
  32. data/app/controllers/concerns/admin/multisite.rb +10 -0
  33. data/app/controllers/concerns/admin/navigation.rb +26 -0
  34. data/app/controllers/concerns/admin/trash.rb +65 -0
  35. data/app/helpers/admin/base_helper.rb +59 -0
  36. data/app/helpers/admin/resources/data_types/belongs_to_helper.rb +91 -0
  37. data/app/helpers/admin/resources/data_types/boolean_helper.rb +28 -0
  38. data/app/helpers/admin/resources/data_types/date_helper.rb +16 -0
  39. data/app/helpers/admin/resources/data_types/datetime_helper.rb +19 -0
  40. data/app/helpers/admin/resources/data_types/dragonfly_helper.rb +56 -0
  41. data/app/helpers/admin/resources/data_types/float_helper.rb +2 -0
  42. data/app/helpers/admin/resources/data_types/has_and_belongs_to_many_helper.rb +53 -0
  43. data/app/helpers/admin/resources/data_types/has_many_helper.rb +66 -0
  44. data/app/helpers/admin/resources/data_types/has_one_helper.rb +36 -0
  45. data/app/helpers/admin/resources/data_types/integer_helper.rb +10 -0
  46. data/app/helpers/admin/resources/data_types/paperclip_helper.rb +36 -0
  47. data/app/helpers/admin/resources/data_types/position_helper.rb +20 -0
  48. data/app/helpers/admin/resources/data_types/selector_helper.rb +11 -0
  49. data/app/helpers/admin/resources/data_types/string_helper.rb +32 -0
  50. data/app/helpers/admin/resources/data_types/text_helper.rb +7 -0
  51. data/app/helpers/admin/resources/data_types/transversal_helper.rb +8 -0
  52. data/app/helpers/admin/resources/data_types/tree_helper.rb +26 -0
  53. data/app/helpers/admin/resources/display_helper.rb +27 -0
  54. data/app/helpers/admin/resources/filters_helper.rb +28 -0
  55. data/app/helpers/admin/resources/form_helper.rb +84 -0
  56. data/app/helpers/admin/resources/list_helper.rb +31 -0
  57. data/app/helpers/admin/resources/pagination_helper.rb +28 -0
  58. data/app/helpers/admin/resources/relationships_helper.rb +34 -0
  59. data/app/helpers/admin/resources/sidebar_helper.rb +40 -0
  60. data/app/helpers/admin/resources/table_helper.rb +66 -0
  61. data/app/helpers/admin/resources_helper.rb +25 -0
  62. data/app/mailers/admin/mailer.rb +20 -0
  63. data/app/views/admin/account/_form.html.erb +11 -0
  64. data/app/views/admin/account/forgot_password.html.erb +19 -0
  65. data/app/views/admin/account/new.html.erb +13 -0
  66. data/app/views/admin/dashboard/_sidebar.html.erb +14 -0
  67. data/app/views/admin/dashboard/index.html.erb +6 -0
  68. data/app/views/admin/dashboard/styles.html.erb +153 -0
  69. data/app/views/admin/dashboard/widgets/_applications.html.erb +44 -0
  70. data/app/views/admin/dashboard/widgets/_default.html.erb +46 -0
  71. data/app/views/admin/dashboard/widgets/_models.html.erb +23 -0
  72. data/app/views/admin/dashboard/widgets/_models_extended.html.erb +25 -0
  73. data/app/views/admin/dashboard/widgets/_resources.html.erb +23 -0
  74. data/app/views/admin/dashboard/widgets/_users.html.erb +23 -0
  75. data/app/views/admin/mailer/reset_password_instructions.html.erb +7 -0
  76. data/app/views/admin/mailer/reset_password_instructions.text.erb +7 -0
  77. data/app/views/admin/resources/_actions.html.erb +23 -0
  78. data/app/views/admin/resources/_edit.html.erb +0 -0
  79. data/app/views/admin/resources/_form.html.erb +41 -0
  80. data/app/views/admin/resources/_form_javascripts.html.erb +0 -0
  81. data/app/views/admin/resources/_form_modal.html.erb +13 -0
  82. data/app/views/admin/resources/_index.html.erb +0 -0
  83. data/app/views/admin/resources/_new.html.erb +0 -0
  84. data/app/views/admin/resources/_pagination.html.erb +15 -0
  85. data/app/views/admin/resources/_show.html.erb +0 -0
  86. data/app/views/admin/resources/edit/_actions.html.erb +7 -0
  87. data/app/views/admin/resources/edit/_recently_edited.html.erb +8 -0
  88. data/app/views/admin/resources/edit.html.erb +22 -0
  89. data/app/views/admin/resources/index.html.erb +81 -0
  90. data/app/views/admin/resources/new.html.erb +21 -0
  91. data/app/views/admin/resources/show.html.erb +27 -0
  92. data/app/views/admin/session/new.html.erb +21 -0
  93. data/app/views/admin/shared/_head.html.erb +15 -0
  94. data/app/views/admin/shared/_modals.html.erb +3 -0
  95. data/app/views/admin/shared/_sidebar_placeholder.html.erb +0 -0
  96. data/app/views/admin/templates/README.md +5 -0
  97. data/app/views/admin/templates/_belongs_to.html.erb +13 -0
  98. data/app/views/admin/templates/_boolean.html.erb +6 -0
  99. data/app/views/admin/templates/_date.html.erb +6 -0
  100. data/app/views/admin/templates/_datetime.html.erb +7 -0
  101. data/app/views/admin/templates/_dragonfly.html.erb +8 -0
  102. data/app/views/admin/templates/_dragonfly_form_preview.html.erb +23 -0
  103. data/app/views/admin/templates/_dragonfly_preview.html.erb +12 -0
  104. data/app/views/admin/templates/_has_and_belongs_to_many.html.erb +21 -0
  105. data/app/views/admin/templates/_has_many.html.erb +20 -0
  106. data/app/views/admin/templates/_has_one.html.erb +14 -0
  107. data/app/views/admin/templates/_paperclip.html.erb +7 -0
  108. data/app/views/admin/templates/_paperclip_preview.html.erb +3 -0
  109. data/app/views/admin/templates/_password.html.erb +6 -0
  110. data/app/views/admin/templates/_position.html.erb +6 -0
  111. data/app/views/admin/templates/_profile_sidebar.html.erb +4 -0
  112. data/app/views/admin/templates/_selector.html.erb +6 -0
  113. data/app/views/admin/templates/_string.html.erb +6 -0
  114. data/app/views/admin/templates/_string_with_preview.html.erb +11 -0
  115. data/app/views/admin/templates/_text.html.erb +11 -0
  116. data/app/views/admin/templates/_text_with_ckeditor.html.erb +24 -0
  117. data/app/views/admin/templates/_text_with_ckeditor_and_assets.html.erb +38 -0
  118. data/app/views/admin/templates/_time.html.erb +9 -0
  119. data/app/views/admin/templates/_tree.html.erb +6 -0
  120. data/app/views/admin/templates/modals/_belongs_to.html.erb +56 -0
  121. data/app/views/admin/templates/modals/_dragonfly.html.erb +9 -0
  122. data/app/views/admin/templates/modals/_has_and_belongs_to_many.html.erb +19 -0
  123. data/app/views/admin/templates/modals/_has_many.html.erb +53 -0
  124. data/app/views/helpers/admin/base/_apps.html.erb +38 -0
  125. data/app/views/helpers/admin/base/_breadcrumbs.html.erb +6 -0
  126. data/app/views/helpers/admin/base/_flash_message.html.erb +3 -0
  127. data/app/views/helpers/admin/base/_login_info.html.erb +37 -0
  128. data/app/views/helpers/admin/dashboard/_resources.html.erb +19 -0
  129. data/app/views/helpers/admin/resources/_actions.html.erb +18 -0
  130. data/app/views/helpers/admin/resources/_errors.html.erb +12 -0
  131. data/app/views/helpers/admin/resources/_filters.html.erb +14 -0
  132. data/app/views/helpers/admin/resources/_search.html.erb +14 -0
  133. data/app/views/helpers/admin/resources/_sidebar.html.erb +17 -0
  134. data/app/views/helpers/admin/resources/_table.html.erb +47 -0
  135. data/app/views/layouts/admin/base.html.erb +40 -0
  136. data/app/views/layouts/admin/headless.html.erb +1 -0
  137. data/app/views/layouts/admin/session.html.erb +44 -0
  138. data/config/locales/typus.ca.models.yml +17 -0
  139. data/config/locales/typus.ca.yml +78 -0
  140. data/config/locales/typus.de.models.yml +17 -0
  141. data/config/locales/typus.de.yml +98 -0
  142. data/config/locales/typus.el.models.yml +17 -0
  143. data/config/locales/typus.el.yml +79 -0
  144. data/config/locales/typus.es.models.yml +18 -0
  145. data/config/locales/typus.es.yml +91 -0
  146. data/config/locales/typus.fr.models.yml +18 -0
  147. data/config/locales/typus.fr.yml +83 -0
  148. data/config/locales/typus.hu.models.yml +17 -0
  149. data/config/locales/typus.hu.yml +77 -0
  150. data/config/locales/typus.it.models.yml +18 -0
  151. data/config/locales/typus.it.yml +86 -0
  152. data/config/locales/typus.locale.models.yml.template +17 -0
  153. data/config/locales/typus.locale.yml.template +78 -0
  154. data/config/locales/typus.pt-BR.models.yml +17 -0
  155. data/config/locales/typus.pt-BR.yml +77 -0
  156. data/config/locales/typus.pt-PT.models.yml +17 -0
  157. data/config/locales/typus.pt-PT.yml +85 -0
  158. data/config/locales/typus.ru.models.yml +17 -0
  159. data/config/locales/typus.ru.yml +77 -0
  160. data/config/locales/typus.zh-CN.models.yml +17 -0
  161. data/config/locales/typus.zh-CN.yml +76 -0
  162. data/config/routes.rb +46 -0
  163. data/lib/generators/templates/config/initializers/typus.rb +35 -0
  164. data/lib/generators/templates/config/initializers/typus_authentication.rb +18 -0
  165. data/lib/generators/templates/config/initializers/typus_resources.rb +24 -0
  166. data/lib/generators/templates/config/typus/README +58 -0
  167. data/lib/generators/templates/config/typus/application.yml +3 -0
  168. data/lib/generators/templates/config/typus/application_roles.yml +4 -0
  169. data/lib/generators/templates/config/typus/typus.yml +15 -0
  170. data/lib/generators/templates/config/typus/typus_roles.yml +4 -0
  171. data/lib/generators/templates/controller.erb +2 -0
  172. data/lib/generators/templates/migration.erb +22 -0
  173. data/lib/generators/templates/view.html.erb +13 -0
  174. data/lib/generators/typus/config_generator.rb +67 -0
  175. data/lib/generators/typus/controller_generator.rb +25 -0
  176. data/lib/generators/typus/initializers_generator.rb +20 -0
  177. data/lib/generators/typus/migration_generator.rb +94 -0
  178. data/lib/generators/typus/typus_generator.rb +52 -0
  179. data/lib/generators/typus/views_generator.rb +19 -0
  180. data/lib/support/active_record.rb +42 -0
  181. data/lib/support/fake_user.rb +47 -0
  182. data/lib/support/hash.rb +12 -0
  183. data/lib/support/object.rb +9 -0
  184. data/lib/support/string.rb +40 -0
  185. data/lib/tasks/typus.rake +11 -0
  186. data/lib/typus/authentication/base.rb +24 -0
  187. data/lib/typus/authentication/devise.rb +19 -0
  188. data/lib/typus/authentication/http_basic.rb +18 -0
  189. data/lib/typus/authentication/none.rb +15 -0
  190. data/lib/typus/authentication/none_with_role.rb +15 -0
  191. data/lib/typus/authentication/session.rb +123 -0
  192. data/lib/typus/configuration.rb +41 -0
  193. data/lib/typus/engine.rb +17 -0
  194. data/lib/typus/i18n.rb +48 -0
  195. data/lib/typus/orm/active_record/admin_user.rb +84 -0
  196. data/lib/typus/orm/active_record/class_methods.rb +104 -0
  197. data/lib/typus/orm/active_record/instance_methods.rb +76 -0
  198. data/lib/typus/orm/active_record/search.rb +44 -0
  199. data/lib/typus/orm/active_record.rb +10 -0
  200. data/lib/typus/orm/base/class_methods.rb +184 -0
  201. data/lib/typus/orm/base/search.rb +70 -0
  202. data/lib/typus/regex.rb +11 -0
  203. data/lib/typus/resources.rb +55 -0
  204. data/lib/typus/version.rb +10 -0
  205. data/lib/typus.rb +212 -0
  206. data/typus.gemspec +32 -0
  207. data/vendor/assets/adapt/css/1200.css +357 -0
  208. data/vendor/assets/adapt/css/1200.min.css +1 -0
  209. data/vendor/assets/adapt/css/1560.css +357 -0
  210. data/vendor/assets/adapt/css/1560.min.css +1 -0
  211. data/vendor/assets/adapt/css/720.css +357 -0
  212. data/vendor/assets/adapt/css/720.min.css +1 -0
  213. data/vendor/assets/adapt/css/960.css +357 -0
  214. data/vendor/assets/adapt/css/960.min.css +1 -0
  215. data/vendor/assets/adapt/css/fluid.css +345 -0
  216. data/vendor/assets/adapt/css/fluid.min.css +1 -0
  217. data/vendor/assets/adapt/css/master.css +120 -0
  218. data/vendor/assets/adapt/css/mobile.css +20 -0
  219. data/vendor/assets/adapt/css/mobile.min.css +1 -0
  220. data/vendor/assets/adapt/css/reset.css +202 -0
  221. data/vendor/assets/adapt/css/text.css +81 -0
  222. data/vendor/assets/adapt/images/h1.png +0 -0
  223. data/vendor/assets/adapt/js/adapt.js +135 -0
  224. data/vendor/assets/adapt/js/adapt.min.js +1 -0
  225. data/vendor/assets/bootstrap-2.3.2/css/bootstrap-responsive.css +1109 -0
  226. data/vendor/assets/bootstrap-2.3.2/css/bootstrap-responsive.min.css +9 -0
  227. data/vendor/assets/bootstrap-2.3.2/css/bootstrap.css +6167 -0
  228. data/vendor/assets/bootstrap-2.3.2/css/bootstrap.min.css +9 -0
  229. data/vendor/assets/bootstrap-2.3.2/img/glyphicons-halflings-white.png +0 -0
  230. data/vendor/assets/bootstrap-2.3.2/img/glyphicons-halflings.png +0 -0
  231. data/vendor/assets/bootstrap-2.3.2/js/bootstrap.js +2280 -0
  232. data/vendor/assets/bootstrap-2.3.2/js/bootstrap.min.js +6 -0
  233. data/vendor/assets/chosen/LICENSE.md +24 -0
  234. data/vendor/assets/chosen/chosen-sprite.png +0 -0
  235. data/vendor/assets/chosen/chosen.css +390 -0
  236. data/vendor/assets/chosen/chosen.jquery.js +902 -0
  237. data/vendor/assets/formalize/css/formalize.css +395 -0
  238. data/vendor/assets/formalize/images/button.png +0 -0
  239. data/vendor/assets/formalize/images/select_arrow.gif +0 -0
  240. data/vendor/assets/formalize/js/jquery.formalize.min.js +1 -0
  241. metadata +329 -0
@@ -0,0 +1,15 @@
1
+ module Typus
2
+ module Authentication
3
+ module None
4
+
5
+ protected
6
+
7
+ include Base
8
+
9
+ def authenticate
10
+ @admin_user = FakeUser.new
11
+ end
12
+
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ module Typus
2
+ module Authentication
3
+ module NoneWithRole
4
+
5
+ protected
6
+
7
+ include Base
8
+
9
+ def authenticate
10
+ @admin_user = MyFakeUser.new
11
+ end
12
+
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,123 @@
1
+ module Typus
2
+ module Authentication
3
+ module Session
4
+
5
+ protected
6
+
7
+ include Base
8
+
9
+ def authenticate
10
+ unless session[:typus_user_id] && admin_user && admin_user.active?
11
+ path = request.path != "/admin/dashboard" ? request.path : nil
12
+ deauthenticate(path)
13
+ end
14
+ end
15
+
16
+ def deauthenticate(return_to = nil)
17
+ session.delete(:typus_user_id)
18
+ redirect_to new_admin_session_path(:return_to => return_to)
19
+ end
20
+
21
+ #--
22
+ # Return the current user. If role does not longer exist on the system
23
+ # admin_user will be signed out from the system.
24
+ #++
25
+ def admin_user
26
+ @admin_user ||= Typus.user_class.find_by_id(session[:typus_user_id])
27
+ end
28
+
29
+ #--
30
+ # This method checks if the user can perform the requested action.
31
+ # It works on models, so its available on the `resources_controller`.
32
+ #++
33
+ def check_if_user_can_perform_action_on_resources
34
+ if @item && @item.is_a?(Typus.user_class)
35
+ check_if_user_can_perform_action_on_user
36
+ else
37
+ not_allowed if admin_user.cannot?(params[:action], @resource.model_name)
38
+ end
39
+ end
40
+
41
+ #--
42
+ # Action is available on: edit, update, toggle and destroy
43
+ #++
44
+ def check_if_user_can_perform_action_on_user
45
+ is_current_user = (admin_user == @item)
46
+ current_user_is_root = admin_user.is_root? && is_current_user
47
+
48
+ case params[:action]
49
+ when 'edit'
50
+ # Edit other items is not allowed unless current user is root
51
+ # and is not the current user.
52
+ not_allowed if admin_user.is_not_root? && !is_current_user
53
+ when 'toggle', 'destroy'
54
+ not_allowed if admin_user.is_not_root? || current_user_is_root
55
+ when 'update'
56
+ # Admin can update himself except setting the status to false!. Other
57
+ # users can update their profile as the attributes (role & status)
58
+ # are protected.
59
+ status_as_boolean = params[@object_name][:status] == "1" ? true : false
60
+
61
+ status_changed = !(@item.status == status_as_boolean)
62
+ role_changed = !(@item.role == params[@object_name][:role])
63
+
64
+ root_changed_his_status_or_role = current_user_is_root && (status_changed || role_changed)
65
+ not_root_tries_to_change_another_user = admin_user.is_not_root? && !is_current_user
66
+
67
+ not_allowed if root_changed_his_status_or_role || not_root_tries_to_change_another_user
68
+ end
69
+ end
70
+
71
+ #--
72
+ # This method checks if the user can perform the requested action.
73
+ # It works on a resource: git, memcached, syslog ...
74
+ #++
75
+ def check_if_user_can_perform_action_on_resource
76
+ resource = params[:controller].remove_prefix.camelize
77
+ not_allowed if admin_user.cannot?(params[:action], resource, { :special => true })
78
+ end
79
+
80
+ #--
81
+ # If item is owned by another user, we only can perform a show action on
82
+ # the item. Updated item is also blocked.
83
+ #++
84
+ def check_resource_ownership
85
+ if admin_user.is_not_root?
86
+
87
+ condition_typus_users = @item.respond_to?(Typus.relationship) && !@item.send(Typus.relationship).include?(admin_user)
88
+ condition_typus_user_id = @item.respond_to?(Typus.user_foreign_key) && !admin_user.owns?(@item)
89
+
90
+ not_allowed if (condition_typus_users || condition_typus_user_id)
91
+ end
92
+ end
93
+
94
+ #--
95
+ # Show only related items it @resource has a foreign_key (Typus.user_foreign_key)
96
+ # related to the logged user.
97
+ #++
98
+ def check_resources_ownership
99
+ if admin_user.is_not_root? && @resource.typus_user_id?
100
+ @resource = @resource.where(Typus.user_foreign_key => admin_user)
101
+ end
102
+ end
103
+
104
+ ##
105
+ # OPTIMIZE: This method should accept args.
106
+ #
107
+ def set_attributes_on_create
108
+ @item.send("#{Typus.user_foreign_key}=", admin_user.id) if @resource.typus_user_id?
109
+ end
110
+
111
+ ##
112
+ # OPTIMIZE: This method should accept args and not perform an update
113
+ # because we are updating the attributes twice!
114
+ #
115
+ def set_attributes_on_update
116
+ if @resource.typus_user_id? && admin_user.is_not_root?
117
+ @item.update_attributes(Typus.user_foreign_key => admin_user.id)
118
+ end
119
+ end
120
+
121
+ end
122
+ end
123
+ end
@@ -0,0 +1,41 @@
1
+ require 'erb'
2
+
3
+ module Typus
4
+ module Configuration
5
+
6
+ # Read configuration from <tt>config/typus/*.yml</tt>.
7
+ def self.models!
8
+ @@config = {}
9
+
10
+ Typus.model_configuration_files.each do |file|
11
+ if data = YAML::load(ERB.new(File.read(file)).result)
12
+ @@config.merge!(data)
13
+ end
14
+ end
15
+
16
+ @@config
17
+ end
18
+
19
+ mattr_accessor :config
20
+ @@config = {}
21
+
22
+ # Read roles from files <tt>config/typus/*_roles.yml</tt>.
23
+ def self.roles!
24
+ @@roles = Hash.new({})
25
+
26
+ Typus.role_configuration_files.each do |file|
27
+ if data = YAML::load(ERB.new(File.read(file)).result)
28
+ data.compact.each do |key, value|
29
+ @@roles[key] = @@roles[key].merge(value)
30
+ end
31
+ end
32
+ end
33
+
34
+ @@roles
35
+ end
36
+
37
+ mattr_accessor :roles
38
+ @@roles = Hash.new({})
39
+
40
+ end
41
+ end
@@ -0,0 +1,17 @@
1
+ module Admin
2
+ class Engine < Rails::Engine
3
+
4
+ initializer :assets do |config|
5
+
6
+ # Add all "frontend libraries" (bootstrap etc.) to the asset pipeline load path.
7
+ # This is necessary because typus uses a nondefault directory structure in vendor/assets.
8
+ extra_asset_paths = Dir.glob(File.join(root, 'vendor/assets/*/{img,images,css,js}'))
9
+ Rails.application.config.assets.paths += extra_asset_paths
10
+
11
+ # Add nondefault image directory to the precompile array, otherwise images in there won't
12
+ # become available for the asset pipeline.
13
+ Rails.application.config.assets.precompile << %r{^img/.+}
14
+
15
+ end
16
+ end
17
+ end
data/lib/typus/i18n.rb ADDED
@@ -0,0 +1,48 @@
1
+ # encoding: utf-8
2
+
3
+ module Typus
4
+ module I18n
5
+
6
+ class << self
7
+
8
+ # Instead of having to translate strings and defining a default value to
9
+ # avoid "missing translation" messages:
10
+ #
11
+ # I18n.t("Hello World!", :default => "Hello World!")
12
+ #
13
+ # We define a Typus translation method which will set up a default value
14
+ # for you: (Interpolation still works)
15
+ #
16
+ # Typus::I18n.t("Hello World!")
17
+ # Typus::I18n.t("Hello %{world}!", :world => @world)
18
+ #
19
+ def t(key, options = {})
20
+ options[:default] ||= key
21
+ ::I18n.t(key, options)
22
+ end
23
+
24
+ def default_locale
25
+ :en
26
+ end
27
+
28
+ def available_locales
29
+ {
30
+ "Brazilian Portuguese" => "pt-BR",
31
+ "Català" => "ca",
32
+ "German" => "de",
33
+ "Greek" => "el",
34
+ "Italiano" => "it",
35
+ "English" => "en",
36
+ "Español" => "es",
37
+ "Français" => "fr",
38
+ "Magyar" => "hu",
39
+ "Portuguese" => "pt-PT",
40
+ "Russian" => "ru",
41
+ "中文" => "zh-CN",
42
+ }
43
+ end
44
+
45
+ end
46
+
47
+ end
48
+ end
@@ -0,0 +1,84 @@
1
+ require 'active_support/concern'
2
+ require 'bcrypt'
3
+ require 'typus/orm/active_record/instance_methods'
4
+
5
+ module Typus
6
+ module Orm
7
+ module ActiveRecord
8
+ module AdminUser
9
+
10
+ extend ActiveSupport::Concern
11
+ include Typus::Orm::ActiveRecord::InstanceMethods
12
+
13
+ included do
14
+ attr_reader :password
15
+ attr_accessor :password_confirmation
16
+
17
+ # attr_protected :role, :status
18
+
19
+ validates :email, :presence => true, :uniqueness => true, :format => { :with => Typus::Regex::Email }
20
+ validates :password, :confirmation => true
21
+ validates :password_digest, :presence => true
22
+ validate :password_must_be_strong
23
+ validates :role, :presence => true
24
+
25
+ serialize :preferences
26
+
27
+ before_save :set_token
28
+ end
29
+
30
+ module ClassMethods
31
+
32
+ def authenticate(email, password)
33
+ user = find_by_email_and_status(email, true)
34
+ user && user.authenticated?(password) ? user : nil
35
+ end
36
+
37
+ def generate(*args)
38
+ options = args.extract_options!
39
+ options[:password] ||= Typus.password
40
+ options[:role] ||= Typus.master_role
41
+ options[:status] = true
42
+ user = new(options)
43
+ user.save ? user : false
44
+ end
45
+
46
+ def roles
47
+ Typus::Configuration.roles.keys.sort
48
+ end
49
+
50
+ def locales
51
+ Typus::I18n.available_locales
52
+ end
53
+
54
+ end
55
+
56
+ def locale
57
+ (preferences && preferences[:locale]) ? preferences[:locale] : ::I18n.default_locale
58
+ end
59
+
60
+ def locale=(locale)
61
+ self.preferences ||= {}
62
+ self.preferences[:locale] = locale
63
+ end
64
+
65
+ def authenticated?(unencrypted_password)
66
+ equal = BCrypt::Password.new(password_digest) == unencrypted_password
67
+ equal ? self : false
68
+ end
69
+
70
+ def password=(unencrypted_password)
71
+ @password = unencrypted_password
72
+ self.password_digest = BCrypt::Password.create(unencrypted_password)
73
+ end
74
+
75
+ def password_must_be_strong(count = 6)
76
+ if password.present? && password.size < count
77
+ errors.add(:password, :too_short, :count => count)
78
+ end
79
+ end
80
+
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,104 @@
1
+ module Typus
2
+ module Orm
3
+ module ActiveRecord
4
+ module ClassMethods
5
+
6
+ include Typus::Orm::Base::ClassMethods
7
+
8
+ # Model fields as an <tt>ActiveSupport::OrderedHash</tt>.
9
+ def model_fields
10
+ ActiveSupport::OrderedHash.new.tap do |hash|
11
+ columns.map { |u| hash[u.name.to_sym] = u.type.to_sym }
12
+ end
13
+ end
14
+
15
+ # Model relationships as an <tt>ActiveSupport::OrderedHash</tt>.
16
+ def model_relationships
17
+ ActiveSupport::OrderedHash.new.tap do |hash|
18
+ reflect_on_all_associations.map { |i| hash[i.name] = i.macro }
19
+ end
20
+ end
21
+
22
+ def typus_supported_attributes
23
+ [:virtual, :custom, :association, :selector, :dragonfly, :paperclip]
24
+ end
25
+
26
+ def typus_fields_for(filter)
27
+ ActiveSupport::OrderedHash.new.tap do |fields_with_type|
28
+ get_typus_fields_for(filter).each do |field|
29
+ typus_supported_attributes.each do |attribute|
30
+ if (value = send("#{attribute}_attribute?", field))
31
+ fields_with_type[field.to_s] = value
32
+ end
33
+ end
34
+ fields_with_type[field.to_s] ||= model_fields[field]
35
+ end
36
+ end
37
+ end
38
+
39
+ def get_typus_fields_for(filter)
40
+ data = read_model_config['fields']
41
+ fields = case filter.to_sym
42
+ when :index then data['index'] || data['list']
43
+ when :new, :create then data['new'] || data['form']
44
+ when :edit, :update, :toggle then data['edit'] || data['form']
45
+ else
46
+ data[filter.to_s]
47
+ end
48
+
49
+ fields ||= data['default'] || typus_default_fields_for(filter)
50
+ fields = fields.extract_settings if fields.is_a?(String)
51
+ fields.map(&:to_sym)
52
+ end
53
+
54
+ def typus_default_fields_for(filter)
55
+ filter.to_sym.eql?(:index) ? ['id'] : model_fields.keys
56
+ end
57
+
58
+ def virtual_attribute?(field)
59
+ :virtual if virtual_fields.include?(field.to_s)
60
+ end
61
+
62
+ def dragonfly_attribute?(field)
63
+ if respond_to?(:dragonfly_attachment_classes) && dragonfly_attachment_classes.map(&:attribute).include?(field)
64
+ :dragonfly
65
+ end
66
+ end
67
+
68
+ def paperclip_attribute?(field)
69
+ if respond_to?(:attachment_definitions) && attachment_definitions.try(:has_key?, field)
70
+ :paperclip
71
+ end
72
+ end
73
+
74
+ def selector_attribute?(field)
75
+ :selector if typus_field_options_for(:selectors).include?(field)
76
+ end
77
+
78
+ def association_attribute?(field)
79
+ reflect_on_association(field).macro if reflect_on_association(field)
80
+ end
81
+
82
+ def typus_filters
83
+ filters = ActiveSupport::OrderedHash.new.tap do |fields_with_type|
84
+ get_typus_filters.each do |field|
85
+ fields_with_type[field.to_s] = association_attribute?(field) || model_fields[field.to_sym]
86
+ end
87
+ end
88
+ # Remove unsupported filters!
89
+ filters.reject { |k, v| [:time].include?(v) }
90
+ end
91
+
92
+ def get_typus_filters
93
+ data = read_model_config['filters'] || ""
94
+ data.extract_settings.map(&:to_sym)
95
+ end
96
+
97
+ def typus_user_id?
98
+ columns.map(&:name).include?(Typus.user_foreign_key)
99
+ end
100
+
101
+ end
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,76 @@
1
+ module Typus
2
+ module Orm
3
+ module ActiveRecord
4
+ module InstanceMethods
5
+
6
+ def to_label
7
+ email
8
+ end
9
+
10
+ def resources
11
+ Typus::Configuration.roles[role.to_s].compact
12
+ end
13
+
14
+ def models
15
+ Typus.models.delete_if { |m| cannot?('read', m) }
16
+ end
17
+
18
+ def applications
19
+ Typus.applications.delete_if { |a| application(a).empty? }
20
+ end
21
+
22
+ def application(name)
23
+ Typus.application(name).delete_if { |r| !resources.keys.include?(r) }
24
+ end
25
+
26
+ def can?(action, resource, options = {})
27
+ resource = resource.to_s
28
+
29
+ return false if !resources.include?(resource)
30
+ return true if resources[resource].include?("all")
31
+
32
+ action = options[:special] ? action : action.acl_action_mapper
33
+
34
+ resources[resource].extract_settings.include?(action)
35
+ end
36
+
37
+ def cannot?(*args)
38
+ !can?(*args)
39
+ end
40
+
41
+ def is_root?
42
+ role == Typus.master_role
43
+ end
44
+
45
+ def is_not_root?
46
+ !is_root?
47
+ end
48
+
49
+ def active?
50
+ status && Typus::Configuration.roles.has_key?(role)
51
+ end
52
+
53
+ def locale
54
+ (preferences && preferences[:locale]) ? preferences[:locale] : ::I18n.default_locale
55
+ end
56
+
57
+ def locale=(locale)
58
+ self.preferences ||= {}
59
+ self.preferences[:locale] = locale
60
+ end
61
+
62
+ def owns?(resource)
63
+ id == resource.send(Typus.user_foreign_key)
64
+ end
65
+
66
+ def set_token
67
+ token = "#{SecureRandom.hex(6)}-#{SecureRandom.hex(6)}"
68
+ token.encode!('UTF-8') if token.respond_to?(:encode)
69
+ self.token = token
70
+ end
71
+ protected :set_token
72
+
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,44 @@
1
+ module Typus
2
+ module Orm
3
+ module ActiveRecord
4
+ module Search
5
+
6
+ include Typus::Orm::Base::Search
7
+
8
+ def build_search_conditions(key, value)
9
+ Array.new.tap do |search|
10
+ query = ::ActiveRecord::Base.connection.quote_string(value.downcase)
11
+
12
+ search_fields = typus_search_fields
13
+ search_fields = search_fields.empty? ? { "name" => "@" } : search_fields
14
+
15
+ search_fields.each do |key, value|
16
+ _query = case value
17
+ when "=" then query
18
+ when "^" then "#{query}%"
19
+ when "@" then "%#{query}%"
20
+ end
21
+
22
+ column_name = (key.match('\.') ? key : "#{table_name}.#{key}")
23
+ table_key = (adapter == 'postgresql') ? "LOWER(TEXT(#{column_name}))" : "#{column_name}"
24
+
25
+ search << "#{table_key} LIKE '#{_query}'"
26
+ end
27
+ end.join(" OR ")
28
+ end
29
+
30
+ # TODO: Use Rails scopes to build the search: where(key => interval)
31
+ def build_filter_interval(interval, key)
32
+ ["#{table_name}.#{key} BETWEEN ? AND ?", interval.first.to_s(:db), interval.last.to_s(:db)]
33
+ end
34
+
35
+ def build_my_joins(params)
36
+ query_params = params.dup
37
+ query_params.reject! { |k, v| !model_relationships.keys.include?(k.to_sym) }
38
+ query_params.compact.map { |k, v| k.to_sym }
39
+ end
40
+
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,10 @@
1
+ if defined?(ActiveRecord)
2
+ require 'typus/orm/active_record/class_methods'
3
+ ActiveRecord::Base.extend Typus::Orm::ActiveRecord::ClassMethods
4
+
5
+ require 'typus/orm/active_record/search'
6
+ ActiveRecord::Base.extend Typus::Orm::ActiveRecord::Search
7
+
8
+ require 'typus/orm/active_record/admin_user'
9
+ ActiveRecord::Base.extend Typus::Orm::ActiveRecord::AdminUser::ClassMethods
10
+ end