restful_controller 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (76) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.rdoc +151 -0
  4. data/Rakefile +44 -0
  5. data/lib/restful/actions.rb +168 -0
  6. data/lib/restful/base.rb +321 -0
  7. data/lib/restful/dummy_responder.rb +11 -0
  8. data/lib/restful/version.rb +7 -0
  9. data/lib/restful.rb +7 -0
  10. data/lib/tasks/resourceful_tasks.rake +4 -0
  11. data/test/actions_definition_test.rb +104 -0
  12. data/test/alternates_controller_test.rb +8 -0
  13. data/test/base_controller_test.rb +8 -0
  14. data/test/blocks_controller_test.rb +28 -0
  15. data/test/custom_notices_controller_test.rb +22 -0
  16. data/test/dummy/README.rdoc +28 -0
  17. data/test/dummy/Rakefile +6 -0
  18. data/test/dummy/app/assets/javascripts/application.js +13 -0
  19. data/test/dummy/app/assets/stylesheets/application.css +13 -0
  20. data/test/dummy/app/controllers/alternates_controller.rb +49 -0
  21. data/test/dummy/app/controllers/application_controller.rb +5 -0
  22. data/test/dummy/app/controllers/base_controller.rb +5 -0
  23. data/test/dummy/app/controllers/blocks_controller.rb +19 -0
  24. data/test/dummy/app/controllers/custom_notices_controller.rb +14 -0
  25. data/test/dummy/app/controllers/documents_controller.rb +11 -0
  26. data/test/dummy/app/controllers/home_controller.rb +5 -0
  27. data/test/dummy/app/helpers/application_helper.rb +2 -0
  28. data/test/dummy/app/models/document.rb +3 -0
  29. data/test/dummy/app/views/base/edit.html.erb +3 -0
  30. data/test/dummy/app/views/base/index.html.erb +3 -0
  31. data/test/dummy/app/views/base/new.html.erb +3 -0
  32. data/test/dummy/app/views/base/show.html.erb +1 -0
  33. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  34. data/test/dummy/bin/bundle +3 -0
  35. data/test/dummy/bin/rails +4 -0
  36. data/test/dummy/bin/rake +4 -0
  37. data/test/dummy/config/application.rb +29 -0
  38. data/test/dummy/config/boot.rb +5 -0
  39. data/test/dummy/config/database.yml +25 -0
  40. data/test/dummy/config/environment.rb +5 -0
  41. data/test/dummy/config/environments/development.rb +29 -0
  42. data/test/dummy/config/environments/production.rb +80 -0
  43. data/test/dummy/config/environments/test.rb +36 -0
  44. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  45. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  46. data/test/dummy/config/initializers/generators.rb +6 -0
  47. data/test/dummy/config/initializers/inflections.rb +16 -0
  48. data/test/dummy/config/initializers/mime_types.rb +5 -0
  49. data/test/dummy/config/initializers/secret_token.rb +12 -0
  50. data/test/dummy/config/initializers/session_store.rb +3 -0
  51. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  52. data/test/dummy/config/locales/en.yml +23 -0
  53. data/test/dummy/config/routes.rb +62 -0
  54. data/test/dummy/config.ru +4 -0
  55. data/test/dummy/db/development.sqlite3 +0 -0
  56. data/test/dummy/db/migrate/20130703024719_create_documents.rb +9 -0
  57. data/test/dummy/db/schema.rb +22 -0
  58. data/test/dummy/db/test.sqlite3 +0 -0
  59. data/test/dummy/log/development.log +11 -0
  60. data/test/dummy/log/test.log +75091 -0
  61. data/test/dummy/public/404.html +58 -0
  62. data/test/dummy/public/422.html +58 -0
  63. data/test/dummy/public/500.html +57 -0
  64. data/test/dummy/public/favicon.ico +0 -0
  65. data/test/dummy/test/fixtures/documents.yml +7 -0
  66. data/test/dummy/test/models/document_test.rb +7 -0
  67. data/test/dummy/tmp/cache/assets/test/sprockets/13fe41fee1fe35b49d145bcc06610705 +0 -0
  68. data/test/dummy/tmp/cache/assets/test/sprockets/2f5173deea6c795b8fdde723bb4b63af +0 -0
  69. data/test/dummy/tmp/cache/assets/test/sprockets/357970feca3ac29060c1e3861e2c0953 +0 -0
  70. data/test/dummy/tmp/cache/assets/test/sprockets/cffd775d018f68ce5dba1ee0d951a994 +0 -0
  71. data/test/dummy/tmp/cache/assets/test/sprockets/d771ace226fc8215a3572e0aa35bb0d6 +0 -0
  72. data/test/dummy/tmp/cache/assets/test/sprockets/f7cbd26ba1d28d48de824f0e94586655 +0 -0
  73. data/test/resourceful_test.rb +8 -0
  74. data/test/support/base_actions.rb +130 -0
  75. data/test/test_helper.rb +39 -0
  76. metadata +268 -0
@@ -0,0 +1,321 @@
1
+ module Restful
2
+ ##
3
+ # This is the main module for Restful implementation, in here class
4
+ # methods that implement the configuration macro that need to be included in
5
+ # controllers is defined.
6
+ #
7
+ # Also in this module exists the definition of instance methods need it by
8
+ # the actions injected to a controller.
9
+ module Base
10
+ ##
11
+ # Through this method, when this module is included in a controller, all
12
+ # Restful functionality is defined for it.
13
+ def self.included(base)
14
+ base.extend ClassMethods
15
+ end
16
+
17
+ ##
18
+ # Instance helper methods for a Restful controller
19
+ module InstanceMethods
20
+ ##
21
+ # Helper method that allows you to refer to the instance variable that
22
+ # represent a single object from the model name defined in the restful
23
+ # macro.
24
+ def resource
25
+ get_resource_ivar
26
+ end
27
+
28
+ ##
29
+ # Helper method that allows you to refer to the instance variable that
30
+ # represents the collection of objects from the model name defined in the
31
+ # restful macro.
32
+ def collection
33
+ get_collection_ivar || begin
34
+ set_collection_ivar class_name.all
35
+ end
36
+ end
37
+
38
+ protected
39
+ ##
40
+ # Return the instance variable name for a single object based on the model
41
+ # name defined in the restful macro, example:
42
+ #
43
+ # @document
44
+ def resource_ivar
45
+ "@#{class_name.model_name.element}"
46
+ end
47
+
48
+ ##
49
+ # Return the instance variable name for a collection of objects based on
50
+ # the model name defined in the restful macro, example:
51
+ #
52
+ # @documents
53
+ def collection_ivar
54
+ "@#{class_name.model_name.collection}"
55
+ end
56
+
57
+ ##
58
+ # Get the object from a single object instance variable.
59
+ def get_resource_ivar
60
+ instance_variable_get resource_ivar
61
+ end
62
+
63
+ ##
64
+ # Set the instance variable for a single object with an object.
65
+ #
66
+ # ==== Params
67
+ # * object: The object to be stored in the instance variable.
68
+ def set_resource_ivar(object)
69
+ instance_variable_set resource_ivar, object
70
+ end
71
+
72
+ ##
73
+ # Get the collection of objects from an instance variable.
74
+ def get_collection_ivar
75
+ instance_variable_get collection_ivar
76
+ end
77
+
78
+ ##
79
+ # Set the instance variable for a collection of objects
80
+ #
81
+ # ==== Params
82
+ # * objects: The objects collections to be stored in the instance
83
+ # variable.
84
+ def set_collection_ivar(objects)
85
+ instance_variable_set collection_ivar, objects
86
+ end
87
+
88
+ ##
89
+ # Get an object from instance variable if it exists, if not then build
90
+ # a new object.
91
+ def build_resource
92
+ get_resource_ivar || begin
93
+ set_resource_ivar class_name.new
94
+ end
95
+ end
96
+
97
+ ##
98
+ # Creates a new object using request params, tries to save the object and
99
+ # set it to the object's instance variable.
100
+ def create_resource
101
+ class_name.new(secure_params).tap do |model|
102
+ model.save
103
+ set_resource_ivar model
104
+ end
105
+ end
106
+
107
+ ##
108
+ # Apply Strong Params, using a string or symbol for method name or a Proc
109
+ # given to the restful macro.
110
+ def secure_params
111
+ return params unless strong_params.present?
112
+
113
+ if strong_params.is_a?(Symbol) || strong_params.is_a?(String)
114
+ return self.send strong_params
115
+ end
116
+
117
+ strong_params.call(params)
118
+ end
119
+
120
+ ##
121
+ # This is a helper method to get the object collection path.
122
+ def collection_path
123
+ self.send "#{class_name.model_name.route_key}_path"
124
+ end
125
+
126
+ ##
127
+ # This is a special method used by create and update actions that allows
128
+ # these methods to receive two blocks, success and failure blocks.
129
+ def respond_with_dual(object, options, &block)
130
+ args = [object, options]
131
+ set_flash options
132
+
133
+ case block.try(:arity)
134
+ when 2
135
+ respond_with(*args) do |responder|
136
+ dummy_responder = Restful::DummyResponder.new
137
+
138
+ if get_resource_ivar.errors.empty?
139
+ block.call responder, dummy_responder
140
+ else
141
+ block.call dummy_responder, responder
142
+ end
143
+ end
144
+ when 1
145
+ respond_with *args, &block
146
+ else
147
+ options[:location] = block.call if block
148
+ respond_with *args
149
+ end
150
+ end
151
+
152
+ ##
153
+ # This method set the flash messages if they are passed within the action
154
+ # ==== Params
155
+ # * options: a hash with :notice or :alert messages
156
+ def set_flash(options = {})
157
+ if options.has_key?(:notice)
158
+ flash[:notice] = options[:notice]
159
+ elsif options.has_key?(:alert)
160
+ flash[:alert] = options[:alert]
161
+ end
162
+ end
163
+
164
+ ##
165
+ # Find an object using the param id and set the object to the instance
166
+ # variable.
167
+ def find_resource
168
+ set_resource_ivar class_name.find(params[:id])
169
+ end
170
+
171
+ ##
172
+ # Find an object, update attributes from params and set instance
173
+ # variable.
174
+ def find_and_update_resource
175
+ model = class_name.find(params[:id])
176
+ model.tap do |m|
177
+ m.update_attributes secure_params
178
+ set_resource_ivar m
179
+ end
180
+ end
181
+
182
+ end
183
+
184
+ ##
185
+ # Class macros to setup restful functionality
186
+ module ClassMethods
187
+ ##
188
+ # List of REST actions
189
+ ACTIONS = [:index, :show, :edit, :update, :new, :create, :destroy]
190
+
191
+ ##
192
+ # Restful is the macro that setup a controller to become restful.
193
+ # This macro accepts 3 params:
194
+ #
195
+ # ==== Params
196
+ # * model: A requires parameter which is a symbol of the model name.
197
+ # * strong_params: A symbol, a string or a proc for the method that
198
+ # should apply the strong parameters to allow Active Model mass
199
+ # assignments.
200
+ # * actions: An array of actions that a controller should implement, if
201
+ # none is passed then all seven REST actions are defined.
202
+ #
203
+ # ==== Examples
204
+ #
205
+ # Simple:
206
+ #
207
+ # class DocumentsController < ApplicationController
208
+ # include restful
209
+ # respond_to :html
210
+ #
211
+ # restful model: :document, strong_params: :document_params
212
+ # end
213
+ #
214
+ # This definition will create the seven REST actions for Document model,
215
+ # this setup a single object instance variable @document and a collection
216
+ # variable @documents.
217
+ #
218
+ # It's expected that this controller will provide a method
219
+ # document_params which will be used to allow mass assignments.
220
+ #
221
+ # Strong params variation:
222
+ #
223
+ # class DocumentsController < ApplicationController
224
+ # include restful
225
+ # respond_to :html
226
+ #
227
+ # restful model: :document,
228
+ # strong_params: ->(params){ params.require(:document).permit :name }
229
+ # end
230
+ #
231
+ # In this example instead of providing a strong params method by string
232
+ # or symbol, a proc is passed to do the same job.
233
+ #
234
+ # Listed actions variation:
235
+ #
236
+ # The last parameter *actions* allows you to list in an array the actions
237
+ # that you want your controller to have:
238
+ #
239
+ # class DocumentsController < ApplicationController
240
+ # include restful
241
+ # respond_to :html
242
+ #
243
+ # restful model: :document, strong_params: :document_params,
244
+ # actions: [:index, :show]
245
+ # end
246
+ #
247
+ # In this case our controller will only respond to those 2 actions. We
248
+ # can do it the other way, indicate list of actions that shouldn't be
249
+ # defined:
250
+ #
251
+ # class DocumentsController < ApplicationController
252
+ # include restful
253
+ # respond_to :html
254
+ #
255
+ # restful model: :document, strong_params: :document_params,
256
+ # actions: [:all, except: [:destroy, :show]]
257
+ # end
258
+ #
259
+ # For this last example all seven REST actions will be defined but
260
+ # :destroy and :show
261
+ #
262
+ # ==== Considerations
263
+ # From previous examples you must have notice by now that the respond_to
264
+ # macro es need it. This is because all REST actions call the
265
+ # respont_with method, which works with the respond_to macro. Just
266
+ # include it in your controllers and list the formats do you wish your
267
+ # controller to respond.
268
+ #
269
+ def restful(model: nil, strong_params: nil, actions: :all)
270
+ self.class_attribute :model_name, :class_name, :strong_params,
271
+ instance_writer: false
272
+
273
+ self.model_name = model
274
+ self.strong_params = strong_params
275
+ self.class_name = class_from_name
276
+
277
+ include InstanceMethods
278
+ include Restful::Actions
279
+
280
+ setup_actions actions unless actions == :all
281
+
282
+ helper_method :collection, :resource
283
+ end
284
+
285
+ protected
286
+ ##
287
+ # Method that calculates the actions to which our controller should
288
+ # respond to.
289
+ def setup_actions(actions)
290
+ keep_actions = actions
291
+
292
+ if actions.include?(:all)
293
+ keep_actions = ACTIONS
294
+ end
295
+
296
+ options = actions.extract_options!
297
+ except_actions = options[:except]
298
+ keep_actions = keep_actions - except_actions
299
+
300
+ (ACTIONS - keep_actions).uniq.each do |action|
301
+ undef_method action.to_sym, "#{action.to_sym}!"
302
+ end
303
+ end
304
+
305
+ ##
306
+ # Method that gets the model class name from the passed symbol.
307
+ def class_from_name
308
+ if model_name.to_s.include? '_'
309
+ ns, *klass = model_name.to_s.split('_').collect(&:camelize)
310
+ begin
311
+ "#{ns}::#{klass.join('')}".constantize
312
+ rescue NameError
313
+ "#{ns}#{klass.join('')}".constantize
314
+ end
315
+ else
316
+ model_name.to_s.camelize.constantize
317
+ end
318
+ end
319
+ end
320
+ end
321
+ end
@@ -0,0 +1,11 @@
1
+ module Restful
2
+ ##
3
+ # This class acts as a respoder for dual block actions.
4
+ # Since there is no action need it from it, it just implements
5
+ # a method_missing for responder that needs to be ignored.
6
+ class DummyResponder
7
+ def method_missing(args) #:nodoc:
8
+ nil
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,7 @@
1
+ ##
2
+ # Library version
3
+ module Restful
4
+ ##
5
+ # Sets the library version
6
+ VERSION = '0.0.2'
7
+ end
data/lib/restful.rb ADDED
@@ -0,0 +1,7 @@
1
+ ##
2
+ # Module that require all the libraries need it by Restful library.
3
+ module Restful
4
+ require 'restful/dummy_responder'
5
+ require 'restful/actions'
6
+ require 'restful/base'
7
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :resourceful do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,104 @@
1
+ require 'test_helper'
2
+
3
+ describe 'Actions definition' do
4
+ it 'have all REST actions when no action is defined' do
5
+ ##
6
+ # Controller class for tests
7
+ class AllDefaultActionsController < BaseController
8
+ respond_to :html
9
+ restful model: :document
10
+ end
11
+
12
+ subject = AllDefaultActionsController.new
13
+
14
+ subject.must_respond_to :index
15
+ subject.must_respond_to :show
16
+ subject.must_respond_to :edit
17
+ subject.must_respond_to :update
18
+ subject.must_respond_to :new
19
+ subject.must_respond_to :create
20
+ subject.must_respond_to :destroy
21
+ end
22
+
23
+ it 'have all REST actions when all actions are defined' do
24
+ ##
25
+ # Controller class for tests
26
+ class AllExplicitActionsController < BaseController
27
+ respond_to :html
28
+ restful model: :document, actions: :all
29
+ end
30
+
31
+ subject = AllExplicitActionsController.new
32
+
33
+ subject.must_respond_to :index
34
+ subject.must_respond_to :show
35
+ subject.must_respond_to :edit
36
+ subject.must_respond_to :update
37
+ subject.must_respond_to :new
38
+ subject.must_respond_to :create
39
+ subject.must_respond_to :destroy
40
+ end
41
+
42
+ it 'have all but except actions' do
43
+ ##
44
+ # Controller class for tests
45
+ class ExceptActionsController < BaseController
46
+ respond_to :html
47
+ restful model: :document,
48
+ actions: [:all,
49
+ except: [:edit, :update, :destroy]]
50
+ end
51
+
52
+ subject = ExceptActionsController.new
53
+
54
+ subject.must_respond_to :index
55
+ subject.must_respond_to :show
56
+ subject.wont_respond_to :edit
57
+ subject.wont_respond_to :update
58
+ subject.must_respond_to :new
59
+ subject.must_respond_to :create
60
+ subject.wont_respond_to :destroy
61
+ end
62
+
63
+ it 'have listed actions but except actions' do
64
+ ##
65
+ # Controller class for tests
66
+ class HandPickActionsController < BaseController
67
+ respond_to :html
68
+ restful model: :document,
69
+ actions: [:index, :show, :destroy,
70
+ except: [:edit, :update, :destroy]]
71
+ end
72
+
73
+ subject = HandPickActionsController.new
74
+
75
+ subject.must_respond_to :index
76
+ subject.must_respond_to :show
77
+ subject.wont_respond_to :edit
78
+ subject.wont_respond_to :update
79
+ subject.wont_respond_to :new
80
+ subject.wont_respond_to :create
81
+ subject.wont_respond_to :destroy
82
+ end
83
+
84
+ it 'have invalid actions listed' do
85
+ ##
86
+ # Controller class for tests
87
+ class InvalidActionsController < BaseController
88
+ respond_to :html
89
+ restful model: :document,
90
+ actions: [:indice, :show, :eliminar,
91
+ except: [:edit, :actualizar, :destroy]]
92
+ end
93
+
94
+ subject = InvalidActionsController.new
95
+
96
+ subject.wont_respond_to :index
97
+ subject.must_respond_to :show
98
+ subject.wont_respond_to :edit
99
+ subject.wont_respond_to :update
100
+ subject.wont_respond_to :new
101
+ subject.wont_respond_to :create
102
+ subject.wont_respond_to :destroy
103
+ end
104
+ end
@@ -0,0 +1,8 @@
1
+ require 'test_helper'
2
+
3
+ describe AlternatesController do
4
+ extend BaseActions
5
+
6
+ test_base_actions model: :document, with_id: 100,
7
+ for_params: { document: { name: 'New document' } }
8
+ end
@@ -0,0 +1,8 @@
1
+ require 'test_helper'
2
+
3
+ describe DocumentsController do
4
+ extend BaseActions
5
+
6
+ test_base_actions model: :document, with_id: 100,
7
+ for_params: { document: { name: 'New document' } }
8
+ end
@@ -0,0 +1,28 @@
1
+ require 'test_helper'
2
+
3
+ describe BlocksController do
4
+ let(:valid_params) { { document: { name: 'Sample' } } }
5
+
6
+ it 'has one response block' do
7
+ post :create, valid_params
8
+
9
+ must_redirect_to root_path
10
+ end
11
+
12
+ it 'has two response blocks with success' do
13
+ params = valid_params.merge id: 100
14
+ post :update, params
15
+
16
+ must_redirect_to root_path
17
+ end
18
+
19
+ it 'has two response blocks with failire' do
20
+ params = valid_params.merge(id: 100).tap do |p|
21
+ p[:document][:name] = ''
22
+ end
23
+
24
+ post :update, params
25
+
26
+ must_render_template :new
27
+ end
28
+ end
@@ -0,0 +1,22 @@
1
+ require 'test_helper'
2
+
3
+ describe CustomNoticesController do
4
+ let(:valid_params) { { document: { name: 'Sample' } } }
5
+
6
+ context 'flash messages' do
7
+ it 'has a notice' do
8
+ post :create, valid_params
9
+
10
+ flash[:notice].must_equal 'A new document was created'
11
+ must_redirect_to root_path
12
+ end
13
+
14
+ it 'has an alert' do
15
+ params = valid_params.merge id: 100
16
+ put :update, params
17
+
18
+ flash[:alert].must_equal 'There are some errors'
19
+ must_redirect_to root_path
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,28 @@
1
+ == README
2
+
3
+ This README would normally document whatever steps are necessary to get the
4
+ application up and running.
5
+
6
+ Things you may want to cover:
7
+
8
+ * Ruby version
9
+
10
+ * System dependencies
11
+
12
+ * Configuration
13
+
14
+ * Database creation
15
+
16
+ * Database initialization
17
+
18
+ * How to run the test suite
19
+
20
+ * Services (job queues, cache servers, search engines, etc.)
21
+
22
+ * Deployment instructions
23
+
24
+ * ...
25
+
26
+
27
+ Please feel free to use a different markup language if you do not plan to run
28
+ <tt>rake doc:app</tt>.
@@ -0,0 +1,6 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require File.expand_path('../config/application', __FILE__)
5
+
6
+ Dummy::Application.load_tasks
@@ -0,0 +1,13 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file.
9
+ //
10
+ // Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require_tree .
@@ -0,0 +1,13 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the top of the
9
+ * compiled file, but it's generally better to create a new file per style scope.
10
+ *
11
+ *= require_self
12
+ *= require_tree .
13
+ */
@@ -0,0 +1,49 @@
1
+ class AlternatesController < BaseController
2
+ include Restful::Base
3
+
4
+ respond_to :html
5
+ restful model: :document, strong_params: :document_params
6
+
7
+ def index
8
+ @documents = Document.all
9
+ index!
10
+ end
11
+
12
+ def new
13
+ @document = Document.new
14
+ new!
15
+ end
16
+
17
+ def create
18
+ @document = Document.new document_params
19
+ @document.save
20
+ create!
21
+ end
22
+
23
+ def edit
24
+ @document = Document.find params[:id]
25
+ edit!
26
+ end
27
+
28
+ def update
29
+ @document = Document.find params[:id]
30
+ @document.update_attributes document_params
31
+ update!
32
+ end
33
+
34
+ def show
35
+ @document = Document.find params[:id]
36
+ show!
37
+ end
38
+
39
+ def destroy
40
+ @document = Document.find params[:id]
41
+ @document.destroy
42
+ destroy!
43
+ end
44
+
45
+ protected
46
+ def document_params
47
+ params.require(:document).permit :name
48
+ end
49
+ end
@@ -0,0 +1,5 @@
1
+ class ApplicationController < ActionController::Base
2
+ # Prevent CSRF attacks by raising an exception.
3
+ # For APIs, you may want to use :null_session instead.
4
+ protect_from_forgery with: :exception
5
+ end
@@ -0,0 +1,5 @@
1
+ class BaseController < ApplicationController
2
+ include Restful::Base
3
+
4
+ respond_to :html
5
+ end
@@ -0,0 +1,19 @@
1
+ class BlocksController < BaseController
2
+ include Restful::Base
3
+
4
+ respond_to :html
5
+ restful model: :document, strong_params: ->(params) { params.require(:document).permit :name }
6
+
7
+ def create
8
+ create! do |success|
9
+ success.html { redirect_to root_path }
10
+ end
11
+ end
12
+
13
+ def update
14
+ update! do |success, failure|
15
+ success.html { redirect_to root_path }
16
+ failure.html { render :new }
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,14 @@
1
+ class CustomNoticesController < BaseController
2
+ include Restful::Base
3
+
4
+ respond_to :html
5
+ restful model: :document, strong_params: ->(params) { params.require(:document).permit :name }
6
+
7
+ def create
8
+ create!(notice: 'A new document was created') { root_url }
9
+ end
10
+
11
+ def update
12
+ update!(alert: 'There are some errors') { root_url }
13
+ end
14
+ end