Sha256: 2b0ce0da98d7171281719355a40a413c1906613e44e13c63c4fac952335dc92d

Contents?: true

Size: 943 Bytes

Versions: 7

Compression:

Stored size: 943 Bytes

Contents

module Madmin
  class Field
    attr_reader :attribute_name, :model, :options

    def self.field_type
      to_s.split("::").last.underscore
    end

    def initialize(attribute_name:, model:, **options)
      @attribute_name = attribute_name
      @model = model
      @options = options
    end

    def value(record)
      record.public_send(attribute_name)
    end

    def to_partial_path(name)
      unless %w[index show form].include? name
        raise ArgumentError, "`partial` must be 'index', 'show', or 'form'"
      end

      "/madmin/fields/#{self.class.field_type}/#{name}"
    end

    def to_param
      attribute_name
    end

    # Used for checking visibility of attribute on an view
    def visible?(action, default: true)
      options.fetch(action.to_sym, default)
    end

    def required?
      model.validators_on(attribute_name).any? { |v| v.is_a? ActiveModel::Validations::PresenceValidator }
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
madmin-1.2.0 lib/madmin/field.rb
madmin-1.1.0 lib/madmin/field.rb
madmin-1.0.2 lib/madmin/field.rb
madmin-1.0.1 lib/madmin/field.rb
madmin-1.0.0 lib/madmin/field.rb
madmin-1.0.0.beta2 lib/madmin/field.rb
madmin-1.0.0.beta1 lib/madmin/field.rb