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