Module: SimpleCrudController

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

Class Method Details

.maybe_authorize(controller, record, parameters) ⇒ Object



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

def self.maybe_authorize(controller, record, parameters)
  return unless parameters[:authorize] && parameters[:authenticate]

  SimpleCrud::Config.authorization_adapter.authorize(controller, record)
end

.render_index(controller, klass, options, parameters) ⇒ Object



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

def self.render_index(controller, klass, options, parameters)
  if parameters[:paginate]
    SimpleCrud::Config.pagination_adapter.paginate(controller, klass, options)
  else
    controller.render({ json: klass.all }.merge(options))
  end
end

Instance Method Details

#check_policies(parameters) ⇒ Object



108
109
110
111
112
113
114
115
# File 'lib/simple_crud/simple_crud_controller.rb', line 108

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)

  throw "no authorization policy configured for #{model}"
end

#check_serializer(parameters) ⇒ Object



117
118
119
120
121
122
123
124
# File 'lib/simple_crud/simple_crud_controller.rb', line 117

def check_serializer(parameters)
  return if parameters[:serializer].blank?

  serializer_name = parameters[:serializer].to_s
  return if Kernel.const_defined?(serializer_name)

  throw "create a valid serializer with name #{serializer_name}"
end

#check_valid_method(method) ⇒ Object



104
105
106
# File 'lib/simple_crud/simple_crud_controller.rb', line 104

def check_valid_method(method)
  throw 'invalid method' unless %i[show index create update destroy].include? method
end

#crud_lambda_for_create(klass, parameters = {}) ⇒ Object



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

def crud_lambda_for_create(klass, parameters = {})
  lambda do
    authenticate_user! if parameters[:authenticate]
    permitted_params = send("#{self.class.simple_crud_controller_model.to_s.underscore}_params")
    SimpleCrudController.maybe_authorize(self, klass.new(permitted_params), parameters)
    render json: klass.create!(permitted_params), status: :created
  end
end

#crud_lambda_for_destroy(klass, parameters = {}) ⇒ Object



91
92
93
94
95
96
97
98
# File 'lib/simple_crud/simple_crud_controller.rb', line 91

def crud_lambda_for_destroy(klass, parameters = {})
  lambda do
    authenticate_user! if parameters[:authenticate]
    requested = klass.find(params[:id])
    SimpleCrudController.maybe_authorize(self, requested, parameters)
    render json: klass.find(params[:id]).destroy
  end
end

#crud_lambda_for_index(klass, parameters = {}) ⇒ Object



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

def crud_lambda_for_index(klass, parameters = {})
  lambda do
    authenticate_user! if parameters[:authenticate]
    SimpleCrudController.maybe_authorize(self, klass.new, parameters)
    options = {}.merge(each_serializer: parameters[:serializer]).compact
    SimpleCrudController.render_index(self, klass, options, parameters)
  end
end

#crud_lambda_for_show(klass, parameters = {}) ⇒ Object



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

def crud_lambda_for_show(klass, parameters = {})
  lambda do
    authenticate_user! if parameters[:authenticate]
    requested = klass.find(params[:id])

    options = {}.merge(serializer: parameters[:serializer]).compact
    SimpleCrudController.maybe_authorize(self, requested, parameters)
    render({ json: requested }.merge(options))
  end
end

#crud_lambda_for_update(klass, parameters = {}) ⇒ Object



81
82
83
84
85
86
87
88
89
# File 'lib/simple_crud/simple_crud_controller.rb', line 81

def crud_lambda_for_update(klass, parameters = {})
  lambda do
    authenticate_user! if parameters[:authenticate]
    requested = klass.find(params[:id])
    SimpleCrudController.maybe_authorize(self, requested, parameters)
    permitted_params = send("#{self.class.simple_crud_controller_model.to_s.underscore}_params")
    render json: requested.update!(permitted_params)
  end
end

#parameters_with_defaults(parameters) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/simple_crud/simple_crud_controller.rb', line 25

def parameters_with_defaults(parameters)
  defaults = { authorize: true, paginate: true, authenticate: true, serializer: nil }
  defaults.each do |key, value|
    parameters[key] = value unless parameters.key?(key)
  end
  parameters
end

#simple_crud_controller_modelObject



100
101
102
# File 'lib/simple_crud/simple_crud_controller.rb', line 100

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

#simple_crud_for(method, parameters = {}) ⇒ 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)



15
16
17
18
19
20
21
22
23
# File 'lib/simple_crud/simple_crud_controller.rb', line 15

def simple_crud_for(method, parameters = {})
  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))
  (method, parameters)
end

#write_metadata(method, parameters) ⇒ Object



33
34
35
36
# File 'lib/simple_crud/simple_crud_controller.rb', line 33

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