Module: Promiscuous::Subscriber::Model

Extended by:
ActiveSupport::Concern
Includes:
Envelope
Included in:
ActiveRecord
Defined in:
lib/promiscuous/subscriber/model.rb

Instance Method Summary collapse

Instance Method Details

#destroy_instanceObject



54
55
56
# File 'lib/promiscuous/subscriber/model.rb', line 54

def destroy_instance
  instance.destroy
end

#fetchObject



29
30
31
32
33
34
35
# File 'lib/promiscuous/subscriber/model.rb', line 29

def fetch
  case operation
  when :create  then fetch_new
  when :update  then fetch_existing
  when :destroy then fetch_existing
  end
end

#fetch_existingObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/promiscuous/subscriber/model.rb', line 13

def fetch_existing
  if foreign_key
    if klass.respond_to?("find_by_#{foreign_key}!")
      klass.__send__("find_by_#{foreign_key}!", id)
    elsif klass.respond_to?("find_by")
      klass.find_by(foreign_key => id)
    else
      record = klass.where(foreign_key => id).first
      raise self.class.missing_record_exception.new(klass, id) if record.nil?
      record
    end
  else
    klass.find(id)
  end
end

#fetch_newObject



5
6
7
8
9
10
11
# File 'lib/promiscuous/subscriber/model.rb', line 5

def fetch_new
  if foreign_key
    klass.new(foreign_key => id)
  else
    klass.new.tap { |o| o.id = id }
  end
end

#processObject



41
42
43
44
45
46
47
48
# File 'lib/promiscuous/subscriber/model.rb', line 41

def process
  super
  case operation
  when :create  then save_instance
  when :update  then save_instance
  when :destroy then destroy_instance
  end
end

#process_attributes?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/promiscuous/subscriber/model.rb', line 37

def process_attributes?
  operation != :destroy
end

#save_instanceObject



50
51
52
# File 'lib/promiscuous/subscriber/model.rb', line 50

def save_instance
  instance.save!
end