Module: Houdini::Model

Extended by:
ActiveSupport::Concern
Defined in:
lib/houdini_rails/model.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#call_after_submit(task_name) ⇒ Object



54
55
56
57
# File 'lib/houdini_rails/model.rb', line 54

def call_after_submit(task_name)
  houdini_task = self.class.houdini_tasks[task_name.to_sym]
  self.send(houdini_task.after_submit) if houdini_task.after_submit
end

#generate_form_html(template_path) ⇒ Object



59
60
61
62
63
# File 'lib/houdini_rails/model.rb', line 59

def generate_form_html(template_path)
  template = Tilt.new(File.join(Rails.root.to_s, template_path))
  # TODO: don't force the template name
  template.render(self, self.class.name.downcase.to_sym => self)
end

#process_postback(task_name, answer) ⇒ Object



49
50
51
52
# File 'lib/houdini_rails/model.rb', line 49

def process_postback(task_name, answer)
  houdini_task = self.class.houdini_tasks[task_name.to_sym]
  self.send(houdini_task.on_task_completion, answer)
end

#send_to_houdini(task_name) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/houdini_rails/model.rb', line 26

def send_to_houdini(task_name)
  houdini_task = self.class.houdini_tasks[task_name.to_sym]
  params = {
    :api_key => Houdini::KEY,
    :identifier => houdini_task.name,
    :postback_url => houdini_postbacks_url(self.class.name, self.id, houdini_task.name, :host => Houdini::RAILS_HOST)
  }
  params[:price] = houdini_task.price if houdini_task.price
  params[:title] = houdini_task.title if houdini_task.title
  params[:form_html] = generate_form_html(houdini_task.form_template) if houdini_task.form_template
  if houdini_task.text #TODO: Refactor this logic and make available on all params
    params[:text] = houdini_task.text
    params[:text] = houdini_task.text.call if houdini_task.text.respond_to?(:call)
    params[:text] = self.send(houdini_task.text) if self.respond_to?(houdini_task.text)
  end
  params[:matched_answers_size] = houdini_task.matched_answers_size if houdini_task.matched_answers_size
  params[:max_iterations] = houdini_task.max_iterations if houdini_task.max_iterations

  result = Houdini::Base.request(houdini_task.api, params)

  call_after_submit(task_name)
end