Module: MotionPrime::ModelSyncMixin::ClassMethods

Defined in:
motion-prime/models/_sync_mixin.rb

Instance Method Summary collapse

Instance Method Details

#fetch(id, options = {}, &block) ⇒ Object

Fetch model from server

Parameters:

  • id (Integer)

    model id

  • options (Hash) (defaults to: {})

    fetch options

  • block (Proc)

    block to be executed after fetch

Options Hash (options):

  • :method (Symbol)

    Http method to calculate url, ‘:get` by default

  • :associations (Boolean or Array)

    Also fetch associations

  • :save (Boolean)

    Save model after fetch



391
392
393
394
# File 'motion-prime/models/_sync_mixin.rb', line 391

def fetch(id, options = {}, &block)
  model = self.new(id: id)
  model.fetch(options, &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, options = {}, &block)
  fetch(id, options.merge(save: true), &block)
end

#fetch_all(options = {}, &block) ⇒ Object

Fetch collection from server

Parameters:

  • options (Hash) (defaults to: {})

    fetch options

  • block (Proc)

    block to be executed after fetch

Options Hash (options):

  • :method (Symbol)

    Http method to calculate url, ‘:get` by default

  • :save (Boolean)

    Save model after fetch



407
408
409
410
411
412
413
414
415
# File 'motion-prime/models/_sync_mixin.rb', line 407

def fetch_all(options = {}, &block)
  use_callback = block_given?
  url = self.new.sync_url(options[:method] || :get, options)

  fetch_all_with_url url, options do |records, status_code, response|
    records.each(&:save) if options[: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.

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :save_associations (Boolean)

    Save included to hash associations



439
440
441
442
443
444
445
# File 'motion-prime/models/_sync_mixin.rb', line 439

def fetch_all_with_attributes(data, options ={}, &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

Parameters:

  • url (String)

    url to fetch

  • block (Proc)

    block to be executed after fetch



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, options = {}, &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: options[: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 = {}, options = {})
  model = super
  if fetch_attributes = options[: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, options = {}, &block)
  options[:block] = block if block_given?
  self._updatable_attributes ||= {}
  self._updatable_attributes[attribute] = options
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