Module: ActsAsMessageable::Model::ClassMethods

Defined in:
lib/acts-as-messageable/model.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) acts_as_messageable(options = {})

Method make ActiveRecord::Base object messageable



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/acts-as-messageable/model.rb', line 16

def acts_as_messageable(options = {})
    default_options = {
      :table_name => "messages",
      :class_name => "ActsAsMessageable::Message",
      :required => [:topic, :body],
      :dependent => :nullify,
      :group_messages => false,
    }
    options = default_options.merge(options)

    has_many  :received_messages_relation,
              :as => :received_messageable,
              :class_name => options[:class_name],
              :dependent => options[:dependent]
    has_many  :sent_messages_relation,
              :as => :sent_messageable,
              :class_name => options[:class_name],
              :dependent => options[:dependent]

    self.messages_class_name = options[:class_name].constantize
    self.messages_class_name.has_ancestry

    if self.messages_class_name.respond_to?(:table_name=)
      self.messages_class_name.table_name = options[:table_name]
      self.messages_class_name.initialize_scopes
    else
      self.messages_class_name.set_table_name(options[:table_name])
      ActiveSupport::Deprecation.warn("Calling set_table_name is deprecated. Please use `self.table_name = 'the_name'` instead.")
    end

    self.messages_class_name.required = Array.wrap(options[:required])
    self.messages_class_name.validates_presence_of self.messages_class_name.required
    self.group_messages = options[:group_messages]

    include ActsAsMessageable::Model::InstanceMethods
end

- (ActiveRecord::Base) resolve_active_record_ancestor

Method recognize real object class



55
56
57
# File 'lib/acts-as-messageable/model.rb', line 55

def resolve_active_record_ancestor
  self.reflect_on_association(:received_messages_relation).active_record
end