lib/puffer/fields/field.rb in puffer-0.0.19 vs lib/puffer/fields/field.rb in puffer-0.0.20
- old
+ new
@@ -1,31 +1,37 @@
module Puffer
class Fields
class Field
- attr_accessor :resource, :field, :options
+ attr_accessor :resource, :field_name, :options, :fields
- def initialize field, *resource_and_options
- @field = field.to_s
+ def initialize field_name, *resource_and_options, &block
+ @field_name = field_name.to_s
@options = resource_and_options.extract_options!
@resource = resource_and_options.first
+ @fields = Puffer::Fields.new
+ block.bind(self).call if block
end
+ def field name, options = {}, &block
+ @fields.field(name, swallow_nil{reflection.klass}, options, &block)
+ end
+
def native?
model == resource
end
def name
- @name ||= field.split('.').last
+ @name ||= field_name.split('.').last
end
def path
- @path ||= field.split('.')[0..-2].join('.')
+ @path ||= field_name.split('.')[0..-2].join('.')
end
- def label
- @label ||= resource.human_attribute_name(field)
+ def human
+ @human ||= model && model.human_attribute_name(name)
end
def order
@order ||= options[:order] || query_column
end
@@ -33,11 +39,11 @@
def type
@type ||= options[:type] ? options[:type].to_sym : (Puffer::Customs.type_for(self) || (column ? column.type : :string))
end
def to_s
- field
+ field_name
end
def reflection
@reflection ||= model && model.reflect_on_association(name.to_sym)
end
@@ -54,28 +60,21 @@
Puffer::Customs.input_for(self).render builder, self
end
def model
@model ||= begin
- associations = field.split('.')
+ associations = field_name.split('.')
associations.pop
temp = resource
while temp.reflect_on_association(association = swallow_nil{associations.shift.to_sym}) do
temp = temp.reflect_on_association(association).klass
end
temp
end if resource
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 field_name, reflection.klass
- end
- fields
- end
+ @association_columns ||= fields
end
def column
@column ||= model && model.columns_hash[name]
end