Method: Effective::CrudController::Submits::ClassMethods#_insert_button

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

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



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'app/controllers/concerns/effective/crud_controller/submits.rb', line 65

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

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

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

  if label # Overwrite the default member action when given a custom label
    buttons.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 button :approve, 'Approve', if: -> { resource.finished? }"
  end

  if args.key?(:unless) && args[:unless].respond_to?(:call) == false
    raise "expected unless: to be callable. Try button :approve, '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 button :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 button :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

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