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



393
394
395
396
# File 'motion-prime/models/_sync_mixin.rb', line 393

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



399
400
401
# File 'motion-prime/models/_sync_mixin.rb', line 399

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



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

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



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

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



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, 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



449
450
451
452
453
454
455
# File 'motion-prime/models/_sync_mixin.rb', line 449

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



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