Module: Solve360::Item

Included in:
Company, Contact
Defined in:
lib/solve360/item.rb

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#createdObject

Base Item fields



11
12
13
# File 'lib/solve360/item.rb', line 11

def created
  @created
end

#fieldsObject

Base item collections



14
15
16
# File 'lib/solve360/item.rb', line 14

def fields
  @fields
end

#flaggedObject

Base Item fields



11
12
13
# File 'lib/solve360/item.rb', line 11

def flagged
  @flagged
end

#idObject

Base Item fields



11
12
13
# File 'lib/solve360/item.rb', line 11

def id
  @id
end

#nameObject

Base Item fields



11
12
13
# File 'lib/solve360/item.rb', line 11

def name
  @name
end

#ownershipObject

Base Item fields



11
12
13
# File 'lib/solve360/item.rb', line 11

def ownership
  @ownership
end

Base item collections



14
15
16
# File 'lib/solve360/item.rb', line 14

def related_items
  @related_items
end

Base item collections



14
15
16
# File 'lib/solve360/item.rb', line 14

def related_items_to_add
  @related_items_to_add
end

#typeidObject

Base Item fields



11
12
13
# File 'lib/solve360/item.rb', line 11

def typeid
  @typeid
end

#updatedObject

Base Item fields



11
12
13
# File 'lib/solve360/item.rb', line 11

def updated
  @updated
end

#viewedObject

Base Item fields



11
12
13
# File 'lib/solve360/item.rb', line 11

def viewed
  @viewed
end

Class Method Details

.included(model) ⇒ Object



4
5
6
7
8
# File 'lib/solve360/item.rb', line 4

def self.included(model)
  model.extend ClassMethods
  model.send(:include, HTTParty)
  model.instance_variable_set(:@field_mapping, {})
end

Instance Method Details



96
97
98
# File 'lib/solve360/item.rb', line 96

def add_related_item(item)
  related_items_to_add << item
end

#initialize(attributes = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/solve360/item.rb', line 16

def initialize(attributes = {})
  attributes.symbolize_keys!
  
  self.fields = {}
  self.related_items = []
  self.related_items_to_add = []
  
  [:fields, :related_items].each do |collection|
    self.send("#{collection}=", attributes[collection]) if attributes[collection]
    attributes.delete collection
  end

  attributes.each do |key, value|
    self.send("#{key}=", value)
  end
end

#map_human_fieldsObject

See Also:

  • Base::map_human_attributes


34
35
36
# File 'lib/solve360/item.rb', line 34

def map_human_fields
  self.class.map_human_fields(self.fields)
end

#new_record?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/solve360/item.rb', line 71

def new_record?
  self.id == nil
end

#saveHash

Save the attributes for the current record to the CRM

If the record is new it will be created on the CRM

Returns:

  • (Hash)

    response values from API



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
# File 'lib/solve360/item.rb', line 43

def save
  response = []
  
  if self.ownership.blank?
    self.ownership = Solve360::Config.config.default_ownership
  end
  
  if new_record?
    response = self.class.request(:post, "/#{self.class.resource_name}", to_request)
    
    if !response["response"]["errors"]
      self.id = response["response"]["item"]["id"]
    end
  else
    response = self.class.request(:put, "/#{self.class.resource_name}/#{id}", to_request)
  end
  
  if response["response"]["errors"]
    message = response["response"]["errors"].map {|k,v| "#{k}: #{v}" }.join("\n")
    raise Solve360::SaveFailure, message
  else
    related_items.concat(related_items_to_add)

    response
  end

end

#to_requestObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/solve360/item.rb', line 75

def to_request
  xml = "<request>"
  
  xml << map_human_fields.collect {|key, value| "<#{key}>#{CGI.escapeHTML(value.to_s)}</#{key}>"}.join("")
  
  if related_items_to_add.size > 0
    xml << "<relateditems>"
    
    related_items_to_add.each do |related_item|
      xml << %Q{<add><relatedto><id>#{related_item["id"]}</id></relatedto></add>}
    end
    
    xml << "</relateditems>"
  end
  
  xml << "<ownership>#{ownership}</ownership>"
  xml << "</request>"
  
  xml
end