Module: Promiscuous::Subscriber::Model::Base::ClassMethods

Defined in:
lib/promiscuous/subscriber/model/base.rb

Defined Under Namespace

Classes: None

Instance Method Summary collapse

Instance Method Details

#__promiscuous_fetch_existing(id) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/promiscuous/subscriber/model/base.rb', line 88

def __promiscuous_fetch_existing(id)
  key = subscribe_foreign_key
  if promiscuous_root_class.respond_to?("find_by_#{key}!")
    promiscuous_root_class.__send__("find_by_#{key}!", id)
  elsif respond_to?("find_by")
    promiscuous_root_class.find_by(key => id)
  else
    instance = promiscuous_root_class.where(key => id).first
    raise __promiscuous_missing_record_exception.new(promiscuous_root_class, id) if instance.nil?
    instance
  end
end

#__promiscuous_fetch_new(id) ⇒ Object



84
85
86
# File 'lib/promiscuous/subscriber/model/base.rb', line 84

def __promiscuous_fetch_new(id)
  new.tap { |m| m.__send__("#{subscribe_foreign_key}=", id) }
end

#__promiscuous_missing_record_exceptionObject



80
81
82
# File 'lib/promiscuous/subscriber/model/base.rb', line 80

def __promiscuous_missing_record_exception
  None
end

#inherited(subclass) ⇒ Object



73
74
75
76
77
# File 'lib/promiscuous/subscriber/model/base.rb', line 73

def inherited(subclass)
  super
  subclass.subscribed_attrs = self.subscribed_attrs.dup
  subclass.register_klass
end

#register_klass(options = {}) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/promiscuous/subscriber/model/base.rb', line 65

def register_klass(options={})
  subscribe_as = options[:as].try(:to_s) || self.name
  return unless subscribe_as

  Promiscuous::Subscriber::Model.mapping[self.subscribe_from] ||= {}
  Promiscuous::Subscriber::Model.mapping[self.subscribe_from][subscribe_as] = self
end

#subscribe(*args) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/promiscuous/subscriber/model/base.rb', line 46

def subscribe(*args)
  options    = args.extract_options!
  attributes = args

  # TODO reject invalid options

  self.subscribe_foreign_key = options[:foreign_key] if options[:foreign_key]

  ([self] + descendants).each { |klass| klass.subscribed_attrs |= attributes }

  if self.subscribe_from && options[:from] && self.subscribe_from != options[:from]
    raise 'Subscribing from different publishers is not supported yet'
  end

  self.subscribe_from ||= options[:from].try(:to_s) || "*"

  self.register_klass(options)
end