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

  assign_options!(options)
  normalize_options!
end

Instance Attribute Details

#block ⇒ Object (readonly)

Returns the value of attribute block.



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

def block
  @block
end

#builder ⇒ Object (readonly)

Returns the value of attribute builder.



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

def builder
  @builder
end

#name ⇒ Object (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#options ⇒ Object (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#template ⇒ Object (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

#defaults ⇒ Object



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

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

#disabled? ⇒ Boolean

Returns:

  • (Boolean)


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

def disabled?
  options[:disabled]
end

#errors ⇒ Object



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

def errors
  error_keys.map { |key| builder.errors(key) }.flatten
end

#field ⇒ Object

Raises:

  • (NotImplementedError)


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

def field
  raise NotImplementedError
end

#form_group(opts = {}) ⇒ Object



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

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

#normalize_options! ⇒ Object



51
52
53
54
# File 'lib/trestle/form/field.rb', line 51

def normalize_options!
  extract_wrapper_options!
  assign_error_class!
end

#readonly? ⇒ Boolean

Returns:

  • (Boolean)


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

def readonly?
  options[:readonly] || admin.try(:readonly?)
end

#render ⇒ Object



29
30
31
32
33
# File 'lib/trestle/form/field.rb', line 29

def render
  form_group do
    field
  end
end