Sha256: afd676804cf23f42d3c1ac0555493fbcb3ca7c9ce53893fad6c3267bb3920fc6

Contents?: true

Size: 1.68 KB

Versions: 1

Compression:

Stored size: 1.68 KB

Contents

module Trestle
  class Resource
    module Toolbar
      class Builder < Trestle::Toolbar::Builder
        delegate :admin, :instance, to: :@template
        delegate :translate, :t, to: :admin

        def new(label: t("buttons.new", default: "New %{model_name}"), **attrs)
          return unless action?(:new)

          defaults = { action: :new, style: :light, icon: "fa fa-plus", class: "btn-new-resource" }
          link(label, defaults.merge(attrs))
        end

        def save(label: t("buttons.save", default: "Save %{model_name}"), **attrs)
          defaults = { style: :success }
          button(label, defaults.merge(attrs))
        end

        def delete(label: t("buttons.delete", default: "Delete %{model_name}"), **attrs)
          return unless action?(:destroy)

          defaults = Trestle::Options.new(action: :destroy, style: :danger, icon: "fa fa-trash", data: { turbo_method: "delete", controller: "confirm-delete", confirm_delete_placement_value: "bottom" })
          link(label, instance, defaults.merge(attrs))
        end

        def dismiss(label: t("buttons.ok", default: "OK"), **attrs)
          return unless @template.modal_request?

          defaults = Trestle::Options.new(type: :button, style: :light, data: { bs_dismiss: "modal" })
          button(label, defaults.merge(attrs))
        end
        alias ok dismiss

        def save_or_dismiss(action=:update)
          if action?(action)
            save
          else
            dismiss
          end
        end

        builder_method :new, :save, :delete, :dismiss, :ok, :save_or_dismiss

      protected
        def action?(action)
          admin.actions.include?(action)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
trestle-0.10.0.pre2 lib/trestle/resource/toolbar.rb