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_new(id) ⇒ Object



100
101
102
# File 'lib/promiscuous/subscriber/model/base.rb', line 100

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

#__promiscuous_missing_record_exceptionObject



96
97
98
# File 'lib/promiscuous/subscriber/model/base.rb', line 96

def __promiscuous_missing_record_exception
  None
end

#inherited(subclass) ⇒ Object



89
90
91
92
93
# File 'lib/promiscuous/subscriber/model/base.rb', line 89

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

#register_klass(options = {}) ⇒ Object



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

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



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/promiscuous/subscriber/model/base.rb', line 62

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