rails_admin_cms 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (150) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.md +36 -0
  4. data/Rakefile +26 -0
  5. data/app/assets/javascripts/rails_admin_cms/all.js.coffee +2 -0
  6. data/app/assets/javascripts/rails_admin_cms/cms.js.coffee +18 -0
  7. data/app/assets/javascripts/rails_admin_cms/cms/flashes.js.coffee +2 -0
  8. data/app/assets/javascripts/rails_admin_cms/cms/with_scope/forms.js.coffee +4 -0
  9. data/app/assets/javascripts/rails_admin_cms/cms/with_scope/viewables.js.coffee +14 -0
  10. data/app/assets/stylesheets/rails_admin_cms/all.css.sass +0 -0
  11. data/app/controllers/cms/attachments_controller.rb +20 -0
  12. data/app/controllers/cms/files_controller.rb +19 -0
  13. data/app/controllers/cms/forms_controller.rb +89 -0
  14. data/app/controllers/cms/pages_controller.rb +13 -0
  15. data/app/controllers/cms/viewables_controller.rb +49 -0
  16. data/app/controllers/concerns/cms/editing.rb +21 -0
  17. data/app/controllers/concerns/cms/localize.rb +15 -0
  18. data/app/helpers/cms/cache_helper.rb +26 -0
  19. data/app/helpers/cms/form_helper.rb +67 -0
  20. data/app/helpers/cms/locale_helper.rb +34 -0
  21. data/app/helpers/cms/view_helper.rb +37 -0
  22. data/app/helpers/cms/viewable_helper.rb +155 -0
  23. data/app/helpers/rails_admin/form_builder_decorator.rb +11 -0
  24. data/app/helpers/rails_admin/navigation_helper.rb +77 -0
  25. data/app/mailers/cms/forms_mailer.rb +48 -0
  26. data/app/models/admin/form/email.rb +41 -0
  27. data/app/models/admin/form/field.rb +25 -0
  28. data/app/models/admin/form/row.rb +36 -0
  29. data/app/models/admin/form/static/object.rb +15 -0
  30. data/app/models/admin/form/structure.rb +18 -0
  31. data/app/models/admin/setting.rb +21 -0
  32. data/app/models/admin/unique_key.rb +15 -0
  33. data/app/models/admin/viewable/block.rb +25 -0
  34. data/app/models/admin/viewable/form.rb +36 -0
  35. data/app/models/admin/viewable/image.rb +18 -0
  36. data/app/models/admin/viewable/link.rb +21 -0
  37. data/app/models/admin/viewable/page.rb +27 -0
  38. data/app/models/admin/viewable/select.rb +15 -0
  39. data/app/models/admin/viewable/text.rb +16 -0
  40. data/app/models/callbacks/form/field_after_create.rb +16 -0
  41. data/app/models/callbacks/form/field_after_destroy.rb +16 -0
  42. data/app/models/callbacks/form/field_before_update.rb +29 -0
  43. data/app/models/callbacks/restrictor.rb +30 -0
  44. data/app/models/callbacks/unique_key_before_update.rb +46 -0
  45. data/app/models/callbacks/viewable_after_destroy.rb +27 -0
  46. data/app/models/callbacks/viewable_before_update.rb +15 -0
  47. data/app/models/form.rb +43 -0
  48. data/app/models/form/email.rb +21 -0
  49. data/app/models/form/field.rb +75 -0
  50. data/app/models/form/row.rb +30 -0
  51. data/app/models/form/static.rb +17 -0
  52. data/app/models/form/static/attachment.rb +62 -0
  53. data/app/models/form/static/base.rb +10 -0
  54. data/app/models/form/static/email.rb +23 -0
  55. data/app/models/form/static/object.rb +12 -0
  56. data/app/models/form/structure.rb +90 -0
  57. data/app/models/setting.rb +85 -0
  58. data/app/models/unique_key.rb +62 -0
  59. data/app/models/viewable.rb +93 -0
  60. data/app/models/viewable/block.rb +39 -0
  61. data/app/models/viewable/field/url.rb +45 -0
  62. data/app/models/viewable/field/uuid.rb +61 -0
  63. data/app/models/viewable/form.rb +67 -0
  64. data/app/models/viewable/image.rb +6 -0
  65. data/app/models/viewable/link.rb +10 -0
  66. data/app/models/viewable/page.rb +24 -0
  67. data/app/models/viewable/select.rb +6 -0
  68. data/app/models/viewable/text.rb +6 -0
  69. data/app/presenters/base_list_presenter.rb +42 -0
  70. data/app/presenters/base_presenter.rb +17 -0
  71. data/app/presenters/viewable/block_presenter.rb +10 -0
  72. data/app/presenters/viewable/form_presenter.rb +15 -0
  73. data/app/presenters/viewable/image_presenter.rb +11 -0
  74. data/app/presenters/viewable/link_list_presenter.rb +44 -0
  75. data/app/presenters/viewable/link_presenter.rb +64 -0
  76. data/app/presenters/viewable/page_presenter.rb +4 -0
  77. data/app/presenters/viewable/select_list_presenter.rb +7 -0
  78. data/app/presenters/viewable/select_presenter.rb +15 -0
  79. data/app/presenters/viewable/text_presenter.rb +11 -0
  80. data/app/presenters/viewable/view_presenter.rb +38 -0
  81. data/app/presenters/viewable_list_presenter.rb +54 -0
  82. data/app/presenters/viewable_presenter.rb +27 -0
  83. data/app/views/cms/forms/create.js.erb +3 -0
  84. data/app/views/cms/shared/_flash_messages.js.erb +2 -0
  85. data/app/views/cms/viewables/update.js.erb +1 -0
  86. data/app/views/layouts/rails_admin/_secondary_navigation.html.haml +10 -0
  87. data/config/initializers/assets.rb +6 -0
  88. data/config/initializers/route_translator.rb +3 -0
  89. data/config/locales/cms.en.yml +32 -0
  90. data/config/locales/cms.fr.yml +32 -0
  91. data/config/locales/flash.en.yml +43 -0
  92. data/config/locales/flash.fr.yml +43 -0
  93. data/config/locales/routes.en.yml +3 -0
  94. data/config/locales/routes.fr.yml +3 -0
  95. data/config/routes.rb +36 -0
  96. data/config/spring.rb +1 -0
  97. data/db/migrate/20151226023652_create_unique_key.rb +19 -0
  98. data/db/migrate/20151226054335_create_viewable_link.rb +14 -0
  99. data/db/migrate/20151227064933_create_viewable_text.rb +10 -0
  100. data/db/migrate/20151227072412_create_rich_rich_images.rich.rb +18 -0
  101. data/db/migrate/20151227072413_add_uri_cache_to_rich_image.rich.rb +6 -0
  102. data/db/migrate/20151227072414_refactor_image_to_file.rich.rb +13 -0
  103. data/db/migrate/20151228231201_create_viewable_image.rb +10 -0
  104. data/db/migrate/20151230050603_create_setting.rb +13 -0
  105. data/db/migrate/20151230072904_add_cms_mail_from_and_bcc_settings.rb +14 -0
  106. data/db/migrate/20151230085947_create_viewable_select.rb +10 -0
  107. data/db/migrate/20151231054310_create_viewable_block.rb +9 -0
  108. data/db/migrate/20151231102148_create_viewable_page.rb +13 -0
  109. data/db/migrate/20160101124428_create_viewable_form.rb +15 -0
  110. data/db/migrate/20160101140244_create_form_structure.rb +9 -0
  111. data/db/migrate/20160101141844_create_form_row.rb +13 -0
  112. data/db/migrate/20160102010317_create_form_field.rb +17 -0
  113. data/db/migrate/20160103120544_create_form_email.rb +18 -0
  114. data/lib/generators/cms/install/USAGE +8 -0
  115. data/lib/generators/cms/install/install_generator.rb +14 -0
  116. data/lib/generators/cms/install/templates/Gemfile +47 -0
  117. data/lib/generators/cms/install/templates/app/assets/javascripts/application.js +19 -0
  118. data/lib/generators/cms/install/templates/app/assets/stylesheets/application.css +17 -0
  119. data/lib/generators/cms/install/templates/app/controllers/application_controller.rb +9 -0
  120. data/lib/generators/cms/install/templates/app/controllers/cms/authenticate.rb +34 -0
  121. data/lib/generators/cms/install/templates/app/models/form/contact.rb +20 -0
  122. data/lib/generators/cms/install/templates/app/views/cms/blocks/_example.html.erb +13 -0
  123. data/lib/generators/cms/install/templates/app/views/cms/forms/contact/_form.html.erb +30 -0
  124. data/lib/generators/cms/install/templates/app/views/cms/forms/contact/new.html.erb +19 -0
  125. data/lib/generators/cms/install/templates/app/views/cms/forms_mailer/contact.html.erb +6 -0
  126. data/lib/generators/cms/install/templates/app/views/cms/forms_mailer/contact.text.erb +6 -0
  127. data/lib/generators/cms/install/templates/app/views/cms/pages/page.html.erb +29 -0
  128. data/lib/generators/cms/install/templates/app/views/layouts/application.html.erb +23 -0
  129. data/lib/generators/cms/install/templates/config/application.rb +36 -0
  130. data/lib/generators/cms/install/templates/config/initializers/rails_admin.rb +97 -0
  131. data/lib/generators/cms/install/templates/config/initializers/rich.rb +142 -0
  132. data/lib/generators/cms/install/templates/config/initializers/simple_form.rb +165 -0
  133. data/lib/generators/cms/install/templates/config/initializers/simple_form_bootstrap.rb +149 -0
  134. data/lib/generators/cms/install/templates/config/locales/routes.en.yml +4 -0
  135. data/lib/generators/cms/install/templates/config/locales/simple_form.en.yml +31 -0
  136. data/lib/generators/cms/install/templates/config/routes.rb +61 -0
  137. data/lib/generators/cms/install/templates/vendor/assets/javascripts/rails_admin/custom/ui.js.coffee +1 -0
  138. data/lib/generators/cms/install/templates/vendor/assets/stylesheets/rails_admin/custom/mixins.sass +0 -0
  139. data/lib/generators/cms/install/templates/vendor/assets/stylesheets/rails_admin/custom/theming.sass +1 -0
  140. data/lib/generators/cms/install/templates/vendor/assets/stylesheets/rails_admin/custom/variables.sass +0 -0
  141. data/lib/generators/cms/install/templates/vendor/assets/stylesheets/rich/editor.css +20 -0
  142. data/lib/rails_admin_cms.rb +28 -0
  143. data/lib/rails_admin_cms/config.rb +45 -0
  144. data/lib/rails_admin_cms/core_ext/boolean.rb +29 -0
  145. data/lib/rails_admin_cms/engine.rb +11 -0
  146. data/lib/rails_admin_cms/inflections.rb +4 -0
  147. data/lib/rails_admin_cms/utils.rb +30 -0
  148. data/lib/rails_admin_cms/version.rb +3 -0
  149. data/lib/tasks/rails_admin_cms_tasks.rake +4 -0
  150. metadata +669 -0
@@ -0,0 +1,93 @@
1
+ module Viewable
2
+ extend ActiveSupport::Concern
3
+
4
+ included do
5
+ self.table_name_prefix = 'viewable_'
6
+
7
+ has_paper_trail if RailsAdminCMS::Config.with_paper_trail?
8
+
9
+ has_one :unique_key, as: :viewable, dependent: :destroy
10
+
11
+ delegate :view_path, :position, :locale, to: :unique_key
12
+ delegate :name, to: :unique_key, prefix: true
13
+
14
+ after_destroy ::Callbacks::ViewableAfterDestroy.new
15
+ before_update ::Callbacks::ViewableBeforeUpdate.new
16
+
17
+ before_destroy :expire_cache
18
+ after_update :expire_cache
19
+ after_touch :expire_cache
20
+
21
+ scope :localized, -> { includes(:unique_key).where(unique_keys: { locale: I18n.locale }) }
22
+ scope :other_locale, ->(locale) { includes(:unique_key).where(unique_keys: { locale: locale }) }
23
+
24
+ delegate :has_unlocalized_fields?, :unlocalized_fields, :viewable_type, :dashed_name, :underscored_name, to: :class
25
+ end
26
+
27
+ class_methods do
28
+ def has_unlocalized_fields(*fields)
29
+ define_singleton_method :unlocalized_fields do
30
+ fields
31
+ end
32
+ end
33
+
34
+ def has_unlocalized_fields?
35
+ unlocalized_fields.any?
36
+ end
37
+
38
+ def unlocalized_fields
39
+ []
40
+ end
41
+
42
+ def viewable_type
43
+ name
44
+ end
45
+
46
+ def dashed_name
47
+ @_dashed_name ||= underscored_name.dasherize
48
+ end
49
+
50
+ def underscored_name
51
+ @_underscored_name ||= name.underscore.gsub('/', '_')
52
+ end
53
+ end
54
+
55
+ def short_type
56
+ viewable_type.demodulize.underscore
57
+ end
58
+
59
+ def list(locale = nil)
60
+ self.class.includes(:unique_key)
61
+ .where(unique_keys: { viewable_type: viewable_type, view_path: view_path, name: unique_key_name })
62
+ .where(unique_keys: { locale: locale || self.locale })
63
+ end
64
+
65
+ def other_locales(position = nil)
66
+ self.class.includes(:unique_key)
67
+ .where(unique_keys: { viewable_type: viewable_type, view_path: view_path, name: unique_key_name })
68
+ .where(unique_keys: { position: position || self.position })
69
+ .where.not(unique_keys: { locale: locale })
70
+ end
71
+
72
+ def unique_key_hash(locale = nil)
73
+ unique_key
74
+ .slice(:viewable_type, :view_path, :name, :position)
75
+ .merge(locale: locale || self.locale)
76
+ end
77
+
78
+ class << self
79
+ def models
80
+ @_models ||= names.map{ |name| "Viewable::#{name.camelize}" }
81
+ end
82
+
83
+ def names
84
+ @_names ||= CMS.rb_all_names 'app/models/viewable'
85
+ end
86
+ end
87
+
88
+ private
89
+
90
+ def expire_cache
91
+ ActionController::Base.new.expire_fragment /#{view_path}/
92
+ end
93
+ end
@@ -0,0 +1,39 @@
1
+ module Viewable
2
+ class Block < ActiveRecord::Base
3
+ include Viewable
4
+ include Field::UUID
5
+ include Admin::Viewable::Block
6
+
7
+ def partial_path
8
+ @_partial_path ||= "cms/blocks/#{partial_name}"
9
+ end
10
+
11
+ def partial_name
12
+ @_partial_name ||= unique_key_name.partition('/').first
13
+ end
14
+
15
+ class << self
16
+ def restricted_names
17
+ @_restricted_names ||= Viewable.names + begin
18
+ CMS.constants.grep(/Helper$/).map{ |name|
19
+ "CMS::#{name}".constantize.instance_methods.grep(/^cms_/)
20
+ }.flatten.map{ |name|
21
+ name.to_s.sub(/^cms_/, '')
22
+ }
23
+ end
24
+ end
25
+
26
+ def names
27
+ @_names ||= begin
28
+ names = CMS.html_names 'app/views/cms/blocks'
29
+ names.each do |name|
30
+ if name.in? restricted_names
31
+ raise ArgumentError, "'cms/blocks/_#{name}.html' partial should be called otherwise, '#{name}' taken"
32
+ end
33
+ end
34
+ names
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,45 @@
1
+ module Viewable
2
+ module Field
3
+ module Url
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ before_update :normalize_url
8
+ after_commit :reload_routes
9
+
10
+ scope :with_url, -> { where('url LIKE :url', url: '/%') }
11
+ end
12
+
13
+ class_methods do
14
+ def names
15
+ raise NotImplementedError
16
+ end
17
+
18
+ def urls
19
+ localized.with_url.pluck(:url)
20
+ end
21
+
22
+ def all_urls
23
+ paths = names.map do |name|
24
+ Rails.application.routes.url_helpers.try "#{name}_path"
25
+ end.compact
26
+ (paths + urls).sort.uniq
27
+ end
28
+ end
29
+
30
+ private
31
+
32
+ def normalize_url
33
+ if url.present?
34
+ segments = url.split('/').reject(&:blank?)
35
+ segments.map{ |segment| segment.to_slug.normalize.to_s }
36
+ self.url = segments.join('/').prepend('/')
37
+ end
38
+ end
39
+
40
+ def reload_routes
41
+ Rails.application.routes_reloader.reload!
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,61 @@
1
+ module Viewable
2
+ module Field
3
+ module UUID
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ after_destroy :destroy_other_uuids
8
+ end
9
+
10
+ class_methods do
11
+ def uuid_prefix
12
+ @_uuid_prefix ||= model_name.human[0]
13
+ end
14
+
15
+ def find_by_uuid(uuid)
16
+ localized.find_by(uuid: uuid)
17
+ end
18
+ end
19
+
20
+ def uuid
21
+ value = read_attribute(:uuid)
22
+ if value.blank?
23
+ value = SecureRandom.uuid
24
+ transaction do
25
+ update_uuid_columns value
26
+ other_locales.each do |viewable|
27
+ viewable.__send__ :update_uuid_columns, value
28
+ end
29
+ end
30
+ end
31
+ value
32
+ end
33
+
34
+ def uuid_with(name)
35
+ "#{self.class.uuid_prefix}[#{uuid}]/#{name}"
36
+ end
37
+
38
+ def other_uuid(locale)
39
+ self.class.other_locale(locale).where(uuid: uuid).first
40
+ end
41
+
42
+ private
43
+
44
+ def destroy_other_uuids
45
+ query = UniqueKey.where('name LIKE :uuid', uuid: "%#{self.class.uuid_prefix}[#{uuid}]%")
46
+ query.map(&:viewable).each do |viewable|
47
+ viewable.destroy!
48
+ end
49
+ end
50
+
51
+ def update_uuid_columns(value)
52
+ attributes = uuid_columns.map{ |key| [key, value] }.to_h
53
+ update_columns attributes
54
+ end
55
+
56
+ def uuid_columns
57
+ [:uuid]
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,67 @@
1
+ module Viewable
2
+ class Form < ActiveRecord::Base
3
+ include Viewable
4
+ include Field::UUID
5
+ include Field::Url
6
+ include Admin::Viewable::Form
7
+
8
+ belongs_to :structure, class_name: 'Form::Structure', dependent: :destroy
9
+ has_many :rows, through: :structure, class_name: 'Form::Row'
10
+
11
+ accepts_nested_attributes_for :structure, allow_destroy: true
12
+
13
+ has_unlocalized_fields :structure
14
+
15
+ class << self
16
+ def names
17
+ @_names ||= CMS.dir_names 'app/views/cms/forms'
18
+ end
19
+
20
+ def static_names
21
+ @_static_names ||= CMS.rb_names 'app/models/form'
22
+ end
23
+
24
+ def static?(name)
25
+ name.in? static_names
26
+ end
27
+
28
+ def not_static?(name)
29
+ !static?(name)
30
+ end
31
+ end
32
+
33
+ def form_name
34
+ @_form_name ||= view_path.split('/')[-2]
35
+ end
36
+
37
+ def static?
38
+ form_name.in? self.class.static_names
39
+ end
40
+
41
+ def not_static?
42
+ !static?
43
+ end
44
+
45
+ private
46
+
47
+ def uuid_columns
48
+ super + [:url]
49
+ end
50
+
51
+ module Splitter
52
+ extend ActiveSupport::Concern
53
+
54
+ included do
55
+ before_destroy :assign_form_name_to_structure
56
+ end
57
+
58
+ private
59
+
60
+ def assign_form_name_to_structure
61
+ ::Form::Structure.form_name = self.form_name
62
+ end
63
+ end
64
+
65
+ include Splitter
66
+ end
67
+ end
@@ -0,0 +1,6 @@
1
+ module Viewable
2
+ class Image < ActiveRecord::Base
3
+ include Viewable
4
+ include Admin::Viewable::Image
5
+ end
6
+ end
@@ -0,0 +1,10 @@
1
+ module Viewable
2
+ class Link < ActiveRecord::Base
3
+ include Viewable
4
+ include Admin::Viewable::Link
5
+
6
+ def page_enum
7
+ Viewable::Page.all_urls + Viewable::Form.all_urls
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,24 @@
1
+ module Viewable
2
+ class Page < ActiveRecord::Base
3
+ include Viewable
4
+ include Field::UUID
5
+ include Field::Url
6
+ include Admin::Viewable::Page
7
+
8
+ class << self
9
+ def names
10
+ @_names ||= CMS.html_names 'app/views/cms/pages'
11
+ end
12
+ end
13
+
14
+ def view_name
15
+ @_view_name ||= view_path.split('/').last
16
+ end
17
+
18
+ private
19
+
20
+ def uuid_columns
21
+ super + [:url]
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,6 @@
1
+ module Viewable
2
+ class Select < ActiveRecord::Base
3
+ include Viewable
4
+ include Admin::Viewable::Select
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ module Viewable
2
+ class Text < ActiveRecord::Base
3
+ include Viewable
4
+ include Admin::Viewable::Text
5
+ end
6
+ end
@@ -0,0 +1,42 @@
1
+ class BaseListPresenter
2
+ attr_reader :context, :list
3
+ alias_method :h, :context
4
+
5
+ delegate(
6
+ :[],
7
+ :all?,
8
+ :any?,
9
+ :count,
10
+ :cycle,
11
+ :detect,
12
+ :drop,
13
+ :each,
14
+ :empty?,
15
+ :first,
16
+ :inject,
17
+ :last,
18
+ :map,
19
+ :none?,
20
+ :one?,
21
+ :pop,
22
+ :reduce,
23
+ :reject,
24
+ :reverse,
25
+ :rotate,
26
+ :sample,
27
+ :select,
28
+ :size,
29
+ :shift,
30
+ :shuffle,
31
+ :slice,
32
+ :sort,
33
+ :sort_by,
34
+ :take_while,
35
+ :zip,
36
+ to: :list
37
+ )
38
+
39
+ def initialize(list, context)
40
+ @list, @context = list, context
41
+ end
42
+ end
@@ -0,0 +1,17 @@
1
+ class BasePresenter
2
+ attr_reader :model, :context
3
+ alias_method :h, :context
4
+ alias_method :m, :model
5
+
6
+ def initialize(model, context)
7
+ @model, @context = model, context
8
+ end
9
+
10
+ def method_missing(name, *args, &block)
11
+ m.__send__(name, *args, &block)
12
+ end
13
+
14
+ def respond_to?(name, include_all = false)
15
+ super || m.respond_to?(name, include_all)
16
+ end
17
+ end
@@ -0,0 +1,10 @@
1
+ module Viewable
2
+ class BlockPresenter < ViewablePresenter
3
+ def render
4
+ h.instance_variable_set :@cms_view_partial, m
5
+ html = h.render m.partial_path
6
+ h.instance_variable_set :@cms_view_partial, nil
7
+ html
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,15 @@
1
+ module Viewable
2
+ class FormPresenter < ViewPresenter
3
+ def initialize(model, context)
4
+ super
5
+ if m.not_static?
6
+ if m.structure.nil?
7
+ m.create_structure!(viewable: m, email: ::Form::Email.new)
8
+ end
9
+ attributes = h.cms_form_instance.attributes
10
+ attributes['structure_id'] = m.structure_id
11
+ h.cms_form_instance = m.rows.build(attributes)
12
+ end
13
+ end
14
+ end
15
+ end