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
394 395 396 397 |
# File 'motion-prime/models/_sync_mixin.rb', line 394 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
400 401 402 |
# File 'motion-prime/models/_sync_mixin.rb', line 400 def fetch!(id, = {}, &block) fetch(id, .merge(save: true), &block) end |
#fetch_all(options = {}, &block) ⇒ Object
Fetch collection from server
410 411 412 413 414 415 416 417 418 |
# File 'motion-prime/models/_sync_mixin.rb', line 410 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.
442 443 444 445 446 447 448 |
# File 'motion-prime/models/_sync_mixin.rb', line 442 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
424 425 426 427 428 429 430 431 432 433 434 |
# File 'motion-prime/models/_sync_mixin.rb', line 424 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
450 451 452 453 454 455 456 |
# File 'motion-prime/models/_sync_mixin.rb', line 450 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
458 459 460 461 462 463 464 |
# File 'motion-prime/models/_sync_mixin.rb', line 458 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
473 474 475 476 477 |
# File 'motion-prime/models/_sync_mixin.rb', line 473 def updatable_attribute(attribute, = {}, &block) [:block] = block if block_given? self._updatable_attributes ||= {} self._updatable_attributes[attribute] = end |
#updatable_attributes(*attrs) ⇒ Object
466 467 468 469 470 471 |
# File 'motion-prime/models/_sync_mixin.rb', line 466 def updatable_attributes(*attrs) return self._updatable_attributes if attrs.blank? attrs.each do |attribute| updatable_attribute(attribute) end end |