Class: Puffer::Field

Inherits:
Object
  • Object
show all
Defined in:
lib/puffer/field.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(field_name, resource, *fieldset_and_options, &block) ⇒ Field

Returns a new instance of Field.



6
7
8
9
10
11
12
13
# File 'lib/puffer/field.rb', line 6

def initialize field_name, resource, *fieldset_and_options, &block
  @field_name = field_name.to_s
  @resource = resource
  @options = fieldset_and_options.extract_options!  
  @field_set = fieldset_and_options.first
  @children = Puffer::FieldSet.new swallow_nil{field_set.name}
  block.bind(self).call if block
end

Instance Attribute Details

#childrenObject

Returns the value of attribute children.



4
5
6
# File 'lib/puffer/field.rb', line 4

def children
  @children
end

#field_nameObject

Returns the value of attribute field_name.



4
5
6
# File 'lib/puffer/field.rb', line 4

def field_name
  @field_name
end

#field_setObject

Returns the value of attribute field_set.



4
5
6
# File 'lib/puffer/field.rb', line 4

def field_set
  @field_set
end

#optionsObject

Returns the value of attribute options.



4
5
6
# File 'lib/puffer/field.rb', line 4

def options
  @options
end

#resourceObject

Returns the value of attribute resource.



4
5
6
# File 'lib/puffer/field.rb', line 4

def resource
  @resource
end

Instance Method Details

#columnObject



80
81
82
# File 'lib/puffer/field.rb', line 80

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

#column_typeObject



84
85
86
87
# File 'lib/puffer/field.rb', line 84

def column_type
  reflection.macro if reflection
  column[:type] if column
end

#componentObject



59
60
61
# File 'lib/puffer/field.rb', line 59

def component
  @component ||= component_class.new self
end

#component_classObject



55
56
57
# File 'lib/puffer/field.rb', line 55

def component_class
  @component_class ||= Puffer.component_for self
end

#custom_typeObject



43
44
45
# File 'lib/puffer/field.rb', line 43

def custom_type
  Puffer.field_type_for self
end

#field(name, options = {}, &block) ⇒ Object



15
16
17
# File 'lib/puffer/field.rb', line 15

def field name, options = {}, &block
  @children.field(name, swallow_nil{reflection.klass}, options, &block)
end

#humanObject



35
36
37
# File 'lib/puffer/field.rb', line 35

def human
  @human ||= model && model.human_attribute_name(name)
end

#input_optionsObject



51
52
53
# File 'lib/puffer/field.rb', line 51

def input_options
  options[:html] || {}
end

#modelObject



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/puffer/field.rb', line 68

def model
  @model ||= begin
    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

#nameObject



27
28
29
# File 'lib/puffer/field.rb', line 27

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

#native?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/puffer/field.rb', line 19

def native?
  model == resource
end

#pathObject



31
32
33
# File 'lib/puffer/field.rb', line 31

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

#reflectionObject



47
48
49
# File 'lib/puffer/field.rb', line 47

def reflection
  @reflection ||= model && model.to_adapter.reflection(name)
end

#render(context, controller, *record_and_options) ⇒ Object



63
64
65
66
# File 'lib/puffer/field.rb', line 63

def render context, controller, *record_and_options
  options = record_and_options.extract_options!
  component.process context, controller, record_and_options.first, options
end

#to_sObject



23
24
25
# File 'lib/puffer/field.rb', line 23

def to_s
  field_name
end

#typeObject



39
40
41
# File 'lib/puffer/field.rb', line 39

def type
  @type ||= options[:type] || custom_type || column_type || :string
end