Class: Exact::Base
- Inherits:
-
Object
show all
- Defined in:
- lib/exact/models/base.rb
Direct Known Subclasses
Account, BankAccount, CostCenter, CostUnit, GLAccount, Item, Layout, Project, SalesInvoice, SalesInvoiceLine, VATCode
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Instance Attribute Details
#client ⇒ Object
Returns the value of attribute client.
6
7
8
|
# File 'lib/exact/models/base.rb', line 6
def client
@client
end
|
Class Method Details
.all(client:) ⇒ Object
50
51
52
53
54
|
# File 'lib/exact/models/base.rb', line 50
def self.all(client:)
client.send(to_s.demodulize.pluralize)
result = client.execute
result.map! { |obj| Exact.const_get("#{to_s.demodulize}Mapping").convert obj }
end
|
.create(attributes: {}, client:) ⇒ Object
56
57
58
59
60
61
62
|
# File 'lib/exact/models/base.rb', line 56
def self.create(attributes: {}, client:)
exact_obj = Exactonline.const_get(exact_endpoint.singularize).new(attributes)
client.send("AddTo#{exact_endpoint}", exact_obj)
result = client.save_changes
result.map! { |obj| Exact.const_get("#{to_s.demodulize}Mapping").convert obj }
result.first
end
|
.create_client(access_token:, division:) ⇒ Object
40
41
42
|
# File 'lib/exact/models/base.rb', line 40
def self.create_client(access_token:, division:)
Exact::Client.new(access_token: access_token, division: division, service: exact_service, endpoint: exact_endpoint)
end
|
.destroy(id: nil, client:) ⇒ Object
117
118
119
120
121
122
123
124
125
126
127
|
# File 'lib/exact/models/base.rb', line 117
def self.destroy(id: nil, client:)
client.send(exact_endpoint).filter("#{exact_guid_attribute} eq guid'#{id}'")
result = client.execute
return false unless result.any?
client.delete_object(result.first)
client.save_changes
true
rescue OData::ServiceError => e
errors.add(:base, e.message)
false
end
|
.exact_endpoint ⇒ Object
30
31
32
33
34
|
# File 'lib/exact/models/base.rb', line 30
def self.exact_endpoint
name = Object.const_get("#{self}::EXACT_ENDPOINT") rescue nil
name = to_s.demodulize.pluralize.camelize unless name
name
end
|
.exact_guid_attribute ⇒ Object
14
15
16
|
# File 'lib/exact/models/base.rb', line 14
def self.exact_guid_attribute
Object.const_get("#{name}::EXACT_GUID")
end
|
.exact_service ⇒ Object
22
23
24
|
# File 'lib/exact/models/base.rb', line 22
def self.exact_service
Object.const_get("#{self}::EXACT_SERVICE")
end
|
.find(id:, client:) ⇒ Object
64
65
66
67
68
69
|
# File 'lib/exact/models/base.rb', line 64
def self.find(id:, client:)
client.send(exact_endpoint).filter("#{exact_guid_attribute} eq guid'#{id}'")
result = client.execute
result.map! { |obj| Exact.const_get("#{to_s.demodulize}Mapping").convert obj }
result.first
end
|
.find_by(field:, value:, guid: false, client:) ⇒ Object
71
72
73
74
75
76
77
78
79
|
# File 'lib/exact/models/base.rb', line 71
def self.find_by(field:, value:, guid: false, client:)
query = "#{field.capitalize} eq "
query << 'guid' if guid
query << "'#{value}'"
client.send(exact_endpoint).filter(query)
result = client.execute
result.map! { |obj| Exact.const_get("#{to_s.demodulize}Mapping").convert obj }
result.first
end
|
.update(id:, attributes: {}, client:) ⇒ Object
81
82
83
84
85
86
87
88
89
90
91
92
|
# File 'lib/exact/models/base.rb', line 81
def self.update(id:, attributes: {}, client:)
client.send(exact_endpoint).filter("#{exact_guid_attribute} eq guid'#{id}'")
result = client.execute
return false unless result.any?
exact_obj = result.first
attributes.each do |attribute, value|
exact_obj.send("#{attribute}=", value) if exact_obj.respond_to? attribute
end
client.update_object(exact_obj)
client.save_changes
true
end
|
Instance Method Details
#destroy(client:) ⇒ Object
129
130
131
132
133
134
135
136
137
138
139
|
# File 'lib/exact/models/base.rb', line 129
def destroy(client:)
client.send(self.class.exact_endpoint).filter("#{self.class.exact_guid_attribute} eq guid'#{guid}'")
result = client.execute
return false unless result.any?
client.delete_object(result.first)
client.save_changes
true
rescue OData::ServiceError => e
errors.add(:base, e.message)
false
end
|
#exact_endpoint ⇒ Object
36
37
38
|
# File 'lib/exact/models/base.rb', line 36
def exact_endpoint
self.class.exact_endpoint
end
|
#exact_guid_attribute ⇒ Object
18
19
20
|
# File 'lib/exact/models/base.rb', line 18
def exact_guid_attribute
self.class.exact_guid_attribute
end
|
#exact_service ⇒ Object
26
27
28
|
# File 'lib/exact/models/base.rb', line 26
def exact_service
self.class.exact_service
end
|
#guid ⇒ Object
10
11
12
|
# File 'lib/exact/models/base.rb', line 10
def guid
send(self.class.exact_guid_attribute)
end
|
#save(client:) ⇒ Object
108
109
110
111
112
113
114
115
|
# File 'lib/exact/models/base.rb', line 108
def save(client:)
if !guid.blank?
update(client: client)
else
self.attributes = self.class.create(attributes: attributes, client: client).attributes
self
end
end
|
#setup_client(access_token: nil, division: nil, reload: false) ⇒ Object
44
45
46
47
48
|
# File 'lib/exact/models/base.rb', line 44
def setup_client(access_token: nil, division: nil, reload: false)
return @client if @client.present? && !reload
return @client = self.class.create_client(access_token: access_token, division: division) if @client.nil? || reload
self
end
|
#update(client:) ⇒ Object
94
95
96
97
98
99
100
101
102
103
104
105
106
|
# File 'lib/exact/models/base.rb', line 94
def update(client:)
return false if guid.blank?
client.send(exact_endpoint).filter("#{exact_guid_attribute} eq guid'#{guid}'")
result = client.execute
return false unless result.any?
exact_obj = result.first
attributes.each do |attribute, value|
exact_obj.send("#{attribute}=", value) if exact_obj.respond_to? attribute
end
client.update_object(exact_obj)
client.save_changes
true
end
|