Module: Effective::Resources::Controller

Included in:
Effective::Resource
Defined in:
app/models/effective/resources/controller.rb

Instance Method Summary collapse

Instance Method Details

#buttonsObject



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
64
65
66
67
68
69
# File 'app/models/effective/resources/controller.rb', line 27

def buttons
  {}.tap do |buttons|
    member_get_actions.each do |action| # default true means it will be overwritten by dsl methods
      buttons[action.to_s.titleize] = { action: action, default: true }
    end

    (member_post_actions - crud_actions).each do |action| # default true means it will be overwritten by dsl methods
      action_name = action.to_s.titleize

      buttons[action_name] = case action
      when :archive
        { action: action, default: true, if: -> { !resource.archived? }, class: 'btn btn-danger', 'data-method' => :post, 'data-confirm' => "Really #{action_name} @resource?"}
      when :unarchive
        { action: action, default: true, if: -> { resource.archived? }, 'data-method' => :post, 'data-confirm' => "Really #{action_name} @resource?" }
      else
        { action: action, default: true, 'data-method' => :post, 'data-confirm' => "Really #{action_name} @resource?"}
      end
    end

    member_delete_actions.each do |action|
      action_name = action.to_s.titleize

      if action == :destroy
        next if buttons.values.find { |v| v[:action] == :archive }.present?
        buttons['Delete'] = { action: action, default: true, 'data-method' => :delete, 'data-confirm' => "Really delete @resource?" }
      else
        buttons[action_name] = { action: action, default: true, 'data-method' => :delete, 'data-confirm' => "Really #{action_name} @resource?" }
      end
    end

    if collection_get_actions.find { |a| a == :index }
      buttons["All #{human_plural_name}".titleize] = { action: :index, default: true }
    end

    if collection_get_actions.find { |a| a == :new }
      buttons["New #{human_name}".titleize] = { action: :new, default: true }
    end

    (collection_get_actions - crud_actions).each do |action|
      buttons[action.to_s.titleize] = { action: action, default: true }
    end
  end
end

#fallback_resource_actionsObject

Used by datatables



114
115
116
117
118
119
120
# File 'app/models/effective/resources/controller.rb', line 114

def fallback_resource_actions
  {
    'Show': { action: :show, default: true },
    'Edit': { action: :edit, default: true },
    'Delete': { action: :destroy, default: true, 'data-method' => :delete, 'data-confirm' => "Really delete @resource?" }
  }
end

#onsObject



140
141
142
# File 'app/models/effective/resources/controller.rb', line 140

def ons
  {}
end

#resource_actionsObject

This is the fallback for render_resource_actions when no actions are specified It is used by datatables



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
106
107
108
109
110
111
# File 'app/models/effective/resources/controller.rb', line 73

def resource_actions
  {}.tap do |actions|
    member_get_actions.reverse_each do |action|
      next unless crud_actions.include?(action)
      actions[action.to_s.titleize] = { action: action, default: true }
    end

    member_get_actions.each do |action|
      next if crud_actions.include?(action)
      actions[action.to_s.titleize] = { action: action, default: true }
    end

    member_post_actions.each do |action|
      next if crud_actions.include?(action)

      action_name = action.to_s.titleize

      actions[action_name] = case action
      when :archive
        { action: action, default: true, if: -> { !resource.archived? }, class: 'btn btn-danger', 'data-method' => :post, 'data-confirm' => "Really #{action_name} @resource?"}
      when :unarchive
        { action: action, default: true, if: -> { resource.archived? }, 'data-method' => :post, 'data-confirm' => "Really #{action_name} @resource?" }
      else
        { action: action, default: true, 'data-method' => :post, 'data-confirm' => "Really #{action_name} @resource?" }
      end
    end

    member_delete_actions.each do |action|
      action_name = action.to_s.titleize

      if action == :destroy
        next if actions.find { |_, v| v[:action] == :archive }.present?
        actions['Delete'] = { action: action, default: true, 'data-method' => :delete, 'data-confirm' => "Really delete @resource?" }
      else
        actions[action_name] = { action: action, default: true, 'data-method' => :delete, 'data-confirm' => "Really #{action_name} @resource?" }
      end
    end
  end
end

#resource_klass_actionsObject

This is the fallback for render_resource_actions when no actions are specified, but a class is given Used by Datatables new



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'app/models/effective/resources/controller.rb', line 124

def resource_klass_actions
  {}.tap do |buttons|
    if collection_get_actions.find { |a| a == :index }
      buttons["All #{human_plural_name}".titleize] = { action: :index, default: true }
    end

    if collection_get_actions.find { |a| a == :new }
      buttons["New #{human_name}".titleize] = { action: :new, default: true }
    end

    (collection_get_actions - crud_actions).each do |action|
      buttons[action.to_s.titleize] = { action: action, default: true }
    end
  end
end

#submitsObject

Used by effective_form_submit The actions we would use to commit. For link_to { ‘Save’: { action: :save }, ‘Continue’: { action: :save }, ‘Add New’: { action: :save }, ‘Approve’: { action: :approve } } Saves a list of commit actions…



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/models/effective/resources/controller.rb', line 11

def submits
  {}.tap do |submits|
    if actions.find { |a| a == :create || a == :update } && EffectiveResources.default_submits['Save']
      submits['Save'] = { action: :save, default: true }
    end

    if actions.find { |a| a == :index } && EffectiveResources.default_submits['Continue']
      submits['Continue'] = { action: :save, redirect: :index, default: true, unless: -> { params[:_datatable_id].present? } }
    end

    if actions.find { |a| a == :new } && EffectiveResources.default_submits['Add New']
      submits['Add New'] = { action: :save, redirect: :new, default: true, unless: -> { params[:_datatable_id].present? } }
    end
  end
end