Class: Puffer::Field
- Inherits:
-
Object
- Object
- Puffer::Field
- Defined in:
- lib/puffer/field.rb
Instance Attribute Summary collapse
-
#children ⇒ Object
Returns the value of attribute children.
-
#field_name ⇒ Object
Returns the value of attribute field_name.
-
#field_set ⇒ Object
Returns the value of attribute field_set.
-
#options ⇒ Object
Returns the value of attribute options.
-
#resource ⇒ Object
Returns the value of attribute resource.
Instance Method Summary collapse
- #column ⇒ Object
- #column_type ⇒ Object
- #component ⇒ Object
- #component_class ⇒ Object
- #custom_type ⇒ Object
- #field(name, options = {}, &block) ⇒ Object
- #human ⇒ Object
-
#initialize(field_name, resource, *fieldset_and_options, &block) ⇒ Field
constructor
A new instance of Field.
- #input_options ⇒ Object
- #model ⇒ Object
- #name ⇒ Object
- #native? ⇒ Boolean
- #path ⇒ Object
- #reflection ⇒ Object
- #render(context, controller, *record_and_options) ⇒ Object
- #to_s ⇒ Object
- #type ⇒ Object
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, *, &block @field_name = field_name.to_s @resource = resource @options = . @field_set = .first @children = Puffer::FieldSet.new swallow_nil{field_set.name} block.bind(self).call if block end |
Instance Attribute Details
#children ⇒ Object
Returns the value of attribute children.
4 5 6 |
# File 'lib/puffer/field.rb', line 4 def children @children end |
#field_name ⇒ Object
Returns the value of attribute field_name.
4 5 6 |
# File 'lib/puffer/field.rb', line 4 def field_name @field_name end |
#field_set ⇒ Object
Returns the value of attribute field_set.
4 5 6 |
# File 'lib/puffer/field.rb', line 4 def field_set @field_set end |
#options ⇒ Object
Returns the value of attribute options.
4 5 6 |
# File 'lib/puffer/field.rb', line 4 def @options end |
#resource ⇒ Object
Returns the value of attribute resource.
4 5 6 |
# File 'lib/puffer/field.rb', line 4 def resource @resource end |
Instance Method Details
#column ⇒ Object
80 81 82 |
# File 'lib/puffer/field.rb', line 80 def column @column ||= model && model.to_adapter.columns_hash[name] end |
#column_type ⇒ Object
84 85 86 87 |
# File 'lib/puffer/field.rb', line 84 def column_type reflection.macro if reflection column[:type] if column end |
#component ⇒ Object
59 60 61 |
# File 'lib/puffer/field.rb', line 59 def component @component ||= component_class.new self end |
#component_class ⇒ Object
55 56 57 |
# File 'lib/puffer/field.rb', line 55 def component_class @component_class ||= Puffer.component_for self end |
#custom_type ⇒ Object
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, = {}, &block @children.field(name, swallow_nil{reflection.klass}, , &block) end |
#human ⇒ Object
35 36 37 |
# File 'lib/puffer/field.rb', line 35 def human @human ||= model && model.human_attribute_name(name) end |
#input_options ⇒ Object
51 52 53 |
# File 'lib/puffer/field.rb', line 51 def [:html] || {} end |
#model ⇒ Object
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 |
#name ⇒ Object
27 28 29 |
# File 'lib/puffer/field.rb', line 27 def name @name ||= field_name.split('.').last end |
#native? ⇒ Boolean
19 20 21 |
# File 'lib/puffer/field.rb', line 19 def native? model == resource end |
#path ⇒ Object
31 32 33 |
# File 'lib/puffer/field.rb', line 31 def path @path ||= field_name.split('.')[0..-2].join('.') end |
#reflection ⇒ Object
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, * = . component.process context, controller, .first, end |
#to_s ⇒ Object
23 24 25 |
# File 'lib/puffer/field.rb', line 23 def to_s field_name end |
#type ⇒ Object
39 40 41 |
# File 'lib/puffer/field.rb', line 39 def type @type ||= [:type] || custom_type || column_type || :string end |