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,93 @@
1
+ require "generators/typus/controller_generator"
2
+
3
+ module Typus
4
+ module Generators
5
+ class ModelGenerator < Rails::Generators::Base
6
+ source_root File.expand_path("../../templates", __FILE__)
7
+
8
+ namespace "typus:model"
9
+
10
+ desc <<-DESC
11
+ Description:
12
+ This generator creates controller and configuration files.
13
+
14
+ Examples:
15
+ `rails generate typus:model user`
16
+ This command creates 2 files like below.
17
+
18
+ * config/typus/user.yml
19
+ * app/controllers/admin/users_controller.rb
20
+
21
+ DESC
22
+
23
+ def generate_models
24
+ Typus.reload!
25
+
26
+ models = parse_model_args
27
+ models.each do |model|
28
+ generate_yaml(model)
29
+ generate_controller(model)
30
+ end
31
+ end
32
+
33
+ protected
34
+ def configuration
35
+ @configuration
36
+ end
37
+
38
+ def parse_args
39
+ ARGV.map { |table| table.singularize.camelize.constantize }
40
+ end
41
+
42
+ def typus_models
43
+ Typus.application_models.reject { |m| Typus.models.include?(m) }.map(&:constantize)
44
+ end
45
+
46
+ def parse_model_args
47
+ ARGV.present? ? parse_args : typus_models
48
+ end
49
+
50
+ def create_yaml_data(model)
51
+ option = {}
52
+
53
+ relationships = [ :has_many, :has_one ].map do |relationship|
54
+ model.reflect_on_all_associations(relationship).map { |i| i.name.to_s }
55
+ end.flatten.join(", ")
56
+ option[:base] = <<-RAW
57
+ #{model}:
58
+ fields:
59
+ default: #{fields_for(model, 'to_label')}
60
+ form: #{fields_for(model)}
61
+ relationships: #{relationships}
62
+ application: Application
63
+ RAW
64
+ option[:roles] = "#{model}: create, read, update, delete"
65
+ option
66
+ end
67
+
68
+ def generate_yaml(model)
69
+ key = model.name.underscore
70
+
71
+ @configuration = create_yaml_data(model)
72
+
73
+ template(
74
+ "config/typus/application.yml",
75
+ "config/typus/#{key}.yml")
76
+ template(
77
+ "config/typus/application_roles.yml",
78
+ "config/typus/#{key}_roles.yml")
79
+ end
80
+
81
+ def fields_for(model, *defaults)
82
+ rejections = %w( ^id$ _type$ type created_at created_on updated_at updated_on deleted_at ).join("|")
83
+ fields = model.table_exists? ? model.columns.map(&:name) : defaults
84
+ fields.reject { |f| f.match(rejections) }.join(", ")
85
+ end
86
+
87
+ def generate_controller(model)
88
+ Typus::Generators::ControllerGenerator.new([model.name.pluralize.camelize]).invoke_all
89
+ end
90
+ end
91
+
92
+ end
93
+ end
@@ -0,0 +1,54 @@
1
+ require "typus"
2
+ require "rails/generators/migration"
3
+
4
+ require "generators/typus/config_generator"
5
+ require "generators/typus/controller_generator"
6
+ require "generators/typus/initializers_generator"
7
+
8
+ module Typus
9
+ module Generators
10
+ class TypusGenerator < Rails::Generators::Base
11
+
12
+ include Rails::Generators::Migration
13
+
14
+ source_root File.expand_path("../../templates", __FILE__)
15
+
16
+ namespace "typus"
17
+
18
+ desc <<-DESC
19
+ Description:
20
+ This generator creates required files to enable an admin panel which allows
21
+ trusted users to edit structured content.
22
+
23
+ To enable session authentication run `rails generate typus:migration`.
24
+
25
+ DESC
26
+
27
+ def generate_initializers
28
+ application "require 'typus'"
29
+ Typus::Generators::InitializersGenerator.new.invoke_all
30
+ end
31
+
32
+ def generate_controllers
33
+ Typus.application_models.each do |model|
34
+ Typus::Generators::ControllerGenerator.new([model.pluralize]).invoke_all
35
+ end
36
+ end
37
+
38
+ def generate_config
39
+ Typus::Generators::ConfigGenerator.new.invoke_all
40
+ end
41
+
42
+ protected
43
+
44
+ def resource
45
+ @resource
46
+ end
47
+
48
+ def sidebar
49
+ @sidebar
50
+ end
51
+
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,19 @@
1
+ module Typus
2
+ module Generators
3
+ class ViewsGenerator < Rails::Generators::Base
4
+
5
+ source_root File.expand_path("../../../../app", __FILE__)
6
+
7
+ desc <<-MSG
8
+ Description:
9
+ Copies Typus views into your application.
10
+
11
+ MSG
12
+
13
+ def copy_views
14
+ directory "views", "app/views"
15
+ end
16
+
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,42 @@
1
+ class ActiveRecord::Base
2
+
3
+ #--
4
+ # On a model:
5
+ #
6
+ # class Post < ActiveRecord::Base
7
+ # def self.statuses
8
+ # { t("Published") => "published",
9
+ # t("Pending") => "pending",
10
+ # t("Draft") => "draft" }
11
+ # end
12
+ # end
13
+ #
14
+ # >> Post.first.status
15
+ # => "published"
16
+ # >> Post.first.mapping(:status)
17
+ # => "Published"
18
+ # >> I18n.locale = :es
19
+ # => :es
20
+ # >> Post.first.mapping(:status)
21
+ # => "Publicado"
22
+ #++
23
+ def mapping(attribute)
24
+ values = self.class.send(attribute.to_s.pluralize)
25
+ array = values.first.is_a?(Array) ? values : values.map { |i| [i, i] }
26
+ value = array.to_a.rassoc(send(attribute))
27
+ value ? value.first : send(attribute)
28
+ end
29
+
30
+ def to_label
31
+ if respond_to?(:name) && name.present?
32
+ name
33
+ else
34
+ [self.class, id].join('#')
35
+ end
36
+ end
37
+
38
+ def identifier
39
+ "##{id}"
40
+ end
41
+
42
+ end
@@ -0,0 +1,47 @@
1
+ class FakeUser
2
+
3
+ def id
4
+ 0
5
+ end
6
+
7
+ def can?(*args)
8
+ true
9
+ end
10
+
11
+ def cannot?(*args)
12
+ !can?(*args)
13
+ end
14
+
15
+ def is_root?
16
+ true
17
+ end
18
+
19
+ def is_not_root?
20
+ !is_root?
21
+ end
22
+
23
+ def applications
24
+ Typus.applications
25
+ end
26
+
27
+ def application(name)
28
+ Typus.application(name)
29
+ end
30
+
31
+ def status
32
+ true
33
+ end
34
+
35
+ def owns?(resource)
36
+ true
37
+ end
38
+
39
+ def locale
40
+ ::I18n.locale
41
+ end
42
+
43
+ def role
44
+ Typus.master_role
45
+ end
46
+
47
+ end
@@ -0,0 +1,8 @@
1
+ class Hash
2
+
3
+ def cleanup
4
+ whitelist = %w(controller action id _input _popup resource attribute)
5
+ delete_if { |k, _| !whitelist.include?(k) }
6
+ end
7
+
8
+ end
@@ -0,0 +1,9 @@
1
+ class Object
2
+
3
+ # OPTIMIZE: Probably there's a better way to verify if a model responds
4
+ # to a STI pattern.
5
+ def self.is_sti?
6
+ (name != base_class.name) && base_class.descends_from_active_record?
7
+ end
8
+
9
+ end
@@ -0,0 +1,40 @@
1
+ class String
2
+
3
+ def extract_settings
4
+ split(',').map(&:strip)
5
+ end
6
+
7
+ def remove_prefix
8
+ split('/')[1..-1].join('/')
9
+ end
10
+
11
+ def extract_class
12
+ remove_prefix.classify.constantize
13
+ end
14
+
15
+ def extract_singular_class
16
+ remove_prefix.camelize.constantize
17
+ end
18
+
19
+ # CRUD: create, read, update, delete
20
+ # Read more at http://en.wikipedia.org/wiki/Create,_read,_update_and_delete
21
+ def acl_action_mapper
22
+ case self
23
+ when 'new', 'create'
24
+ 'create'
25
+ when 'index', 'show'
26
+ 'read'
27
+ when 'edit', 'update', 'position', 'toggle', 'relate', 'unrelate'
28
+ 'update'
29
+ when 'destroy', 'trash'
30
+ 'delete'
31
+ else
32
+ self
33
+ end
34
+ end
35
+
36
+ def to_resource
37
+ self.underscore.pluralize
38
+ end
39
+
40
+ end
@@ -0,0 +1,11 @@
1
+ namespace :typus do
2
+
3
+ desc 'List current roles.'
4
+ task :roles => :environment do
5
+ Typus::Configuration.roles.each do |role|
6
+ puts "'#{role.first}' role has access to:"
7
+ role.last.each { |k, v| puts "- #{k}: #{v}" }
8
+ end
9
+ end
10
+
11
+ end
@@ -0,0 +1,215 @@
1
+ require "support/active_record" if defined?(ActiveRecord)
2
+ require "support/hash"
3
+ require "support/object"
4
+ require "support/string"
5
+
6
+ require "typus/engine"
7
+ require "typus/regex"
8
+ require "typus/version"
9
+
10
+ require "typus/orm/base/class_methods"
11
+ require "typus/orm/base/search"
12
+ require "typus/orm/active_record"
13
+
14
+ autoload :FakeUser, "support/fake_user"
15
+
16
+ module Typus
17
+
18
+ autoload :Configuration, "typus/configuration"
19
+ autoload :I18n, "typus/i18n"
20
+ autoload :Resources, "typus/resources"
21
+
22
+ module Authentication
23
+ autoload :Base, "typus/authentication/base"
24
+ autoload :None, "typus/authentication/none"
25
+ autoload :NoneWithRole, "typus/authentication/none_with_role"
26
+ autoload :HttpBasic, "typus/authentication/http_basic"
27
+ autoload :Session, "typus/authentication/session"
28
+
29
+ autoload :Devise, "typus/authentication/devise"
30
+ end
31
+
32
+ mattr_accessor :default_locale
33
+ @@default_locale = :en
34
+
35
+ mattr_accessor :admin_title
36
+ @@admin_title = "Typus"
37
+
38
+ mattr_accessor :admin_title_link
39
+ @@admin_title_link = nil
40
+
41
+ # Set authentication (none, http_basic, session)
42
+ mattr_accessor :authentication
43
+ @@authentication = :none
44
+
45
+ mattr_accessor :config_folder
46
+ @@config_folder = "config/typus"
47
+
48
+ mattr_accessor :username
49
+ @@username = "admin"
50
+
51
+ mattr_accessor :subdomain
52
+ @@subdomain = nil
53
+
54
+ # Define a default password for http authentication.
55
+ mattr_accessor :password
56
+ @@password = "columbia"
57
+
58
+ # Configure the e-mail address which will be shown in Admin::Mailer.
59
+ # If not set `forgot_password` feature is disabled.
60
+ mattr_accessor :mailer_sender
61
+ @@mailer_sender = nil
62
+
63
+ ##
64
+ # Define `paperclip` attachment styles.
65
+ #
66
+
67
+ mattr_accessor :file_preview
68
+ @@file_preview = :medium
69
+
70
+ mattr_accessor :file_thumbnail
71
+ @@file_thumbnail = :thumb
72
+
73
+ ##
74
+ # Define `dragonfly` attachment styles.
75
+ #
76
+
77
+ mattr_accessor :image_preview_size
78
+ @@image_preview_size = '530x'
79
+
80
+ mattr_accessor :image_thumb_size
81
+ @@image_thumb_size = 'x100'
82
+
83
+ mattr_accessor :image_table_thumb_size
84
+ @@image_table_thumb_size = '25x25#'
85
+
86
+ # Define the default relationship table.
87
+ mattr_accessor :relationship
88
+ @@relationship = "typus_users"
89
+
90
+ mattr_accessor :master_role
91
+ @@master_role = "admin"
92
+
93
+ mattr_accessor :user_class_name
94
+ @@user_class_name = "AdminUser"
95
+
96
+ mattr_accessor :user_foreign_key
97
+ @@user_foreign_key = "admin_user_id"
98
+
99
+ mattr_accessor :quick_sidebar
100
+ @@quick_sidebar = false
101
+
102
+ mattr_accessor :ip_whitelist
103
+ @@ip_whitelist = []
104
+
105
+ mattr_accessor :link_to_view_site
106
+ @@link_to_view_site = false
107
+
108
+ mattr_accessor :link_to_help
109
+ @@link_to_help = false
110
+
111
+ class << self
112
+
113
+ # Default way to setup typus. Run `rails generate typus` to create a
114
+ # fresh initializer with all configuration values.
115
+ def setup
116
+ yield self
117
+ reload!
118
+ end
119
+
120
+ def applications
121
+ hash = {}
122
+
123
+ Typus::Configuration.config.map { |i| i.last["application"] }.compact.uniq.each do |app|
124
+ settings = app.extract_settings
125
+ hash[settings.first] = settings.size > 1 ? settings.last : 1000
126
+ end
127
+
128
+ hash.sort { |a1, a2| a1[1].to_i <=> a2[1].to_i }.map(&:first)
129
+ end
130
+
131
+ # Lists modules of an application.
132
+ def application(name)
133
+ array = []
134
+
135
+ Typus::Configuration.config.each do |i|
136
+ settings = i.last["application"]
137
+ array << i.first if settings && settings.extract_settings.first.eql?(name)
138
+ end
139
+
140
+ array.compact.uniq
141
+ end
142
+
143
+ # Lists models from the configuration file.
144
+ def models
145
+ Typus::Configuration.config.map(&:first).sort
146
+ end
147
+
148
+ # Lists resources, which are tableless models. This is done by looking at
149
+ # the roles, which handle the permissions for this kind of data.
150
+ def resources
151
+ if roles = Typus::Configuration.roles
152
+ roles.keys.map do |key|
153
+ Typus::Configuration.roles[key].keys
154
+ end.flatten.sort.uniq.delete_if { |x| models.include?(x) }
155
+ else
156
+ []
157
+ end
158
+ end
159
+
160
+ # Lists models under <tt>app/models</tt>.
161
+ def detect_application_models
162
+ model_dir = Rails.root.join("app/models")
163
+ Dir.chdir(model_dir.to_s) { Dir["**/*.rb"].reject {|f| f["concerns/"] } }
164
+ end
165
+
166
+ def application_models
167
+ detect_application_models.map do |model|
168
+ class_name = model.sub(/\.rb$/,"").camelize
169
+ klass = class_name.split("::").inject(Object) { |klass,part| klass.const_get(part) }
170
+ class_name if is_active_record?(klass)
171
+ end.compact
172
+ end
173
+
174
+ def is_active_record?(klass)
175
+ (defined?(ActiveRecord) && klass < ActiveRecord::Base && !klass.abstract_class?)
176
+ end
177
+ private :is_active_record?
178
+
179
+ def user_class
180
+ user_class_name.constantize rescue nil
181
+ end
182
+
183
+ def user_class_as_symbol
184
+ user_class_name.underscore.to_sym
185
+ end
186
+
187
+ def root
188
+ Rails.root.join(config_folder)
189
+ end
190
+
191
+ def model_configuration_files
192
+ app = Typus.root.join("**", "*.yml")
193
+ lib = Rails.root.join("lib", "*", "config", "typus", "**", "*.yml")
194
+ Dir[app.to_s, lib.to_s].reject { |f| f.match(/_roles.yml/) }.sort
195
+ end
196
+
197
+ def role_configuration_files
198
+ app = Typus.root.join("**", "*_roles.yml")
199
+ lib = Rails.root.join("lib", "*", "config", "typus", "**", "*_roles.yml")
200
+ Dir[app.to_s, lib.to_s].sort
201
+ end
202
+
203
+ def reload!
204
+ Typus::Configuration.roles!
205
+ Typus::Configuration.models!
206
+ end
207
+
208
+ # this should be overridden in typus-i18n
209
+ def available_locales
210
+ { 'English' => 'en' }
211
+ end
212
+
213
+ end
214
+
215
+ end