Method: Effective::CrudController::Submits::ClassMethods#_insert_submit

Defined in:
app/controllers/concerns/effective/crud_controller/submits.rb

#_insert_submit(action, label = nil, args = {}) ⇒ Object

:only, :except, :if, :unless, :redirect, :success, :danger, :class



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/controllers/concerns/effective/crud_controller/submits.rb', line 23

def _insert_submit(action, label = nil, args = {})
  raise 'expected args to be a Hash or false' unless args.kind_of?(Hash) || args == false

  if label == false
    submits.delete_if { |label, args| args[:action] == action }; return
  end

  if args == false
    submits.delete(label); return
  end

  if label # Overwrite the default member action when given a custom submit
    submits.delete_if { |label, args| args[:default] && args[:action] == action }
  end

  if args.key?(:if) && args[:if].respond_to?(:call) == false
    raise "expected if: to be callable. Try submit :approve, 'Save and Approve', if: -> { resource.finished? }"
  end

  if args.key?(:unless) && args[:unless].respond_to?(:call) == false
    raise "expected unless: to be callable. Try submit :approve, 'Save and Approve', unless: -> { resource.declined? }"
  end

  if args.key?(:only)
    args[:only] = Array(args[:only])
    raise "expected only: to be a symbol or array of symbols. Try submit :approve, 'Save and Approve', only: [:edit]" unless args[:only].all? { |v| v.kind_of?(Symbol) }
  end

  if args.key?(:except)
    args[:except] = Array(args[:except])
    raise "expected except: to be a symbol or array of symbols. Try submit :approve, 'Save and Approve', except: [:edit]" unless args[:except].all? { |v| v.kind_of?(Symbol) }
  end

  if args.key?(:redirect_to) # Normalize this option to redirect
    args[:redirect] = args.delete(:redirect_to)
  end

  args[:action] = action

  (submits[label] ||= {}).merge!(args)
end