Class: CaringForm::Field::Base

Inherits:
Struct
  • Object
show all
Defined in:
lib/caring_form/field/base.rb

Direct Known Subclasses

Collection, Dummy, Email, FullName, Metadata, String, Submit, Tel, Text, UsZipCode

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Base

Returns a new instance of Base.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/caring_form/field/base.rb', line 28

def initialize(*args)
  super

  self.type             ||= :string
  self.optional         ||= false
  self.input_tag        ||= 'input'
  self.wrapper_tag      ||= 'li'
  self.wrapper_html     ||= {}
  self.input_html       ||= {}
  self.label_html       ||= {}
  self.ancillary        ||= false
  self.required_message ||= "Enter the #{name.to_s.humanize.downcase}"

  self.label = default_label if self.label.nil?

  @referee = Tools::Referee.new(self)
end

Instance Attribute Details

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



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

def name
  @name
end

Class Method Details

.register(type) ⇒ Object



24
25
26
# File 'lib/caring_form/field/base.rb', line 24

def self.register(type)
  CaringForm::FieldContainer.register_field(self, type)
end

Instance Method Details

#apply_to_model(klass) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/caring_form/field/base.rb', line 46

def apply_to_model(klass)
  klass.send(:attr_accessor, name)
  options = {:message => required_message}
  if @referee.is_rule?(:optional)
    referee = @referee
    options[:if] = lambda do |form|
      !referee.decide(:optional, form)
    end
    klass.send(:validates_presence_of, name, options)
  elsif !optional
    options[:if] = self.if if self.if.present?
    klass.send(:validates_presence_of, name, options)
  end
end

#default_labelObject



99
100
101
# File 'lib/caring_form/field/base.rb', line 99

def default_label
  self.name.to_s.humanize.titleize
end

#input_properties(form, options = {}) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/caring_form/field/base.rb', line 61

def input_properties(form, options = {})
  options = normalize_options(form, options)
  req = {
    :as           => options.fetch(:as, simple_type),
    :collection   => options.fetch(:collection, collection),
    :prompt       => options.fetch(:prompt, prompt),
    :label        => options.fetch(:label, label),
    :wrapper      => options.fetch(:wrapper, wrapper),
    :wrapper_tag  => options.fetch(:wrapper_tag, wrapper_tag),
    :wrapper_html => options.fetch(:wrapper_html, wrapper_html).merge(:class => 'field'),
    :input_html   => options.fetch(:input_html, input_html),
    :label_html   => options.fetch(:label_html, label_html)
  }
  req[:input_html][:autofocus] = 'autofocus' if form.autofocus?(name)
  req[:input_html][:title] ||= required_message if required_message.present?
  if form.errors[self.name].present?
    req[:wrapper_html][:class] = "field-with-errors #{req[:wrapper_html][:class]}".rstrip
  end

  if form.index_on.present?
    req[:label_html][:index] = req[:input_html][:index] = form.index_on
  end
  if @referee.decide(:optional, form)
    req[:required] = false
  else
    req[:input_html][:required] = 'required'
  end
  req[:input_html].merge!(@referee.rule_as_data_attribute(:optional, form))
  req[:input_html][:name] = name if ancillary
  set_hint!(req)
  set_remote_validator!(req)
  req.deep_merge!(options)
end

#render(form, builder, options = {}) ⇒ Object



103
104
105
106
107
108
109
110
111
# File 'lib/caring_form/field/base.rb', line 103

def render(form, builder, options = {})
  properties = input_properties(form, options)
  if properties[:generate] == false
    ''
  else
    properties[:required] = true unless optional
    builder.input(name, properties)
  end
end

#simple_typeObject



95
96
97
# File 'lib/caring_form/field/base.rb', line 95

def simple_type
  raise "override me!"
end