Sha256: 76a867b7a1422ed6b6cb31c31226e087a88bb9e18e6e20c6ca0435936168a693

Contents?: true

Size: 1.01 KB

Versions: 7

Compression:

Stored size: 1.01 KB

Contents

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

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

    def initialize(attribute_name:, model:, resource:, **options)
      @attribute_name = attribute_name
      @model = model
      @resource = resource
      @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

    def searchable?
      false
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
madmin-1.2.11 lib/madmin/field.rb
madmin-1.2.10 lib/madmin/field.rb
madmin-1.2.9 lib/madmin/field.rb
madmin-1.2.8 lib/madmin/field.rb
madmin-1.2.7 lib/madmin/field.rb
madmin-1.2.6 lib/madmin/field.rb
madmin-1.2.5 lib/madmin/field.rb