Class: EasyForm::Field

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(builder, args, &block) ⇒ Field

Returns a new instance of Field.



4
5
6
7
8
9
# File 'lib/easy_form/field.rb', line 4

def initialize(builder, args, &block)
  @builder = builder
  @options = args.extract_options!
  @args    = args
  @block   = block
end

Instance Attribute Details

#argsObject

Returns the value of attribute args.



3
4
5
# File 'lib/easy_form/field.rb', line 3

def args
  @args
end

#builderObject

Returns the value of attribute builder.



3
4
5
# File 'lib/easy_form/field.rb', line 3

def builder
  @builder
end

#optionsObject

Returns the value of attribute options.



3
4
5
# File 'lib/easy_form/field.rb', line 3

def options
  @options
end

#typeObject

Returns the value of attribute type.



3
4
5
# File 'lib/easy_form/field.rb', line 3

def type
  @type
end

Instance Method Details

#fieldObject



11
12
13
# File 'lib/easy_form/field.rb', line 11

def field
  @args[1]
end

#input(options = {}) ⇒ Object



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

def input(options={})
  builder.send(*args, @options.merge(options), &@block)
end

#label_textObject



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

def label_text
  options[:label] || model.human_attribute_name(field)
end

#modelObject



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

def model
  builder.object.class
end

#required?Boolean

Returns:

  • (Boolean)


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

def required?
  field && model.validators_on(field).any?{ActiveRecord::Validations::PresenceValidator}
end

#template_nameObject



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

def template_name
  builder.options[:template] || options[:template] || :default
end

#to_sObject



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

def to_s
  EasyForm::Template.render(template_name, binding)
end