Module: Connections::Connector::ClassMethods

Defined in:
lib/connections/connector.rb

Instance Method Summary collapse

Instance Method Details

#connects_with(*types) ⇒ Object



6
7
8
9
10
11
12
13
14
15
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
52
53
54
55
56
# File 'lib/connections/connector.rb', line 6

def connects_with(*types)
  has_many :connections, :as => :connector, :dependent => :delete_all, :class_name => 'Connections::Connection'
  types.each do |t|
    class_eval do

      # user.follows, requires a Follow model
      has_many :"#{t.to_s.pluralize}", :as => :connector

      # user.follow(other_user)
      define_method t do |connectable|
        Connections::Connection.create do |c|
          c.type = t.to_s.classify
          c.connector = self
          c.connectable = connectable
        end
      end

      # user.unfollow(other_user)
      define_method :"un#{t}" do |connectable|
        scope = active_connections(t, connectable)
        Object.const_defined?(t.to_s.classify) ? scope.destroy_all : scope.delete_all
      end

      # user.follows?(other_user)
      define_method :"#{t.to_s.pluralize}?" do |connectable|
        active_connections(t, connectable).exists?
      end

      # user.toggle_follow(other_user)
      define_method :"toggle_#{t}" do |connectable|
        if send("#{t.to_s.pluralize}?", connectable)
          send("un#{t}", connectable)
        else
          send(t, connectable)
        end
      end

      # user.following(:user)
      define_method :"#{t.to_s.sub(/e$/,'')}ing" do |*args|
        class_name = args.first
        if class_name
          klass = class_name.to_s.classify.constantize
          klass.joins(:incoming_connections).where("connections_connections.type = ? AND connector_type = ? AND connector_id = ?", t.to_s.classify, self.class.base_class.to_s, self)
        else
          connections.where('connections_connections.type = ?', t.to_s.classify)
        end
      end

    end
  end
end