Module: SimpleCrud::ControllerHelpers

Included in:
SimpleCrudController
Defined in:
lib/simple_crud/controller_helpers.rb

Overview

Class-level helpers shared by the CRUD lambdas defined in SimpleCrudController.

Instance Method Summary collapse

Instance Method Details

#build_record(controller, klass, parameters) ⇒ Object



44
45
46
# File 'lib/simple_crud/controller_helpers.rb', line 44

def build_record(controller, klass, parameters)
  parameters[:build] ? controller.instance_exec(&parameters[:build]) : klass.new
end

#call_scope(scope, controller) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/simple_crud/controller_helpers.rb', line 58

def call_scope(scope, controller)
  user_method = SimpleCrud::Config.user_method
  user = controller.respond_to?(user_method) ? controller.public_send(user_method) : nil
  return scope.call(user) if scope.arity == 1

  scope.call(user, controller.params)
end

#find_record(klass, controller, parameters) ⇒ Object

Raises:

  • (ActiveRecord::RecordNotFound)


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

def find_record(klass, controller, parameters)
  record = lookup_record(klass, controller, parameters)
  raise ActiveRecord::RecordNotFound, "couldn't find #{klass}" if record.nil?
  unless record.is_a?(ActiveRecord::Base)
    raise ActiveRecord::RecordNotFound, "#{klass} finder must return a single record"
  end

  record
end

#index_records(controller, relation, options, parameters) ⇒ Object



66
67
68
69
70
# File 'lib/simple_crud/controller_helpers.rb', line 66

def index_records(controller, relation, options, parameters)
  return relation unless parameters[:paginate]

  SimpleCrud::Config.pagination_adapter.paginated_records(controller, relation, options)
end

#index_relation(controller, klass, parameters) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/simple_crud/controller_helpers.rb', line 48

def index_relation(controller, klass, parameters)
  if parameters[:scope]
    call_scope(parameters[:scope], controller)
  elsif parameters[:authorize]
    SimpleCrud::Config.authorization_adapter.policy_scope(controller, klass)
  else
    klass.all
  end
end

#lookup_record(klass, controller, parameters) ⇒ Object



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

def lookup_record(klass, controller, parameters)
  finder = parameters[:finder]
  return klass.find(controller.params[:id]) if finder.nil?
  return klass.send(finder, controller.params) unless finder.respond_to?(:call)

  finder.call(controller.params)
end

#maybe_authorize(controller, record, parameters) ⇒ Object



6
7
8
9
10
# File 'lib/simple_crud/controller_helpers.rb', line 6

def maybe_authorize(controller, record, parameters)
  return unless parameters[:authorize]

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

#persist_and_render(controller, record, parameters, options, persist, &block) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/simple_crud/controller_helpers.rb', line 90

def persist_and_render(controller, record, parameters, options, persist, &block)
  saved = parameters[:raise_on_invalid] ? persist.call(bang: true) : persist.call(bang: false)
  return render_persisted(controller, record, saved, options) unless block || parameters[:html]

  controller.instance_variable_set(:@record, record)
  return controller.instance_exec(record, saved, &block) if block

  if saved
    controller.redirect_to(redirect_target(controller, record, options[:redirect]))
  else
    controller.render(options[:failure_template])
  end
end

#redirect_target(controller, record, target) ⇒ Object



109
110
111
# File 'lib/simple_crud/controller_helpers.rb', line 109

def redirect_target(controller, record, target)
  target.is_a?(Proc) ? controller.instance_exec(record, &target) : target
end

#render_index(controller, klass, options, parameters, &block) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/simple_crud/controller_helpers.rb', line 12

def render_index(controller, klass, options, parameters, &block)
  relation = index_relation(controller, klass, parameters)

  if parameters[:html] || block
    records = index_records(controller, relation, options, parameters)
    controller.instance_variable_set(:@records, records)
    block ? controller.instance_exec(records, &block) : controller.render(:index)
  elsif parameters[:paginate]
    SimpleCrud::Config.pagination_adapter.paginate(controller, relation, options)
  else
    controller.render({ json: relation }.merge(options))
  end
end

#render_new(controller, record, parameters, &block) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/simple_crud/controller_helpers.rb', line 35

def render_new(controller, record, parameters, &block)
  if parameters[:html] || block
    controller.instance_variable_set(:@record, record)
    block ? controller.instance_exec(record, &block) : controller.render(:new)
  else
    controller.render json: record
  end
end

#render_persisted(controller, record, saved, options) ⇒ Object



113
114
115
116
117
# File 'lib/simple_crud/controller_helpers.rb', line 113

def render_persisted(controller, record, saved, options)
  return controller.render(json: record, status: options[:status]) if saved

  controller.render json: { errors: record.errors.full_messages }, status: 422
end

#render_show(controller, record, options, parameters, &block) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/simple_crud/controller_helpers.rb', line 26

def render_show(controller, record, options, parameters, &block)
  if parameters[:html] || block
    controller.instance_variable_set(:@record, record)
    block ? controller.instance_exec(record, &block) : controller.render(:show)
  else
    controller.render({ json: record }.merge(options))
  end
end

#save_and_render(controller, record, parameters, options, persist, &block) ⇒ Object



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

def save_and_render(controller, record, parameters, options, persist, &block)
  persist_and_render(controller, record, parameters,
                     options.merge(redirect: parameters[:redirect] || record), persist, &block)
end