Class: Trestle::Form::Field

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(builder, template, name, options = {}, &block) ⇒ Field

Returns a new instance of Field.



8
9
10
11
12
13
# File 'lib/trestle/form/field.rb', line 8

def initialize(builder, template, name, options={}, &block)
  @builder, @template, @name, @block = builder, template, name, block

  @options = defaults.merge(options)
  extract_options!
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



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

def block
  @block
end

#builderObject (readonly)

Returns the value of attribute builder.



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

def builder
  @builder
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#templateObject (readonly)

Returns the value of attribute template.



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

def template
  @template
end

Instance Method Details

#defaultsObject



37
38
39
# File 'lib/trestle/form/field.rb', line 37

def defaults
  Trestle::Options.new(readonly: admin.readonly?)
end

#errorsObject



15
16
17
18
19
# File 'lib/trestle/form/field.rb', line 15

def errors
  errors = builder.errors(name)
  errors += builder.errors(name.to_s.sub(/_id$/, '')) if name.to_s =~ /_id$/
  errors
end

#extract_options!Object



41
42
43
# File 'lib/trestle/form/field.rb', line 41

def extract_options!
  @wrapper = extract_wrapper_options(*Fields::FormGroup::WRAPPER_OPTIONS).merge(options.delete(:wrapper))
end

#fieldObject

Raises:

  • (NotImplementedError)


33
34
35
# File 'lib/trestle/form/field.rb', line 33

def field
  raise NotImplementedError
end

#form_group(opts = {}) ⇒ Object



21
22
23
24
25
# File 'lib/trestle/form/field.rb', line 21

def form_group(opts={})
  @builder.form_group(name, @wrapper.merge(opts)) do
    yield
  end
end

#renderObject



27
28
29
30
31
# File 'lib/trestle/form/field.rb', line 27

def render
  form_group do
    field
  end
end