Sha256: c72e7979f9df6b15cdbf06b5baa668e9904277121cdc832c26683e2db4bf5b68

Contents?: true

Size: 1.63 KB

Versions: 7

Compression:

Stored size: 1.63 KB

Contents

module Stationed
  # Base class for CRUD-style tags
  class CrudTag
    attr_reader :context, :method, :resource, :model

    def initialize(context, method, resource, options = {})
      @context  = context
      @method   = method
      @resource = resource
      @model    = Array(resource).last
      @options  = options
    end

    def index
      context.send method, label(:index), model, options
    end

    def new
      *nested_parts, _ = Array(resource)
      context.send method, label(:new), [:new, *nested_parts, model_name.singular], options
    end

    def show
      context.send method, label(:show), [*resource], options
    end

    def edit
      context.send method, label(:edit), [:edit, *resource], options
    end

    def destroy
      context.send method, label(:destroy), [*resource], options.merge(data: { method: :delete })
    end

    private

    def options
      default_options.merge(@options)
    end

    def model_name
      @model_name ||=
        if @model.respond_to?(:model_name)
          @model.model_name
        elsif @model.class.respond_to?(:model_name)
          @model.class.model_name
        else
          fail ArgumentError,
               'Pass in an ActiveModel-compliant record or class'
        end
    end

    def default_options
      {}
    end

    def label(action)
      context.translate(
        :"helpers.buttons.#{model_name.i18n_key}.#{action}",
        default: [
          :"helpers.buttons.defaults.#{action}",
          "#{action.capitalize} %{model}"
        ],
        model: model_name.human,
        plural: model_name.human.pluralize,
        id: model.to_param
      )
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
stationed-0.6.0 lib/stationed/crud_tag.rb
stationed-0.5.0 lib/stationed/crud_tag.rb
stationed-0.4.0 lib/stationed/crud_tag.rb
stationed-0.3.0 lib/stationed/crud_tag.rb
stationed-0.2.0 lib/stationed/crud_tag.rb
stationed-0.1.0 lib/stationed/crud_tag.rb
stationed-0.0.1 lib/stationed/crud_tag.rb