Sha256: c049e65296306dbde2f46b3dafc4605db1a90eddc5cc293f02d8465d88443763

Contents?: true

Size: 1.93 KB

Versions: 4

Compression:

Stored size: 1.93 KB

Contents

module Puffer
  class Fields
    class Field

      attr_accessor :resource, :field, :options

      def initialize resource, field, options = {}
        @resource = resource
        @field = field.to_s
        @options = options
      end

      def native?
        model == resource
      end

      def name
        @name ||= field.split('.').last
      end

      def path
        @path ||= field.split('.')[0..-2].join('.')
      end

      def label
        @label ||= resource.human_attribute_name(field)
      end

      def order
        @order ||= options[:order] || query_column
      end

      def type
        @type ||= options[:type] ? options[:type].to_sym : (Puffer::Fields.offered_type(self) || (column ? column.type : :string))
      end

      def to_s
        field
      end

      def reflection
        @reflection ||= model.reflect_on_association(name.to_sym)
      end

      def collection?
        [:has_many, :has_and_belongs_to_many].include? type
      end

      def input_options
        {}
      end

      def model
        unless @model
          @model = resource
          associations = field.split('.')
          associations.pop
          while @model.reflect_on_association(association = swallow_nil{associations.shift.to_sym}) do
            @model = @model.reflect_on_association(association).klass
          end
        end
        @model
      end

      def association_columns
        raise "Can`t find records for association building. Please set :columns option for '#{field}' field." unless options[:columns].present?
        @reflection_fields ||= begin
          fields = Puffer::Fields.new
          options[:columns].each do |field_name|
            fields.field reflection.klass, field_name
          end
          fields
        end
      end

      def column
        @column ||= model.columns_hash[name]
      end

      def query_column
        "#{model.table_name}.#{name}" if column
      end

    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
puffer-0.0.12 lib/puffer/fields/field.rb
puffer-0.0.11 lib/puffer/fields/field.rb
puffer-0.0.10 lib/puffer/fields/field.rb
puffer-0.0.9 lib/puffer/fields/field.rb