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
|
# File 'lib/dm-is-friendly/is/friendly.rb', line 6
def is_friendly(options = {})
options = {:require_acceptance => true, :friendship_class => "Friendship" }.merge(options)
list = self.name.split("::")
list.shift if DataMapper::Ext.blank?(list.first)
reference_model = self
reference_model_name = list.pop
reference_model_key = DataMapper::Inflector.underscore(reference_model_name).to_sym
namespace = list.empty? ? ::Object : DataMapper::Ext::Object.full_const_get(list.join('::'))
@friendly_config = FriendlyConfig.new(reference_model_name, "#{namespace}::#{options[:friendship_class]}", options[:require_acceptance])
def self.friendly_config; @friendly_config; end
DataMapper::Model.new(options[:friendship_class], namespace) do
if options[:require_acceptance]
property :accepted_at, DateTime
end
belongs_to reference_model_key, reference_model, :key => true
belongs_to :friend, :model => reference_model, :child_key => [:friend_id], :key => true
end
has n, :friendships, :model => options[:friendship_class]
has n, :friends_by_me, self, :through => :friendships, :via => reference_model_key
has n, :friended_by, self, :through => :friendships, :via => reference_model_key
include DataMapper::Is::Friendly::InstanceMethods
end
|