class MultipleMan::Subscribers::ModelSubscriber

Attributes

klass[W]
model_class[RW]
options[RW]

Public Class Methods

new(klass, options) click to toggle source
Calls superclass method MultipleMan::Subscribers::Base::new
# File lib/multiple_man/subscribers/model_subscriber.rb, line 4
def initialize(klass, options)
  self.model_class = klass
  super(options[:to] || klass.name)
  self.options = options
end

Public Instance Methods

create(payload) click to toggle source
# File lib/multiple_man/subscribers/model_subscriber.rb, line 12
def create(payload)
  id = payload[:id]
  model = find_model(id)
  MultipleMan::ModelPopulator.new(model, options[:fields]).populate(id: find_conditions(id), data: payload[:data])
  model.save!
end
Also aliased as: update, seed
destroy(payload) click to toggle source
# File lib/multiple_man/subscribers/model_subscriber.rb, line 22
def destroy(payload)
  model = find_model(payload[:id])
  model.destroy!
end
seed(payload)
Alias for: create
update(payload)
Alias for: create

Private Instance Methods

cleanse_id(hash) click to toggle source
# File lib/multiple_man/subscribers/model_subscriber.rb, line 37
def cleanse_id(hash)
  if hash.keys.length > 1 && hash.keys.include?("id")
    id = hash.delete("id")
    hash.merge("source_id" => id)
  else
    hash
  end
end
find_conditions(id) click to toggle source
# File lib/multiple_man/subscribers/model_subscriber.rb, line 33
def find_conditions(id)
  id.kind_of?(Hash) ? cleanse_id(id) : {multiple_man_identifier: id}
end
find_model(id) click to toggle source
# File lib/multiple_man/subscribers/model_subscriber.rb, line 29
def find_model(id)
  model_class.where(find_conditions(id)).first || model_class.new
end