Module: HasRelationship::Base

Defined in:
lib/has_relationship/has_relationship.rb

Instance Method Summary collapse

Instance Method Details

#has_inverse_relationship(types, options = {}) ⇒ Object



55
56
57
58
59
60
61
62
63
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/has_relationship/has_relationship.rb', line 55

def has_inverse_relationship(types, options={})
  types = [types] unless types.kind_of?(Array)

  types.each do |type|
    class_name = options[:class_name] ? options[:class_name].classify : type.to_s.classify
    base_class = class_name.constantize.base_class.name.to_s

    # default relationship: Class2_Class1, where Class1 is the class of this class, 
    #   Class2 is the class of that this class has a relationship with
    relationship = "#{class_name}_#{self.to_s}"
  
    unless options[:singular].present?
      as = options[:as] ? options[:as].to_s : type.to_s
      options[:singular] = (as.pluralize != as) 
    end
  
    if options[:as]
      resource_name = options[:as].to_sym
      relationship = "#{options[:as].to_s.classify}_#{self.to_s}"
    elsif options[:singular]
      resource_name = type.to_s.tableize.singularize.to_sym
    else
      resource_name = type.to_s.tableize.to_sym
    end

    relationship = options[:relationship] if options[:relationship] 
    
    relationships = relationship.kind_of?(Array) ? relationship : [relationship]
    
    relationship_through = options[:singular] ? ("#{self.to_s}_#{resource_name}").to_s.tableize.singularize.to_sym : ("#{self.to_s}_#{resource_name}").to_s.tableize.to_sym
  
    class_eval do
      through_conditions = (relationship.kind_of?(Array) ? nil : {:relationship => relationship})
      
      if options[:singular]
        has_one relationship_through, :as => :relation2, :dependent => :destroy, :class_name => "HasRelationship::Relationship", 
                                      :conditions => through_conditions
        has_one resource_name, :through => relationship_through, :class_name => class_name, 
                               :source => :relation1, :source_type => base_class, 
                               :conditions => ["relationships.relation1_type = ? and relationships.relationship in (?)", base_class, relationships], :select => options[:select]
      else
        has_many relationship_through, :as => :relation2, :dependent => :destroy, :class_name => "HasRelationship::Relationship", 
                                       :conditions => through_conditions
        has_many resource_name, :through => relationship_through, :readonly => false, :class_name => class_name, 
                                :source => :relation1, :source_type => base_class, 
                                :conditions => ["relationships.relation1_type = ? and relationships.relationship in (?)", base_class, relationships], :select => options[:select]
      end
    end
  end
end

#has_relationship(types, options = {}) ⇒ Object



3
4
5
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
# File 'lib/has_relationship/has_relationship.rb', line 3

def has_relationship(types, options={})
  types = [types] unless types.kind_of?(Array)

  types.each do |type|
    class_name = options[:class_name] ? options[:class_name].classify : type.to_s.classify
    base_class = class_name.constantize.base_class.name.to_s

    # default relationship: Class1_Class2, where Class1 is the class of this class, 
    #   Class2 is the class of that this class has a relationship with
    relationship = "#{self.to_s}_#{class_name}"    
  
    unless options[:singular].present?
      as = options[:as] ? options[:as].to_s : type.to_s
    
      options[:singular] = (as.pluralize != as) 
    end
  
    if options[:as]
      resource_name = options[:as].to_sym
      relationship = "#{self.to_s}_#{options[:as].to_s.classify}"
    elsif options[:singular]
      resource_name = type.to_s.tableize.singularize.to_sym
    else
      resource_name = type.to_s.tableize.to_sym
    end

    relationship = options[:relationship] if options[:relationship] 
    
    relationships = relationship.kind_of?(Array) ? relationship : [relationship]
    
    relationship_through = options[:singular] ? ("#{self.to_s}_#{resource_name}").to_s.tableize.singularize.to_sym : ("#{self.to_s}_#{resource_name}").to_s.tableize.to_sym
  
    class_eval do
      through_conditions = (relationship.kind_of?(Array) ? nil : {:relationship => relationship})
      
      if options[:singular]
        has_one relationship_through, :as => :relation1, :dependent => :destroy, :class_name => "HasRelationship::Relationship", 
                                      :conditions => through_conditions
        has_one resource_name, :through => relationship_through, :class_name => class_name, 
                               :source => :relation2, :source_type => base_class, 
                               :conditions => ["relationships.relation2_type = ? and relationships.relationship in (?)", base_class, relationships], :select => options[:select]
      else
        has_many relationship_through, :as => :relation1, :dependent => :destroy, :class_name => "HasRelationship::Relationship", 
                                       :conditions => through_conditions
        has_many resource_name, :through => relationship_through, :readonly => false, :class_name => class_name, 
                                :source => :relation2, :source_type => base_class, 
                                :conditions => ["relationships.relation2_type = ? and relationships.relationship in (?)", base_class, relationships], :select => options[:select]
      end
    end
  end
end