Module: MotionPrime::ModelSyncMixin::ClassMethods
- Defined in:
- motion-prime/models/_sync_mixin.rb
Instance Method Summary collapse
-
#fetch(id, options = {}, &block) ⇒ Object
Fetch model from server.
-
#fetch!(id, options = {}, &block) ⇒ Object
Fetch model from server and save on local.
-
#fetch_all(options = {}, &block) ⇒ Object
Fetch collection from server.
-
#fetch_all_with_attributes(data, options = {}, &block) ⇒ Object
Assign collection attributes, using fetch.
-
#fetch_all_with_url(url, options = {}, &block) ⇒ Object
Fetch collection from server using url.
- #new(data = {}, options = {}) ⇒ Object
- #sync_url(url = nil, &block) ⇒ Object
- #updatable_attribute(attribute, options = {}, &block) ⇒ Object
- #updatable_attributes(*attrs) ⇒ Object
Instance Method Details
#fetch(id, options = {}, &block) ⇒ Object
Fetch model from server
391 392 393 394 |
# File 'motion-prime/models/_sync_mixin.rb', line 391 def fetch(id, = {}, &block) model = self.new(id: id) model.fetch(, &block) end |
#fetch!(id, options = {}, &block) ⇒ Object
Fetch model from server and save on local
397 398 399 |
# File 'motion-prime/models/_sync_mixin.rb', line 397 def fetch!(id, = {}, &block) fetch(id, .merge(save: true), &block) end |
#fetch_all(options = {}, &block) ⇒ Object
Fetch collection from server
407 408 409 410 411 412 413 414 415 |
# File 'motion-prime/models/_sync_mixin.rb', line 407 def fetch_all( = {}, &block) use_callback = block_given? url = self.new.sync_url([:method] || :get, ) fetch_all_with_url url, do |records, status_code, response| records.each(&:save) if [:save] block.call(records, status_code, response) if use_callback end if !url.blank? end |
#fetch_all_with_attributes(data, options = {}, &block) ⇒ Object
Assign collection attributes, using fetch.
439 440 441 442 443 444 445 |
# File 'motion-prime/models/_sync_mixin.rb', line 439 def fetch_all_with_attributes(data, ={}, &block) data.map do |attrs| item = self.new item.fetch_with_attributes(attrs) item end end |
#fetch_all_with_url(url, options = {}, &block) ⇒ Object
Fetch collection from server using url
421 422 423 424 425 426 427 428 429 430 431 |
# File 'motion-prime/models/_sync_mixin.rb', line 421 def fetch_all_with_url(url, = {}, &block) use_callback = block_given? App.delegate.api_client.get(url) do |response, status_code| if response.present? records = fetch_all_with_attributes(response, save_associations: [:save], &block) else records = [] end block.call(records, status_code, response) if use_callback end end |
#new(data = {}, options = {}) ⇒ Object
447 448 449 450 451 452 453 |
# File 'motion-prime/models/_sync_mixin.rb', line 447 def new(data = {}, = {}) model = super if fetch_attributes = [:fetch_attributes] model.fetch_with_attributes(fetch_attributes) end model end |
#sync_url(url = nil, &block) ⇒ Object
455 456 457 458 459 460 461 |
# File 'motion-prime/models/_sync_mixin.rb', line 455 def sync_url(url = nil, &block) if url || block_given? self._sync_url = url || block else self._sync_url end end |
#updatable_attribute(attribute, options = {}, &block) ⇒ Object
470 471 472 473 474 |
# File 'motion-prime/models/_sync_mixin.rb', line 470 def updatable_attribute(attribute, = {}, &block) [:block] = block if block_given? self._updatable_attributes ||= {} self._updatable_attributes[attribute] = end |
#updatable_attributes(*attrs) ⇒ Object
463 464 465 466 467 468 |
# File 'motion-prime/models/_sync_mixin.rb', line 463 def updatable_attributes(*attrs) return self._updatable_attributes if attrs.blank? attrs.each do |attribute| updatable_attribute(attribute) end end |