lockstep_rails 0.3.22

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 (174) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.md +216 -0
  4. data/Rakefile +31 -0
  5. data/app/assets/config/lockstep_rails_manifest.js +0 -0
  6. data/app/concepts/lockstep/active_records/association.rb +78 -0
  7. data/app/concepts/lockstep/api_record.rb +1118 -0
  8. data/app/concepts/lockstep/api_records/scopes.rb +20 -0
  9. data/app/concepts/lockstep/client.rb +162 -0
  10. data/app/concepts/lockstep/error.rb +50 -0
  11. data/app/concepts/lockstep/exceptions.rb +4 -0
  12. data/app/concepts/lockstep/query.rb +409 -0
  13. data/app/concepts/lockstep/query_methods.rb +75 -0
  14. data/app/concepts/lockstep/relation_array.rb +100 -0
  15. data/app/helpers/types.rb +3 -0
  16. data/app/models/lockstep/account.rb +15 -0
  17. data/app/models/lockstep/connection.rb +19 -0
  18. data/app/models/lockstep/contact.rb +9 -0
  19. data/app/models/lockstep/customer_summary.rb +6 -0
  20. data/app/models/lockstep/invoice.rb +7 -0
  21. data/app/models/lockstep/invoice_summary.rb +6 -0
  22. data/app/models/lockstep/invoices/address.rb +24 -0
  23. data/app/models/lockstep/note.rb +7 -0
  24. data/app/models/lockstep/payment.rb +7 -0
  25. data/app/models/lockstep/payment_summary.rb +6 -0
  26. data/app/models/lockstep/user.rb +10 -0
  27. data/app/platform_api/model_template.rb.erb +27 -0
  28. data/app/platform_api/schema/action_result.rb +15 -0
  29. data/app/platform_api/schema/activity.rb +141 -0
  30. data/app/platform_api/schema/activity_fetch_result.rb +26 -0
  31. data/app/platform_api/schema/activity_stream_item.rb +58 -0
  32. data/app/platform_api/schema/activity_x_ref.rb +37 -0
  33. data/app/platform_api/schema/aging.rb +24 -0
  34. data/app/platform_api/schema/api_key.rb +71 -0
  35. data/app/platform_api/schema/api_key_fetch_result.rb +26 -0
  36. data/app/platform_api/schema/app_enrollment.rb +88 -0
  37. data/app/platform_api/schema/app_enrollment_custom_field.rb +67 -0
  38. data/app/platform_api/schema/app_enrollment_custom_field_fetch_result.rb +26 -0
  39. data/app/platform_api/schema/app_enrollment_fetch_result.rb +26 -0
  40. data/app/platform_api/schema/application.rb +89 -0
  41. data/app/platform_api/schema/application_fetch_result.rb +26 -0
  42. data/app/platform_api/schema/ar_aging_header_info.rb +47 -0
  43. data/app/platform_api/schema/ar_header_info.rb +118 -0
  44. data/app/platform_api/schema/assembly.rb +68 -0
  45. data/app/platform_api/schema/at_risk_invoice_summary.rb +90 -0
  46. data/app/platform_api/schema/at_risk_invoice_summary_fetch_result.rb +26 -0
  47. data/app/platform_api/schema/attachment.rb +92 -0
  48. data/app/platform_api/schema/attachment_fetch_result.rb +26 -0
  49. data/app/platform_api/schema/attachment_header_info.rb +41 -0
  50. data/app/platform_api/schema/batch_sync.rb +18 -0
  51. data/app/platform_api/schema/bulk_currency_conversion.rb +19 -0
  52. data/app/platform_api/schema/cashflow_report.rb +35 -0
  53. data/app/platform_api/schema/code_definition.rb +58 -0
  54. data/app/platform_api/schema/code_definition_fetch_result.rb +26 -0
  55. data/app/platform_api/schema/company.rb +243 -0
  56. data/app/platform_api/schema/company_fetch_result.rb +26 -0
  57. data/app/platform_api/schema/company_sync.rb +142 -0
  58. data/app/platform_api/schema/connector_info.rb +27 -0
  59. data/app/platform_api/schema/constructor_info.rb +128 -0
  60. data/app/platform_api/schema/contact.rb +148 -0
  61. data/app/platform_api/schema/contact_fetch_result.rb +26 -0
  62. data/app/platform_api/schema/contact_sync.rb +109 -0
  63. data/app/platform_api/schema/country.rb +62 -0
  64. data/app/platform_api/schema/country_fetch_result.rb +26 -0
  65. data/app/platform_api/schema/credit_memo_applied.rb +104 -0
  66. data/app/platform_api/schema/credit_memo_applied_fetch_result.rb +26 -0
  67. data/app/platform_api/schema/credit_memo_applied_sync.rb +67 -0
  68. data/app/platform_api/schema/credit_memo_invoice.rb +77 -0
  69. data/app/platform_api/schema/currency.rb +31 -0
  70. data/app/platform_api/schema/currency_fetch_result.rb +26 -0
  71. data/app/platform_api/schema/currency_rate.rb +28 -0
  72. data/app/platform_api/schema/custom_attribute_data.rb +18 -0
  73. data/app/platform_api/schema/custom_attribute_named_argument.rb +24 -0
  74. data/app/platform_api/schema/custom_attribute_typed_argument.rb +16 -0
  75. data/app/platform_api/schema/custom_field_definition.rb +76 -0
  76. data/app/platform_api/schema/custom_field_definition_fetch_result.rb +26 -0
  77. data/app/platform_api/schema/custom_field_sync.rb +71 -0
  78. data/app/platform_api/schema/custom_field_value.rb +75 -0
  79. data/app/platform_api/schema/custom_field_value_fetch_result.rb +26 -0
  80. data/app/platform_api/schema/customer_details.rb +98 -0
  81. data/app/platform_api/schema/customer_details_payment.rb +60 -0
  82. data/app/platform_api/schema/customer_summary.rb +88 -0
  83. data/app/platform_api/schema/customer_summary_fetch_result.rb +26 -0
  84. data/app/platform_api/schema/daily_sales_outstanding_report.rb +25 -0
  85. data/app/platform_api/schema/developer_account_submit.rb +23 -0
  86. data/app/platform_api/schema/email.rb +160 -0
  87. data/app/platform_api/schema/email_fetch_result.rb +26 -0
  88. data/app/platform_api/schema/erp.rb +23 -0
  89. data/app/platform_api/schema/erp_fetch_result.rb +26 -0
  90. data/app/platform_api/schema/erp_info.rb +18 -0
  91. data/app/platform_api/schema/erp_info_data.rb +23 -0
  92. data/app/platform_api/schema/event_info.rb +59 -0
  93. data/app/platform_api/schema/exception.rb +41 -0
  94. data/app/platform_api/schema/field_info.rb +105 -0
  95. data/app/platform_api/schema/financial_account.rb +90 -0
  96. data/app/platform_api/schema/financial_account_balance_history.rb +90 -0
  97. data/app/platform_api/schema/financial_account_balance_history_fetch_result.rb +26 -0
  98. data/app/platform_api/schema/financial_account_fetch_result.rb +26 -0
  99. data/app/platform_api/schema/financial_year_setting.rb +74 -0
  100. data/app/platform_api/schema/financial_year_setting_fetch_result.rb +26 -0
  101. data/app/platform_api/schema/invite.rb +25 -0
  102. data/app/platform_api/schema/invite_data.rb +18 -0
  103. data/app/platform_api/schema/invite_submit.rb +15 -0
  104. data/app/platform_api/schema/invoice.rb +226 -0
  105. data/app/platform_api/schema/invoice_address.rb +97 -0
  106. data/app/platform_api/schema/invoice_fetch_result.rb +29 -0
  107. data/app/platform_api/schema/invoice_history.rb +188 -0
  108. data/app/platform_api/schema/invoice_history_fetch_result.rb +26 -0
  109. data/app/platform_api/schema/invoice_line.rb +142 -0
  110. data/app/platform_api/schema/invoice_line_sync.rb +208 -0
  111. data/app/platform_api/schema/invoice_payment_detail.rb +65 -0
  112. data/app/platform_api/schema/invoice_summary.rb +85 -0
  113. data/app/platform_api/schema/invoice_summary_fetch_result.rb +26 -0
  114. data/app/platform_api/schema/invoice_sync.rb +280 -0
  115. data/app/platform_api/schema/lead.rb +32 -0
  116. data/app/platform_api/schema/member_info.rb +36 -0
  117. data/app/platform_api/schema/method_base.rb +128 -0
  118. data/app/platform_api/schema/method_info.rb +137 -0
  119. data/app/platform_api/schema/module.rb +44 -0
  120. data/app/platform_api/schema/module_handle.rb +15 -0
  121. data/app/platform_api/schema/note.rb +72 -0
  122. data/app/platform_api/schema/note_fetch_result.rb +26 -0
  123. data/app/platform_api/schema/parameter_info.rb +64 -0
  124. data/app/platform_api/schema/payment.rb +147 -0
  125. data/app/platform_api/schema/payment_applied.rb +95 -0
  126. data/app/platform_api/schema/payment_applied_fetch_result.rb +26 -0
  127. data/app/platform_api/schema/payment_applied_sync.rb +74 -0
  128. data/app/platform_api/schema/payment_detail.rb +110 -0
  129. data/app/platform_api/schema/payment_detail_fetch_result.rb +26 -0
  130. data/app/platform_api/schema/payment_detail_header.rb +43 -0
  131. data/app/platform_api/schema/payment_fetch_result.rb +26 -0
  132. data/app/platform_api/schema/payment_summary.rb +79 -0
  133. data/app/platform_api/schema/payment_summary_fetch_result.rb +26 -0
  134. data/app/platform_api/schema/payment_sync.rb +112 -0
  135. data/app/platform_api/schema/problem_details.rb +31 -0
  136. data/app/platform_api/schema/property_info.rb +60 -0
  137. data/app/platform_api/schema/provisioning.rb +28 -0
  138. data/app/platform_api/schema/provisioning_finalize_request.rb +28 -0
  139. data/app/platform_api/schema/provisioning_response.rb +42 -0
  140. data/app/platform_api/schema/risk_rate.rb +57 -0
  141. data/app/platform_api/schema/runtime_field_handle.rb +13 -0
  142. data/app/platform_api/schema/runtime_method_handle.rb +13 -0
  143. data/app/platform_api/schema/runtime_type_handle.rb +13 -0
  144. data/app/platform_api/schema/state.rb +22 -0
  145. data/app/platform_api/schema/state_fetch_result.rb +26 -0
  146. data/app/platform_api/schema/status.rb +72 -0
  147. data/app/platform_api/schema/struct_layout_attribute.rb +16 -0
  148. data/app/platform_api/schema/sync_entity_result.rb +34 -0
  149. data/app/platform_api/schema/sync_request.rb +71 -0
  150. data/app/platform_api/schema/sync_request_fetch_result.rb +26 -0
  151. data/app/platform_api/schema/sync_submit.rb +15 -0
  152. data/app/platform_api/schema/test_argument_exception.rb +41 -0
  153. data/app/platform_api/schema/test_timeout_exception.rb +41 -0
  154. data/app/platform_api/schema/transfer_owner.rb +16 -0
  155. data/app/platform_api/schema/transfer_owner_submit.rb +15 -0
  156. data/app/platform_api/schema/type.rb +278 -0
  157. data/app/platform_api/schema/type_info.rb +287 -0
  158. data/app/platform_api/schema/uri.rb +15 -0
  159. data/app/platform_api/schema/user_account.rb +152 -0
  160. data/app/platform_api/schema/user_account_fetch_result.rb +26 -0
  161. data/app/platform_api/schema/user_role.rb +50 -0
  162. data/app/platform_api/schema/user_role_fetch_result.rb +26 -0
  163. data/app/platform_api/schema/webhook.rb +96 -0
  164. data/app/platform_api/schema/webhook_fetch_result.rb +26 -0
  165. data/app/platform_api/schema/webhook_history_table_storage.rb +64 -0
  166. data/app/platform_api/schema/webhook_history_table_storage_fetch_result.rb +26 -0
  167. data/app/platform_api/swagger.json +22056 -0
  168. data/config/routes.rb +2 -0
  169. data/lib/lockstep_rails/engine.rb +4 -0
  170. data/lib/lockstep_rails/version.rb +3 -0
  171. data/lib/lockstep_rails.rb +5 -0
  172. data/lib/tasks/lockstep_rails_tasks.rake +4 -0
  173. data/lib/tasks/update_api_schema.rake +159 -0
  174. metadata +230 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 9061d2c0869b6f4178fcd240f0e1337b3cbdc62500d9c8922f9b6c73812359da
4
+ data.tar.gz: 2ca982684af590994f83a992494e527ef79a29b66102f27fe7021c4e39312e89
5
+ SHA512:
6
+ metadata.gz: b7e815ea5d2b411df62b258454d57437cd569181d8da0a3f2fb62b7666aa45dad0181845631e7d0d6f1c0ac0e517dbb00aba1a20075e444bbb94897eb8be5dea
7
+ data.tar.gz: 107df8a7b2031c9bd00170a64eb0bb0b98e59f3332d3a8bed509b49883b94c2a102ed01db8efd73b8ccecb8f1227162d64950d0d01787c3a3389c6f40cfee4c2
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2022 Vivek AG
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,216 @@
1
+ # Lockstep
2
+ A financial service SDK for rails for innovative accounting solutions providers.
3
+
4
+ ### Who are we?
5
+ Lockstep automates accounting workflows to improve your operational efficiency and cash flow. Accelerate payments through automated customer communications, enhanced collections activity management, and innovative forecasting and reporting.
6
+
7
+ ### lockstep_rails
8
+ lockstep_rails makes it easy to interact with lockstep.io's REST API. It adheres to the ActiveRecord pattern. LockstepRails is fully ActiveModel compliant, meaning you can use validations and Rails forms.
9
+
10
+ Ruby/Rails developers should feel right at home.
11
+
12
+ If you're used to `Lockstep::Contact.create(:email_address => "test@emaildomain.com")`, then this is for you.
13
+
14
+ ### Features
15
+ * ActiveRecord-like API, almost no learning curve
16
+ * Validations
17
+ * Rails forms and scaffolds **just work**
18
+
19
+ ## Installation
20
+ Add this line to your application's Gemfile:
21
+
22
+ ```ruby
23
+ gem 'lockstep_rails'
24
+ ```
25
+
26
+ And then execute:
27
+ ```bash
28
+ $ bundle
29
+ ```
30
+
31
+ Or install it yourself as:
32
+ ```bash
33
+ $ gem install lockstep_rails
34
+ ```
35
+
36
+ ## License
37
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
38
+
39
+ # Usage
40
+
41
+ ### Available Records
42
+ * Lockstep::Contact
43
+ * Lockstep::Connection
44
+ * Lockstep::Account
45
+ * Lockstep::User
46
+ * Lockstep::Note
47
+ * Lockstep::Invoice
48
+ * Lockstep::InvoiceSummary
49
+ * Lockstep::CustomerSummary
50
+ * Lockstep::Payments
51
+ * Lockstep::PaymentSummary
52
+
53
+ ## ActiveModel Interfaces
54
+
55
+ `lockstep_rails` supports the standard CRUD interface of ActiveRecord.
56
+
57
+ ### Create
58
+ You can create a record by calling `new` or `create`
59
+
60
+ ```ruby
61
+ Lockstep::Contact.create(email_address: "inbox@companyemail.com")
62
+ ```
63
+
64
+ You can create records from associations as well. This will automatically association the relationship with the parent record
65
+ ```ruby
66
+ connection = Lockstep::Connection.find(company_id: "17544da8-7be8-4ee2-8c62-cbd81428c68b")
67
+ connection.contacts.create(email_address: "inbox@companyemail.com")
68
+ ```
69
+
70
+ ### Update
71
+ Similar to ActiveRecord, you can update a record by calling `update`, `update_attributes` or `update_attribute`
72
+
73
+ Examples:
74
+ ```ruby
75
+ connection = Lockstep::Connection.find(company_id: "17544da8-7be8-4ee2-8c62-cbd81428c68b")
76
+ connection.update(company_name: "New name")
77
+
78
+ # Or
79
+ connection.update_attributes(company_name: "New name")
80
+
81
+ # Or
82
+ connection.update_attribute(:company_name, "New name")
83
+ ```
84
+
85
+ ### Find
86
+ Similar to ActiveRecord, you can find a record by calling `find`, `find_by` or `find_by_company_id`
87
+
88
+ Examples:
89
+ ```ruby
90
+ connection = Lockstep::Connection.find(company_id: "17544da8-7be8-4ee2-8c62-cbd81428c68b")
91
+ connection.update(company_name: "New name")
92
+
93
+ # Or
94
+ connection.update_attributes(company_name: "New name")
95
+
96
+ # Or
97
+ connection.update_attribute(:company_name, "New name")
98
+ ```
99
+
100
+ ### Where
101
+ Similar to ActiveRecord, you can search for records by calling `where`.
102
+
103
+ Unless you are accessing the record via `first`, you will have to call `execute` to fetch the records
104
+
105
+ Examples:
106
+ ```ruby
107
+ Lockstep::Connection.where(status: "Active").execute
108
+ ```
109
+
110
+ #### Chaining
111
+ You can chain queries while calling where.
112
+
113
+ ```ruby
114
+ Lockstep::Connection.where(status: "Active").where(default_currency_code: "USD").execute
115
+ ```
116
+
117
+ #### OR Query
118
+ You can build an or query by calling `or`
119
+
120
+ ```ruby
121
+ Lockstep::Connection.where(status: "Active").or(Lockstep::Connection.where(default_currency_code: "USD")).execute
122
+ ```
123
+
124
+ #### Advanced Queries
125
+ [Searchlight](https://developer.lockstep.io/docs/querying-with-searchlight) supports building complex comparisons in the query.
126
+
127
+ | Suffice | Searchlight Query | Example |
128
+ |--------------|-----------------------|---------------------------------------------------|
129
+ | _eq | Equals | where(name_eq: "Test Name") |
130
+ | _gt | Greater Than | where(amount_gt: 100) |
131
+ | _ge | Greater Than Or Equal | where(amount_ge: 100) |
132
+ | _lt | Less Than | where(amount_lt: 100) |
133
+ | _le | Less Than Or Equal | where(amount_le: 100) |
134
+ | _ne | Not Equal | where(name_ne: "tak") |
135
+ | _in | IN | where(name_in: ["tik", "tok"]) |
136
+ | _contains | CONTAINS | where(name_contains: "kit") |
137
+ | _starts_with | STARTSWITH | where(name_starts_with: "tik") |
138
+ | _ends_with | ENDSWITH | where(name_ends_with: "tok") |
139
+ | _is | IS | where(name_is: nil) where(name_is: "NOT NULL") |
140
+
141
+ #### Searchlight Queries
142
+ Using the rails standard queries generates searchlight compatible queries.
143
+ if you wish, you can always write raw [Searchlight](https://developer.lockstep.io/docs/querying-with-searchlight) queries by passing a string in the where block
144
+
145
+ ```ruby
146
+ Lockstep::Connection.where("isActive is true OR default_currency_code in ('USD', 'INR')").execute
147
+ ```
148
+
149
+ ### Count
150
+ You can fetch the count of available records by calling `count`
151
+
152
+ ```ruby
153
+ Lockstep::Connection.count
154
+ # 1000
155
+
156
+ Lockstep::Connection.where(status: "Active").count
157
+ # 100
158
+ ```
159
+
160
+ ### Limit
161
+ You can limit the number of records being fetched by passing `limit`
162
+
163
+ The default limit is 200.
164
+
165
+ ```ruby
166
+ Lockstep::Connection.limit(100).execute
167
+
168
+ Lockstep::Connection.where(status: "Active").limit(100).execute
169
+ ```
170
+
171
+ ### Pagination
172
+ You can paginate the records by passing the `page`
173
+
174
+ ```ruby
175
+ Lockstep::Connection.page(2).execute
176
+
177
+ Lockstep::Connection.where(status: "Active").limit(100).page(2).execute
178
+ ```
179
+
180
+ ## Adding relationships with ActiveRecord
181
+ If you would like to link the `Lockstep::ApiRecord` with an `ActiveRecord::Base`, you can do so by including `Lockstep::ActiveRecords::Association`
182
+
183
+ This gives you access to 2 methods for defining relationships
184
+ * lockstep_has_many
185
+ * lockstep_belongs_to
186
+
187
+ ```ruby
188
+ class User < ActiveRecord::Base
189
+ include Lockstep::ActiveRecords::Association
190
+
191
+ lockstep_has_many :connections, class_name: "Lockstep::Connection", primary_key: 'company_id', foreign_key: "lockstep_company_id"
192
+ lockstep_belongs_to :account, class_name: "Lockstep::Account", primary_key: 'company_id', foreign_key: "lockstep_company_id"
193
+
194
+ end
195
+ ```
196
+
197
+ #### Loader
198
+ You could define your own loader for fetching the records by passing a `loader` Proc
199
+
200
+ ```ruby
201
+ class User < ActiveRecord::Base
202
+ include Lockstep::ActiveRecords::Association
203
+
204
+ lockstep_has_many :connections, class_name: "Lockstep::Connection", loader: -> (record) { Lockstep::Connection.where(is_active: true).where(company_id: record.lockstep_company_id) }
205
+ lockstep_belongs_to :account, class_name: "Lockstep::Account", loader: -> (record) { Lockstep::Account.where(is_active: true).first }
206
+
207
+ end
208
+ ```
209
+
210
+ # Updating API Schema
211
+
212
+ To update the schema to the latest one on https://api.dev.lockstep.io/swagger/v1/swagger.json, run
213
+
214
+ ```bash
215
+ bundle exec rake update_api_schema
216
+ ```
data/Rakefile ADDED
@@ -0,0 +1,31 @@
1
+ begin
2
+ require "bundler/setup"
3
+ rescue LoadError
4
+ puts "You must `gem install bundler` and `bundle install` to run rake tasks"
5
+ end
6
+
7
+ require "rdoc/task"
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = "rdoc"
11
+ rdoc.title = "LockstepRails"
12
+ rdoc.options << "--line-numbers"
13
+ rdoc.rdoc_files.include("README.md")
14
+ rdoc.rdoc_files.include("lib/**/*.rb")
15
+ end
16
+
17
+ load "rails/tasks/statistics.rake"
18
+
19
+ require "bundler/gem_tasks"
20
+
21
+ require "rake/testtask"
22
+
23
+ Rake::TestTask.new(:test) do |t|
24
+ t.libs << "test"
25
+ t.pattern = "test/**/*_test.rb"
26
+ t.verbose = false
27
+ end
28
+
29
+ load "lib/tasks/update_api_schema.rake"
30
+
31
+ task default: :test
File without changes
@@ -0,0 +1,78 @@
1
+ module Lockstep::ActiveRecords::Association
2
+ extend ActiveSupport::Concern
3
+
4
+ included do
5
+
6
+ end
7
+
8
+ class_methods do
9
+ def lockstep_has_many_relations
10
+ @lockstep_has_many_relations ||= {}
11
+ end
12
+
13
+ def lockstep_belongs_to_relations
14
+ @lockstep_belongs_to_relations ||= {}
15
+ end
16
+
17
+ def lockstep_has_many(attribute, class_name:, primary_key: 'lockstep_id', foreign_key: nil, polymorphic: nil, loader: nil)
18
+ lockstep_has_many_relations[attribute] = {
19
+ name: attribute, class_name: class_name, primary_key: primary_key,
20
+ foreign_key: foreign_key, polymorphic: polymorphic, loader: loader
21
+ }
22
+
23
+ define_method(attribute) do
24
+ return lockstep_attributes[attribute] unless lockstep_attributes[attribute].nil?
25
+ return nil if lockstep_loaded_associations.include?(attribute)
26
+
27
+ lockstep_loaded_associations << attribute
28
+
29
+ relation_config = self.class.lockstep_has_many_relations[attribute]
30
+ if relation_config[:loader].present?
31
+ lockstep_attributes[attribute] = relation_config[:loader].call(self)
32
+ else
33
+ query = { relation_config[:foreign_key] => self.send(relation_config[:primary_key]) }
34
+ if relation_config[:polymorphic]
35
+ polymorphic_config = Lockstep::RelationArray.has_many_polymorphic_attributes(self, relation_config[:polymorphic])
36
+ query.merge!(polymorphic_config)
37
+ end
38
+ related_objects = relation_config[:class_name].constantize.send(:where, query).execute
39
+ lockstep_attributes[attribute] = Lockstep::RelationArray.new self, related_objects, attribute, relation_config[:class_name]
40
+ end
41
+
42
+ lockstep_attributes[attribute]
43
+ end
44
+ end
45
+
46
+ def lockstep_belongs_to(attribute, class_name:, primary_key: nil, foreign_key: 'lockstep_id', polymorphic: nil, loader: nil)
47
+ lockstep_belongs_to_relations[attribute] = {
48
+ name: attribute, class_name: class_name, primary_key: primary_key,
49
+ foreign_key: foreign_key, polymorphic: polymorphic, loader: loader
50
+ }
51
+
52
+ define_method(attribute) do
53
+ return lockstep_attributes[attribute] unless lockstep_attributes[attribute].nil?
54
+ return nil if lockstep_loaded_associations.include?(attribute)
55
+
56
+ lockstep_loaded_associations << attribute
57
+
58
+ relation_config = self.class.lockstep_belongs_to_relations[attribute]
59
+ if relation_config[:loader].present?
60
+ lockstep_attributes[attribute] = relation_config[:loader].call(self)
61
+ else
62
+ lockstep_attributes[attribute] = relation_config[:class_name].constantize.send(:find_by, relation_config[:primary_key] => self.send(relation_config[:foreign_key]))
63
+ end
64
+
65
+ lockstep_attributes[attribute]
66
+ end
67
+ end
68
+ end
69
+
70
+ def lockstep_loaded_associations
71
+ @loaded_associations ||= []
72
+ end
73
+
74
+ def lockstep_attributes
75
+ @lockstep_attributes ||= {}
76
+ end
77
+
78
+ end