cashctrl 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (130) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +418 -136
  3. data/cashctrl.gemspec +1 -1
  4. data/lib/cashctrl/client.rb +106 -103
  5. data/lib/cashctrl/collection.rb +6 -5
  6. data/lib/cashctrl/configuration.rb +1 -1
  7. data/lib/cashctrl/error.rb +3 -1
  8. data/lib/cashctrl/errors.rb +76 -0
  9. data/lib/cashctrl/localized_string.rb +2 -2
  10. data/lib/cashctrl/models/account.rb +5 -1
  11. data/lib/cashctrl/models/article.rb +9 -1
  12. data/lib/cashctrl/models/asset.rb +6 -1
  13. data/lib/cashctrl/models/attachment.rb +14 -0
  14. data/lib/cashctrl/models/bank.rb +3 -1
  15. data/lib/cashctrl/models/category.rb +1 -1
  16. data/lib/cashctrl/models/certificate_document.rb +1 -1
  17. data/lib/cashctrl/models/certificate_template.rb +1 -1
  18. data/lib/cashctrl/models/cost_center.rb +3 -1
  19. data/lib/cashctrl/models/currency.rb +1 -1
  20. data/lib/cashctrl/models/custom_field.rb +1 -1
  21. data/lib/cashctrl/models/custom_field_group.rb +1 -1
  22. data/lib/cashctrl/models/file.rb +2 -1
  23. data/lib/cashctrl/models/fiscal_period.rb +1 -1
  24. data/lib/cashctrl/models/fiscal_period_task.rb +1 -1
  25. data/lib/cashctrl/models/history.rb +1 -1
  26. data/lib/cashctrl/models/insurance_type.rb +1 -1
  27. data/lib/cashctrl/models/journal.rb +10 -1
  28. data/lib/cashctrl/models/journal_import.rb +1 -1
  29. data/lib/cashctrl/models/journal_import_entry.rb +1 -1
  30. data/lib/cashctrl/models/journal_item.rb +20 -0
  31. data/lib/cashctrl/models/loader.rb +3 -0
  32. data/lib/cashctrl/models/location.rb +1 -1
  33. data/lib/cashctrl/models/order.rb +8 -1
  34. data/lib/cashctrl/models/order_book_entry.rb +1 -1
  35. data/lib/cashctrl/models/order_document.rb +1 -1
  36. data/lib/cashctrl/models/order_item.rb +23 -0
  37. data/lib/cashctrl/models/order_layout.rb +1 -1
  38. data/lib/cashctrl/models/order_payment.rb +1 -1
  39. data/lib/cashctrl/models/person.rb +7 -1
  40. data/lib/cashctrl/models/report.rb +1 -1
  41. data/lib/cashctrl/models/report_collection.rb +1 -1
  42. data/lib/cashctrl/models/report_element.rb +1 -1
  43. data/lib/cashctrl/models/rounding.rb +2 -1
  44. data/lib/cashctrl/models/salary_book_entry.rb +1 -1
  45. data/lib/cashctrl/models/salary_certificate.rb +2 -1
  46. data/lib/cashctrl/models/salary_document.rb +1 -1
  47. data/lib/cashctrl/models/salary_field.rb +1 -1
  48. data/lib/cashctrl/models/salary_layout.rb +1 -1
  49. data/lib/cashctrl/models/salary_payment.rb +1 -1
  50. data/lib/cashctrl/models/salary_setting.rb +1 -1
  51. data/lib/cashctrl/models/salary_statement.rb +3 -1
  52. data/lib/cashctrl/models/salary_status.rb +1 -1
  53. data/lib/cashctrl/models/salary_sum.rb +1 -1
  54. data/lib/cashctrl/models/salary_template.rb +1 -1
  55. data/lib/cashctrl/models/salary_type.rb +1 -1
  56. data/lib/cashctrl/models/sequence_number.rb +1 -1
  57. data/lib/cashctrl/models/setting.rb +1 -1
  58. data/lib/cashctrl/models/tax.rb +2 -1
  59. data/lib/cashctrl/models/text.rb +1 -1
  60. data/lib/cashctrl/models/title.rb +1 -1
  61. data/lib/cashctrl/models/unit.rb +1 -1
  62. data/lib/cashctrl/relation.rb +271 -0
  63. data/lib/cashctrl/resource.rb +300 -57
  64. data/lib/cashctrl/resources/account/bank.rb +3 -13
  65. data/lib/cashctrl/resources/account/category.rb +3 -9
  66. data/lib/cashctrl/resources/account/cost_center/category.rb +3 -9
  67. data/lib/cashctrl/resources/account/cost_center.rb +3 -25
  68. data/lib/cashctrl/resources/account.rb +4 -30
  69. data/lib/cashctrl/resources/attachments.rb +32 -0
  70. data/lib/cashctrl/resources/base.rb +160 -84
  71. data/lib/cashctrl/resources/currency.rb +1 -10
  72. data/lib/cashctrl/resources/custom_field/group.rb +3 -11
  73. data/lib/cashctrl/resources/custom_field.rb +3 -15
  74. data/lib/cashctrl/resources/file/category.rb +4 -10
  75. data/lib/cashctrl/resources/file.rb +44 -28
  76. data/lib/cashctrl/resources/fiscal_period/task.rb +2 -12
  77. data/lib/cashctrl/resources/fiscal_period.rb +17 -59
  78. data/lib/cashctrl/resources/history.rb +3 -21
  79. data/lib/cashctrl/resources/import_session.rb +43 -0
  80. data/lib/cashctrl/resources/inventory/article/category.rb +3 -9
  81. data/lib/cashctrl/resources/inventory/article/import.rb +13 -0
  82. data/lib/cashctrl/resources/inventory/article.rb +3 -18
  83. data/lib/cashctrl/resources/inventory/asset/category.rb +3 -9
  84. data/lib/cashctrl/resources/inventory/asset.rb +3 -18
  85. data/lib/cashctrl/resources/inventory/unit.rb +1 -1
  86. data/lib/cashctrl/resources/journal/import/entry.rb +1 -22
  87. data/lib/cashctrl/resources/journal/import.rb +1 -7
  88. data/lib/cashctrl/resources/journal.rb +19 -57
  89. data/lib/cashctrl/resources/location.rb +1 -1
  90. data/lib/cashctrl/resources/order/book_entry.rb +1 -1
  91. data/lib/cashctrl/resources/order/category.rb +4 -15
  92. data/lib/cashctrl/resources/order/document.rb +13 -21
  93. data/lib/cashctrl/resources/order/layout.rb +1 -1
  94. data/lib/cashctrl/resources/order/payment.rb +9 -29
  95. data/lib/cashctrl/resources/order.rb +21 -68
  96. data/lib/cashctrl/resources/organization.rb +13 -0
  97. data/lib/cashctrl/resources/person/category.rb +3 -9
  98. data/lib/cashctrl/resources/person/import.rb +11 -0
  99. data/lib/cashctrl/resources/person/title.rb +1 -1
  100. data/lib/cashctrl/resources/person.rb +4 -23
  101. data/lib/cashctrl/resources/report/collection.rb +10 -42
  102. data/lib/cashctrl/resources/report/element.rb +14 -46
  103. data/lib/cashctrl/resources/report.rb +4 -30
  104. data/lib/cashctrl/resources/rounding.rb +1 -1
  105. data/lib/cashctrl/resources/salary/book_entry.rb +1 -1
  106. data/lib/cashctrl/resources/salary/category.rb +3 -9
  107. data/lib/cashctrl/resources/salary/certificate/document.rb +14 -39
  108. data/lib/cashctrl/resources/salary/certificate/template.rb +2 -8
  109. data/lib/cashctrl/resources/salary/certificate.rb +2 -12
  110. data/lib/cashctrl/resources/salary/document.rb +14 -34
  111. data/lib/cashctrl/resources/salary/field.rb +3 -17
  112. data/lib/cashctrl/resources/salary/insurance/type.rb +1 -1
  113. data/lib/cashctrl/resources/salary/layout.rb +1 -1
  114. data/lib/cashctrl/resources/salary/payment.rb +9 -29
  115. data/lib/cashctrl/resources/salary/setting.rb +1 -1
  116. data/lib/cashctrl/resources/salary/statement.rb +19 -54
  117. data/lib/cashctrl/resources/salary/status.rb +3 -11
  118. data/lib/cashctrl/resources/salary/sum.rb +1 -1
  119. data/lib/cashctrl/resources/salary/template.rb +2 -8
  120. data/lib/cashctrl/resources/salary/type.rb +1 -8
  121. data/lib/cashctrl/resources/sequence_number.rb +1 -7
  122. data/lib/cashctrl/resources/setting.rb +6 -24
  123. data/lib/cashctrl/resources/tax.rb +1 -1
  124. data/lib/cashctrl/resources/text.rb +1 -1
  125. data/lib/cashctrl/schema.yml +111 -0
  126. data/lib/cashctrl/version.rb +2 -2
  127. data/lib/cashctrl.rb +35 -25
  128. data/lib/generators/model_generator.rb +19 -7
  129. data/sig/cashctrl.rbs +1 -1
  130. metadata +12 -2
@@ -1,23 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Cashctrl
3
+ module CashCtrl
4
4
  module Resources
5
5
  class Account
6
6
  class Bank < Base
7
+ include Attachments
8
+
7
9
  api_path "account/bank"
8
10
  model_class Models::Bank
9
-
10
- class << self
11
- def update_attachments(id:, file_ids:)
12
- file_ids_param = Array(file_ids).join(",")
13
- client.post("#{api_path}/update_attachments.json", id: id, fileIds: file_ids_param)
14
- end
15
- end
16
-
17
- def update_attachments(id:, file_ids:)
18
- file_ids_param = Array(file_ids).join(",")
19
- client.post("#{api_path}/update_attachments.json", id: id, fileIds: file_ids_param)
20
- end
21
11
  end
22
12
  end
23
13
  end
@@ -1,20 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Cashctrl
3
+ module CashCtrl
4
4
  module Resources
5
5
  class Account
6
6
  class Category < Base
7
7
  api_path "account/category"
8
8
  model_class Models::Category
9
9
 
10
- class << self
11
- def tree
12
- client.get("#{api_path}/tree.json")
13
- end
14
- end
15
-
16
- def tree
17
- client.get("#{api_path}/tree.json")
10
+ def tree(id = nil)
11
+ tree_collection(client.get("#{api_path}/tree.json", api_params(id: id)))
18
12
  end
19
13
  end
20
14
  end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Cashctrl
3
+ module CashCtrl
4
4
  module Resources
5
5
  class Account
6
6
  class CostCenter
@@ -8,14 +8,8 @@ module Cashctrl
8
8
  api_path "account/costcenter/category"
9
9
  model_class Models::Category
10
10
 
11
- class << self
12
- def tree
13
- client.get("#{api_path}/tree.json")
14
- end
15
- end
16
-
17
- def tree
18
- client.get("#{api_path}/tree.json")
11
+ def tree(id = nil)
12
+ tree_collection(client.get("#{api_path}/tree.json", api_params(id: id)))
19
13
  end
20
14
  end
21
15
  end
@@ -1,31 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Cashctrl
3
+ module CashCtrl
4
4
  module Resources
5
5
  class Account
6
6
  class CostCenter < Base
7
+ include Attachments
8
+
7
9
  api_path "account/costcenter"
8
10
  model_class Models::CostCenter
9
11
 
10
- class << self
11
- def balance(id:, date: nil)
12
- params = { id: id }
13
- params[:date] = date if date
14
- result = client.get_raw("#{api_path}/balance", params)
15
- BigDecimal(result.to_s)
16
- end
17
-
18
- def categorize(ids:, target:)
19
- ids_param = Array(ids).join(",")
20
- client.post("#{api_path}/categorize.json", ids: ids_param, target: target)
21
- end
22
-
23
- def update_attachments(id:, file_ids:)
24
- file_ids_param = Array(file_ids).join(",")
25
- client.post("#{api_path}/update_attachments.json", id: id, fileIds: file_ids_param)
26
- end
27
- end
28
-
29
12
  def balance(id:, date: nil)
30
13
  params = { id: id }
31
14
  params[:date] = date if date
@@ -37,11 +20,6 @@ module Cashctrl
37
20
  ids_param = Array(ids).join(",")
38
21
  client.post("#{api_path}/categorize.json", ids: ids_param, target: target)
39
22
  end
40
-
41
- def update_attachments(id:, file_ids:)
42
- file_ids_param = Array(file_ids).join(",")
43
- client.post("#{api_path}/update_attachments.json", id: id, fileIds: file_ids_param)
44
- end
45
23
  end
46
24
  end
47
25
  end
@@ -1,36 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Cashctrl
3
+ module CashCtrl
4
4
  module Resources
5
5
  class Account < Base
6
+ include Attachments
7
+
6
8
  api_path "account"
7
9
  model_class Models::Account
8
10
 
9
- class << self
10
- def find_by_number(number)
11
- list.find { |account| account.number == number }
12
- end
13
-
14
- def balance(id:, date: nil)
15
- params = { id: id }
16
- params[:date] = date if date
17
- result = client.get_raw("#{api_path}/balance", params)
18
- BigDecimal(result.to_s)
19
- end
20
-
21
- def categorize(ids:, target:)
22
- ids_param = Array(ids).join(",")
23
- client.post("#{api_path}/categorize.json", ids: ids_param, target: target)
24
- end
25
-
26
- def update_attachments(id:, file_ids:)
27
- file_ids_param = Array(file_ids).join(",")
28
- client.post("#{api_path}/update_attachments.json", id: id, fileIds: file_ids_param)
29
- end
30
- end
31
-
32
11
  def find_by_number(number)
33
- list.find { |account| account.number == number }
12
+ all.find { |account| account.number.to_s == number.to_s }
34
13
  end
35
14
 
36
15
  def balance(id:, date: nil)
@@ -44,11 +23,6 @@ module Cashctrl
44
23
  ids_param = Array(ids).join(",")
45
24
  client.post("#{api_path}/categorize.json", ids: ids_param, target: target)
46
25
  end
47
-
48
- def update_attachments(id:, file_ids:)
49
- file_ids_param = Array(file_ids).join(",")
50
- client.post("#{api_path}/update_attachments.json", id: id, fileIds: file_ids_param)
51
- end
52
26
  end
53
27
  end
54
28
  end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CashCtrl
4
+ module Resources
5
+ module Attachments
6
+ def update_attachments(id:, file_ids:)
7
+ client.post("#{api_path}/update_attachments.json", id: id, fileIds: csv(file_ids))
8
+ end
9
+
10
+ def add_attachments(id:, file_ids:)
11
+ response = client.get("#{api_path}/read.json", id: id)
12
+ existing = Array(response.dig("data", "attachments")).map { |attachment| attachment["fileId"] }
13
+
14
+ update_attachments(id: id, file_ids: (existing + Array(file_ids)).uniq)
15
+ end
16
+
17
+ def attach(id, *files)
18
+ add_attachments(id: id, file_ids: files.flatten.map { |file| attachment_file_id(file) })
19
+ end
20
+
21
+ private
22
+
23
+ def attachment_file_id(file)
24
+ case file
25
+ when Integer then file
26
+ when Models::File then file.id
27
+ else File.with_client(client).upload(file)
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -1,81 +1,64 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Cashctrl
3
+ module CashCtrl
4
4
  module Resources
5
5
  class Base
6
- class << self
7
- def api_path(path = nil)
8
- if path
9
- @api_path = path
10
- else
11
- @api_path
12
- end
13
- end
14
-
15
- def model_class(klass = nil)
16
- if klass
17
- @model_class = klass
18
- else
19
- @model_class || Resource
20
- end
21
- end
22
-
23
- def client
24
- Cashctrl.client
25
- end
6
+ PAGE_SIZE = 100
26
7
 
27
- def list(**params)
28
- response = client.get("#{api_path}/list.json", params)
29
- Collection.from_response(response, resource_class: model_class)
8
+ def self.api_path(path = nil)
9
+ if path
10
+ @api_path = path
11
+ else
12
+ @api_path
30
13
  end
14
+ end
31
15
 
32
- def all(...)
33
- list(...)
16
+ def self.model_class(klass = nil)
17
+ if klass
18
+ @model_class = klass
19
+ else
20
+ @model_class || Resource
34
21
  end
22
+ end
35
23
 
36
- def read(id:)
37
- response = client.get("#{api_path}/read.json", id: id)
38
- model_class.from_response(response)
39
- end
24
+ def self.client
25
+ CashCtrl.client
26
+ end
40
27
 
41
- def find(id)
42
- read(id: id)
43
- end
28
+ def self.list_params(*names)
29
+ @list_params = own_list_params + names.map { |name| camelize_key(name).to_sym } if names.any?
30
+ inherited = superclass.respond_to?(:list_params) ? superclass.list_params : []
31
+ inherited + own_list_params
32
+ end
44
33
 
45
- def create(**params)
46
- response = client.post("#{api_path}/create.json", params)
47
- wrap_response(response)
48
- end
34
+ def self.own_list_params
35
+ @list_params || []
36
+ end
49
37
 
50
- def update(id:, **params)
51
- response = client.post("#{api_path}/update.json", { id: id, **params })
52
- wrap_response(response)
53
- end
38
+ def self.camelize_key(key)
39
+ key.to_s.gsub(/_([a-z])/) { Regexp.last_match(1).upcase }
40
+ end
54
41
 
55
- def delete(ids:)
56
- ids_param = Array(ids).join(",")
57
- client.post("#{api_path}/delete.json", ids: ids_param)
58
- end
42
+ list_params :query, :category_id, :fiscal_period_id, :only_active, :columns
59
43
 
60
- def first
61
- list.first
62
- end
44
+ def self.with_client(client)
45
+ resource = allocate
46
+ resource.send(:initialize, client)
47
+ resource
48
+ end
63
49
 
64
- def last
65
- list.last
66
- end
50
+ def self.new(attributes = {}, **)
51
+ with_client(client).build(attributes, **)
52
+ end
67
53
 
68
- private
54
+ def self.method_missing(name, ...)
55
+ return super unless public_method_defined?(name)
69
56
 
70
- def wrap_response(response)
71
- return response unless response.is_a?(Hash) && response["data"]
57
+ with_client(client).public_send(name, ...)
58
+ end
72
59
 
73
- if response["data"].is_a?(Array)
74
- Collection.from_response(response, resource_class: model_class)
75
- else
76
- model_class.from_response(response["data"])
77
- end
78
- end
60
+ def self.respond_to_missing?(name, include_private = false)
61
+ public_method_defined?(name) || super
79
62
  end
80
63
 
81
64
  attr_reader :client
@@ -92,68 +75,161 @@ module Cashctrl
92
75
  self.class.model_class
93
76
  end
94
77
 
78
+ def build(attributes = {}, **kwargs)
79
+ model_class.new(camelize_keys(normalize_attributes(attributes.merge(kwargs))), resource: self)
80
+ end
81
+
95
82
  def list(**params)
96
- response = client.get("#{api_path}/list.json", params)
97
- Collection.from_response(response, resource_class: model_class)
83
+ response = client.get("#{api_path}/list.json", api_params(**params))
84
+ Collection.from_response(response, resource_class: model_class, resource: self)
85
+ end
86
+
87
+ def all
88
+ Relation.new(self)
98
89
  end
99
90
 
100
- def all(...)
101
- list(...)
91
+ [
92
+ :where, :order, :limit, :offset, :first, :last, :count, :exists?, :pluck,
93
+ :find_by, :find_by!, :find_each, :in_batches, :find_in_batches, :delete_all, :destroy_all, :includes
94
+ ].each do |method_name|
95
+ define_method(method_name) do |*args, **kwargs, &block|
96
+ all.public_send(method_name, *args, **kwargs, &block)
97
+ end
98
+ end
99
+
100
+ def pageable?
101
+ method(:list).owner == Base
102
+ end
103
+
104
+ def list_param?(key)
105
+ self.class.list_params.include?(self.class.camelize_key(key).to_sym)
102
106
  end
103
107
 
104
108
  def read(id:)
105
109
  response = client.get("#{api_path}/read.json", id: id)
106
- model_class.from_response(response)
110
+ raise NotFoundError, "#{self.class.name} #{id} not found" unless response.is_a?(Hash) && response["data"]
111
+
112
+ model_class.from_response(response, resource: self)
113
+ rescue ValidationError => e
114
+ raise unless e.errors.empty?
115
+
116
+ raise NotFoundError.new(e.message, response: e.response)
117
+ end
118
+
119
+ def find(*ids)
120
+ many = ids.first.is_a?(Array) || ids.size > 1
121
+ ids = ids.flatten
122
+ raise ArgumentError, "find needs at least one id" if ids.empty?
123
+
124
+ many ? ids.map { |id| read(id: id) } : read(id: ids.first)
107
125
  end
108
126
 
109
- def find(id)
110
- read(id: id)
127
+ def create(...)
128
+ record = build(...)
129
+ record.save ? read_back(record) : record
111
130
  end
112
131
 
113
- def create(**params)
114
- response = client.post("#{api_path}/create.json", params)
115
- wrap_response(response)
132
+ def create!(...)
133
+ record = build(...)
134
+ record.save!
135
+ read_back(record)
136
+ end
137
+
138
+ def update(id, **changes)
139
+ record = find(id)
140
+ record.reload if record.update(normalize_attributes(changes))
141
+ record
142
+ end
143
+
144
+ def update!(id, **changes)
145
+ record = find(id)
146
+ record.update!(normalize_attributes(changes))
147
+ record.reload
116
148
  end
117
149
 
118
- def update(id:, **params)
119
- response = client.post("#{api_path}/update.json", { id: id, **params })
150
+ def replace(id, **attributes)
151
+ response = client.post("#{api_path}/update.json", camelize_keys({ id: id, **attributes }))
120
152
  wrap_response(response)
121
153
  end
122
154
 
123
- def delete(ids:)
124
- ids_param = Array(ids).join(",")
125
- client.post("#{api_path}/delete.json", ids: ids_param)
155
+ def delete(*ids)
156
+ client.post("#{api_path}/delete.json", ids: csv(ids.flatten))
126
157
  end
127
158
 
128
- def first
129
- list.first
159
+ def destroy(*ids)
160
+ records = find(*ids)
161
+ Array(records).each(&:destroy)
162
+ records
130
163
  end
131
164
 
132
- def last
133
- list.last
165
+ def save_record(record)
166
+ if record.new_record?
167
+ client.post("#{api_path}/create.json", record_attributes(record))
168
+ else
169
+ client.post("#{api_path}/update.json", { "id" => record.id, **record_attributes(record) })
170
+ end
134
171
  end
135
172
 
136
173
  def list_csv(**params)
137
- client.get_raw("#{api_path}/list.csv", params)
174
+ client.get_raw("#{api_path}/list.csv", api_params(**params))
138
175
  end
139
176
 
140
177
  def list_xlsx(**params)
141
- client.get_raw("#{api_path}/list.xlsx", params)
178
+ client.get_raw("#{api_path}/list.xlsx", api_params(**params))
142
179
  end
143
180
 
144
181
  def list_pdf(**params)
145
- client.get_raw("#{api_path}/list.pdf", params)
182
+ client.get_raw("#{api_path}/list.pdf", api_params(**params))
146
183
  end
147
184
 
148
185
  private
149
186
 
187
+ def normalize_attributes(attributes)
188
+ associations = model_class.associations
189
+
190
+ attributes.to_h do |key, value|
191
+ association = associations[key.to_sym]
192
+ next [key, value] unless association
193
+
194
+ [association[:foreign_key], value.is_a?(Resource) ? value.id : value]
195
+ end
196
+ end
197
+
198
+ def record_attributes(record)
199
+ record.api_attributes
200
+ end
201
+
202
+ def api_params(**params)
203
+ camelize_keys(params.compact)
204
+ end
205
+
206
+ def camelize_keys(params)
207
+ params.transform_keys { |key| self.class.camelize_key(key) }
208
+ end
209
+
210
+ def csv(values)
211
+ Array(values).join(",")
212
+ end
213
+
214
+ def tree_collection(response)
215
+ Collection.from_response(response, resource_class: model_class, resource: self)
216
+ end
217
+
218
+ def read_back(record)
219
+ return record if record.new_record?
220
+
221
+ record.reload
222
+ rescue NotImplementedError
223
+ record.response
224
+ end
225
+
150
226
  def wrap_response(response)
151
227
  return response unless response.is_a?(Hash) && response["data"]
152
228
 
153
229
  if response["data"].is_a?(Array)
154
- Collection.from_response(response, resource_class: model_class)
230
+ Collection.from_response(response, resource_class: model_class, resource: self)
155
231
  else
156
- model_class.from_response(response["data"])
232
+ model_class.from_response(response["data"], resource: self)
157
233
  end
158
234
  end
159
235
  end
@@ -1,20 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Cashctrl
3
+ module CashCtrl
4
4
  module Resources
5
5
  class Currency < Base
6
6
  api_path "currency"
7
7
  model_class Models::Currency
8
8
 
9
- class << self
10
- def exchange_rate(from:, to:, date: nil)
11
- params = { from: from, to: to }
12
- params[:date] = date if date
13
- result = client.get_raw("#{api_path}/exchangerate", params)
14
- BigDecimal(result.to_s)
15
- end
16
- end
17
-
18
9
  def exchange_rate(from:, to:, date: nil)
19
10
  params = { from: from, to: to }
20
11
  params[:date] = date if date
@@ -1,22 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Cashctrl
3
+ module CashCtrl
4
4
  module Resources
5
5
  class CustomField
6
6
  class Group < Base
7
7
  api_path "customfield/group"
8
8
  model_class Models::CustomFieldGroup
9
9
 
10
- class << self
11
- def reorder(ids:)
12
- ids_param = Array(ids).join(",")
13
- client.post("#{api_path}/reorder.json", ids: ids_param)
14
- end
15
- end
16
-
17
- def reorder(ids:)
18
- ids_param = Array(ids).join(",")
19
- client.post("#{api_path}/reorder.json", ids: ids_param)
10
+ def reorder(ids:, target:, before: nil)
11
+ client.post("#{api_path}/reorder.json", api_params(ids: csv(ids), target: target, before: before))
20
12
  end
21
13
  end
22
14
  end
@@ -1,29 +1,17 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Cashctrl
3
+ module CashCtrl
4
4
  module Resources
5
5
  class CustomField < Base
6
6
  api_path "customfield"
7
7
  model_class Models::CustomField
8
8
 
9
- class << self
10
- def types
11
- client.get("#{api_path}/types.json")
12
- end
13
-
14
- def reorder(ids:)
15
- ids_param = Array(ids).join(",")
16
- client.post("#{api_path}/reorder.json", ids: ids_param)
17
- end
18
- end
19
-
20
9
  def types
21
10
  client.get("#{api_path}/types.json")
22
11
  end
23
12
 
24
- def reorder(ids:)
25
- ids_param = Array(ids).join(",")
26
- client.post("#{api_path}/reorder.json", ids: ids_param)
13
+ def reorder(ids:, target:, before: nil)
14
+ client.post("#{api_path}/reorder.json", api_params(ids: csv(ids), target: target, before: before))
27
15
  end
28
16
  end
29
17
  end
@@ -1,20 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Cashctrl
3
+ module CashCtrl
4
4
  module Resources
5
- class CashctrlFile
5
+ class File
6
6
  class Category < Base
7
7
  api_path "file/category"
8
8
  model_class Models::Category
9
9
 
10
- class << self
11
- def tree
12
- client.get("#{api_path}/tree.json")
13
- end
14
- end
15
-
16
- def tree
17
- client.get("#{api_path}/tree.json")
10
+ def tree(id = nil)
11
+ tree_collection(client.get("#{api_path}/tree.json", api_params(id: id)))
18
12
  end
19
13
  end
20
14
  end