Module: Effective::CrudController::Submits

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

Instance Method Summary collapse

Instance Method Details

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



64
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 64

def _insert_button(action, label = nil, args = {})
  raise "expected first argument to be a Symbol. Try button :approve, 'Approve'" unless action.kind_of?(Symbol)
  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

#_insert_on(action, args = {}) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
# File 'app/controllers/concerns/effective/crud_controller/submits.rb', line 107

def _insert_on(action, args = {})
  raise "expected first argument to be a Symbol. Try on :approve, success: -> { ... }" unless action.kind_of?(Symbol)
  raise 'expected args to be a Hash' unless args.kind_of?(Hash)

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

  args[:action] = action

  (ons[action] ||= {}).merge!(args)
end

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

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



21
22
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
# File 'app/controllers/concerns/effective/crud_controller/submits.rb', line 21

def _insert_submit(action, label = nil, args = {})
  raise "expected first argument to be a Symbol. Try submit :approve, 'Approve'" unless action.kind_of?(Symbol)
  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

#buttonsObject

{ ‘Approve’ => { action: approve, …}}



11
12
13
# File 'app/controllers/concerns/effective/crud_controller/submits.rb', line 11

def buttons
  @_effective_buttons ||= effective_resource.buttons()
end

#onsObject

{ :approve => { redirect: .. }}



16
17
18
# File 'app/controllers/concerns/effective/crud_controller/submits.rb', line 16

def ons
  @_effective_ons ||= effective_resource.ons()
end

#submitsObject

{ ‘Save’ => { action: save, …}}



6
7
8
# File 'app/controllers/concerns/effective/crud_controller/submits.rb', line 6

def submits
  @_effective_submits ||= effective_resource.submits()
end