alchemy_cms 5.2.1 → 6.0.0.b1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (261) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ci.yml +6 -14
  3. data/.gitignore +0 -1
  4. data/.hound.yml +1 -1
  5. data/.rubocop.yml +46 -4
  6. data/CHANGELOG.md +81 -9
  7. data/Gemfile +4 -2
  8. data/README.md +5 -2
  9. data/alchemy_cms.gemspec +78 -65
  10. data/app/assets/javascripts/alchemy/admin.js +0 -2
  11. data/app/assets/javascripts/alchemy/alchemy.base.js.coffee +0 -27
  12. data/app/assets/javascripts/alchemy/alchemy.confirm_dialog.js.coffee +2 -1
  13. data/app/assets/javascripts/alchemy/alchemy.dialog.js.coffee +1 -1
  14. data/app/assets/javascripts/alchemy/alchemy.dragndrop.js.coffee +0 -25
  15. data/app/assets/javascripts/alchemy/alchemy.element_editors.js.coffee +1 -1
  16. data/app/assets/javascripts/alchemy/alchemy.elements_window.js.coffee +2 -0
  17. data/app/assets/javascripts/alchemy/alchemy.fixed_elements.js +1 -1
  18. data/app/assets/javascripts/alchemy/alchemy.gui.js.coffee +3 -1
  19. data/app/assets/javascripts/alchemy/alchemy.image_overlay.coffee +1 -1
  20. data/app/assets/javascripts/alchemy/alchemy.link_dialog.js.coffee +40 -27
  21. data/app/assets/javascripts/alchemy/templates/node_folder.hbs +1 -1
  22. data/app/assets/stylesheets/alchemy/admin.scss +1 -1
  23. data/app/assets/stylesheets/alchemy/archive.scss +4 -4
  24. data/app/assets/stylesheets/alchemy/buttons.scss +0 -4
  25. data/app/assets/stylesheets/alchemy/elements.scss +73 -61
  26. data/app/assets/stylesheets/alchemy/images.scss +8 -0
  27. data/app/assets/stylesheets/alchemy/node-select.scss +4 -3
  28. data/app/assets/stylesheets/alchemy/page-select.scss +1 -0
  29. data/app/controllers/alchemy/admin/attachments_controller.rb +6 -2
  30. data/app/controllers/alchemy/admin/base_controller.rb +5 -7
  31. data/app/controllers/alchemy/admin/elements_controller.rb +58 -34
  32. data/app/controllers/alchemy/admin/essence_audios_controller.rb +30 -0
  33. data/app/controllers/alchemy/admin/essence_files_controller.rb +0 -14
  34. data/app/controllers/alchemy/admin/essence_pictures_controller.rb +8 -79
  35. data/app/controllers/alchemy/admin/essence_videos_controller.rb +33 -0
  36. data/app/controllers/alchemy/admin/ingredients_controller.rb +30 -0
  37. data/app/controllers/alchemy/admin/layoutpages_controller.rb +0 -1
  38. data/app/controllers/alchemy/admin/pages_controller.rb +6 -13
  39. data/app/controllers/alchemy/admin/pictures_controller.rb +35 -9
  40. data/app/controllers/alchemy/api/elements_controller.rb +10 -5
  41. data/app/controllers/alchemy/api/pages_controller.rb +3 -4
  42. data/app/controllers/concerns/alchemy/admin/archive_overlay.rb +13 -3
  43. data/app/controllers/concerns/alchemy/admin/crop_action.rb +26 -0
  44. data/app/decorators/alchemy/element_editor.rb +23 -1
  45. data/app/decorators/alchemy/ingredient_editor.rb +154 -0
  46. data/app/helpers/alchemy/admin/elements_helper.rb +1 -0
  47. data/app/helpers/alchemy/admin/essences_helper.rb +1 -1
  48. data/app/helpers/alchemy/admin/ingredients_helper.rb +42 -0
  49. data/app/helpers/alchemy/elements_block_helper.rb +22 -7
  50. data/app/helpers/alchemy/elements_helper.rb +12 -5
  51. data/app/helpers/alchemy/pages_helper.rb +3 -11
  52. data/app/jobs/alchemy/base_job.rb +11 -0
  53. data/app/jobs/alchemy/publish_page_job.rb +11 -0
  54. data/app/models/alchemy/content.rb +1 -6
  55. data/app/models/alchemy/content/factory.rb +23 -27
  56. data/app/models/alchemy/element.rb +39 -72
  57. data/app/models/alchemy/element/definitions.rb +29 -27
  58. data/app/models/alchemy/element/element_contents.rb +131 -122
  59. data/app/models/alchemy/element/element_essences.rb +100 -98
  60. data/app/models/alchemy/element/element_ingredients.rb +176 -0
  61. data/app/models/alchemy/element/presenters.rb +89 -87
  62. data/app/models/alchemy/elements_repository.rb +126 -0
  63. data/app/models/alchemy/essence_audio.rb +12 -0
  64. data/app/models/alchemy/essence_headline.rb +40 -0
  65. data/app/models/alchemy/essence_picture.rb +4 -116
  66. data/app/models/alchemy/essence_richtext.rb +12 -0
  67. data/app/models/alchemy/essence_video.rb +12 -0
  68. data/app/models/alchemy/image_cropper_settings.rb +87 -0
  69. data/app/models/alchemy/ingredient.rb +219 -0
  70. data/app/models/alchemy/ingredient_validator.rb +97 -0
  71. data/app/models/alchemy/ingredients/audio.rb +29 -0
  72. data/app/models/alchemy/ingredients/boolean.rb +21 -0
  73. data/app/models/alchemy/ingredients/datetime.rb +20 -0
  74. data/app/models/alchemy/ingredients/file.rb +30 -0
  75. data/app/models/alchemy/ingredients/headline.rb +42 -0
  76. data/app/models/alchemy/ingredients/html.rb +19 -0
  77. data/app/models/alchemy/ingredients/link.rb +16 -0
  78. data/app/models/alchemy/ingredients/node.rb +23 -0
  79. data/app/models/alchemy/ingredients/page.rb +23 -0
  80. data/app/models/alchemy/ingredients/picture.rb +41 -0
  81. data/app/models/alchemy/ingredients/richtext.rb +57 -0
  82. data/app/models/alchemy/ingredients/select.rb +10 -0
  83. data/app/models/alchemy/ingredients/text.rb +17 -0
  84. data/app/models/alchemy/ingredients/video.rb +33 -0
  85. data/app/models/alchemy/language.rb +0 -11
  86. data/app/models/alchemy/page.rb +66 -32
  87. data/app/models/alchemy/page/fixed_attributes.rb +53 -51
  88. data/app/models/alchemy/page/page_elements.rb +186 -205
  89. data/app/models/alchemy/page/page_naming.rb +66 -64
  90. data/app/models/alchemy/page/page_natures.rb +139 -142
  91. data/app/models/alchemy/page/page_scopes.rb +113 -102
  92. data/app/models/alchemy/page/publisher.rb +50 -0
  93. data/app/models/alchemy/page/url_path.rb +1 -1
  94. data/app/models/alchemy/page_version.rb +58 -0
  95. data/app/models/alchemy/picture.rb +3 -1
  96. data/app/models/alchemy/picture/calculations.rb +2 -8
  97. data/app/models/alchemy/picture/preprocessor.rb +2 -0
  98. data/app/models/alchemy/picture/transformations.rb +24 -96
  99. data/app/models/concerns/alchemy/picture_thumbnails.rb +181 -0
  100. data/app/models/concerns/alchemy/touch_elements.rb +2 -2
  101. data/app/presenters/alchemy/picture_view.rb +88 -0
  102. data/app/serializers/alchemy/element_serializer.rb +5 -0
  103. data/app/serializers/alchemy/page_tree_serializer.rb +3 -2
  104. data/app/services/alchemy/delete_elements.rb +44 -0
  105. data/app/services/alchemy/duplicate_element.rb +56 -0
  106. data/app/views/alchemy/admin/attachments/_archive_overlay.html.erb +1 -2
  107. data/app/views/alchemy/admin/attachments/_file_to_assign.html.erb +3 -3
  108. data/app/views/alchemy/admin/attachments/assign.js.erb +11 -0
  109. data/app/views/alchemy/admin/crop.html.erb +36 -0
  110. data/app/views/alchemy/admin/elements/_element.html.erb +14 -10
  111. data/app/views/alchemy/admin/elements/{_element_footer.html.erb → _footer.html.erb} +0 -0
  112. data/app/views/alchemy/admin/elements/{_new_element_form.html.erb → _form.html.erb} +1 -1
  113. data/app/views/alchemy/admin/elements/{_element_header.html.erb → _header.html.erb} +1 -1
  114. data/app/views/alchemy/admin/elements/{_element_toolbar.html.erb → _toolbar.html.erb} +5 -6
  115. data/app/views/alchemy/admin/elements/{trash.js.erb → destroy.js.erb} +1 -3
  116. data/app/views/alchemy/admin/elements/new.html.erb +3 -3
  117. data/app/views/alchemy/admin/elements/order.js.erb +0 -17
  118. data/app/views/alchemy/admin/elements/update.js.erb +3 -2
  119. data/app/views/alchemy/admin/essence_audios/edit.html.erb +7 -0
  120. data/app/views/alchemy/admin/essence_pictures/update.js.erb +0 -1
  121. data/app/views/alchemy/admin/essence_videos/edit.html.erb +11 -0
  122. data/app/views/alchemy/admin/ingredients/_audio_fields.html.erb +4 -0
  123. data/app/views/alchemy/admin/ingredients/_file_fields.html.erb +18 -0
  124. data/app/views/alchemy/admin/ingredients/_picture_fields.html.erb +25 -0
  125. data/app/views/alchemy/admin/ingredients/_video_fields.html.erb +8 -0
  126. data/app/views/alchemy/admin/ingredients/edit.html.erb +4 -0
  127. data/app/views/alchemy/admin/layoutpages/edit.html.erb +0 -5
  128. data/app/views/alchemy/admin/nodes/_node.html.erb +2 -2
  129. data/app/views/alchemy/admin/pages/_anchor_link.html.erb +1 -1
  130. data/app/views/alchemy/admin/pages/_external_link.html.erb +1 -1
  131. data/app/views/alchemy/admin/pages/_file_link.html.erb +1 -1
  132. data/app/views/alchemy/admin/pages/_form.html.erb +0 -6
  133. data/app/views/alchemy/admin/pages/_internal_link.html.erb +1 -1
  134. data/app/views/alchemy/admin/pages/_tinymce_custom_config.html.erb +5 -2
  135. data/app/views/alchemy/admin/pages/edit.html.erb +36 -24
  136. data/app/views/alchemy/admin/partials/_remote_search_form.html.erb +2 -4
  137. data/app/views/alchemy/admin/partials/_routes.html.erb +7 -11
  138. data/app/views/alchemy/admin/pictures/_filter_and_size_bar.html.erb +4 -8
  139. data/app/views/alchemy/admin/pictures/_infos.html.erb +0 -1
  140. data/app/views/alchemy/admin/pictures/_picture_to_assign.html.erb +4 -4
  141. data/app/views/alchemy/admin/pictures/assign.js.erb +10 -0
  142. data/app/views/alchemy/admin/resources/_form.html.erb +1 -0
  143. data/app/views/alchemy/essences/_essence_audio_editor.html.erb +4 -0
  144. data/app/views/alchemy/essences/_essence_audio_view.html.erb +15 -0
  145. data/app/views/alchemy/essences/_essence_file_editor.html.erb +15 -6
  146. data/app/views/alchemy/essences/_essence_headline_editor.html.erb +36 -0
  147. data/app/views/alchemy/essences/_essence_headline_view.html.erb +10 -0
  148. data/app/views/alchemy/essences/_essence_link_editor.html.erb +8 -4
  149. data/app/views/alchemy/essences/_essence_picture_editor.html.erb +27 -12
  150. data/app/views/alchemy/essences/_essence_picture_view.html.erb +3 -3
  151. data/app/views/alchemy/essences/_essence_text_editor.html.erb +12 -4
  152. data/app/views/alchemy/essences/_essence_video_editor.html.erb +4 -0
  153. data/app/views/alchemy/essences/_essence_video_view.html.erb +18 -0
  154. data/app/views/alchemy/essences/shared/_essence_picture_tools.html.erb +21 -16
  155. data/app/views/alchemy/essences/shared/_linkable_essence_tools.html.erb +2 -2
  156. data/app/views/alchemy/ingredients/_audio_editor.html.erb +5 -0
  157. data/app/views/alchemy/ingredients/_audio_view.html.erb +14 -0
  158. data/app/views/alchemy/ingredients/_boolean_editor.html.erb +11 -0
  159. data/app/views/alchemy/ingredients/_boolean_view.html.erb +1 -0
  160. data/app/views/alchemy/ingredients/_datetime_editor.html.erb +17 -0
  161. data/app/views/alchemy/ingredients/_datetime_view.html.erb +9 -0
  162. data/app/views/alchemy/ingredients/_file_editor.html.erb +50 -0
  163. data/app/views/alchemy/ingredients/_file_view.html.erb +17 -0
  164. data/app/views/alchemy/ingredients/_headline_editor.html.erb +30 -0
  165. data/app/views/alchemy/ingredients/_headline_view.html.erb +9 -0
  166. data/app/views/alchemy/ingredients/_html_editor.html.erb +8 -0
  167. data/app/views/alchemy/ingredients/_html_view.html.erb +1 -0
  168. data/app/views/alchemy/ingredients/_link_editor.html.erb +24 -0
  169. data/app/views/alchemy/ingredients/_link_view.html.erb +9 -0
  170. data/app/views/alchemy/ingredients/_node_editor.html.erb +25 -0
  171. data/app/views/alchemy/ingredients/_node_view.html.erb +1 -0
  172. data/app/views/alchemy/ingredients/_page_editor.html.erb +24 -0
  173. data/app/views/alchemy/ingredients/_page_view.html.erb +4 -0
  174. data/app/views/alchemy/ingredients/_picture_editor.html.erb +59 -0
  175. data/app/views/alchemy/ingredients/_picture_view.html.erb +5 -0
  176. data/app/views/alchemy/ingredients/_richtext_editor.html.erb +12 -0
  177. data/app/views/alchemy/ingredients/_richtext_view.html.erb +3 -0
  178. data/app/views/alchemy/ingredients/_select_editor.html.erb +29 -0
  179. data/app/views/alchemy/ingredients/_select_view.html.erb +1 -0
  180. data/app/views/alchemy/ingredients/_text_editor.html.erb +19 -0
  181. data/app/views/alchemy/ingredients/_text_view.html.erb +16 -0
  182. data/app/views/alchemy/ingredients/_video_editor.html.erb +5 -0
  183. data/app/views/alchemy/ingredients/_video_view.html.erb +17 -0
  184. data/app/views/alchemy/ingredients/shared/_link_tools.html.erb +20 -0
  185. data/app/views/alchemy/ingredients/shared/_picture_tools.html.erb +57 -0
  186. data/config/brakeman.ignore +66 -159
  187. data/config/initializers/dragonfly.rb +10 -0
  188. data/config/locales/alchemy.en.yml +23 -15
  189. data/config/routes.rb +17 -22
  190. data/db/migrate/20201207131309_create_page_versions.rb +19 -0
  191. data/db/migrate/20201207135820_add_page_version_id_to_alchemy_elements.rb +76 -0
  192. data/db/migrate/20210205143548_rename_public_on_and_public_until_on_alchemy_pages.rb +10 -0
  193. data/db/migrate/20210326105046_add_sanitized_body_to_alchemy_essence_richtexts.rb +7 -0
  194. data/db/migrate/20210406093436_add_alchemy_essence_headlines.rb +12 -0
  195. data/db/migrate/20210506135919_create_essence_audios.rb +19 -0
  196. data/db/migrate/20210506140258_create_essence_videos.rb +23 -0
  197. data/db/migrate/20210508091432_create_alchemy_ingredients.rb +22 -0
  198. data/lib/alchemy/admin/preview_url.rb +2 -0
  199. data/lib/alchemy/deprecation.rb +1 -1
  200. data/lib/alchemy/dragonfly/processors/auto_orient.rb +18 -0
  201. data/lib/alchemy/dragonfly/processors/crop_resize.rb +35 -0
  202. data/lib/alchemy/elements_finder.rb +14 -60
  203. data/lib/alchemy/essence.rb +1 -2
  204. data/lib/alchemy/hints.rb +8 -4
  205. data/lib/alchemy/page_layout.rb +0 -13
  206. data/lib/alchemy/permissions.rb +30 -29
  207. data/lib/alchemy/resource.rb +13 -3
  208. data/lib/alchemy/tasks/tidy.rb +29 -0
  209. data/lib/alchemy/test_support.rb +2 -11
  210. data/lib/alchemy/test_support/essence_shared_examples.rb +0 -1
  211. data/lib/alchemy/test_support/factories/element_factory.rb +8 -8
  212. data/lib/alchemy/test_support/factories/essence_audio_factory.rb +7 -0
  213. data/lib/alchemy/test_support/factories/essence_video_factory.rb +7 -0
  214. data/lib/alchemy/test_support/factories/ingredient_factory.rb +25 -0
  215. data/lib/alchemy/test_support/factories/page_factory.rb +20 -1
  216. data/lib/alchemy/test_support/factories/page_version_factory.rb +23 -0
  217. data/lib/alchemy/test_support/having_crop_action_examples.rb +170 -0
  218. data/lib/alchemy/test_support/having_picture_thumbnails_examples.rb +646 -0
  219. data/lib/alchemy/test_support/shared_ingredient_editor_examples.rb +21 -0
  220. data/lib/alchemy/test_support/shared_ingredient_examples.rb +57 -0
  221. data/lib/alchemy/tinymce.rb +17 -0
  222. data/lib/alchemy/upgrader/six_point_zero.rb +21 -0
  223. data/lib/alchemy/upgrader/tasks/add_page_versions.rb +33 -0
  224. data/lib/alchemy/upgrader/tasks/ingredients_migrator.rb +51 -0
  225. data/lib/alchemy/version.rb +1 -1
  226. data/lib/generators/alchemy/elements/elements_generator.rb +1 -0
  227. data/lib/generators/alchemy/elements/templates/view.html.erb +9 -0
  228. data/lib/generators/alchemy/elements/templates/view.html.haml +9 -0
  229. data/lib/generators/alchemy/elements/templates/view.html.slim +9 -0
  230. data/lib/generators/alchemy/ingredient/ingredient_generator.rb +38 -0
  231. data/lib/generators/alchemy/ingredient/templates/editor.html.erb +14 -0
  232. data/lib/generators/alchemy/ingredient/templates/model.rb.tt +13 -0
  233. data/lib/generators/alchemy/ingredient/templates/view.html.erb +1 -0
  234. data/lib/generators/alchemy/install/templates/dragonfly.rb.tt +1 -1
  235. data/lib/tasks/alchemy/thumbnails.rake +2 -0
  236. data/lib/tasks/alchemy/tidy.rake +12 -0
  237. data/lib/tasks/alchemy/upgrade.rake +26 -0
  238. data/package.json +2 -1
  239. data/package/admin.js +11 -1
  240. data/package/src/__tests__/i18n.spec.js +23 -0
  241. data/package/src/file_editors.js +28 -0
  242. data/package/src/i18n.js +1 -3
  243. data/package/src/image_cropper.js +103 -0
  244. data/package/src/image_loader.js +58 -0
  245. data/package/src/node_tree.js +5 -5
  246. data/package/src/picture_editors.js +169 -0
  247. data/package/src/utils/__tests__/ajax.spec.js +20 -12
  248. data/package/src/utils/ajax.js +8 -3
  249. data/vendor/assets/javascripts/jquery_plugins/jquery.Jcrop.min.js +3 -18
  250. data/vendor/assets/stylesheets/jquery.Jcrop.min.scss +2 -28
  251. metadata +284 -48
  252. data/app/assets/javascripts/alchemy/alchemy.image_cropper.js.coffee +0 -44
  253. data/app/assets/javascripts/alchemy/alchemy.trash_window.js.coffee +0 -30
  254. data/app/assets/stylesheets/alchemy/trash.scss +0 -8
  255. data/app/controllers/alchemy/admin/trash_controller.rb +0 -44
  256. data/app/views/alchemy/admin/essence_files/assign.js.erb +0 -3
  257. data/app/views/alchemy/admin/essence_pictures/assign.js.erb +0 -4
  258. data/app/views/alchemy/admin/essence_pictures/crop.html.erb +0 -48
  259. data/app/views/alchemy/admin/trash/clear.js.erb +0 -4
  260. data/app/views/alchemy/admin/trash/index.html.erb +0 -31
  261. data/lib/alchemy/test_support/factories.rb +0 -20
@@ -0,0 +1,176 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Alchemy
4
+ class Element < BaseRecord
5
+ # Methods concerning ingredients for elements
6
+ #
7
+ module ElementIngredients
8
+ extend ActiveSupport::Concern
9
+
10
+ included do
11
+ attr_accessor :autogenerate_ingredients
12
+
13
+ has_many :ingredients,
14
+ class_name: "Alchemy::Ingredient",
15
+ inverse_of: :element,
16
+ dependent: :destroy
17
+
18
+ before_create :build_ingredients,
19
+ unless: -> { autogenerate_ingredients == false }
20
+
21
+ accepts_nested_attributes_for :ingredients
22
+ validates_associated :ingredients, on: :update
23
+ end
24
+
25
+ # Find first ingredient from element by given role.
26
+ def ingredient_by_role(role)
27
+ ingredients.detect { |ingredient| ingredient.role == role.to_s }
28
+ end
29
+
30
+ # Find first ingredient from element by given type.
31
+ def ingredient_by_type(type)
32
+ ingredients_by_type(type).first
33
+ end
34
+
35
+ # All ingredients from element by given type.
36
+ def ingredients_by_type(type)
37
+ ingredients.select do |ingredient|
38
+ ingredient.type == Ingredient.normalize_type(type)
39
+ end
40
+ end
41
+
42
+ # Copy current ingredient's ingredients to given target element
43
+ def copy_ingredients_to(element)
44
+ ingredients.map do |ingredient|
45
+ Ingredient.copy(ingredient, element_id: element.id)
46
+ end
47
+ end
48
+
49
+ # Returns all element ingredient definitions from the +elements.yml+ file
50
+ def ingredient_definitions
51
+ definition.fetch(:ingredients, [])
52
+ end
53
+
54
+ # Returns the definition for given ingredient role
55
+ def ingredient_definition_for(role)
56
+ if ingredient_definitions.blank?
57
+ log_warning "Element #{name} is missing the ingredient definition for #{role}"
58
+ nil
59
+ else
60
+ ingredient_definitions.find { |d| d[:role] == role.to_s }
61
+ end
62
+ end
63
+
64
+ # Returns an array of all Richtext ingredients ids from elements
65
+ #
66
+ # This is used to re-initialize the TinyMCE editor in the element editor.
67
+ #
68
+ def richtext_ingredients_ids
69
+ ids = ingredients.select(&:has_tinymce?).collect(&:id)
70
+ expanded_nested_elements = nested_elements.expanded
71
+ if expanded_nested_elements.present?
72
+ ids += expanded_nested_elements.collect(&:richtext_ingredients_ids)
73
+ end
74
+ ids.flatten
75
+ end
76
+
77
+ # Has any of the ingredients validations defined?
78
+ def has_validations?
79
+ ingredients.any?(&:has_validations?)
80
+ end
81
+
82
+ # All element ingredients where the validation has failed.
83
+ def ingredients_with_errors
84
+ ingredients.select { |i| i.errors.any? }
85
+ end
86
+
87
+ # True if the element has a ingredient for given name
88
+ # that has a non blank value.
89
+ def has_value_for?(role)
90
+ ingredient_by_role(role)&.value.present?
91
+ end
92
+
93
+ # Ingredient validation error messages
94
+ #
95
+ # == Error messages are translated via I18n
96
+ #
97
+ # Inside your translation file add translations like:
98
+ #
99
+ # alchemy:
100
+ # ingredient_validations:
101
+ # name_of_the_element:
102
+ # role_of_the_ingredient:
103
+ # validation_error_type: Error Message
104
+ #
105
+ # NOTE: +validation_error_type+ has to be one of:
106
+ #
107
+ # * blank
108
+ # * taken
109
+ # * invalid
110
+ #
111
+ # === Example:
112
+ #
113
+ # de:
114
+ # alchemy:
115
+ # ingredient_validations:
116
+ # contactform:
117
+ # email:
118
+ # invalid: 'Die Email hat nicht das richtige Format'
119
+ #
120
+ #
121
+ # == Error message translation fallbacks
122
+ #
123
+ # In order to not translate every single ingredient for every element
124
+ # you can provide default error messages per content name:
125
+ #
126
+ # === Example
127
+ #
128
+ # en:
129
+ # alchemy:
130
+ # ingredient_validations:
131
+ # fields:
132
+ # email:
133
+ # invalid: E-Mail has wrong format
134
+ # blank: E-Mail can't be blank
135
+ #
136
+ # And even further you can provide general field agnostic error messages:
137
+ #
138
+ # === Example
139
+ #
140
+ # en:
141
+ # alchemy:
142
+ # ingredient_validations:
143
+ # errors:
144
+ # invalid: %{field} has wrong format
145
+ # blank: %{field} can't be blank
146
+ #
147
+ def ingredient_error_messages
148
+ messages = []
149
+ ingredients_with_errors.map { |i| [i.role, i.errors.details] }.each do |role, error_details|
150
+ error_details[:value].each do |error_detail|
151
+ error = error_detail[:error]
152
+ messages << Alchemy.t(
153
+ "#{name}.#{role}.#{error}",
154
+ scope: "ingredient_validations",
155
+ default: [
156
+ "fields.#{role}.#{error}".to_sym,
157
+ "errors.#{error}".to_sym,
158
+ ],
159
+ field: Alchemy::Ingredient.translated_label_for(role, name),
160
+ )
161
+ end
162
+ end
163
+ messages
164
+ end
165
+
166
+ private
167
+
168
+ # Builds ingredients for this element as described in the +elements.yml+
169
+ def build_ingredients
170
+ self.ingredients = ingredient_definitions.map do |attributes|
171
+ Ingredient.build(role: attributes[:role], element: self)
172
+ end
173
+ end
174
+ end
175
+ end
176
+ end
@@ -1,108 +1,110 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Alchemy
4
- # Methods used for presenting an Alchemy Element.
5
- #
6
- module Element::Presenters
7
- extend ActiveSupport::Concern
4
+ class Element < BaseRecord
5
+ # Methods used for presenting an Alchemy Element.
6
+ #
7
+ module Presenters
8
+ extend ActiveSupport::Concern
9
+
10
+ module ClassMethods
11
+ # Human name for displaying elements in select boxes and element editor views.
12
+ #
13
+ # The name is beeing translated from given name value as described in +config/alchemy/elements.yml+
14
+ #
15
+ # Translate the name in your +config/locales+ language file.
16
+ #
17
+ # == Example:
18
+ #
19
+ # de:
20
+ # alchemy:
21
+ # element_names:
22
+ # contactform: 'Kontakt Formular'
23
+ #
24
+ # If no translation is found a humanized name is used.
25
+ #
26
+ def display_name_for(name)
27
+ Alchemy.t(name, scope: "element_names", default: name.to_s.humanize)
28
+ end
29
+ end
8
30
 
9
- module ClassMethods
10
- # Human name for displaying elements in select boxes and element editor views.
31
+ # Returns the translated name
11
32
  #
12
- # The name is beeing translated from given name value as described in +config/alchemy/elements.yml+
33
+ # @see Alchemy::Element::Presenters#display_name_for
13
34
  #
14
- # Translate the name in your +config/locales+ language file.
35
+ def display_name
36
+ self.class.display_name_for(definition["name"] || name)
37
+ end
38
+
39
+ # Returns a preview text for element.
15
40
  #
16
- # == Example:
41
+ # It's taken from the first Content found in the +elements.yml+ definition file.
17
42
  #
18
- # de:
19
- # alchemy:
20
- # element_names:
21
- # contactform: 'Kontakt Formular'
43
+ # You can flag a Content as +as_element_title+ to take this as preview.
22
44
  #
23
- # If no translation is found a humanized name is used.
45
+ # @param maxlength [Fixnum] (60)
46
+ # Length of characters after the text will be cut off.
24
47
  #
25
- def display_name_for(name)
26
- Alchemy.t(name, scope: "element_names", default: name.to_s.humanize)
48
+ def preview_text(maxlength = 60)
49
+ preview_text_from_preview_content(maxlength) || preview_text_from_nested_elements(maxlength)
27
50
  end
28
- end
29
51
 
30
- # Returns the translated name
31
- #
32
- # @see Alchemy::Element::Presenters#display_name_for
33
- #
34
- def display_name
35
- self.class.display_name_for(definition["name"] || name)
36
- end
37
-
38
- # Returns a preview text for element.
39
- #
40
- # It's taken from the first Content found in the +elements.yml+ definition file.
41
- #
42
- # You can flag a Content as +as_element_title+ to take this as preview.
43
- #
44
- # @param maxlength [Fixnum] (60)
45
- # Length of characters after the text will be cut off.
46
- #
47
- def preview_text(maxlength = 60)
48
- preview_text_from_preview_content(maxlength) || preview_text_from_nested_elements(maxlength)
49
- end
50
-
51
- # Generates a preview text containing Element#display_name and Element#preview_text.
52
- #
53
- # It is displayed inside the head of the Element in the Elements.list overlay window from the Alchemy Admin::Page#edit view.
54
- #
55
- # === Example
56
- #
57
- # A Element described as:
58
- #
59
- # - name: funky_element
60
- # display_name: Funky Element
61
- # contents:
62
- # - name: headline
63
- # type: EssenceText
64
- # - name: text
65
- # type EssenceRichtext
66
- # as_element_title: true
67
- #
68
- # With "I want to tell you a funky story" as stripped_body for the EssenceRichtext Content produces:
69
- #
70
- # Funky Element: I want to tell ...
71
- #
72
- # @param maxlength [Fixnum] (30)
73
- # Length of characters after the text will be cut off.
74
- #
75
- def display_name_with_preview_text(maxlength = 30)
76
- "#{display_name}: #{preview_text(maxlength)}"
77
- end
52
+ # Generates a preview text containing Element#display_name and Element#preview_text.
53
+ #
54
+ # It is displayed inside the head of the Element in the Elements.list overlay window from the Alchemy Admin::Page#edit view.
55
+ #
56
+ # === Example
57
+ #
58
+ # A Element described as:
59
+ #
60
+ # - name: funky_element
61
+ # display_name: Funky Element
62
+ # contents:
63
+ # - name: headline
64
+ # type: EssenceText
65
+ # - name: text
66
+ # type EssenceRichtext
67
+ # as_element_title: true
68
+ #
69
+ # With "I want to tell you a funky story" as stripped_body for the EssenceRichtext Content produces:
70
+ #
71
+ # Funky Element: I want to tell ...
72
+ #
73
+ # @param maxlength [Fixnum] (30)
74
+ # Length of characters after the text will be cut off.
75
+ #
76
+ def display_name_with_preview_text(maxlength = 30)
77
+ "#{display_name}: #{preview_text(maxlength)}"
78
+ end
78
79
 
79
- # Returns a dom id used for elements html id tag.
80
- #
81
- def dom_id
82
- "#{name}_#{id}"
83
- end
80
+ # Returns a dom id used for elements html id tag.
81
+ #
82
+ def dom_id
83
+ "#{name}_#{id}"
84
+ end
84
85
 
85
- # The content that's used for element's preview text.
86
- #
87
- # It tries to find one of element's contents that is defined +as_element_title+.
88
- # Takes element's first content if no content is defined +as_element_title+.
89
- #
90
- # @return (Alchemy::Content)
91
- #
92
- def preview_content
93
- @_preview_content ||= contents.detect(&:preview_content?) || contents.first
94
- end
86
+ # The content that's used for element's preview text.
87
+ #
88
+ # It tries to find one of element's contents that is defined +as_element_title+.
89
+ # Takes element's first content if no content is defined +as_element_title+.
90
+ #
91
+ # @return (Alchemy::Content)
92
+ #
93
+ def preview_content
94
+ @_preview_content ||= contents.detect(&:preview_content?) || contents.first
95
+ end
95
96
 
96
- private
97
+ private
97
98
 
98
- def preview_text_from_nested_elements(maxlength)
99
- return if all_nested_elements.empty?
99
+ def preview_text_from_nested_elements(maxlength)
100
+ return if all_nested_elements.empty?
100
101
 
101
- all_nested_elements.first.preview_text(maxlength)
102
- end
102
+ all_nested_elements.first.preview_text(maxlength)
103
+ end
103
104
 
104
- def preview_text_from_preview_content(maxlength)
105
- preview_content.try!(:preview_text, maxlength)
105
+ def preview_text_from_preview_content(maxlength)
106
+ preview_content.try!(:preview_text, maxlength)
107
+ end
106
108
  end
107
109
  end
108
110
  end
@@ -0,0 +1,126 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Alchemy
4
+ # Mimics ActiveRecord query interface
5
+ # but does this on the preloaded elements
6
+ class ElementsRepository
7
+ include Enumerable
8
+
9
+ # An empty set of elements
10
+ def self.none
11
+ new([])
12
+ end
13
+
14
+ # @param [ActiveRecord::Relation]
15
+ def initialize(elements)
16
+ @elements = elements.to_a
17
+ end
18
+
19
+ # All visible elements
20
+ # @return [Alchemy::ElementRepository]
21
+ def visible
22
+ self.class.new select(&:public)
23
+ end
24
+
25
+ # All not fixed elements
26
+ # @return [Alchemy::ElementRepository]
27
+ def hidden
28
+ self.class.new reject(&:public)
29
+ end
30
+
31
+ # All elements with given name(s)
32
+ # @param [Array<String|Symbol>|String|Symbol]
33
+ # @return [Alchemy::ElementRepository]
34
+ def named(*names)
35
+ names.flatten!
36
+ self.class.new(select { |e| e.name.in?(names.map!(&:to_s)) })
37
+ end
38
+
39
+ # Filter elements by given attribute and value
40
+ # @param [Array|Hash]
41
+ # @return [Alchemy::ElementRepository]
42
+ def where(attrs)
43
+ self.class.new(
44
+ select do |element|
45
+ attrs.all? do |attr, value|
46
+ element.public_send(attr) == value
47
+ end
48
+ end
49
+ )
50
+ end
51
+
52
+ # All elements excluding those wth given name(s)
53
+ # @param [Array<String|Symbol>|String|Symbol]
54
+ # @return [Alchemy::ElementRepository]
55
+ def excluded(*names)
56
+ names.flatten!
57
+ self.class.new(reject { |e| e.name.in?(names.map!(&:to_s)) })
58
+ end
59
+
60
+ # All fixed elements
61
+ # @return [Alchemy::ElementRepository]
62
+ def fixed
63
+ self.class.new select(&:fixed)
64
+ end
65
+
66
+ # All not fixed elements
67
+ # @return [Alchemy::ElementRepository]
68
+ def unfixed
69
+ self.class.new reject(&:fixed)
70
+ end
71
+
72
+ # All folded elements
73
+ # @return [Alchemy::ElementRepository]
74
+ def folded
75
+ self.class.new select(&:folded)
76
+ end
77
+
78
+ # All expanded elements
79
+ # @return [Alchemy::ElementRepository]
80
+ def expanded
81
+ self.class.new reject(&:folded)
82
+ end
83
+
84
+ # All not nested top level elements
85
+ # @return [Alchemy::ElementRepository]
86
+ def not_nested
87
+ self.class.new(select { |e| e.parent_element_id.nil? })
88
+ end
89
+
90
+ # Elements in reversed order
91
+ # @return [Alchemy::ElementRepository]
92
+ def reverse
93
+ self.class.new elements.reverse
94
+ end
95
+
96
+ # Elements in random order
97
+ # @return [Alchemy::ElementRepository]
98
+ def random
99
+ self.class.new Array(elements).shuffle
100
+ end
101
+
102
+ # Elements off setted by
103
+ # @return [Alchemy::ElementRepository]
104
+ def offset(offset)
105
+ self.class.new elements[offset.to_i..-1]
106
+ end
107
+
108
+ # Elements limitted by
109
+ # @return [Alchemy::ElementRepository]
110
+ def limit(limit)
111
+ self.class.new elements[0..(limit.to_i - 1)]
112
+ end
113
+
114
+ def children_of(parent)
115
+ self.class.new(select { |e| e.parent_element_id == parent.id })
116
+ end
117
+
118
+ def each(&blk)
119
+ elements.each(&blk)
120
+ end
121
+
122
+ private
123
+
124
+ attr_reader :elements
125
+ end
126
+ end