locomotive_cms 2.0.0.rc12 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (519) hide show
  1. data/Gemfile +35 -28
  2. data/LICENSE +1 -1
  3. data/README.textile +9 -4
  4. data/app/assets/images/locomotive/icons/flags/de.png +0 -0
  5. data/app/assets/images/locomotive/icons/flags/en.png +0 -0
  6. data/app/assets/images/locomotive/icons/flags/es.png +0 -0
  7. data/app/assets/images/locomotive/icons/flags/et.png +0 -0
  8. data/app/assets/images/locomotive/icons/flags/fr.png +0 -0
  9. data/app/assets/images/locomotive/icons/flags/it.png +0 -0
  10. data/app/assets/images/locomotive/icons/flags/nb.png +0 -0
  11. data/app/assets/images/locomotive/icons/flags/nl.png +0 -0
  12. data/app/assets/images/locomotive/icons/flags/pl.png +0 -0
  13. data/app/assets/images/locomotive/icons/flags/pt-BR.png +0 -0
  14. data/app/assets/images/locomotive/icons/flags/ru.png +0 -0
  15. data/app/assets/javascripts/aloha/plugins/custom/locomotive_media/css/image.css +1 -1
  16. data/app/assets/javascripts/aloha/plugins/custom/locomotive_media/lib/locomotive_media-plugin.js +10 -21
  17. data/app/assets/javascripts/locomotive/models/content_entry.js.coffee +1 -1
  18. data/app/assets/javascripts/locomotive/models/content_type.js.coffee +6 -0
  19. data/app/assets/javascripts/locomotive/models/translation.js.coffee +5 -0
  20. data/app/assets/javascripts/locomotive/utils/aloha_settings.js.coffee +20 -2
  21. data/app/assets/javascripts/locomotive/utils/tinymce_settings.js.coffee +6 -6
  22. data/app/assets/javascripts/locomotive/views/application_view.js.coffee +4 -4
  23. data/app/assets/javascripts/locomotive/views/content_assets/picker_item_view.js.coffee +1 -1
  24. data/app/assets/javascripts/locomotive/views/content_entries/_form_view.js.coffee +10 -3
  25. data/app/assets/javascripts/locomotive/views/content_entries/_popup_form_view.js.coffee +7 -6
  26. data/app/assets/javascripts/locomotive/views/content_entries/index_view.js.coffee +1 -1
  27. data/app/assets/javascripts/locomotive/views/content_types/_form_view.js.coffee +4 -1
  28. data/app/assets/javascripts/locomotive/views/content_types/custom_field_entry_view.js.coffee +2 -2
  29. data/app/assets/javascripts/locomotive/views/content_types/custom_fields_view.coffee +5 -0
  30. data/app/assets/javascripts/locomotive/views/content_types/edit_view.coffee +1 -1
  31. data/app/assets/javascripts/locomotive/views/content_types/select_options_view.js.coffee +6 -6
  32. data/app/assets/javascripts/locomotive/views/current_site/edit_view.js.coffee +2 -1
  33. data/app/assets/javascripts/locomotive/views/editable_elements/edit_all_view.js.coffee +1 -1
  34. data/app/assets/javascripts/locomotive/views/editable_elements/file_view.js.coffee +2 -2
  35. data/app/assets/javascripts/locomotive/views/inline_editor/application_view.js.coffee +36 -15
  36. data/app/assets/javascripts/locomotive/views/inline_editor/toolbar_view.js.coffee +30 -14
  37. data/app/assets/javascripts/locomotive/views/pages/_form_view.js.coffee +24 -11
  38. data/app/assets/javascripts/locomotive/views/pages/edit_view.js.coffee +1 -1
  39. data/app/assets/javascripts/locomotive/views/pages/list_view.js.coffee +2 -2
  40. data/app/assets/javascripts/locomotive/views/shared/fields/file_view.js.coffee +5 -4
  41. data/app/assets/javascripts/locomotive/views/shared/fields/has_many_view.js.coffee +2 -1
  42. data/app/assets/javascripts/locomotive/views/shared/fields/many_to_many_view.js.coffee +1 -1
  43. data/app/assets/javascripts/locomotive/views/shared/fields/select_view.js.coffee +6 -2
  44. data/app/assets/javascripts/locomotive/views/shared/form_view.js.coffee +17 -12
  45. data/app/assets/javascripts/locomotive/views/shared/list_item_view.js.coffee +1 -1
  46. data/app/assets/javascripts/locomotive/views/sites/membership_entry_view.js.coffee +2 -1
  47. data/app/assets/javascripts/locomotive/views/snippets/_form_view.js.coffee +19 -3
  48. data/app/assets/javascripts/locomotive/views/theme_assets/index_view.js.coffee +8 -1
  49. data/app/assets/javascripts/locomotive/views/translations/_form_view.js.coffee +20 -0
  50. data/app/assets/javascripts/locomotive/views/translations/edit_view.js.coffee +6 -0
  51. data/app/assets/javascripts/locomotive/views/translations/new_view.js.coffee +8 -0
  52. data/app/assets/javascripts/tinymce/plugins/jqueryinlinepopups/editor_plugin.js +2 -2
  53. data/app/assets/javascripts/tinymce/plugins/locomotive_media/editor_plugin.js +1 -0
  54. data/app/assets/javascripts/tinymce/plugins/locomotive_media/langs/en.js +1 -0
  55. data/app/assets/javascripts/tinymce/plugins/locomotive_media/langs/fr.js +1 -0
  56. data/app/assets/javascripts/tinymce/plugins/locomotive_media/langs/ru.js +1 -0
  57. data/app/assets/stylesheets/locomotive.css +2 -1
  58. data/app/assets/stylesheets/locomotive/backoffice/_box.css.scss +3 -13
  59. data/app/assets/stylesheets/locomotive/backoffice/_buttons.css.scss +39 -16
  60. data/app/assets/stylesheets/locomotive/backoffice/application.css.scss +45 -56
  61. data/app/assets/stylesheets/locomotive/backoffice/codemirror_changes.css.scss +2 -0
  62. data/app/assets/stylesheets/locomotive/backoffice/content_assets.css.scss +5 -8
  63. data/app/assets/stylesheets/locomotive/backoffice/datepicker.css.scss +23 -9
  64. data/app/assets/stylesheets/locomotive/backoffice/formtastic_changes.css.scss +36 -32
  65. data/app/assets/stylesheets/locomotive/backoffice/layout.css.scss +2 -1
  66. data/app/assets/stylesheets/locomotive/backoffice/menu/_colors.css.scss +2 -1
  67. data/app/assets/stylesheets/locomotive/backoffice/menu/default.css.scss +4 -39
  68. data/app/assets/stylesheets/locomotive/backoffice/menu/main.css.scss +1 -1
  69. data/app/assets/stylesheets/locomotive/backoffice/menu/sub.css.scss +28 -36
  70. data/app/assets/stylesheets/locomotive/inline_editor.css +2 -1
  71. data/app/assets/stylesheets/locomotive/inline_editor/_buttons.css.scss +1 -11
  72. data/app/assets/stylesheets/locomotive/inline_editor/toolbar.css.scss +3 -4
  73. data/app/assets/stylesheets/locomotive/not_logged_in.css +1 -0
  74. data/app/assets/stylesheets/locomotive/not_logged_in/layout.css.scss +17 -8
  75. data/app/assets/stylesheets/locomotive/shared/_helpers.css.scss +21 -41
  76. data/app/assets/stylesheets/locomotive/shared/common.css.scss +2 -1
  77. data/app/assets/stylesheets/locomotive/shared/content_locale_picker.css.scss +11 -13
  78. data/app/cells/locomotive/content_locale_picker/show.html.haml +2 -2
  79. data/app/cells/locomotive/content_locale_picker_cell.rb +6 -2
  80. data/app/cells/locomotive/global_actions_cell.rb +1 -1
  81. data/app/cells/locomotive/main_menu/show.html.haml +1 -1
  82. data/app/cells/locomotive/main_menu_cell.rb +2 -2
  83. data/app/cells/locomotive/settings_menu_cell.rb +1 -0
  84. data/app/controllers/locomotive/api/accounts_controller.rb +70 -0
  85. data/app/controllers/locomotive/api/base_controller.rb +8 -0
  86. data/app/controllers/locomotive/api/content_assets_controller.rb +7 -9
  87. data/app/controllers/locomotive/api/content_entries_controller.rb +18 -14
  88. data/app/controllers/locomotive/api/content_types_controller.rb +56 -9
  89. data/app/controllers/locomotive/api/current_site_controller.rb +47 -1
  90. data/app/controllers/locomotive/api/documentation_controller.rb +15 -0
  91. data/app/controllers/locomotive/api/memberships_controller.rb +49 -14
  92. data/app/controllers/locomotive/api/my_account_controller.rb +19 -2
  93. data/app/controllers/locomotive/api/pages_controller.rb +56 -9
  94. data/app/controllers/locomotive/api/sites_controller.rb +62 -14
  95. data/app/controllers/locomotive/api/snippets_controller.rb +56 -9
  96. data/app/controllers/locomotive/api/theme_assets_controller.rb +7 -9
  97. data/app/controllers/locomotive/api/tokens_controller.rb +32 -5
  98. data/app/controllers/locomotive/api/translations_controller.rb +33 -0
  99. data/app/controllers/locomotive/public/base_controller.rb +1 -1
  100. data/app/controllers/locomotive/public/content_entries_controller.rb +13 -12
  101. data/app/controllers/locomotive/public/pages_controller.rb +3 -1
  102. data/app/controllers/locomotive/public/robots_controller.rb +2 -2
  103. data/app/controllers/locomotive/public/sitemaps_controller.rb +2 -2
  104. data/app/controllers/locomotive/sites_controller.rb +2 -2
  105. data/app/controllers/locomotive/snippets_controller.rb +6 -1
  106. data/app/controllers/locomotive/translations_controller.rb +39 -0
  107. data/app/helpers/locomotive/accounts_helper.rb +1 -1
  108. data/app/helpers/locomotive/base_helper.rb +68 -2
  109. data/app/helpers/locomotive/content_entries_helper.rb +27 -1
  110. data/app/helpers/locomotive/custom_fields_helper.rb +2 -2
  111. data/app/helpers/locomotive/pages_helper.rb +30 -2
  112. data/app/helpers/locomotive/snippets_helper.rb +20 -0
  113. data/app/helpers/locomotive/translations_helper.rb +19 -0
  114. data/app/inputs/locomotive/code_input.rb +29 -7
  115. data/app/inputs/locomotive/file_input.rb +5 -1
  116. data/app/inputs/locomotive/locale_input.rb +3 -2
  117. data/app/inputs/locomotive/locales_input.rb +3 -2
  118. data/app/mailers/locomotive/notifications.rb +7 -1
  119. data/app/models/locomotive/ability.rb +9 -3
  120. data/app/models/locomotive/account.rb +11 -5
  121. data/app/models/locomotive/content_asset.rb +2 -7
  122. data/app/models/locomotive/content_entry.rb +26 -20
  123. data/app/models/locomotive/content_type.rb +56 -28
  124. data/app/models/locomotive/editable_control.rb +0 -4
  125. data/app/models/locomotive/editable_element.rb +15 -4
  126. data/app/models/locomotive/editable_file.rb +11 -5
  127. data/app/models/locomotive/editable_long_text.rb +1 -7
  128. data/app/models/locomotive/editable_short_text.rb +12 -3
  129. data/app/models/locomotive/extensions/content_type/default_values.rb +2 -4
  130. data/app/models/locomotive/extensions/page/editable_elements.rb +22 -3
  131. data/app/models/locomotive/extensions/page/listed.rb +1 -1
  132. data/app/models/locomotive/extensions/page/parse.rb +18 -12
  133. data/app/models/locomotive/extensions/page/redirect.rb +6 -4
  134. data/app/models/locomotive/extensions/page/render.rb +1 -1
  135. data/app/models/locomotive/extensions/page/templatized.rb +48 -19
  136. data/app/models/locomotive/extensions/page/tree.rb +14 -2
  137. data/app/models/locomotive/extensions/site/locales.rb +11 -3
  138. data/app/models/locomotive/extensions/site/subdomain_domains.rb +7 -2
  139. data/app/models/locomotive/membership.rb +0 -4
  140. data/app/models/locomotive/page.rb +5 -13
  141. data/app/models/locomotive/site.rb +16 -23
  142. data/app/models/locomotive/snippet.rb +1 -9
  143. data/app/models/locomotive/theme_asset.rb +24 -28
  144. data/app/models/locomotive/translation.rb +35 -0
  145. data/app/presenters/locomotive/account_presenter.rb +12 -4
  146. data/app/presenters/locomotive/base_presenter.rb +122 -20
  147. data/app/presenters/locomotive/content_asset_presenter.rb +22 -11
  148. data/app/presenters/locomotive/content_entry_presenter.rb +151 -36
  149. data/app/presenters/locomotive/content_field_presenter.rb +76 -0
  150. data/app/presenters/locomotive/content_type_presenter.rb +66 -8
  151. data/app/presenters/locomotive/editable_control_presenter.rb +9 -8
  152. data/app/presenters/locomotive/editable_element_presenter.rb +26 -7
  153. data/app/presenters/locomotive/editable_file_presenter.rb +26 -5
  154. data/app/presenters/locomotive/editable_short_text_presenter.rb +21 -4
  155. data/app/presenters/locomotive/membership_presenter.rb +14 -16
  156. data/app/presenters/locomotive/page_presenter.rb +60 -14
  157. data/app/presenters/locomotive/site_presenter.rb +23 -8
  158. data/app/presenters/locomotive/snippet_presenter.rb +10 -8
  159. data/app/presenters/locomotive/theme_asset_presenter.rb +32 -15
  160. data/app/presenters/locomotive/translation_presenter.rb +9 -0
  161. data/app/uploaders/locomotive/editable_file_uploader.rb +4 -0
  162. data/app/views/locomotive/content_assets/_asset.html.haml +1 -1
  163. data/app/views/locomotive/content_assets/_picker.html.haml +1 -1
  164. data/app/views/locomotive/content_entries/_form.html.haml +2 -2
  165. data/app/views/locomotive/content_entries/_list.html.haml +1 -2
  166. data/app/views/locomotive/content_entries/edit.html.haml +6 -3
  167. data/app/views/locomotive/content_entries/index.html.haml +2 -2
  168. data/app/views/locomotive/content_entries/new.html.haml +5 -2
  169. data/app/views/locomotive/content_types/edit.html.haml +2 -2
  170. data/app/views/locomotive/current_site/edit.html.haml +1 -1
  171. data/app/views/locomotive/custom_fields/_form.html.haml +4 -4
  172. data/app/views/locomotive/custom_fields/_select_templates.html.haml +2 -2
  173. data/app/views/locomotive/custom_fields/types/_has_many.html.haml +6 -34
  174. data/app/views/locomotive/custom_fields/types/_has_many_form.html.haml +31 -0
  175. data/app/views/locomotive/custom_fields/types/_many_to_many.html.haml +2 -3
  176. data/app/views/locomotive/layouts/not_logged_in.html.haml +1 -1
  177. data/app/views/locomotive/my_account/edit.html.haml +2 -2
  178. data/app/views/locomotive/pages/_form.html.haml +8 -4
  179. data/app/views/locomotive/pages/_page.html.haml +7 -1
  180. data/app/views/locomotive/pages/edit.html.haml +1 -1
  181. data/app/views/locomotive/pages/index.html.haml +1 -1
  182. data/app/views/locomotive/public/pages/show_toolbar.html.haml +2 -3
  183. data/app/views/locomotive/public/sitemaps/show.xml.builder +4 -4
  184. data/app/views/locomotive/shared/_head.html.haml +3 -2
  185. data/app/views/locomotive/shared/_locale_picker_link.html.haml +3 -4
  186. data/app/views/locomotive/shared/actions/_contents.html.haml +3 -1
  187. data/app/views/locomotive/shared/menu/_contents.html.haml +14 -3
  188. data/app/views/locomotive/sites/_domains.html.haml +1 -1
  189. data/app/views/locomotive/sites/_form.html.haml +5 -5
  190. data/app/views/locomotive/sites/_memberships.html.haml +4 -2
  191. data/app/views/locomotive/snippets/_form.html.haml +2 -2
  192. data/app/views/locomotive/snippets/_snippet.html.haml +1 -1
  193. data/app/views/locomotive/snippets/edit.html.haml +1 -1
  194. data/app/views/locomotive/theme_assets/_asset.html.haml +1 -1
  195. data/app/views/locomotive/theme_assets/_form.html.haml +6 -2
  196. data/app/views/locomotive/theme_assets/edit.html.haml +1 -1
  197. data/app/views/locomotive/theme_assets/index.html.haml +15 -11
  198. data/app/views/locomotive/translations/_form.html.haml +12 -0
  199. data/app/views/locomotive/translations/edit.html.haml +15 -0
  200. data/app/views/locomotive/translations/index.html.haml +28 -0
  201. data/app/views/locomotive/translations/new.html.haml +12 -0
  202. data/config/initializers/deny_phantomjs_ttf.rb +29 -0
  203. data/config/locales/admin_ui.de.yml +1 -0
  204. data/config/locales/admin_ui.en.yml +22 -2
  205. data/config/locales/admin_ui.es.yml +1 -0
  206. data/config/locales/admin_ui.et.yml +14 -0
  207. data/config/locales/admin_ui.fr.yml +36 -14
  208. data/config/locales/admin_ui.it.yml +1 -0
  209. data/config/locales/admin_ui.nb.yml +1 -0
  210. data/config/locales/admin_ui.pl.yml +319 -0
  211. data/config/locales/admin_ui.pt-BR.yml +38 -6
  212. data/config/locales/admin_ui.ru.yml +1 -0
  213. data/config/locales/carrierwave.pl.yml +4 -0
  214. data/config/locales/default.fr.yml +3 -1
  215. data/config/locales/default.pl.yml +317 -0
  216. data/config/locales/devise.de.yml +1 -0
  217. data/config/locales/devise.en.yml +1 -0
  218. data/config/locales/devise.es.yml +1 -0
  219. data/config/locales/devise.et.yml +1 -0
  220. data/config/locales/devise.fr.yml +3 -2
  221. data/config/locales/devise.it.yml +1 -0
  222. data/config/locales/devise.nb.yml +1 -0
  223. data/config/locales/devise.nl.yml +1 -0
  224. data/config/locales/devise.pl.yml +64 -0
  225. data/config/locales/devise.pt-BR.yml +1 -0
  226. data/config/locales/devise.ru.yml +1 -0
  227. data/config/locales/flash.en.yml +9 -0
  228. data/config/locales/flash.pl.yml +106 -0
  229. data/config/locales/formtastic.en.yml +2 -0
  230. data/config/locales/formtastic.fr.yml +3 -1
  231. data/config/locales/formtastic.pl.yml +105 -0
  232. data/config/routes.rb +46 -38
  233. data/features/api/accounts.feature +25 -0
  234. data/features/api/authentication.feature +19 -0
  235. data/features/api/authorization/accounts.feature +125 -0
  236. data/features/api/authorization/content_assets.feature +147 -0
  237. data/features/api/authorization/content_entries.feature +202 -0
  238. data/features/api/authorization/content_types.feature +237 -0
  239. data/features/api/authorization/current_site.feature +30 -0
  240. data/features/api/authorization/memberships.feature +225 -0
  241. data/features/api/authorization/pages.feature +187 -0
  242. data/features/api/authorization/sites.feature +213 -0
  243. data/features/api/authorization/snippets.feature +179 -0
  244. data/features/api/authorization/theme_assets.feature +185 -0
  245. data/features/api/authorization/translations.feature +253 -0
  246. data/features/api/content_entries.feature +169 -0
  247. data/features/api/content_types.feature +156 -0
  248. data/features/api/editable_elements.feature +190 -0
  249. data/features/api/entries_custom_fields.feature +150 -0
  250. data/features/api/pages.feature +72 -0
  251. data/features/backoffice/authorization/account_settings.feature +28 -0
  252. data/features/backoffice/authorization/content_type.feature +35 -0
  253. data/features/backoffice/authorization/current_site.feature +53 -0
  254. data/features/backoffice/authorization/inline_front_end_editing.feature +46 -0
  255. data/features/backoffice/authorization/pages.feature +95 -0
  256. data/features/backoffice/authorization/theme_assets.feature +50 -0
  257. data/features/backoffice/content_types/edit.feature +20 -0
  258. data/features/backoffice/content_types/has_many.feature +60 -0
  259. data/features/backoffice/content_types/many_to_many.feature +42 -0
  260. data/features/backoffice/contents.feature +70 -0
  261. data/features/backoffice/editable_elements.feature +22 -0
  262. data/features/backoffice/login.feature +33 -0
  263. data/features/backoffice/mounting.feature +13 -0
  264. data/features/backoffice/pages.feature +35 -0
  265. data/features/backoffice/site.feature +45 -0
  266. data/features/backoffice/snippets.feature +39 -0
  267. data/features/backoffice/theme_assets.feature +72 -0
  268. data/features/backoffice/translations.feature +44 -0
  269. data/features/public/basic.feature +30 -0
  270. data/features/public/contact_form.feature +78 -0
  271. data/features/public/content_entries.feature +56 -0
  272. data/features/public/editable_elements.feature +138 -0
  273. data/features/public/has_many.feature +8 -0
  274. data/features/public/inheritance.feature +157 -0
  275. data/features/public/inline_front_end_editing.feature +26 -0
  276. data/features/public/many_to_many.feature +64 -0
  277. data/features/public/pagination.feature +8 -0
  278. data/features/public/robots.feature +22 -0
  279. data/features/public/sitemap.feature +74 -0
  280. data/features/public/snippets.feature +21 -0
  281. data/features/public/tablerow.feature +42 -0
  282. data/features/step_definitions/api_steps.rb +176 -0
  283. data/features/step_definitions/backoffice/mounting_steps.rb +22 -0
  284. data/features/step_definitions/backoffice_steps.rb +43 -0
  285. data/features/step_definitions/content_assets_steps.rb +12 -0
  286. data/features/step_definitions/content_types_steps.rb +106 -0
  287. data/features/step_definitions/current_site_steps.rb +43 -0
  288. data/features/step_definitions/editable_elements_steps.rb +24 -0
  289. data/features/step_definitions/membership_steps.rb +19 -0
  290. data/features/step_definitions/more_web_steps.rb +96 -0
  291. data/features/step_definitions/page_steps.rb +110 -0
  292. data/features/step_definitions/pagination_steps.rb +35 -0
  293. data/features/step_definitions/pickle_steps.rb +100 -0
  294. data/features/step_definitions/relationships_steps.rb +107 -0
  295. data/features/step_definitions/site_steps.rb +100 -0
  296. data/features/step_definitions/snippet_steps.rb +37 -0
  297. data/features/step_definitions/theme_asset_steps.rb +64 -0
  298. data/features/step_definitions/translation_steps.rb +7 -0
  299. data/features/step_definitions/web_steps.rb +197 -0
  300. data/features/step_definitions/within_steps.rb +14 -0
  301. data/features/support/cleaner.rb +4 -0
  302. data/features/support/env.rb +82 -0
  303. data/features/support/factory_girl.rb +2 -0
  304. data/features/support/http.rb +22 -0
  305. data/features/support/paths.rb +62 -0
  306. data/features/support/pickle.rb +24 -0
  307. data/features/support/selectors.rb +57 -0
  308. data/lib/generators/locomotive/install/install_generator.rb +5 -0
  309. data/lib/generators/locomotive/install/templates/dragonfly.rb +2 -3
  310. data/lib/generators/locomotive/install/templates/locomotive.rb +2 -2
  311. data/lib/locomotive.rb +13 -2
  312. data/lib/locomotive/action_controller/locale_helpers.rb +1 -1
  313. data/lib/locomotive/action_controller/public_responder.rb +17 -1
  314. data/lib/locomotive/action_controller/responder.rb +3 -2
  315. data/lib/locomotive/action_controller/url_helpers.rb +1 -1
  316. data/lib/locomotive/cancan.rb +22 -0
  317. data/lib/locomotive/carrierwave.rb +2 -0
  318. data/lib/locomotive/carrierwave/asset.rb +17 -7
  319. data/lib/locomotive/configuration.rb +6 -2
  320. data/lib/locomotive/core_ext.rb +5 -4
  321. data/lib/locomotive/custom_fields.rb +11 -0
  322. data/lib/locomotive/dependencies.rb +3 -3
  323. data/lib/locomotive/dragonfly.rb +1 -1
  324. data/lib/locomotive/engine.rb +24 -8
  325. data/lib/locomotive/formtastic.rb +26 -1
  326. data/lib/locomotive/liquid/drops/current_user.rb +2 -2
  327. data/lib/locomotive/liquid/drops/page.rb +5 -1
  328. data/lib/locomotive/liquid/filters/date.rb +19 -0
  329. data/lib/locomotive/liquid/filters/html.rb +7 -7
  330. data/lib/locomotive/liquid/filters/misc.rb +5 -7
  331. data/lib/locomotive/liquid/filters/text.rb +11 -0
  332. data/lib/locomotive/liquid/filters/translate.rb +16 -0
  333. data/lib/locomotive/liquid/patches.rb +21 -0
  334. data/lib/locomotive/liquid/tags/consume.rb +13 -2
  335. data/lib/locomotive/liquid/tags/csrf.rb +3 -3
  336. data/lib/locomotive/liquid/tags/editable/base.rb +11 -4
  337. data/lib/locomotive/liquid/tags/inline_editor.rb +5 -2
  338. data/lib/locomotive/liquid/tags/locale_switcher.rb +12 -4
  339. data/lib/locomotive/liquid/tags/nav.rb +1 -1
  340. data/lib/locomotive/liquid/tags/paginate.rb +4 -1
  341. data/lib/locomotive/liquid/tags/seo.rb +2 -2
  342. data/lib/locomotive/liquid/tags/snippet.rb +10 -2
  343. data/lib/locomotive/liquid/tags/with_scope.rb +11 -0
  344. data/lib/locomotive/middlewares.rb +2 -1
  345. data/lib/locomotive/middlewares/fonts.rb +2 -2
  346. data/lib/locomotive/middlewares/permalink.rb +22 -0
  347. data/lib/locomotive/misc/api_documentation.rb +114 -0
  348. data/lib/locomotive/misc/api_documentation/bootstrap.html.haml +196 -0
  349. data/lib/locomotive/mongoid.rb +2 -1
  350. data/lib/locomotive/mongoid/document.rb +3 -2
  351. data/lib/locomotive/mongoid/liquid.rb +39 -0
  352. data/lib/locomotive/mongoid/presenter.rb +73 -0
  353. data/lib/locomotive/presentable.rb +216 -0
  354. data/lib/locomotive/render.rb +76 -24
  355. data/lib/locomotive/routing/site_dispatcher.rb +15 -4
  356. data/lib/locomotive/version.rb +1 -1
  357. data/lib/tasks/development.rake +29 -0
  358. data/lib/tasks/locomotive.rake +58 -7
  359. data/spec/cells/locomotive/global_actions_spec.rb +98 -0
  360. data/spec/cells/locomotive/main_menu_cell_spec.rb +99 -0
  361. data/spec/cells/locomotive/settings_menu_cell_spec.rb +107 -0
  362. data/spec/dummy/Rakefile +7 -0
  363. data/spec/dummy/app/assets/javascripts/application.js +9 -0
  364. data/spec/dummy/app/assets/javascripts/locomotive_misc.js.coffee +2 -0
  365. data/spec/dummy/app/assets/stylesheets/application.css +7 -0
  366. data/spec/dummy/app/cells/locomotive/main_menu_cell.rb +9 -0
  367. data/spec/dummy/app/controllers/application_controller.rb +3 -0
  368. data/spec/dummy/app/controllers/foo_controller.rb +10 -0
  369. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  370. data/spec/dummy/app/models/foo.rb +7 -0
  371. data/spec/dummy/app/views/foo/index.html.haml +1 -0
  372. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  373. data/spec/dummy/app/views/locomotive/shared/_main_app_head.html.haml +1 -0
  374. data/spec/dummy/config.ru +4 -0
  375. data/spec/dummy/config/application.rb +57 -0
  376. data/spec/dummy/config/boot.rb +20 -0
  377. data/spec/dummy/config/environment.rb +5 -0
  378. data/spec/dummy/config/environments/development.rb +38 -0
  379. data/spec/dummy/config/environments/production.rb +60 -0
  380. data/spec/dummy/config/environments/test.rb +39 -0
  381. data/spec/dummy/config/initializers/carrierwave.rb +8 -0
  382. data/spec/dummy/config/initializers/devise.rb +190 -0
  383. data/spec/dummy/config/initializers/dragonfly.rb +21 -0
  384. data/spec/dummy/config/initializers/formtastic.rb +5 -0
  385. data/spec/dummy/config/initializers/haml.rb +2 -0
  386. data/spec/dummy/config/initializers/locomotive.rb +80 -0
  387. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  388. data/spec/dummy/config/initializers/session_store.rb +8 -0
  389. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  390. data/spec/dummy/config/locales/en.yml +10 -0
  391. data/spec/dummy/config/locales/fr.yml +10 -0
  392. data/spec/dummy/config/mongoid.yml +23 -0
  393. data/spec/dummy/config/routes.rb +9 -0
  394. data/spec/dummy/public/404.html +26 -0
  395. data/spec/dummy/public/422.html +26 -0
  396. data/spec/dummy/public/500.html +26 -0
  397. data/spec/dummy/script/rails +6 -0
  398. data/spec/fixtures/assets/5k.png +0 -0
  399. data/spec/fixtures/assets/5k_2.png +0 -0
  400. data/spec/fixtures/assets/application.js +1 -0
  401. data/spec/fixtures/assets/main.css +1 -0
  402. data/spec/fixtures/assets/wrong.txt +1 -0
  403. data/spec/fixtures/images/logo1.jpg +0 -0
  404. data/spec/fixtures/images/logo2.jpg +0 -0
  405. data/spec/lib/core_ext_spec.rb +37 -0
  406. data/spec/lib/locomotive/configuration_spec.rb +26 -0
  407. data/spec/lib/locomotive/httparty/webservice_spec.rb +33 -0
  408. data/spec/lib/locomotive/liquid/drops/content_entry_spec.rb +46 -0
  409. data/spec/lib/locomotive/liquid/drops/current_user.rb +63 -0
  410. data/spec/lib/locomotive/liquid/drops/page_spec.rb +143 -0
  411. data/spec/lib/locomotive/liquid/drops/site_spec.rb +32 -0
  412. data/spec/lib/locomotive/liquid/filters/date_spec.rb +68 -0
  413. data/spec/lib/locomotive/liquid/filters/html_spec.rb +224 -0
  414. data/spec/lib/locomotive/liquid/filters/misc_spec.rb +56 -0
  415. data/spec/lib/locomotive/liquid/filters/resize_spec.rb +61 -0
  416. data/spec/lib/locomotive/liquid/filters/text_spec.rb +32 -0
  417. data/spec/lib/locomotive/liquid/filters/translate_spec.rb +50 -0
  418. data/spec/lib/locomotive/liquid/tags/consume_spec.rb +52 -0
  419. data/spec/lib/locomotive/liquid/tags/csrf_spec.rb +26 -0
  420. data/spec/lib/locomotive/liquid/tags/editable/short_text_spec.rb +46 -0
  421. data/spec/lib/locomotive/liquid/tags/extends_spec.rb +58 -0
  422. data/spec/lib/locomotive/liquid/tags/locale_switcher_spec.rb +47 -0
  423. data/spec/lib/locomotive/liquid/tags/nav_spec.rb +142 -0
  424. data/spec/lib/locomotive/liquid/tags/paginate_spec.rb +139 -0
  425. data/spec/lib/locomotive/liquid/tags/seo_spec.rb +135 -0
  426. data/spec/lib/locomotive/liquid/tags/with_scope_spec.rb +49 -0
  427. data/spec/lib/locomotive/presentable_spec.rb +184 -0
  428. data/spec/lib/locomotive/render_spec.rb +209 -0
  429. data/spec/lib/locomotive/routing/site_dispatcher_spec.rb +258 -0
  430. data/spec/mailers/locomotive/notifications_spec.rb +57 -0
  431. data/spec/models/locomotive/ability_spec.rb +132 -0
  432. data/spec/models/locomotive/account_spec.rb +87 -0
  433. data/spec/models/locomotive/content_asset_spec.rb +46 -0
  434. data/spec/models/locomotive/content_entry_spec.rb +269 -0
  435. data/spec/models/locomotive/content_type_spec.rb +391 -0
  436. data/spec/models/locomotive/editable_control_spec.rb +70 -0
  437. data/spec/models/locomotive/editable_file_spec.rb +95 -0
  438. data/spec/models/locomotive/editable_long_text_spec.rb +50 -0
  439. data/spec/models/locomotive/editable_short_text_spec.rb +174 -0
  440. data/spec/models/locomotive/extensions/page/editable_elements_spec.rb +67 -0
  441. data/spec/models/locomotive/extensions/page/redirect_spec.rb +28 -0
  442. data/spec/models/locomotive/extensions/page/render_spec.rb +88 -0
  443. data/spec/models/locomotive/extensions/site/locales_spec.rb +52 -0
  444. data/spec/models/locomotive/extensions/site/subdomain_domains_spec.rb +25 -0
  445. data/spec/models/locomotive/membership_spec.rb +61 -0
  446. data/spec/models/locomotive/page_spec.rb +427 -0
  447. data/spec/models/locomotive/site_spec.rb +176 -0
  448. data/spec/models/locomotive/snippet_spec.rb +77 -0
  449. data/spec/models/locomotive/theme_asset_spec.rb +128 -0
  450. data/spec/requests/seo_trailing_slash_spec.rb +25 -0
  451. data/spec/support/carrierwave.rb +50 -0
  452. data/spec/support/cells.rb +3 -0
  453. data/spec/support/controller.rb +32 -0
  454. data/spec/support/factories.rb +156 -0
  455. data/spec/support/locomotive.rb +37 -0
  456. data/spec/support/matchers.rb +127 -0
  457. data/spec/support/middlewares.rb +0 -0
  458. data/vendor/assets/fonts/locomotive/fontawesome-webfont.eot +0 -0
  459. data/vendor/assets/fonts/locomotive/fontawesome-webfont.svg +255 -0
  460. data/vendor/assets/fonts/locomotive/fontawesome-webfont.ttf +0 -0
  461. data/vendor/assets/fonts/locomotive/fontawesome-webfont.woff +0 -0
  462. data/vendor/assets/javascripts/locomotive/backbone.modelbinding.js +34 -9
  463. data/vendor/assets/javascripts/locomotive/backbone.sync.js +2 -2
  464. data/vendor/assets/javascripts/locomotive/form_submit_notification.js +2 -2
  465. data/vendor/assets/javascripts/locomotive/growl.js +10 -10
  466. data/vendor/assets/javascripts/locomotive/menu_toggler.js +8 -4
  467. data/vendor/assets/javascripts/locomotive/slugify.js +21 -5
  468. data/vendor/assets/stylesheets/locomotive/font-awesome.scss +329 -0
  469. data/vendor/assets/stylesheets/locomotive/jquery/{ui.css → ui.css.scss} +19 -19
  470. metadata +538 -195
  471. data/app/assets/images/locomotive/background/light.png +0 -0
  472. data/app/assets/images/locomotive/datepicker/ui-widget-left-icon.png +0 -0
  473. data/app/assets/images/locomotive/datepicker/ui-widget-right-icon.png +0 -0
  474. data/app/assets/images/locomotive/form/folded-arrow-off.png +0 -0
  475. data/app/assets/images/locomotive/form/folded-arrow-on.png +0 -0
  476. data/app/assets/images/locomotive/form/icons/drag.png +0 -0
  477. data/app/assets/images/locomotive/form/icons/edit.png +0 -0
  478. data/app/assets/images/locomotive/form/icons/error.png +0 -0
  479. data/app/assets/images/locomotive/form/icons/spinner.gif +0 -0
  480. data/app/assets/images/locomotive/form/icons/trash.png +0 -0
  481. data/app/assets/images/locomotive/form/pen.png +0 -0
  482. data/app/assets/images/locomotive/form/spinner.gif +0 -0
  483. data/app/assets/images/locomotive/icons/actions.png +0 -0
  484. data/app/assets/images/locomotive/icons/asset_add.png +0 -0
  485. data/app/assets/images/locomotive/icons/asset_switch.png +0 -0
  486. data/app/assets/images/locomotive/icons/membership_edit.png +0 -0
  487. data/app/assets/images/locomotive/icons/membership_lock.png +0 -0
  488. data/app/assets/images/locomotive/inline_editor/back.png +0 -0
  489. data/app/assets/images/locomotive/list/icons/cross.png +0 -0
  490. data/app/assets/images/locomotive/list/icons/drag.png +0 -0
  491. data/app/assets/images/locomotive/list/icons/move.png +0 -0
  492. data/app/assets/images/locomotive/list/icons/move_off.png +0 -0
  493. data/app/assets/images/locomotive/list/icons/node_closed.png +0 -0
  494. data/app/assets/images/locomotive/list/icons/node_open.png +0 -0
  495. data/app/assets/images/locomotive/list/icons/pencil.png +0 -0
  496. data/app/assets/images/locomotive/list/icons/pencil_off.png +0 -0
  497. data/app/assets/images/locomotive/list/icons/popup.png +0 -0
  498. data/app/assets/images/locomotive/list/icons/popup_off.png +0 -0
  499. data/app/assets/images/locomotive/list/icons/redirect.png +0 -0
  500. data/app/assets/images/locomotive/list/icons/states.png +0 -0
  501. data/app/assets/images/locomotive/list/icons/template.png +0 -0
  502. data/app/assets/images/locomotive/list/icons/toggle.png +0 -0
  503. data/app/assets/images/locomotive/list/icons/toggle_off.png +0 -0
  504. data/app/assets/images/locomotive/list/icons/trash.png +0 -0
  505. data/app/assets/images/locomotive/list/icons/trash_off.png +0 -0
  506. data/app/assets/images/locomotive/menu/icons.png +0 -0
  507. data/app/assets/images/locomotive/menu/popup/add.png +0 -0
  508. data/app/assets/images/locomotive/menu/popup/bottom-right-corner.png +0 -0
  509. data/app/assets/images/locomotive/menu/popup/bullet.png +0 -0
  510. data/app/assets/images/locomotive/plugins/selectmenu/arrow.png +0 -0
  511. data/app/assets/images/locomotive/plugins/selectmenu/background.png +0 -0
  512. data/app/assets/images/locomotive/plugins/sites_picker_entry_bg.png +0 -0
  513. data/app/assets/images/locomotive/simple/light_bg.png +0 -0
  514. data/app/assets/images/locomotive/simple/wrapper_bg.png +0 -0
  515. data/app/assets/stylesheets/locomotive/backoffice/menu/_icons.css.scss +0 -30
  516. data/app/uploaders/locomotive/theme_uploader.rb +0 -19
  517. data/lib/locomotive/mongoid/model_extensions.rb +0 -1
  518. data/lib/tasks/cucumber.rake +0 -66
  519. data/vendor/assets/stylesheets/locomotive/jquery.css +0 -3
@@ -0,0 +1,35 @@
1
+ class Locomotive::Translation
2
+
3
+ include Locomotive::Mongoid::Document
4
+
5
+ ## fields ##
6
+ field :key
7
+ field :values, type: Hash, default: {}
8
+
9
+ ## associations ##
10
+ belongs_to :site, class_name: 'Locomotive::Site'
11
+
12
+ ## validations ##
13
+ validates_uniqueness_of :key, scope: :site_id
14
+ validates_presence_of :site, :key
15
+
16
+ ## scopes ##
17
+ scope :ordered, :order_by => [[:key, :asc]]
18
+
19
+ ## callbacks ##
20
+ before_validation :underscore_key
21
+
22
+ ## methods ##
23
+
24
+ protected
25
+
26
+ # Make sure the translation key is underscored
27
+ # since it is the unique way to use it in a liquid template.
28
+ #
29
+ def underscore_key
30
+ if self.key
31
+ self.key = self.key.permalink.underscore
32
+ end
33
+ end
34
+
35
+ end
@@ -1,11 +1,19 @@
1
1
  module Locomotive
2
2
  class AccountPresenter < BasePresenter
3
3
 
4
- delegate :name, :email, :locale, :to => :source
4
+ ## properties ##
5
+ properties :name, :email, :locale
6
+ property :admin, only_getter: true
5
7
 
6
- def included_methods
7
- super + %w(name email locale)
8
+ with_options only_setter: true do |presenter|
9
+ presenter.properties :password, :password_confirmation
10
+ end
11
+
12
+ ## other getters / setters ##
13
+
14
+ def admin
15
+ self.__source.admin?
8
16
  end
9
17
 
10
18
  end
11
- end
19
+ end
@@ -1,46 +1,148 @@
1
1
  class Locomotive::BasePresenter
2
2
 
3
+ include Locomotive::Presentable
4
+
3
5
  include ActionView::Helpers::SanitizeHelper
4
6
  extend ActionView::Helpers::SanitizeHelper::ClassMethods
5
7
  include ActionView::Helpers::TextHelper
6
8
  include ActionView::Helpers::NumberHelper
7
9
 
8
- attr_reader :source, :options, :ability, :depth
10
+ ## default properties ##
11
+ with_options allow_nil: true do |presenter|
12
+ presenter.properties :id, :_id
13
+ end
14
+ properties :created_at, :updated_at, type: 'Date', only_getter: true
9
15
 
10
- delegate :created_at, :updated_at, :to => :source
16
+ ## utility accessors ##
17
+ attr_reader :__ability, :__depth
11
18
 
12
- def initialize(object, options = {})
13
- @source = object
14
- @options = options || {}
15
- @depth = options[:depth] || 0
16
- @ability = options[:ability]
19
+ # Set the ability object to check if we can or not
20
+ # get a property.
21
+ #
22
+ def after_initialize
23
+ @__depth = self.__options[:depth] || 0
24
+ @__ability = self.__options[:ability]
17
25
 
18
- if @options[:current_account] && @options[:current_site]
19
- @ability = Locomotive::Ability.new @options[:current_account], @options[:current_site]
26
+ if self.__options[:current_account] && self.__options[:current_site]
27
+ @__ability = Locomotive::Ability.new self.__options[:current_account], self.__options[:current_site]
20
28
  end
21
29
  end
22
30
 
23
- def id
24
- self.source.persisted? || self.source.embedded? ? self.source._id.to_s : nil
31
+ # Get the id of the source only if it has been persisted
32
+ # or if it's an embedded document.
33
+ #
34
+ # @return [ String ] The id of the source, nil if not persisted or not embedded.
35
+ #
36
+ def _id
37
+ self.__source.persisted? || self.__source.embedded? ? self.__source._id.to_s : nil
25
38
  end
26
39
 
27
- alias :_id :id
40
+ alias :id :_id
28
41
 
42
+ # Check if there is an ability object used for permissions.
43
+ #
44
+ # @return [ Boolean ] True if defined, false otherwise
45
+ #
29
46
  def ability?
30
- self.ability.present?
47
+ self.__ability.present?
48
+ end
49
+
50
+ # Shortcut to the site taken from the source.
51
+ #
52
+ # @return [ Object ] The site or nil if not found
53
+ #
54
+ def site
55
+ self.__source.try(:site)
56
+ end
57
+
58
+ # Shortcut to the errors of the source
59
+ #
60
+ # @return [ Hash ] The errors or an empty hash if no errors
61
+ #
62
+ def errors
63
+ self.__source.errors.messages.to_hash.stringify_keys
64
+ end
65
+
66
+ # Tell if we have to include errors or not
67
+ #
68
+ # @return [ Boolean ] True if the source has errors.
69
+ #
70
+ def include_errors?
71
+ !!self.__options[:include_errors]
72
+ end
73
+
74
+ # Tell if the presenter is aimed to be used in a html view
75
+ #
76
+ # @return [ Boolean ] True if used in a HTML view
77
+ #
78
+ def html_view?
79
+ !!self.__options[:html_view]
80
+ end
81
+
82
+ # Mimic format used by to_json
83
+ #
84
+ # @param [ String ] text The string representing the time
85
+ #
86
+ # @return [ Time ] The time or nil if the input is nil or empty
87
+ #
88
+ def formatted_time(text)
89
+ return nil if text.blank?
90
+ format = '%Y-%m-%dT%H:%M:%S%Z'
91
+ ::Time.strptime(text, format)
31
92
  end
32
93
 
33
- def included_methods
34
- %w(id _id created_at updated_at)
94
+ # Return the set of setters with their options.
95
+ #
96
+ # @param [ Hash ] options Some options: with_ids (add id and _id)
97
+ #
98
+ # @return [ Hash ] The setters
99
+ #
100
+ def self.setters_to_hash(options = {})
101
+ options = { without_ids: true }.merge(options)
102
+
103
+ {}.tap do |hash|
104
+ self.setters.each do |name|
105
+ next if %w(id _id).include?(name.to_s) && options[:without_ids]
106
+ hash[name] = self.getters_or_setters_to_hash(name)
107
+ end
108
+ end
35
109
  end
36
110
 
37
- def as_json(methods = nil)
38
- methods ||= self.included_methods
111
+ # Return the set of getters with their options.
112
+ #
113
+ # @return [ Hash ] The getters
114
+ #
115
+ def self.getters_to_hash
39
116
  {}.tap do |hash|
40
- methods.each do |meth|
41
- hash[meth] = self.send(meth.to_sym) rescue nil
117
+ self.getters.each do |name|
118
+ next if %w(_id screated_at updated_at).include?(name)
119
+ hash[name] = self.getters_or_setters_to_hash(name)
120
+ end
121
+
122
+ %w(created_at updated_at).each do |name|
123
+ hash[name] = self.getters_or_setters_to_hash(name)
42
124
  end
43
125
  end
44
126
  end
45
127
 
46
- end
128
+ def self.getters_or_setters_to_hash(name)
129
+ options = self.property_options[name] || {}
130
+
131
+ attributes = {
132
+ type: options[:type] || 'String',
133
+ required: options[:required].nil? ? true : options[:required],
134
+ alias_of: self.alias_of(name),
135
+ description: options[:description]
136
+ }
137
+
138
+ if options[:presenter]
139
+ attributes[:collection_of] = {
140
+ name: options[:presenter].to_s.demodulize.gsub(/Presenter$/, '').underscore,
141
+ params: options[:presenter].setters_to_hash
142
+ }
143
+ end
144
+
145
+ attributes
146
+ end
147
+
148
+ end
@@ -1,36 +1,47 @@
1
1
  module Locomotive
2
2
  class ContentAssetPresenter < BasePresenter
3
3
 
4
- delegate :content_type, :width, :height, :vignette_url, :to => :source
4
+ ## properties ##
5
+
6
+ properties :content_type, :width, :height
7
+
8
+ with_options only_getter: true do |presenter|
9
+ presenter.properties :filename, :full_filename, :short_name, :extname, :raw_size
10
+ presenter.properties :vignette_url, :url, :content_type_text
11
+ end
12
+
13
+ property :source, only_setter: true
14
+
15
+ ## custom getters / setters ##
5
16
 
6
17
  def full_filename
7
- self.source.source_filename
18
+ self.__source.source_filename
8
19
  end
9
20
 
10
21
  def filename
11
- truncate(self.source.source_filename, :length => 22)
22
+ truncate(self.__source.source_filename, length: 22)
12
23
  end
13
24
 
14
25
  def short_name
15
- truncate(self.source.name, :length => 15)
26
+ truncate(self.__source.name, length: 15)
16
27
  end
17
28
 
18
29
  def extname
19
- truncate(self.source.extname, :length => 3)
30
+ truncate(self.__source.extname, length: 3)
20
31
  end
21
32
 
22
33
  def content_type_text
23
- value = self.source.content_type.to_s == 'other' ? self.extname : self.source.content_type
34
+ value = self.__source.content_type.to_s == 'other' ? self.extname : self.__source.content_type
24
35
  value.blank? ? '?' : value
25
36
  end
26
37
 
27
- def url
28
- self.source.source.url
38
+ def raw_size
39
+ self.__source.size
29
40
  end
30
41
 
31
- def included_methods
32
- super + %w(full_filename filename short_name extname content_type content_type_text url vignette_url width height)
42
+ def url
43
+ self.__source.source.url
33
44
  end
34
45
 
35
46
  end
36
- end
47
+ end
@@ -1,60 +1,175 @@
1
1
  module Locomotive
2
2
  class ContentEntryPresenter < BasePresenter
3
3
 
4
- delegate :_label, :_slug, :_position, :translated_in, :seo_title,
5
- :meta_keywords, :meta_description, :select_custom_fields,
6
- :file_custom_fields, :has_many_custom_fields, :many_to_many_custom_fields, :to => :source
4
+ ## properties ##
7
5
 
8
- # Lists of all the attributes editable thru the html form for instance
9
- #
10
- # @returns [ List ] a list of attributes (string)
11
- #
12
- def safe_attributes
13
- self.source.custom_fields_safe_attributes + %w(_slug seo_title meta_keywords meta_description _destroy)
6
+ properties :_label, only_getter: true
7
+
8
+ property :_slug
9
+
10
+ property :_position, required: false
11
+
12
+ properties :seo_title, :meta_keywords, :meta_description, required: false
13
+
14
+ with_options only_getter: true do |presenter|
15
+ presenter.properties :content_type_slug, :translated_in
16
+
17
+ presenter.property :errors, if: Proc.new { include_errors? }
14
18
  end
15
19
 
16
- def filtered_custom_fields_methods
17
- self.source.custom_fields_methods do |rule|
18
- if self.source.is_a_custom_field_many_relationship?(rule['name'])
19
- # avoid circular dependencies, it should accept only one deep level
20
- self.depth == 0
21
- else
22
- true
23
- end
24
- end
20
+ with_options only_getter: true, if: Proc.new { html_view? } do |presenter|
21
+ presenter.properties :safe_attributes
22
+ presenter.properties :select_custom_fields, :file_custom_fields, :has_many_custom_fields, :many_to_many_custom_fields
25
23
  end
26
24
 
25
+ ## callbacks ##
26
+
27
+ set_callback :set_attributes, :after, :set_dynamic_attributes
28
+
29
+ ## other getters / setters ##
30
+
27
31
  def errors
28
- self.source.errors.to_hash.stringify_keys
32
+ super
29
33
  end
30
34
 
31
35
  def content_type_slug
32
- self.source.content_type.slug
36
+ self.__source.content_type.slug
33
37
  end
34
38
 
35
- def included_methods
36
- default_list = %w(_label _slug _position content_type_slug select_custom_fields file_custom_fields has_many_custom_fields many_to_many_custom_fields translated_in safe_attributes)
37
- default_list << 'errors' if !!self.options[:include_errors]
38
- super + self.filtered_custom_fields_methods + default_list
39
+ def safe_attributes
40
+ self.__source.custom_fields_safe_setters + %w(_slug seo_title meta_keywords meta_description)
39
41
  end
40
42
 
41
- def as_json(methods = nil)
42
- methods ||= self.included_methods
43
+ ## other methods ##
44
+
45
+ def as_json_with_custom_fields
46
+ as_json_without_custom_fields.merge(self.custom_fields_to_hash)
47
+ end
48
+
49
+ alias_method_chain :as_json, :custom_fields
50
+
51
+ def as_json_for_html_view
52
+ self.__options[:html_view] = true
53
+ as_json_without_custom_fields.merge(self.custom_fields_to_hash)
54
+ end
55
+
56
+ protected
57
+
58
+ # Build the hash representing the values of all
59
+ # the custom fields of the content entry.
60
+ # It also includes the ones from relationships fields
61
+ #
62
+ # @return [ Hash ] key the name of the custom field and value from the source
63
+ #
64
+ def custom_fields_to_hash
65
+ base = self.__source.custom_fields_basic_attributes
66
+ base.merge!(self.many_relationships_to_hash)
67
+ base.merge!(self.belongs_to_to_hash)
68
+ end
69
+
70
+ # Build the hash storing for each *belongs_to* type field the target entry.
71
+ #
72
+ # @return [ Hash ] The hash whose name is the name of the belongs_to field
73
+ #
74
+ def belongs_to_to_hash
43
75
  {}.tap do |hash|
44
- methods.each do |meth|
45
- hash[meth]= (if self.source.custom_fields_methods.include?(meth.to_s)
46
- if self.source.is_a_custom_field_many_relationship?(meth.to_s)
47
- # go deeper
48
- self.source.send(meth).ordered.map { |entry| entry.to_presenter(:depth => self.depth + 1) }
76
+ self.__source.belongs_to_custom_fields.each do |name, _|
77
+ if target = self.__source.send(name.to_sym)
78
+ if self.html_view?
79
+ hash["#{name}_id"] = target._id
49
80
  else
50
- self.source.send(meth) rescue nil
81
+ hash[name.to_s] = target._slug
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end
87
+
88
+ # Build the hash storing for each *many* type field
89
+ # the list of entries.
90
+ #
91
+ # @return [ Hash ] The hash whose name is the name of the relationhip
92
+ #
93
+ def many_relationships_to_hash
94
+ {}.tap do |hash|
95
+ (self.__source.has_many_custom_fields + self.__source.many_to_many_custom_fields).each do |name, _|
96
+ if self.__depth == 0
97
+ list = self.__source.send(name.to_sym).ordered
98
+ hash[name.to_s] = list.map do |entry|
99
+ if self.html_view?
100
+ entry.to_presenter(depth: self.__depth + 1, html_view: true).as_json
101
+ else
102
+ entry._slug
103
+ end
51
104
  end
52
- else
53
- self.send(meth.to_sym) rescue nil
54
- end)
105
+ end
106
+ end
107
+ end
108
+ end
109
+
110
+ # Set the values of the dynamic attributes for the
111
+ # content entry instance. This method is called automatically
112
+ # (callback) when the attributes= is called.
113
+ #
114
+ def set_dynamic_attributes
115
+ # process the basic types
116
+ self.__source.custom_fields_basic_attributes = @_attributes
117
+
118
+ # now the relationships
119
+ self.__source.relationship_custom_fields.each do |rule|
120
+ if rule['type'] == 'belongs_to'
121
+ self.set_belongs_to_attribute(rule['name'], rule['class_name'])
122
+ elsif rule['type'] == 'many_to_many'
123
+ self.set_many_to_many_attribute(rule['name'], rule['class_name'])
55
124
  end
56
125
  end
57
126
  end
58
127
 
128
+ def set_belongs_to_attribute(name, class_name)
129
+ id_or_slug = @_attributes[name] || @_attributes["#{name}_id"]
130
+
131
+ return if id_or_slug.nil?
132
+
133
+ entry = self.fetch_content_entries(class_name, id_or_slug).first
134
+
135
+ if entry
136
+ self.__source.send(:"#{name}_id=", entry._id)
137
+
138
+ if position = @_attributes["#{name}_position"]
139
+ self.__source.send(:"#{name}_position=", position)
140
+ end
141
+ end
142
+ end
143
+
144
+ def set_many_to_many_attribute(name, class_name)
145
+ ids_or_slugs = @_attributes[name] || @_attributes["#{name}_ids"]
146
+
147
+ return if ids_or_slugs.nil?
148
+
149
+ entries = self.fetch_content_entries(class_name, ids_or_slugs)
150
+
151
+ unless entries.empty?
152
+ entries = entries.sort do |a, b|
153
+ (ids_or_slugs.index(a._id.to_s) || ids_or_slugs.index(a._permalink)) <=>
154
+ (ids_or_slugs.index(b._id.to_s) || ids_or_slugs.index(b._permalink))
155
+ end # keep the original order
156
+
157
+ self.__source.send(:"#{name}=", entries.to_a)
158
+ end
159
+ end
160
+
161
+ # Return all the ids of the content entries matching the ids or the slugs
162
+ #
163
+ # @param [ Array ] ids_or_slugs Array of ids or slugs
164
+ #
165
+ # @return [ Array ] The matching entries
166
+ #
167
+ def fetch_content_entries(class_name, ids_or_slugs)
168
+ ids_or_slugs = [*ids_or_slugs]
169
+ klass = class_name.constantize
170
+
171
+ klass.any_of({ :_slug.in => ids_or_slugs }, { :_id.in => ids_or_slugs })
172
+ end
173
+
59
174
  end
60
- end
175
+ end