Module: Munificent::Admin::StateMachineHelper

Defined in:
app/helpers/munificent/admin/state_machine_helper.rb

Class Method Summary collapse

Class Method Details

.aasm_buttons(resource, tag_name: "li") ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'app/helpers/munificent/admin/state_machine_helper.rb', line 6

def aasm_buttons(resource, tag_name: "li")
  resource.aasm.permitted_transitions.each do |transition|
    event = transition.fetch(:event).to_s

    content_for(:aasm_buttons) do
      tag.public_send(tag_name) do
        button_to(
          event.humanize,
          send("#{event}_#{resource.class.name.split('::').last.underscore}_path", resource),
          method: :post,
          data: { confirm: "Are you sure?" },
        )
      end
    end
  end

  content_for(:aasm_buttons).presence
end

.define_actions(controller, resource_class) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/helpers/munificent/admin/state_machine_helper.rb', line 33

def define_actions(controller, resource_class)
  singular_name = resource_class.name.split("::").last.underscore

  resource_class.aasm.events.each do |event|
    controller.define_method(event.name) do
      resource.public_send("#{event.name}!")
      redirect_to(send("#{singular_name}_path", resource),
        notice: "#{event.name.to_s.humanize} was successful",
      )
    end
  end

  nil
end

.define_routes(router, resource_class) ⇒ Object



25
26
27
28
29
30
31
# File 'app/helpers/munificent/admin/state_machine_helper.rb', line 25

def define_routes(router, resource_class)
  router.send(:member) do
    resource_class.aasm.events.each do |event|
      router.send(:post, event.to_s)
    end
  end
end