Module: SimpleCrudController
- Includes:
- SimpleCrud::ActionLambdas
- Defined in:
- lib/simple_crud/simple_crud_controller.rb
Overview
Extended onto a controller for CRUD actions.
Class Method Summary collapse
Instance Method Summary collapse
- #check_policies(parameters) ⇒ Object
- #check_serializer(parameters) ⇒ Object
- #check_valid_method(method) ⇒ Object
- #parameters_with_defaults(parameters) ⇒ Object
- #simple_crud_controller_model ⇒ Object
- #simple_crud_defaults(options = {}) ⇒ Object
-
#simple_crud_for(method, parameters = {}, &block) ⇒ Object
Possible options: authorize: check authorization via Config.authorization_adapter (Pundit by default) paginate: paginate the list via Config.pagination_adapter (wor-paginate by default) authenticate: true calls authenticate_user! inside the action lambda; false skips it authenticate_headers: whether shared examples set auth headers and run the unauthorized test (defaults to authenticate:); set independently when a base-controller before_action handles auth serializer: use a particular serializer (both each_serializer and serializer) html: render the action's ERB template instead of JSON (index/show/new/edit/create/update/destroy) finder: custom record lookup (Proc/lambda or Symbol) for :show/:update/:destroy/:edit scope: custom index scope (Proc/lambda taking current_user and optional params), overrides policy_scope build: custom record builder (Proc/lambda) for :new/:create, invoked with the controller as self raise_on_invalid: use strict create!/update! semantics instead of returning 422 A block given to simple_crud_for renders explicitly: it receives the records for :index, the record for :show/:new/:edit, or the record and a saved flag for :create/:update/:destroy.
- #simple_crud_inherited_defaults ⇒ Object
- #write_metadata(method, parameters) ⇒ Object
Class Method Details
.extended(base) ⇒ Object
22 23 24 |
# File 'lib/simple_crud/simple_crud_controller.rb', line 22 def self.extended(base) base.include(SimpleCrud::CacheHelpers) end |
Instance Method Details
#check_policies(parameters) ⇒ Object
90 91 92 93 94 95 96 97 |
# File 'lib/simple_crud/simple_crud_controller.rb', line 90 def check_policies(parameters) return if !parameters.key?(:authorize) || !parameters[:authorize] model = simple_crud_controller_model return if SimpleCrud::Config..policy_defined?(model) raise ArgumentError, "no authorization policy configured for #{model}" end |
#check_serializer(parameters) ⇒ Object
99 100 101 102 103 104 |
# File 'lib/simple_crud/simple_crud_controller.rb', line 99 def check_serializer(parameters) name = parameters[:serializer].to_s return if name.blank? || Kernel.const_defined?(name) raise ArgumentError, "create a valid serializer with name #{name}" end |
#check_valid_method(method) ⇒ Object
86 87 88 |
# File 'lib/simple_crud/simple_crud_controller.rb', line 86 def check_valid_method(method) raise ArgumentError, 'invalid method' unless i[show index create update destroy new edit].include? method end |
#parameters_with_defaults(parameters) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/simple_crud/simple_crud_controller.rb', line 64 def parameters_with_defaults(parameters) defaults = { authorize: true, paginate: true, authenticate: true, authenticate_headers: nil, serializer: nil, serializer_options: nil, status: nil, after_persist: nil, html: false, finder: nil, scope: nil, build: nil, raise_on_invalid: false } defaults.merge(simple_crud_inherited_defaults).each do |key, value| parameters[key] = value unless parameters.key?(key) end parameters[:authenticate_headers] = parameters[:authenticate] if parameters[:authenticate_headers].nil? parameters end |
#simple_crud_controller_model ⇒ Object
82 83 84 |
# File 'lib/simple_crud/simple_crud_controller.rb', line 82 def simple_crud_controller_model to_s.split('::').last.sub('Controller', '').singularize.classify.constantize end |
#simple_crud_defaults(options = {}) ⇒ Object
51 52 53 |
# File 'lib/simple_crud/simple_crud_controller.rb', line 51 def simple_crud_defaults( = {}) @simple_crud_defaults = simple_crud_inherited_defaults.merge() end |
#simple_crud_for(method, parameters = {}, &block) ⇒ Object
Possible options: authorize: check authorization via Config.authorization_adapter (Pundit by default) paginate: paginate the list via Config.pagination_adapter (wor-paginate by default) authenticate: true calls authenticate_user! inside the action lambda; false skips it authenticate_headers: whether shared examples set auth headers and run the unauthorized test (defaults to authenticate:); set independently when a base-controller before_action handles auth serializer: use a particular serializer (both each_serializer and serializer) html: render the action's ERB template instead of JSON (index/show/new/edit/create/update/destroy) finder: custom record lookup (Proc/lambda or Symbol) for :show/:update/:destroy/:edit scope: custom index scope (Proc/lambda taking current_user and optional params), overrides policy_scope build: custom record builder (Proc/lambda) for :new/:create, invoked with the controller as self raise_on_invalid: use strict create!/update! semantics instead of returning 422 A block given to simple_crud_for renders explicitly: it receives the records for :index, the record for :show/:new/:edit, or the record and a saved flag for :create/:update/:destroy
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/simple_crud/simple_crud_controller.rb', line 40 def simple_crud_for(method, parameters = {}, &block) parameters[:block] = true if block parameters = parameters_with_defaults(parameters) klass = simple_crud_controller_model check_valid_method(method) check_policies(parameters) check_serializer(parameters) define_method(method, send("crud_lambda_for_#{method}", klass, parameters, &block)) (method, parameters) end |
#simple_crud_inherited_defaults ⇒ Object
55 56 57 58 59 60 61 62 |
# File 'lib/simple_crud/simple_crud_controller.rb', line 55 def simple_crud_inherited_defaults ancestors.each do |ancestor| next unless ancestor.instance_variable_defined?(:@simple_crud_defaults) return ancestor.instance_variable_get(:@simple_crud_defaults) end {} end |
#write_metadata(method, parameters) ⇒ Object
77 78 79 80 |
# File 'lib/simple_crud/simple_crud_controller.rb', line 77 def (method, parameters) ||= {} [method] = parameters end |