Module: Formidable

Defined in:
lib/formidable.rb,
lib/formidable/model.rb,
lib/formidable/timer.rb,
lib/formidable/config.rb,
lib/formidable/remote.rb,
lib/formidable/attempt.rb,
lib/formidable/railtie.rb,
lib/formidable/commands.rb,
lib/formidable/controller.rb,
lib/formidable/view_helpers.rb

Defined Under Namespace

Modules: Controller, Model, ViewHelpers Classes: Attempt, Commands, Config, Railtie, Remote, Timer

Constant Summary collapse

HOST =
"www.getformidable.com"
VERSION =
1
CONFIG_PATH =
"config/formidable.yml"
@@filtered_params =
[]

Class Method Summary collapse

Class Method Details

.clear_filtered_parametersObject



139
140
141
# File 'lib/formidable.rb', line 139

def clear_filtered_parameters
  @@filtered_params = []
end

.configure(args) ⇒ Object



131
132
133
# File 'lib/formidable.rb', line 131

def configure(args)
  Config.load(args)
end

.filter_parameters(*params) ⇒ Object



135
136
137
# File 'lib/formidable.rb', line 135

def filter_parameters(*params)
  @@filtered_params = @@filtered_params | params.flatten
end

.track(args) ⇒ Object



38
39
40
41
42
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
90
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
122
123
124
125
126
127
128
129
# File 'lib/formidable.rb', line 38

def track(args)
  return if Config.api_key.empty?

  raise "Must define a form." unless args[:form]

  errors = args[:errors] || {}
  errors = {:base => errors} if errors.any? and errors.kind_of?(Array)

  # values will be nil if errors and no args[:values]
  # we want this for later
  values =
  if Config.track_values and errors.any?
    args[:values]
  else
    {}
  end

  times = args[:times]
  total_time = args[:total_time]
  attempt = args[:attempt]

  if args[:timing_data]
    tt, t = Timer.parse(args[:timing_data])
    total_time ||= tt
    times ||= t
  end

  # get as much data as we can from the request object
  # don't override anything that was set
  request = Thread.current[:formidable_request]
  if request
    # Rails 3
    if request.env["action_dispatch.parameter_filter"]
      filter_parameters(request.env["action_dispatch.parameter_filter"])
    end

    # if values aren't set, get them from request and delete
    values ||= request.params.reject{|k, v| [:utf8, :action, :controller, :authenticity_token, :commit, :formidable].include?(k.to_sym) }

    if timing_data = request.params[:formidable]
      tt, t = Timer.parse(request.params[:formidable])
      total_time ||= tt
      times ||= t
    end
  end

  times ||= {}
  values ||= {}

  cookies = Thread.current[:formidable_cookies]
  if cookies
    attempt ||= Attempt.parse(cookies, args[:form], errors.empty?)
  end

  # filter values
  values.delete_if{|k,v| filter?(k)}

  # flatten everything
  flatten(errors, true)
  flatten(values)
  flatten(times)

  # delete values if no errors for field
  values.delete_if{|k,v| !errors[k] or errors[k].empty?}

  # time to strip the prefix
  prefix = args[:prefix]
  if prefix
    regex = Regexp.new('\A' + prefix.to_s + '\[([^\]]+)\]')
    remove_prefix(errors, regex)
    remove_prefix(values, regex)
    remove_prefix(times, regex)
  end

  data = {
    :form => args[:form],
    :errors => errors,
    :values => values,
    :times => times,
    :total_time => total_time,
    :attempt => attempt
  }

  if Config.thread
    Thread.new do
      Remote.send(data)
    end
    true
  else
    Remote.send(data)
  end
end