Module: SocialConnections::InstanceMethods

Defined in:
lib/social_connections/acts_as_connectable.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/social_connections/acts_as_connectable.rb', line 64

def method_missing(name, *args)
  if acts_as_connectable_verbs.include? name

    verb = name
    object = args[0]
    options = args[1] || {}

    # both the subject (us) and the object may have additional recipients,
    # we add them here
    options[:additional_recipients] ||= []
    options[:additional_recipients].concat(additional_recipients)
    options[:additional_recipients].concat(object.send(:additional_recipients)) if object.respond_to?( :additional_recipients, true )

    create_activity(verb, object, options)
  elsif acts_as_connectable_verb_questions.include? name
    verb = name[0..-2]
    object = args[0]
    SocialActivity.exists(self, verb, object)
  elsif verbs.collect {|v| (v.to_s + '_by_count').to_sym }.include? name
    verb = name.to_s.split(/_by_count/)
    object = self
    SocialActivity.objects_by_verb_count(object, verb)
  else
    super
  end
end

Instance Method Details

#acts_as_connectable_optionsObject



52
53
54
# File 'lib/social_connections/acts_as_connectable.rb', line 52

def acts_as_connectable_options
  self.class.acts_as_connectable_options
end

#acts_as_connectable_verb_questionsObject



60
61
62
# File 'lib/social_connections/acts_as_connectable.rb', line 60

def acts_as_connectable_verb_questions
  acts_as_connectable_verbs.collect {|v| (v.to_s + '?').to_sym }
end

#acts_as_connectable_verbsObject



56
57
58
# File 'lib/social_connections/acts_as_connectable.rb', line 56

def acts_as_connectable_verbs
  acts_as_connectable_options[:verbs] || [ :likes ]
end

#connect_to(other) ⇒ Object

Raises:

  • (ArgumentError)


31
32
33
34
35
36
37
38
# File 'lib/social_connections/acts_as_connectable.rb', line 31

def connect_to(other)
  raise ArgumentError.new("other cannot be nil") if other.nil?
  c = SocialConnection.new
  c.target = other
  c.source = self
  c.save
  c
end

#connected_to?(other) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
50
# File 'lib/social_connections/acts_as_connectable.rb', line 47

def connected_to?(other)
  SocialConnection.where("source_id = ? and source_type = ? and target_id = ? and target_type = ?",
                         self.id, self.class.base_class.name, other.id, other.class.base_class.name).exists?
end

#disconnect_from(other) ⇒ Object

Raises:

  • (ArgumentError)


40
41
42
43
44
45
# File 'lib/social_connections/acts_as_connectable.rb', line 40

def disconnect_from(other)
  raise ArgumentError.new("other cannot be nil") if other.nil?
  c = SocialConnection.by_source_and_target(self, other).first
  raise ArgumentError.new("cannot disconnect, connection doesn't exist. source=#{self}, target=#{other}") if c.nil?
  c.destroy
end

#incoming_social_connectionsObject



27
28
29
# File 'lib/social_connections/acts_as_connectable.rb', line 27

def incoming_social_connections
  SocialConnection.where("target_id = ? and target_type = ?", self.id, self.class.base_class.name)
end

#outgoing_social_connectionsObject



23
24
25
# File 'lib/social_connections/acts_as_connectable.rb', line 23

def outgoing_social_connections
  SocialConnection.where("source_id = ? and source_type = ?", self.id, self.class.base_class.name)
end