Class: ActsAsIcontact::CustomField

Inherits:
Resource
  • Object
show all
Defined in:
lib/acts_as_icontact/resources/custom_field.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Resource

#==, all, #connection, #error, #errors, find, find_by_id, first, #method_missing, #new_record?, #save, #save!

Constructor Details

#initialize(properties = {}) ⇒ CustomField

Has a default fieldType of “text” and displayToUser of “false” if not overridden as options.



5
6
7
8
9
# File 'lib/acts_as_icontact/resources/custom_field.rb', line 5

def initialize(properties={})
  # Capture privateName as the ID field if it was passed in
  @customFieldId = properties["privateName"] || properties[:privateName]
  super({:fieldType => "text", :displayToUser => false}.merge(properties))
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class ActsAsIcontact::Resource

Class Method Details

.boolean_fieldsObject

Treats displayToUser as a boolean field (accepts true and false)



34
35
36
# File 'lib/acts_as_icontact/resources/custom_field.rb', line 34

def self.boolean_fields
  super << "displayToUser"
end

.find_by_string(value) ⇒ Object

Searches on privateName. For this class it’s the proper way to find by the primary key anyway.



24
25
26
# File 'lib/acts_as_icontact/resources/custom_field.rb', line 24

def self.find_by_string(value)
  find_by_id(value)
end

.listObject

Returns an array listing all custom fields. This is very convenient for certain tasks (e.g. the mapping in our Rails integration).



44
45
46
47
48
49
# File 'lib/acts_as_icontact/resources/custom_field.rb', line 44

def self.list
  list = []
  fields = self.all
  fields.each {|f| list << f.privateName}
  list
end

.primary_keyObject

Uses privateName as its primary key field



39
40
41
# File 'lib/acts_as_icontact/resources/custom_field.rb', line 39

def self.primary_key
  "privateName"
end

.required_on_createObject

Requires privateName, displayToUser, fieldType



29
30
31
# File 'lib/acts_as_icontact/resources/custom_field.rb', line 29

def self.required_on_create
  super + %w(privateName displayToUser fieldType)
end

Instance Method Details

#idObject

Uses privateName as its ID in resource URLs. The custom field resource is just weird that way.



12
13
14
# File 'lib/acts_as_icontact/resources/custom_field.rb', line 12

def id
  @customFieldId
end

#validate_on_save(fields) ⇒ Object

privateName must not contain certain punctuation; fieldType must be “text” or “checkbox”.



17
18
19
20
# File 'lib/acts_as_icontact/resources/custom_field.rb', line 17

def validate_on_save(fields)
  raise ActsAsIcontact::ValidationError, "privateName cannot contain spaces, quotes, slashes or brackets" if fields["privateName"] =~ /[\s\"\'\/\\\[\]]/
  raise ActsAsIcontact::ValidationError, "fieldType must be 'text' or 'checkbox'" unless fields["fieldType"] =~ /^(text|checkbox)$/
end