Module: SimpleCrudController

Extended by:
SimpleCrud::ControllerHelpers
Includes:
SimpleCrud::ActionLambdas
Defined in:
lib/simple_crud/simple_crud_controller.rb

Overview

Extended onto a controller for CRUD actions.

Instance Method Summary collapse

Methods included from SimpleCrud::ControllerHelpers

build_record, call_scope, find_record, index_records, index_relation, lookup_record, maybe_authorize, persist_and_render, redirect_target, render_edit, render_form, render_index, render_new, render_persisted, render_show, save_and_render

Methods included from SimpleCrud::ActionLambdas

#crud_lambda_for_create, #crud_lambda_for_destroy, #crud_lambda_for_edit, #crud_lambda_for_index, #crud_lambda_for_new, #crud_lambda_for_show, #crud_lambda_for_update

Instance Method Details

#check_policies(parameters) ⇒ Object

Raises:

  • (ArgumentError)


62
63
64
65
66
67
68
69
# File 'lib/simple_crud/simple_crud_controller.rb', line 62

def check_policies(parameters)
  return if !parameters.key?(:authorize) || !parameters[:authorize]

  model = simple_crud_controller_model
  return if SimpleCrud::Config.authorization_adapter.policy_defined?(model)

  raise ArgumentError, "no authorization policy configured for #{model}"
end

#check_serializer(parameters) ⇒ Object

Raises:

  • (ArgumentError)


71
72
73
74
75
76
# File 'lib/simple_crud/simple_crud_controller.rb', line 71

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

Raises:

  • (ArgumentError)


58
59
60
# File 'lib/simple_crud/simple_crud_controller.rb', line 58

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



40
41
42
43
44
45
46
47
# File 'lib/simple_crud/simple_crud_controller.rb', line 40

def parameters_with_defaults(parameters)
  defaults = {
    authorize: true, paginate: true, authenticate: true, serializer: nil,
    html: false, finder: nil, scope: nil, build: nil, raise_on_invalid: false
  }
  defaults.each { |key, value| parameters[key] = value unless parameters.key?(key) }
  parameters
end

#simple_crud_controller_modelObject



54
55
56
# File 'lib/simple_crud/simple_crud_controller.rb', line 54

def simple_crud_controller_model
  to_s.split('::').last.sub('Controller', '').singularize.classify.constantize
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: use devise to authenticate 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



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/simple_crud/simple_crud_controller.rb', line 28

def simple_crud_for(method, parameters = {}, &block)
  parameters[:html] = true if block && !parameters.key?(:html)
  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

#write_metadata(method, parameters) ⇒ Object



49
50
51
52
# File 'lib/simple_crud/simple_crud_controller.rb', line 49

def (method, parameters)
  @simple_crud_metadata ||= {}
  @simple_crud_metadata[method] = parameters
end