archangel 0.0.8 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (153) hide show
  1. checksums.yaml +4 -4
  2. data/.jshintrc +1 -1
  3. data/.reek.yml +42 -0
  4. data/.rubocop.yml +1 -12
  5. data/.rubocop_exclude.yml +12 -0
  6. data/.rubocop_todo.yml +0 -4
  7. data/.travis.yml +5 -7
  8. data/Gemfile +12 -14
  9. data/MIT-LICENSE +1 -1
  10. data/README.md +1 -1
  11. data/app/controllers/archangel/application_controller.rb +0 -2
  12. data/app/controllers/archangel/auth/registrations_controller.rb +12 -0
  13. data/app/controllers/archangel/backend/assets_controller.rb +10 -259
  14. data/app/controllers/archangel/backend/collections_controller.rb +5 -214
  15. data/app/controllers/archangel/backend/entries_controller.rb +25 -239
  16. data/app/controllers/archangel/backend/pages_controller.rb +11 -261
  17. data/app/controllers/archangel/backend/profiles_controller.rb +19 -12
  18. data/app/controllers/archangel/backend/sites_controller.rb +17 -16
  19. data/app/controllers/archangel/backend/templates_controller.rb +5 -232
  20. data/app/controllers/archangel/backend/users_controller.rb +10 -260
  21. data/app/controllers/archangel/backend/widgets_controller.rb +5 -226
  22. data/app/controllers/archangel/frontend/pages_controller.rb +6 -7
  23. data/app/controllers/concerns/archangel/actionable_concern.rb +9 -0
  24. data/app/controllers/concerns/archangel/controllers/backend/resourceful_concern.rb +28 -0
  25. data/app/controllers/concerns/archangel/controllers/resourceful_concern.rb +258 -0
  26. data/app/controllers/concerns/archangel/paginatable_concern.rb +2 -0
  27. data/app/controllers/concerns/archangel/seoable_concern.rb +9 -8
  28. data/app/models/archangel/metatag.rb +12 -0
  29. data/app/models/archangel/page.rb +7 -1
  30. data/app/models/archangel/site.rb +6 -1
  31. data/app/models/archangel/user.rb +7 -0
  32. data/app/views/archangel/backend/pages/_form.html.erb +12 -2
  33. data/app/views/archangel/backend/pages/_metatag_fields.html.erb +6 -0
  34. data/app/views/archangel/backend/pages/show.html.erb +0 -10
  35. data/app/views/archangel/backend/profiles/_form.html.erb +2 -0
  36. data/app/views/archangel/backend/sites/_form.html.erb +11 -2
  37. data/app/views/archangel/backend/sites/_metatag_fields.html.erb +6 -0
  38. data/app/views/archangel/backend/sites/show.html.erb +0 -10
  39. data/app/views/archangel/frontend/pages/show.json.jbuilder +1 -2
  40. data/archangel.gemspec +18 -11
  41. data/bin/archangel +6 -4
  42. data/config/locales/active_record.en.yml +3 -4
  43. data/config/locales/en.yml +6 -0
  44. data/db/migrate/20190101220919_add_preferences_to_archangel_users.rb +5 -0
  45. data/db/migrate/20190113130750_create_archangel_metatags.rb +14 -0
  46. data/db/migrate/20190113130751_remove_meta_keywords_and_meta_description_from_archangel_pages.rb +6 -0
  47. data/db/migrate/20190113130752_remove_meta_keywords_and_meta_description_from_archangel_sites.rb +6 -0
  48. data/docs/Developers.md +2 -2
  49. data/docs/Extension/Developers.md +1 -1
  50. data/docs/Extension/Uploaders.md +53 -1
  51. data/docs/Theme/Developers.md +8 -2
  52. data/lib/archangel.rb +2 -2
  53. data/lib/archangel/commands/base_command.rb +35 -0
  54. data/lib/archangel/commands/extension_command.rb +128 -0
  55. data/lib/archangel/{command/templates/extension → commands/templates/common}/.editorconfig +0 -0
  56. data/lib/archangel/{command/templates/extension → commands/templates/common}/.gitignore +0 -0
  57. data/lib/archangel/{command/templates/extension → commands/templates/common}/.rspec +0 -0
  58. data/lib/archangel/{command/templates/extension → commands/templates/common}/.rubocop.yml +0 -0
  59. data/lib/archangel/{command/templates/extension → commands/templates/common}/MIT-LICENSE +0 -0
  60. data/lib/archangel/{command/templates/extension → commands/templates/common}/spec/rails_helper.rb.tt +0 -3
  61. data/lib/archangel/{command/templates/extension → commands/templates/common}/spec/spec_helper.rb +0 -0
  62. data/lib/archangel/{command/templates/extension → commands/templates/common}/spec/support/.keep +0 -0
  63. data/lib/archangel/{command → commands}/templates/extension/Gemfile +9 -10
  64. data/lib/archangel/{command → commands}/templates/extension/README.md +0 -0
  65. data/lib/archangel/{command → commands}/templates/extension/Rakefile +0 -0
  66. data/lib/archangel/{command → commands}/templates/extension/app/assets/javascripts/archangel/auth/%extension_name%.js.tt +0 -0
  67. data/lib/archangel/{command → commands}/templates/extension/app/assets/javascripts/archangel/backend/%extension_name%.js.tt +0 -0
  68. data/lib/archangel/{command → commands}/templates/extension/app/assets/javascripts/archangel/frontend/%extension_name%.js.tt +0 -0
  69. data/lib/archangel/{command → commands}/templates/extension/app/assets/stylesheets/archangel/auth/%extension_name%.css.tt +0 -0
  70. data/lib/archangel/{command → commands}/templates/extension/app/assets/stylesheets/archangel/backend/%extension_name%.css.tt +0 -0
  71. data/lib/archangel/{command → commands}/templates/extension/app/assets/stylesheets/archangel/frontend/%extension_name%.css.tt +0 -0
  72. data/lib/archangel/{command → commands}/templates/extension/bin/rails.tt +0 -0
  73. data/lib/archangel/{command → commands}/templates/extension/config/locales/en.yml.tt +0 -0
  74. data/lib/archangel/{command → commands}/templates/extension/config/routes.rb +0 -0
  75. data/lib/archangel/{command/templates/extension/extension.gemspec → commands/templates/extension/extension.gemspec.tt} +0 -0
  76. data/lib/archangel/{command → commands}/templates/extension/lib/%extension_name%.rb.tt +0 -0
  77. data/lib/archangel/{command → commands}/templates/extension/lib/%extension_name%/engine.rb.tt +0 -0
  78. data/lib/archangel/{command → commands}/templates/extension/lib/%extension_name%/factories.rb.tt +0 -0
  79. data/lib/archangel/{command → commands}/templates/extension/lib/%extension_name%/version.rb.tt +0 -0
  80. data/lib/archangel/{command → commands}/templates/extension/lib/generators/%extension_name%/install/install_generator.rb.tt +0 -0
  81. data/lib/archangel/commands/templates/theme/Gemfile +27 -0
  82. data/lib/archangel/commands/templates/theme/README.md +44 -0
  83. data/lib/archangel/commands/templates/theme/Rakefile +29 -0
  84. data/lib/archangel/commands/templates/theme/app/themes/%theme_base_name%/assets/javascripts/%theme_base_name%/auth.js +2 -0
  85. data/lib/archangel/commands/templates/theme/app/themes/%theme_base_name%/assets/javascripts/%theme_base_name%/backend.js +2 -0
  86. data/lib/archangel/commands/templates/theme/app/themes/%theme_base_name%/assets/javascripts/%theme_base_name%/frontend.js +2 -0
  87. data/lib/archangel/commands/templates/theme/app/themes/%theme_base_name%/assets/stylesheets/%theme_base_name%/auth.css +4 -0
  88. data/lib/archangel/commands/templates/theme/app/themes/%theme_base_name%/assets/stylesheets/%theme_base_name%/backend.css +4 -0
  89. data/lib/archangel/commands/templates/theme/app/themes/%theme_base_name%/assets/stylesheets/%theme_base_name%/frontend.css +4 -0
  90. data/lib/archangel/commands/templates/theme/app/themes/%theme_base_name%/config/locales/en.yml.tt +4 -0
  91. data/lib/archangel/commands/templates/theme/app/themes/%theme_base_name%/views/layouts/frontend.liquid +18 -0
  92. data/lib/archangel/commands/templates/theme/bin/rails.tt +11 -0
  93. data/lib/archangel/commands/templates/theme/lib/%theme_name%.rb.tt +9 -0
  94. data/lib/archangel/commands/templates/theme/lib/%theme_name%/engine.rb.tt +29 -0
  95. data/lib/archangel/commands/templates/theme/lib/%theme_name%/version.rb.tt +5 -0
  96. data/lib/archangel/commands/templates/theme/theme.gemspec.tt +25 -0
  97. data/lib/archangel/commands/theme_command.rb +129 -0
  98. data/lib/archangel/config.rb +11 -2
  99. data/lib/archangel/liquid/drop.rb +26 -0
  100. data/lib/archangel/liquid/drops/page_drop.rb +1 -1
  101. data/lib/archangel/liquid/drops/site_drop.rb +1 -1
  102. data/lib/archangel/liquid/filters/link_to_filter.rb +1 -0
  103. data/lib/archangel/liquid/tags/application_tag.rb +18 -5
  104. data/lib/archangel/liquid/tags/collection_tag.rb +1 -1
  105. data/lib/archangel/liquid/tags/csrf_meta_tags_tag.rb +1 -0
  106. data/lib/archangel/liquid/tags/locale_tag.rb +1 -0
  107. data/lib/archangel/liquid/tags/meta_tags_tag.rb +1 -0
  108. data/lib/archangel/liquid/tags/noembed_tag.rb +140 -0
  109. data/lib/archangel/liquid/tags/text_direction_tag.rb +1 -0
  110. data/lib/archangel/liquid/tags/theme_javascript_tag.rb +1 -0
  111. data/lib/archangel/liquid/tags/theme_stylesheet_tag.rb +1 -0
  112. data/lib/archangel/testing_support/factories/archangel_assets.rb +2 -2
  113. data/lib/archangel/testing_support/factories/archangel_collections.rb +1 -1
  114. data/lib/archangel/testing_support/factories/archangel_entries.rb +1 -1
  115. data/lib/archangel/testing_support/factories/archangel_fields.rb +4 -4
  116. data/lib/archangel/testing_support/factories/archangel_metatags.rb +21 -0
  117. data/lib/archangel/testing_support/factories/archangel_pages.rb +5 -7
  118. data/lib/archangel/testing_support/factories/archangel_sites.rb +1 -3
  119. data/lib/archangel/testing_support/factories/archangel_templates.rb +7 -7
  120. data/lib/archangel/testing_support/factories/archangel_users.rb +10 -10
  121. data/lib/archangel/testing_support/factories/archangel_widgets.rb +2 -2
  122. data/lib/archangel/testing_support/matchers/have_meta.rb +18 -0
  123. data/lib/archangel/testing_support/matchers/have_title.rb +18 -0
  124. data/lib/archangel/testing_support/rake/dummy_rake.rb +0 -2
  125. data/lib/archangel/version.rb +1 -1
  126. data/lib/generators/archangel/install/templates/config/archangel.yml +52 -37
  127. data/spec/controllers/archangel/backend/entries_controller_spec.rb +3 -3
  128. data/spec/controllers/archangel/backend/pages_controller_spec.rb +17 -1
  129. data/spec/controllers/archangel/backend/profiles_controller_spec.rb +1 -1
  130. data/spec/controllers/archangel/backend/sites_controller_spec.rb +6 -6
  131. data/spec/controllers/archangel/backend/users_controller_spec.rb +2 -2
  132. data/spec/controllers/archangel/frontend/pages_controller_spec.rb +2 -6
  133. data/spec/controllers/concerns/archangel/controllers/backend/resourceful_concern_spec.rb +90 -0
  134. data/spec/features/auth/registration_spec.rb +18 -0
  135. data/spec/features/frontend/liquid_drop_variables_spec.rb +51 -57
  136. data/spec/features/frontend/pages/homepage_redirect_spec.rb +2 -2
  137. data/spec/features/frontend/pages/metatags_spec.rb +73 -0
  138. data/spec/helpers/archangel/flash_helper_spec.rb +14 -0
  139. data/spec/lib/archangel/{command/extension_spec.rb → commands/extension_command_spec.rb} +3 -3
  140. data/spec/lib/archangel/commands/theme_command_spec.rb +100 -0
  141. data/spec/lib/archangel/liquid/drops/page_drop_spec.rb +1 -9
  142. data/spec/lib/archangel/liquid/drops/site_drop_spec.rb +2 -12
  143. data/spec/lib/archangel/liquid/tags/noembed_tag_spec.rb +76 -0
  144. data/spec/mailers/archangel/{application_job_spec.rb → application_mailer_spec.rb} +0 -0
  145. data/spec/models/archangel/metatag_spec.rb +22 -0
  146. data/spec/models/archangel/page_spec.rb +5 -2
  147. data/spec/models/archangel/site_spec.rb +2 -0
  148. data/spec/rails_helper.rb +1 -0
  149. data/spec/uploaders/archangel/application_uploader_spec.rb +1 -1
  150. data/spec/uploaders/archangel/asset_uploader_spec.rb +1 -1
  151. metadata +108 -93
  152. data/.reek +0 -44
  153. data/lib/archangel/command/extension.rb +0 -97
@@ -9,202 +9,7 @@ module Archangel
9
9
  # Backend widgets controller
10
10
  #
11
11
  class WidgetsController < BackendController
12
- before_action :set_resources, only: %i[index]
13
- before_action :set_resource, only: %i[destroy edit show update]
14
- before_action :set_new_resource, only: %i[create new]
15
-
16
- ##
17
- # Backend widgets
18
- #
19
- # Formats
20
- # HTML, JSON
21
- #
22
- # Request
23
- # GET /backend/widgets
24
- # GET /backend/widgets.json
25
- #
26
- # Response
27
- # [
28
- # {
29
- # "id": 123,
30
- # "site_id": 123,
31
- # "name": "Widget 1 Name",
32
- # "slug": "widget_1_slug",
33
- # "content": "</p>Content of Widget 1</p>",
34
- # "template_id": 123,
35
- # "deleted_at": null,
36
- # "created_at": "YYYY-MM-DDTHH:MM:SS.MSZ",
37
- # "updated_at": "YYYY-MM-DDTHH:MM:SS.MSZ"
38
- # },
39
- # ...
40
- # ]
41
- #
42
- def index
43
- respond_with @widgets
44
- end
45
-
46
- ##
47
- # Backend widget
48
- #
49
- # Formats
50
- # HTML, JSON
51
- #
52
- # Params
53
- # [String] slug - the widget slug
54
- #
55
- # Request
56
- # GET /backend/widgets/:slug
57
- # GET /backend/widgets/:slug.json
58
- #
59
- # Response
60
- # {
61
- # "id": 123,
62
- # "site_id": 123,
63
- # "name": "Widget Name",
64
- # "slug": "widget_slug",
65
- # "content": "</p>Content of the Widget</p>",
66
- # "template_id": 123,
67
- # "deleted_at": null,
68
- # "created_at": "YYYY-MM-DDTHH:MM:SS.MSZ",
69
- # "updated_at": "YYYY-MM-DDTHH:MM:SS.MSZ"
70
- # }
71
- #
72
- def show
73
- respond_with @widget
74
- end
75
-
76
- ##
77
- # New backend widget
78
- #
79
- # Formats
80
- # HTML, JSON
81
- #
82
- # Request
83
- # GET /backend/widgets/new
84
- # GET /backend/widgets/new.json
85
- #
86
- # Response
87
- # {
88
- # "id": null,
89
- # "site_id": 123,
90
- # "name": null,
91
- # "slug": null,
92
- # "content": null,
93
- # "template_id": null,
94
- # "deleted_at": null,
95
- # "created_at": null,
96
- # "updated_at": null
97
- # }
98
- #
99
- def new
100
- respond_with @widget
101
- end
102
-
103
- ##
104
- # Create backend widget
105
- #
106
- # Formats
107
- # HTML, JSON
108
- #
109
- # Request
110
- # POST /backend/widgets
111
- # POST /backend/widgets.json
112
- #
113
- # Paramaters
114
- # {
115
- # "widget": {
116
- # "name": "Widget Name",
117
- # "slug": "widget_slug",
118
- # "content": "</p>Content of the Widget</p>",
119
- # "template_id": 123
120
- # }
121
- # }
122
- #
123
- def create
124
- @widget.save
125
-
126
- respond_with @widget, location: -> { location_after_create }
127
- end
128
-
129
- ##
130
- # Edit backend widget
131
- #
132
- # Formats
133
- # HTML, JSON
134
- #
135
- # Params
136
- # [String] slug - the widget slug
137
- #
138
- # Request
139
- # GET /backend/widgets/:slug/edit
140
- # GET /backend/widgets/:slug/edit.json
141
- #
142
- # Response
143
- # {
144
- # "id": 123,
145
- # "site_id": 123,
146
- # "name": "Widget Name",
147
- # "slug": "widget_slug",
148
- # "content": "</p>Content of the Widget</p>",
149
- # "template_id": 123,
150
- # "deleted_at": null,
151
- # "created_at": "YYYY-MM-DDTHH:MM:SS.MSZ",
152
- # "updated_at": "YYYY-MM-DDTHH:MM:SS.MSZ"
153
- # }
154
- #
155
- def edit
156
- respond_with @widget
157
- end
158
-
159
- ##
160
- # Update backend widget
161
- #
162
- # Formats
163
- # HTML, JSON
164
- #
165
- # Params
166
- # [String] slug - the widget slug
167
- #
168
- # Request
169
- # PATCH /backend/widgets/:slug
170
- # PATCH /backend/widgets/:slug.json
171
- # PUT /backend/widgets/:slug
172
- # PUT /backend/widgets/:slug.json
173
- #
174
- # Paramaters
175
- # {
176
- # "widget": {
177
- # "name": "Widget Name",
178
- # "slug": "widget_slug",
179
- # "content": "</p>Content of the Widget</p>",
180
- # "template_id": 123
181
- # }
182
- # }
183
- #
184
- def update
185
- @widget.update(resource_params)
186
-
187
- respond_with @widget, location: -> { location_after_update }
188
- end
189
-
190
- ##
191
- # Destroy backend widget
192
- #
193
- # Formats
194
- # HTML, JSON
195
- #
196
- # Params
197
- # [String] slug - the widget slug
198
- #
199
- # Request
200
- # DELETE /backend/widgets/:slug
201
- # DELETE /backend/widgets/:slug.json
202
- #
203
- def destroy
204
- @widget.destroy
205
-
206
- respond_with @widget, location: -> { location_after_destroy }
207
- end
12
+ include Archangel::Controllers::Backend::ResourcefulConcern
208
13
 
209
14
  protected
210
15
 
@@ -212,7 +17,7 @@ module Archangel
212
17
  %w[content name slug template_id]
213
18
  end
214
19
 
215
- def set_resources
20
+ def resources_content
216
21
  @widgets = current_site.widgets
217
22
  .order(name: :asc)
218
23
  .page(page_num).per(per_page)
@@ -220,7 +25,7 @@ module Archangel
220
25
  authorize @widgets
221
26
  end
222
27
 
223
- def set_resource
28
+ def resource_content
224
29
  resource_id = params.fetch(:id)
225
30
 
226
31
  @widget = current_site.widgets.find_by!(slug: resource_id)
@@ -228,37 +33,11 @@ module Archangel
228
33
  authorize @widget
229
34
  end
230
35
 
231
- def set_new_resource
232
- new_params = action_name.to_sym == :create ? resource_params : nil
233
-
234
- @widget = current_site.widgets.new(new_params)
36
+ def resource_new_content
37
+ @widget = current_site.widgets.new(resource_new_params)
235
38
 
236
39
  authorize @widget
237
40
  end
238
-
239
- def resource_params
240
- params.require(resource_namespace).permit(permitted_attributes)
241
- end
242
-
243
- def resource_namespace
244
- controller_name.singularize.to_sym
245
- end
246
-
247
- def location_after_create
248
- location_after_save
249
- end
250
-
251
- def location_after_update
252
- location_after_save
253
- end
254
-
255
- def location_after_destroy
256
- location_after_save
257
- end
258
-
259
- def location_after_save
260
- backend_widgets_path
261
- end
262
41
  end
263
42
  end
264
43
  end
@@ -33,8 +33,6 @@ module Archangel
33
33
  # "path": "path/to/page",
34
34
  # "content": "</p>Content of the page</p>",
35
35
  # "homepage": false,
36
- # "meta_keywords": "keywords, for, the, page",
37
- # "meta_description": "Description of the page",
38
36
  # "published_at": "YYYY-MM-DDTHH:MM:SS.MSZ",
39
37
  # "created_at": "YYYY-MM-DDTHH:MM:SS.MSZ",
40
38
  # "updated_at": "YYYY-MM-DDTHH:MM:SS.MSZ",
@@ -80,11 +78,12 @@ module Archangel
80
78
  # @return [Object] the page meta tags
81
79
  #
82
80
  def page_meta_tags
83
- {
84
- title: @page.title,
85
- description: @page.meta_description,
86
- keywords: @page.meta_keywords.to_s.split(",")
87
- }
81
+ [
82
+ current_site.metatags,
83
+ @page.metatags
84
+ ].flatten.inject({}) do |tags, metatag|
85
+ tags.merge(metatag.name => metatag.content)
86
+ end.merge(title: @page.title)
88
87
  end
89
88
 
90
89
  ##
@@ -23,6 +23,7 @@ module Archangel
23
23
  # Controller action name as a symbol
24
24
  #
25
25
  # @return [Symbol] the action name
26
+ #
26
27
  def action
27
28
  action_name.to_sym
28
29
  end
@@ -34,6 +35,7 @@ module Archangel
34
35
  # Collection actions can be modified by overwriting `#collection_actions`
35
36
  #
36
37
  # @return [Boolean] if action is a collection action
38
+ #
37
39
  def collection_action?
38
40
  collection_actions.include?(action)
39
41
  end
@@ -45,6 +47,7 @@ module Archangel
45
47
  # Edit actions can be modified by overwriting `#edit_actions`
46
48
  #
47
49
  # @return [Boolean] if action is an edit action
50
+ #
48
51
  def edit_action?
49
52
  edit_actions.include?(action)
50
53
  end
@@ -52,6 +55,7 @@ module Archangel
52
55
  # Test if action is the `index` action
53
56
  #
54
57
  # @return [Boolean] if action is the index action
58
+ #
55
59
  def index_action?
56
60
  action?(:index)
57
61
  end
@@ -63,6 +67,7 @@ module Archangel
63
67
  # Member actions can be modified by overwriting `#member_actions`
64
68
  #
65
69
  # @return [Boolean] if action is an edit action
70
+ #
66
71
  def member_action?
67
72
  member_actions.include?(action)
68
73
  end
@@ -74,6 +79,7 @@ module Archangel
74
79
  # New actions can be modified by overwriting `#new_actions`
75
80
  #
76
81
  # @return [Boolean] if action is an edit action
82
+ #
77
83
  def new_action?
78
84
  new_actions.include?(action)
79
85
  end
@@ -85,6 +91,7 @@ module Archangel
85
91
  # RESTful actions can be modified by overwriting `#restful_actions`
86
92
  #
87
93
  # @return [Boolean] if action is an edit action
94
+ #
88
95
  def restful_action?
89
96
  restful_actions.include?(action)
90
97
  end
@@ -96,6 +103,7 @@ module Archangel
96
103
  # Save actions can be modified by overwriting `#save_actions`
97
104
  #
98
105
  # @return [Boolean] if action is an edit action
106
+ #
99
107
  def save_action?
100
108
  save_actions.include?(action)
101
109
  end
@@ -107,6 +115,7 @@ module Archangel
107
115
  # Show actions can be modified by overwriting `#show_actions`
108
116
  #
109
117
  # @return [Boolean] if action is a show action
118
+ #
110
119
  def show_action?
111
120
  show_actions.include?(action)
112
121
  end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Archangel
4
+ ##
5
+ # Controller concerns
6
+ #
7
+ module Controllers
8
+ ##
9
+ # Backend controller concerns
10
+ #
11
+ module Backend
12
+ ##
13
+ # Resourceful concern
14
+ #
15
+ module ResourcefulConcern
16
+ extend ActiveSupport::Concern
17
+
18
+ include Archangel::Controllers::ResourcefulConcern
19
+
20
+ protected
21
+
22
+ def resource_namespace
23
+ :backend
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,258 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Archangel
4
+ ##
5
+ # Controller concerns
6
+ #
7
+ module Controllers
8
+ ##
9
+ # Resourceful concern
10
+ #
11
+ module ResourcefulConcern
12
+ extend ActiveSupport::Concern
13
+
14
+ ##
15
+ # Resources
16
+ #
17
+ # Formats
18
+ # HTML, JSON
19
+ #
20
+ # Request
21
+ # GET /resources
22
+ # GET /resources.json
23
+ #
24
+ # Response
25
+ # [
26
+ # {
27
+ # "id": 123,
28
+ # ...
29
+ # "created_at": "YYYY-MM-DDTHH:MM:SS.MSZ",
30
+ # "updated_at": "YYYY-MM-DDTHH:MM:SS.MSZ"
31
+ # },
32
+ # ...
33
+ # ]
34
+ #
35
+ def index
36
+ resources = resources_content
37
+
38
+ respond_with resources
39
+ end
40
+
41
+ ##
42
+ # Resource
43
+ #
44
+ # Formats
45
+ # HTML, JSON
46
+ #
47
+ # Params
48
+ # [Integer] id - the resource ID
49
+ #
50
+ # Request
51
+ # GET /resources/:id
52
+ # GET /resources/:id.json
53
+ #
54
+ # Response
55
+ # {
56
+ # "id": 123,
57
+ # ...
58
+ # "created_at": "YYYY-MM-DDTHH:MM:SS.MSZ",
59
+ # "updated_at": "YYYY-MM-DDTHH:MM:SS.MSZ"
60
+ # }
61
+ #
62
+ def show
63
+ resource = resource_content
64
+
65
+ respond_with resource
66
+ end
67
+
68
+ ##
69
+ # New resource
70
+ #
71
+ # Formats
72
+ # HTML, JSON
73
+ #
74
+ # Request
75
+ # GET /resources/new
76
+ # GET /resources/new.json
77
+ #
78
+ # Response
79
+ # {
80
+ # "id": null,
81
+ # ...
82
+ # "created_at": null,
83
+ # "updated_at": null
84
+ # }
85
+ #
86
+ def new
87
+ resource = resource_new_content
88
+
89
+ respond_with resource
90
+ end
91
+
92
+ ##
93
+ # Create resource
94
+ #
95
+ # Formats
96
+ # HTML, JSON
97
+ #
98
+ # Request
99
+ # POST /resources
100
+ # POST /resources.json
101
+ #
102
+ # Paramaters
103
+ # {
104
+ # "resource": {
105
+ # "id": 123,
106
+ # ...
107
+ # }
108
+ # }
109
+ #
110
+ def create
111
+ resource = resource_new_content
112
+
113
+ resource.save unless resource.blank?
114
+
115
+ respond_with resource, location: -> { location_after_create }
116
+ end
117
+
118
+ ##
119
+ # Edit resource
120
+ #
121
+ # Formats
122
+ # HTML, JSON
123
+ #
124
+ # Params
125
+ # [Integer] id - the resource ID
126
+ #
127
+ # Request
128
+ # GET /resources/:id/edit
129
+ # GET /resources/:id/edit.json
130
+ #
131
+ # Response
132
+ # {
133
+ # "id": 123,
134
+ # ...
135
+ # "created_at": "YYYY-MM-DDTHH:MM:SS.MSZ",
136
+ # "updated_at": "YYYY-MM-DDTHH:MM:SS.MSZ"
137
+ # }
138
+ #
139
+ def edit
140
+ resource = resource_content
141
+
142
+ respond_with resource
143
+ end
144
+
145
+ ##
146
+ # Update resource
147
+ #
148
+ # Formats
149
+ # HTML, JSON
150
+ #
151
+ # Params
152
+ # [Integer] id - the resource ID
153
+ #
154
+ # Request
155
+ # PATCH /resources/:id
156
+ # PATCH /resources/:id.json
157
+ # PUT /resources/:id
158
+ # PUT /resources/:id.json
159
+ #
160
+ # Paramaters
161
+ # {
162
+ # "resource": {
163
+ # "id": 123,
164
+ # ...
165
+ # }
166
+ # }
167
+ #
168
+ def update
169
+ resource = resource_content
170
+
171
+ resource.update(resource_params)
172
+
173
+ respond_with resource, location: -> { location_after_update }
174
+ end
175
+
176
+ ##
177
+ # Destroy resource
178
+ #
179
+ # Formats
180
+ # HTML, JSON
181
+ #
182
+ # Params
183
+ # [Integer] id - the resource ID
184
+ #
185
+ # Request
186
+ # DELETE /resources/:id
187
+ # DELETE /resources/:id.json
188
+ #
189
+ def destroy
190
+ resource = resource_content
191
+
192
+ resource.destroy unless resource.blank?
193
+
194
+ respond_with resource, location: -> { location_after_destroy }
195
+ end
196
+
197
+ protected
198
+
199
+ def permitted_attributes
200
+ %w[]
201
+ end
202
+
203
+ def resources_content
204
+ []
205
+ end
206
+
207
+ def resource_content
208
+ {}
209
+ end
210
+
211
+ def resource_new_content
212
+ action_name.to_sym == :create ? {} : nil
213
+ end
214
+
215
+ def resource_controller
216
+ controller_name.to_sym
217
+ end
218
+
219
+ def resource_scope
220
+ resource_controller.to_s.singularize.to_sym
221
+ end
222
+
223
+ def resource_namespace
224
+ nil
225
+ end
226
+
227
+ def resource_params
228
+ params.require(resource_scope).permit(permitted_attributes)
229
+ end
230
+
231
+ def resource_new_params
232
+ action_name.to_sym == :create ? resource_params : nil
233
+ end
234
+
235
+ def location_after_create
236
+ location_after_save
237
+ end
238
+
239
+ def location_after_update
240
+ location_after_save
241
+ end
242
+
243
+ def location_after_destroy
244
+ location_after_save
245
+ end
246
+
247
+ def location_after_save
248
+ resources_path
249
+ end
250
+
251
+ def resources_path(options = {})
252
+ location_path = [resource_namespace, resource_controller].compact
253
+
254
+ archangel.polymorphic_path(location_path, options)
255
+ end
256
+ end
257
+ end
258
+ end