Module: DataMapper::Is::Friendly

Defined in:
lib/dm-is-friendly/is/friendly.rb

Defined Under Namespace

Modules: InstanceMethods Classes: FriendlyConfig

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.friendly_configObject



18
# File 'lib/dm-is-friendly/is/friendly.rb', line 18

def self.friendly_config; @friendly_config; end

Instance Method Details

#is_friendly(options = {}) ⇒ 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
# 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