Module: CatForms::Form::ClassMethods

Defined in:
lib/cat_forms.rb

Instance Method Summary collapse

Instance Method Details

#attribute(name, type, opts = {}) ⇒ Object

Overrides how Virtus handles attributes of the wrong type. Ideally virtus would coerce everything to the right type, use the default, or return nil. There’s a related discussion here: github.com/solnic/virtus/issues/99



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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
# File 'lib/cat_forms.rb', line 43

def attribute(name, type, opts = {})
  super(name, type, opts)
  define_method(name) do
    data = super()
    default_value = -> { opts[:default].respond_to?(:call) ? opts[:default].call : opts[:default] }

    # If they want a BigDecimal or Int, but data is a string, strip
    # the stuff of bad data and try to convert manually.
    if type == BigDecimal or type == Integer
      if data.class == String
        data.gsub!(/[^\d\.]/, '')
        if data.present?
          if type == BigDecimal
            data = BigDecimal.new(data)
          else
            data = Integer(data)
          end
        end
      end
    end

    #if type.class != Array and !data.is_a?(type) and !data.nil? and (type != CatForms::Boolean and !data.is_a?(Boolean))
      #puts "cat forms expected a #{type} for #{ name }, received #{ data.inspect }"
      #puts caller
    #end

    if type == CatForms::Boolean and (data == true or data == false)
      return data
    end

    if type.class == Array
      # Not validating more right now.
      return data
    end

    if data.is_a?(type)
      # Override for default string
      if type == String and opts.has_key?(:default) and data == ""
        return default_value.call
      else
        return data
      end
    end

    return default_value.call
  end
end

#custom_attribute(attribute_name) ⇒ Object



159
160
161
162
163
# File 'lib/cat_forms.rb', line 159

def custom_attribute attribute_name
  class_eval do
    attr_reader attribute_name
  end
end

#form_attribute(name, klass, options = {}) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/cat_forms.rb', line 127

def form_attribute name, klass, options={}
  # Setup the default values for each attribute.
  if !options.has_key?(:default)
    options[:default] =
      if klass.kind_of?(Array)
        []
      elsif klass == BigDecimal or klass == Boolean or klass == CatForms::Boolean
        nil
      elsif klass.respond_to?(:new)
        proc { klass.new }
      else
        nil
      end
  end
  attribute name, klass, options
  if options[:validations]
    validates(name, options[:validations])
  end

  # Define an association_attributes= method
  # Rails's fields_for looks for this.
  if klass.kind_of?(Array)
    define_method "#{name}_attributes=" do |hash|
      hash.each do |_index, opts|
        # If the array is nil, set it to an empty array.
        self.send("#{name}=", []) if self.send(name).nil?
        self.send(name) << klass.first.new(opts)
      end
    end
  end
end

#form_name(name) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/cat_forms.rb', line 91

def form_name name
  name = name.to_s

  def name.i18n_key
    self
  end

  def name.human
    self
  end

  def name.singular
    self
  end

  def name.plural
    self
  end

  def name.param_key
    self
  end

  self.class.instance_eval do
    define_method :model_name do
      name
    end
  end

  self._form_name = name
end

#model_nameObject



123
124
125
# File 'lib/cat_forms.rb', line 123

def model_name
  self._form_name || super
end

#validates_associated(*associations) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
# File 'lib/cat_forms.rb', line 165

def validates_associated(*associations)
  validates_each(associations) do |record, _associate_name, value|
    (value.respond_to?(:each) ? value : [value]).each do |rec|
      if rec && !rec.valid?
        rec.errors.each do |key, val|
          record.errors.add(key, val)
        end
      end
    end
  end
end