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
has_many :"#{t.to_s.pluralize}", :as => :connector
define_method t do |connectable|
Connections::Connection.create do |c|
c.type = t.to_s.classify
c.connector = self
c.connectable = connectable
end
end
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
define_method :"#{t.to_s.pluralize}?" do |connectable|
active_connections(t, connectable).exists?
end
define_method :"toggle_#{t}" do |connectable|
if send("#{t.to_s.pluralize}?", connectable)
send("un#{t}", connectable)
else
send(t, connectable)
end
end
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
|