Class: CF::FormField

Inherits:
Object
  • Object
show all
Includes:
Client
Defined in:
lib/cf/form_field.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ FormField

Initializes a new “form” object

Usage of form.new(hash):

line = CF::Line.create("Digitize", "Survey") do |l|   
  CF::Station.create({:line => l, :type => "work"}) do |s|
    CF::TaskForm.create({:station => s, :title => "Enter text from a business card image", :instruction => "Describe"}) do |i|
      CF::FormField.new({:form => i, :label => "First Name", :field_type => "SA", :required => "true"})
    end
  end
end


36
37
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
# File 'lib/cf/form_field.rb', line 36

def initialize(options={})
  @form  = options[:form]
  @label        = options[:label]
  @field_type   = options[:field_type]
  @valid_type   = options[:valid_type]
  @required     = options[:required]
  
  if !@form.nil?
    options.delete(:form)
    party_param = 
    {
      :body => 
      {
        :api_key => CF.api_key,
        :form_field => options
      }
    }
    resp =  HTTParty.post("#{CF.api_url}#{CF.api_version}/lines/#{CF.}/#{@form.station.line['title'].downcase}/stations/#{@form.station.index}/form_fields.json",party_param)
    resp.parsed_response.each_pair do |k,v|
      self.send("#{k}=",v) if self.respond_to?(k)
    end
    if resp.code != 200
      self.errors = resp.parsed_response['error']['message']
    end
    self.form_field_params = options
    @form.station.form.form_fields = self
    return self
  else
    @form_field_params = options
  end
end

Instance Attribute Details

#errorsObject

Contains error message



25
26
27
# File 'lib/cf/form_field.rb', line 25

def errors
  @errors
end

#field_typeObject

field_type for “form” object, e.g. :field_type => “SA”



10
11
12
# File 'lib/cf/form_field.rb', line 10

def field_type
  @field_type
end

#form_field_paramsObject

Parameters for form fields



22
23
24
# File 'lib/cf/form_field.rb', line 22

def form_field_params
  @form_field_params
end

#idObject

ID of form field



16
17
18
# File 'lib/cf/form_field.rb', line 16

def id
  @id
end

#labelObject

Label for “form” object, e.g. :label => “First Name”



7
8
9
# File 'lib/cf/form_field.rb', line 7

def label
  @label
end

#requiredObject

required boolean either true or false, e.g. :required => “true” & if false then you don’t need to mention



13
14
15
# File 'lib/cf/form_field.rb', line 13

def required
  @required
end

#station_idObject

station id attribute required for API Calls



19
20
21
# File 'lib/cf/form_field.rb', line 19

def station_id
  @station_id
end

#valid_typeObject

field_type for “form” object, e.g. :field_type => “SA”



10
11
12
# File 'lib/cf/form_field.rb', line 10

def valid_type
  @valid_type
end

Instance Method Details

#to_sObject

:nodoc:



68
69
70
# File 'lib/cf/form_field.rb', line 68

def to_s # :nodoc:
  "{:id => => #{self.id}, :label => #{self.label}, :field_type => #{self.field_type}, :required => #{self.required}, :errors => #{self.errors}}"
end