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)


77
78
79
80
81
82
83
84
# File 'lib/simple_crud/simple_crud_controller.rb', line 77

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)


86
87
88
89
90
91
# File 'lib/simple_crud/simple_crud_controller.rb', line 86

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)


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

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



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/simple_crud/simple_crud_controller.rb', line 52

def parameters_with_defaults(parameters)
  defaults = {
    authorize: true, paginate: true, authenticate: true, authenticate_headers: nil,
    serializer: 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_modelObject



69
70
71
# File 'lib/simple_crud/simple_crud_controller.rb', line 69

def simple_crud_controller_model
  to_s.split('::').last.sub('Controller', '').singularize.classify.constantize
end

#simple_crud_defaults(options = {}) ⇒ Object



39
40
41
# File 'lib/simple_crud/simple_crud_controller.rb', line 39

def simple_crud_defaults(options = {})
  @simple_crud_defaults = simple_crud_inherited_defaults.merge(options)
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



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

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_defaultsObject



43
44
45
46
47
48
49
50
# File 'lib/simple_crud/simple_crud_controller.rb', line 43

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



64
65
66
67
# File 'lib/simple_crud/simple_crud_controller.rb', line 64

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