typus-rails4 4.0.0.beta6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (198) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +488 -0
  3. data/Gemfile +57 -0
  4. data/Gemfile.lock +253 -0
  5. data/MIT-LICENSE +20 -0
  6. data/README.md +101 -0
  7. data/Rakefile +51 -0
  8. data/app/assets/javascripts/typus/application.js +11 -0
  9. data/app/assets/javascripts/typus/custom.js +2 -0
  10. data/app/assets/javascripts/typus/jquery-2.1.1.min.js +4 -0
  11. data/app/assets/javascripts/typus/jquery.application.js +30 -0
  12. data/app/assets/stylesheets/typus/application.css +6 -0
  13. data/app/assets/stylesheets/typus/custom.scss +4 -0
  14. data/app/assets/stylesheets/typus/overrides.scss +10 -0
  15. data/app/assets/stylesheets/typus/signin.scss +45 -0
  16. data/app/controllers/admin/account_controller.rb +47 -0
  17. data/app/controllers/admin/base_controller.rb +40 -0
  18. data/app/controllers/admin/dashboard_controller.rb +6 -0
  19. data/app/controllers/admin/resource_controller.rb +8 -0
  20. data/app/controllers/admin/resources_controller.rb +268 -0
  21. data/app/controllers/admin/session_controller.rb +43 -0
  22. data/app/controllers/concerns/admin/actions.rb +88 -0
  23. data/app/controllers/concerns/admin/acts_as_list.rb +35 -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 +49 -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 +27 -0
  34. data/app/controllers/concerns/admin/trash.rb +65 -0
  35. data/app/helpers/admin/base_helper.rb +78 -0
  36. data/app/helpers/admin/resources/data_types/belongs_to_helper.rb +88 -0
  37. data/app/helpers/admin/resources/data_types/boolean_helper.rb +33 -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 +49 -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 +59 -0
  43. data/app/helpers/admin/resources/data_types/has_many_helper.rb +76 -0
  44. data/app/helpers/admin/resources/data_types/has_one_helper.rb +44 -0
  45. data/app/helpers/admin/resources/data_types/integer_helper.rb +13 -0
  46. data/app/helpers/admin/resources/data_types/paperclip_helper.rb +49 -0
  47. data/app/helpers/admin/resources/data_types/selector_helper.rb +11 -0
  48. data/app/helpers/admin/resources/data_types/string_helper.rb +38 -0
  49. data/app/helpers/admin/resources/data_types/text_helper.rb +6 -0
  50. data/app/helpers/admin/resources/data_types/transversal_helper.rb +8 -0
  51. data/app/helpers/admin/resources/data_types/tree_helper.rb +28 -0
  52. data/app/helpers/admin/resources/display_helper.rb +27 -0
  53. data/app/helpers/admin/resources/filters_helper.rb +18 -0
  54. data/app/helpers/admin/resources/form_helper.rb +85 -0
  55. data/app/helpers/admin/resources/list_helper.rb +31 -0
  56. data/app/helpers/admin/resources/pagination_helper.rb +28 -0
  57. data/app/helpers/admin/resources/relationships_helper.rb +33 -0
  58. data/app/helpers/admin/resources/sidebar_helper.rb +34 -0
  59. data/app/helpers/admin/resources/table_helper.rb +69 -0
  60. data/app/helpers/admin/resources_helper.rb +25 -0
  61. data/app/mailers/admin/mailer.rb +19 -0
  62. data/app/views/admin/account/_form.html.erb +17 -0
  63. data/app/views/admin/account/forgot_password.html.erb +16 -0
  64. data/app/views/admin/account/new.html.erb +10 -0
  65. data/app/views/admin/dashboard/_sidebar.html.erb +14 -0
  66. data/app/views/admin/dashboard/index.html.erb +8 -0
  67. data/app/views/admin/dashboard/styles.html.erb +153 -0
  68. data/app/views/admin/dashboard/widgets/_applications.html.erb +44 -0
  69. data/app/views/admin/dashboard/widgets/_default.html.erb +43 -0
  70. data/app/views/admin/dashboard/widgets/_models.html.erb +23 -0
  71. data/app/views/admin/dashboard/widgets/_models_extended.html.erb +25 -0
  72. data/app/views/admin/dashboard/widgets/_resources.html.erb +23 -0
  73. data/app/views/admin/dashboard/widgets/_users.html.erb +23 -0
  74. data/app/views/admin/mailer/reset_password_instructions.html.erb +7 -0
  75. data/app/views/admin/mailer/reset_password_instructions.text.erb +7 -0
  76. data/app/views/admin/resource/_sidebar.html.erb +14 -0
  77. data/app/views/admin/resources/_edit.html.erb +0 -0
  78. data/app/views/admin/resources/_form.html.erb +28 -0
  79. data/app/views/admin/resources/_form_javascripts.html.erb +0 -0
  80. data/app/views/admin/resources/_form_modal.html.erb +13 -0
  81. data/app/views/admin/resources/_index.html.erb +0 -0
  82. data/app/views/admin/resources/_new.html.erb +0 -0
  83. data/app/views/admin/resources/_pagination.html.erb +15 -0
  84. data/app/views/admin/resources/_show.html.erb +0 -0
  85. data/app/views/admin/resources/_sidebar.html.erb +1 -0
  86. data/app/views/admin/resources/destroy.html.erb +16 -0
  87. data/app/views/admin/resources/edit.html.erb +22 -0
  88. data/app/views/admin/resources/edit/_actions.html.erb +7 -0
  89. data/app/views/admin/resources/edit/_recently_edited.html.erb +8 -0
  90. data/app/views/admin/resources/index.html.erb +29 -0
  91. data/app/views/admin/resources/new.html.erb +20 -0
  92. data/app/views/admin/resources/show.html.erb +26 -0
  93. data/app/views/admin/session/new.html.erb +18 -0
  94. data/app/views/admin/shared/_head.html.erb +24 -0
  95. data/app/views/admin/shared/_modals.html.erb +3 -0
  96. data/app/views/admin/shared/_sidebar_placeholder.html.erb +0 -0
  97. data/app/views/admin/templates/README.md +5 -0
  98. data/app/views/admin/templates/_belongs_to.html.erb +8 -0
  99. data/app/views/admin/templates/_boolean.html.erb +7 -0
  100. data/app/views/admin/templates/_date.html.erb +6 -0
  101. data/app/views/admin/templates/_datetime.html.erb +8 -0
  102. data/app/views/admin/templates/_dragonfly.html.erb +6 -0
  103. data/app/views/admin/templates/_dragonfly_form_preview.html.erb +21 -0
  104. data/app/views/admin/templates/_dragonfly_preview.html.erb +12 -0
  105. data/app/views/admin/templates/_has_and_belongs_to_many.html.erb +21 -0
  106. data/app/views/admin/templates/_has_many.html.erb +18 -0
  107. data/app/views/admin/templates/_has_one.html.erb +14 -0
  108. data/app/views/admin/templates/_json.html.erb +11 -0
  109. data/app/views/admin/templates/_paperclip.html.erb +5 -0
  110. data/app/views/admin/templates/_paperclip_form_preview.html.erb +21 -0
  111. data/app/views/admin/templates/_paperclip_preview.html.erb +12 -0
  112. data/app/views/admin/templates/_password.html.erb +6 -0
  113. data/app/views/admin/templates/_position.html.erb +5 -0
  114. data/app/views/admin/templates/_profile_sidebar.html.erb +4 -0
  115. data/app/views/admin/templates/_selector.html.erb +4 -0
  116. data/app/views/admin/templates/_string.html.erb +4 -0
  117. data/app/views/admin/templates/_string_with_preview.html.erb +11 -0
  118. data/app/views/admin/templates/_text.html.erb +11 -0
  119. data/app/views/admin/templates/_text_with_ckeditor.html.erb +24 -0
  120. data/app/views/admin/templates/_text_with_ckeditor_and_assets.html.erb +38 -0
  121. data/app/views/admin/templates/_time.html.erb +9 -0
  122. data/app/views/admin/templates/_tree.html.erb +6 -0
  123. data/app/views/admin/templates/modals/_belongs_to.html.erb +60 -0
  124. data/app/views/admin/templates/modals/_dragonfly.html.erb +13 -0
  125. data/app/views/admin/templates/modals/_has_and_belongs_to_many.html.erb +25 -0
  126. data/app/views/admin/templates/modals/_has_many.html.erb +59 -0
  127. data/app/views/admin/templates/modals/_paperclip.html.erb +13 -0
  128. data/app/views/helpers/admin/base/_apps.html.erb +86 -0
  129. data/app/views/helpers/admin/base/_breadcrumbs.html.erb +6 -0
  130. data/app/views/helpers/admin/base/_flash_message.html.erb +3 -0
  131. data/app/views/helpers/admin/base/_login_info.html.erb +0 -0
  132. data/app/views/helpers/admin/dashboard/_resources.html.erb +19 -0
  133. data/app/views/helpers/admin/resources/_actions.html.erb +18 -0
  134. data/app/views/helpers/admin/resources/_errors.html.erb +12 -0
  135. data/app/views/helpers/admin/resources/_filters.html.erb +16 -0
  136. data/app/views/helpers/admin/resources/_search.html.erb +17 -0
  137. data/app/views/helpers/admin/resources/_sidebar.html.erb +4 -0
  138. data/app/views/helpers/admin/resources/_table.html.erb +45 -0
  139. data/app/views/helpers/admin/resources/sidebar/_item.html.erb +26 -0
  140. data/app/views/helpers/admin/resources/sidebar/_items.html.erb +14 -0
  141. data/app/views/helpers/admin/resources/sidebar/_items_index.html.erb +57 -0
  142. data/app/views/layouts/admin/base.html.erb +30 -0
  143. data/app/views/layouts/admin/headless.html.erb +1 -0
  144. data/app/views/layouts/admin/session.html.erb +26 -0
  145. data/bin/run-tests +7 -0
  146. data/bin/setup +9 -0
  147. data/config/locales/typus.en.yml +125 -0
  148. data/config/routes.rb +46 -0
  149. data/lib/generators/templates/config/initializers/typus.rb +34 -0
  150. data/lib/generators/templates/config/initializers/typus_authentication.rb +18 -0
  151. data/lib/generators/templates/config/initializers/typus_resources.rb +24 -0
  152. data/lib/generators/templates/config/typus/README +58 -0
  153. data/lib/generators/templates/config/typus/application.yml +3 -0
  154. data/lib/generators/templates/config/typus/application_roles.yml +4 -0
  155. data/lib/generators/templates/config/typus/typus.yml +15 -0
  156. data/lib/generators/templates/config/typus/typus_roles.yml +4 -0
  157. data/lib/generators/templates/controller.erb +2 -0
  158. data/lib/generators/templates/migration.erb +22 -0
  159. data/lib/generators/templates/view.html.erb +13 -0
  160. data/lib/generators/typus/config_generator.rb +67 -0
  161. data/lib/generators/typus/controller_generator.rb +26 -0
  162. data/lib/generators/typus/initializers_generator.rb +20 -0
  163. data/lib/generators/typus/migration_generator.rb +94 -0
  164. data/lib/generators/typus/model_generator.rb +93 -0
  165. data/lib/generators/typus/typus_generator.rb +54 -0
  166. data/lib/generators/typus/views_generator.rb +19 -0
  167. data/lib/support/active_record.rb +42 -0
  168. data/lib/support/fake_user.rb +47 -0
  169. data/lib/support/hash.rb +8 -0
  170. data/lib/support/object.rb +9 -0
  171. data/lib/support/string.rb +40 -0
  172. data/lib/tasks/typus.rake +11 -0
  173. data/lib/typus.rb +215 -0
  174. data/lib/typus/authentication/base.rb +39 -0
  175. data/lib/typus/authentication/devise.rb +19 -0
  176. data/lib/typus/authentication/http_basic.rb +18 -0
  177. data/lib/typus/authentication/none.rb +15 -0
  178. data/lib/typus/authentication/none_with_role.rb +15 -0
  179. data/lib/typus/authentication/session.rb +123 -0
  180. data/lib/typus/configuration.rb +43 -0
  181. data/lib/typus/engine.rb +4 -0
  182. data/lib/typus/orm/active_record.rb +10 -0
  183. data/lib/typus/orm/active_record/admin_user.rb +65 -0
  184. data/lib/typus/orm/active_record/class_methods.rb +104 -0
  185. data/lib/typus/orm/active_record/instance_methods.rb +76 -0
  186. data/lib/typus/orm/active_record/search.rb +43 -0
  187. data/lib/typus/orm/base/class_methods.rb +188 -0
  188. data/lib/typus/orm/base/search.rb +70 -0
  189. data/lib/typus/regex.rb +11 -0
  190. data/lib/typus/resources.rb +55 -0
  191. data/lib/typus/version.rb +10 -0
  192. data/typus.gemspec +27 -0
  193. data/vendor/assets/chosen/LICENSE.md +24 -0
  194. data/vendor/assets/chosen/chosen-sprite.png +0 -0
  195. data/vendor/assets/chosen/chosen-sprite@2x.png +0 -0
  196. data/vendor/assets/chosen/chosen.jquery.js +1239 -0
  197. data/vendor/assets/chosen/chosen.scss +437 -0
  198. metadata +271 -0
@@ -0,0 +1,39 @@
1
+ module Typus
2
+ module Authentication
3
+ module Base
4
+
5
+ def admin_user
6
+ @admin_user
7
+ end
8
+
9
+ def current_role
10
+ admin_user.role.to_sym
11
+ end
12
+
13
+ def authenticate
14
+ end
15
+
16
+ def check_if_user_can_perform_action_on_user
17
+ end
18
+
19
+ def check_if_user_can_perform_action_on_resources
20
+ end
21
+
22
+ def check_if_user_can_perform_action_on_resource
23
+ end
24
+
25
+ def check_resource_ownership
26
+ end
27
+
28
+ def check_resources_ownership
29
+ end
30
+
31
+ def set_attributes_on_create
32
+ end
33
+
34
+ def set_attributes_on_update
35
+ end
36
+
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,19 @@
1
+ module Typus
2
+ module Authentication
3
+ module Devise
4
+
5
+ protected
6
+
7
+ include Base
8
+
9
+ def admin_user
10
+ send("current_#{Typus.user_class_name.underscore}")
11
+ end
12
+
13
+ def authenticate
14
+ send("authenticate_#{Typus.user_class_name.underscore}!")
15
+ end
16
+
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,18 @@
1
+ module Typus
2
+ module Authentication
3
+ module HttpBasic
4
+
5
+ protected
6
+
7
+ include Base
8
+
9
+ def authenticate
10
+ @admin_user = FakeUser.new
11
+ authenticate_or_request_with_http_basic(Typus.admin_title) do |username, password|
12
+ username == Typus.username && password == Typus.password
13
+ end
14
+ end
15
+
16
+ end
17
+ end
18
+ end
@@ -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,43 @@
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
+ # For some weird reason we can't use {}.compact
29
+ data = data.delete_if { |_, v| v.blank? }
30
+ data.each do |key, value|
31
+ @@roles[key] = @@roles[key].merge(value)
32
+ end
33
+ end
34
+ end
35
+
36
+ @@roles
37
+ end
38
+
39
+ mattr_accessor :roles
40
+ @@roles = Hash.new({})
41
+
42
+ end
43
+ end
@@ -0,0 +1,4 @@
1
+ module Admin
2
+ class Engine < Rails::Engine
3
+ end
4
+ 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
@@ -0,0 +1,65 @@
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
+ has_secure_password
15
+
16
+ # attr_protected :role, :status
17
+
18
+ validates :email, presence: true, uniqueness: true, format: { with: Typus::Regex::Email }
19
+ validates :password, length: { minimum: 8 }, allow_nil: true
20
+ validates :role, presence: true
21
+
22
+ serialize :preferences
23
+
24
+ before_save :set_token
25
+ end
26
+
27
+ module ClassMethods
28
+
29
+ def authenticate(email, password)
30
+ user = find_by_email_and_status(email, true)
31
+ user && user.authenticate(password) ? user : nil
32
+ end
33
+
34
+ def generate(*args)
35
+ options = args.extract_options!
36
+ options[:password] ||= Typus.password
37
+ options[:role] ||= Typus.master_role
38
+ options[:status] = true
39
+ user = new(options)
40
+ user.save ? user : false
41
+ end
42
+
43
+ def roles
44
+ Typus::Configuration.roles.keys.sort
45
+ end
46
+
47
+ def locales
48
+ Typus.available_locales
49
+ end
50
+
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
+ end
63
+ end
64
+ end
65
+ 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