Sha256: 592d7e131086e7a41dd209ced9c33a4955c36463f2f24846d879314aeeb27620

Contents?: true

Size: 1.14 KB

Versions: 1

Compression:

Stored size: 1.14 KB

Contents

# frozen_string_literal: true

module StateMachineButtons
  module Renderer
    def state_events_buttons(object, state_method: :state, url: nil, without: nil)
      model_name = object.model.model_name.name.underscore
      excepted_actions = without.is_a?(Array) ? without.map(&:to_sym) : [without.to_sym]
      transitions = object.model.send("#{state_method}_transitions").reject do |t|
        excepted_actions.include? t.event
      end
      content_tag(:div, class: 'btn-group-vertical') do
        transitions.each do |event|
          button event: event, model_name: model_name, object: object, state_method: state_method, url: url
        end
      end
    end

    private

    def button(event:, model_name:, object:, state_method:, url:)
      href = url || send("admin_#{model_name}_path", object.model, model_name => { state_method => event.to })
      concat(
        link_to(
          href,
          class: "btn btn-sm btn-xs btn-#{object.send("#{state_method}_button_color", event.event)}",
          method: :patch
        ) do
          t("state_machines.#{model_name}.#{state_method}.events.#{event.event}")
        end
      )
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
state_machine_buttons-0.1.0 lib/state_machine_buttons/renderer.rb