Module: Formidable::Model

Defined in:
lib/formidable/model.rb

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/formidable/model.rb', line 5

def included(base)
  base.class_eval do

    base.send :alias_method, :original_valid?, :valid?

    def valid?(*args)
      valid = original_valid?(*args)
      return valid unless @formidable

      model_name = self.class.name.underscore

      if valid
        errors = {}
      else
        error_fix = {}
        self.errors.each do |k,v|
          error_fix[k] ||= []
          error_fix[k] << v
        end
        errors = {"#{model_name}" => error_fix}
      end

      request = Thread.current[:formidable_request]
      values = request ? request.params : {"#{model_name}" => self.attributes}

      data = {
        :form => @formidable[:form],
        :errors => errors,
        :values => values,
        :prefix => model_name
      }

      Formidable.track(data)

      valid
    end # valid?

    def make_formidable(form)
      @formidable = {:form => form}
      self
    end # make_formidable
  end
end