has-relationship 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.6
1
+ 0.0.7
@@ -4,7 +4,8 @@ module HasRelationship
4
4
  types = [types] unless types.kind_of?(Array)
5
5
 
6
6
  types.each do |type|
7
- class_name = options[:class_name] ? options[:class_name] : type.to_s.classify
7
+ class_name = options[:class_name] ? options[:class_name].classify : type.to_s.classify
8
+ base_class = class_name.constantize.base_class.name.to_s
8
9
 
9
10
  # default relationship: Class1_Class2, where Class1 is the class of this class,
10
11
  # Class2 is the class of that this class has a relationship with
@@ -32,20 +33,26 @@ module HasRelationship
32
33
  relationship_through = options[:singular] ? "relationship_through_#{relationships.join("_").to_s.tableize.singularize}".to_sym : "relationship_through_#{relationships.join("_").to_s.tableize}".to_sym
33
34
 
34
35
  HasRelationship::Relationship.class_eval do
35
- belongs_to "relation2_#{class_name.downcase}".to_sym, :class_name => class_name, :foreign_key => "relation2_id"
36
-
37
36
  before_validation do
38
37
  self.relation2 = self.send("relation2_#{class_name.downcase}") unless self.relation2.present? or self.send("relation2_#{class_name.downcase}").nil?
39
38
  end
40
39
  end
41
40
 
42
41
  class_eval do
42
+ through_conditions = (relationship.kind_of?(Array) ? nil : {:relationship => relationship})
43
+
43
44
  if options[:singular]
44
- has_one relationship_through, :as => :relation1, :dependent => :destroy, :class_name => "HasRelationship::Relationship", :conditions => {:relationship => relationship}
45
- has_one resource_name, :through => relationship_through, :class_name => class_name, :source => "relation2_#{class_name.downcase}".to_s, :conditions => ["relationships.relation2_type = ? and relationships.relationship in (?)", class_name, relationships], :select => options[:select]
45
+ has_one relationship_through, :as => :relation1, :dependent => :destroy, :class_name => "HasRelationship::Relationship",
46
+ :conditions => through_conditions
47
+ has_one resource_name, :through => relationship_through, :class_name => class_name,
48
+ :source => :relation2, :source_type => base_class,
49
+ :conditions => ["relationships.relation2_type = ? and relationships.relationship in (?)", base_class, relationships], :select => options[:select]
46
50
  else
47
- has_many relationship_through, :as => :relation1, :dependent => :destroy, :class_name => "HasRelationship::Relationship", :conditions => {:relationship => relationship}
48
- has_many resource_name, :through => relationship_through, :readonly => false, :class_name => class_name, :source => "relation2_#{class_name.downcase}".to_sym, :conditions => ["relationships.relation2_type = ? and relationships.relationship in (?)", class_name, relationships], :select => options[:select]
51
+ has_many relationship_through, :as => :relation1, :dependent => :destroy, :class_name => "HasRelationship::Relationship",
52
+ :conditions => through_conditions
53
+ has_many resource_name, :through => relationship_through, :readonly => false, :class_name => class_name,
54
+ :source => :relation2, :source_type => base_class,
55
+ :conditions => ["relationships.relation2_type = ? and relationships.relationship in (?)", base_class, relationships], :select => options[:select]
49
56
  end
50
57
  end
51
58
  end
@@ -55,7 +62,8 @@ module HasRelationship
55
62
  types = [types] unless types.kind_of?(Array)
56
63
 
57
64
  types.each do |type|
58
- class_name = options[:class_name] ? options[:class_name] : type.to_s.classify
65
+ class_name = options[:class_name] ? options[:class_name].classify : type.to_s.classify
66
+ base_class = class_name.constantize.base_class.name.to_s
59
67
 
60
68
  # default relationship: Class2_Class1, where Class1 is the class of this class,
61
69
  # Class2 is the class of that this class has a relationship with
@@ -82,20 +90,26 @@ module HasRelationship
82
90
  relationship_through = options[:singular] ? "inverse_relationship_through_#{relationships.join("_").to_s.tableize.singularize}".to_sym : "inverse_relationship_through_#{relationships.join("_").to_s.tableize}".to_sym
83
91
 
84
92
  HasRelationship::Relationship.class_eval do
85
- belongs_to "relation1_#{class_name.downcase}".to_sym, :class_name => class_name, :foreign_key => "relation1_id"
86
-
87
93
  before_validation do
88
94
  self.relation1 = self.send("relation1_#{class_name.downcase}") unless self.relation1.present? or self.send("relation1_#{class_name.downcase}").nil?
89
95
  end
90
96
  end
91
97
 
92
98
  class_eval do
99
+ through_conditions = (relationship.kind_of?(Array) ? nil : {:relationship => relationship})
100
+
93
101
  if options[:singular]
94
- has_one relationship_through, :as => :relation2, :dependent => :destroy, :class_name => "HasRelationship::Relationship", :conditions => {:relationship => relationship}
95
- has_one resource_name, :through => relationship_through, :class_name => class_name, :source => "relation1_#{class_name.downcase}".to_sym, :conditions => ["relationships.relation1_type = ? and relationships.relationship in (?)", class_name, relationships], :select => options[:select]
102
+ has_one relationship_through, :as => :relation2, :dependent => :destroy, :class_name => "HasRelationship::Relationship",
103
+ :conditions => through_conditions
104
+ has_one resource_name, :through => relationship_through, :class_name => class_name,
105
+ :source => :relation1, :source_type => base_class,
106
+ :conditions => ["relationships.relation1_type = ? and relationships.relationship in (?)", base_class, relationships], :select => options[:select]
96
107
  else
97
- has_many relationship_through, :as => :relation2, :dependent => :destroy, :class_name => "HasRelationship::Relationship", :conditions => {:relationship => relationship}
98
- has_many resource_name, :through => relationship_through, :readonly => false, :class_name => class_name, :source => "relation1_#{class_name.downcase}".to_sym, :conditions => ["relationships.relation1_type = ? and relationships.relationship in (?)", class_name, relationships], :select => options[:select]
108
+ has_many relationship_through, :as => :relation2, :dependent => :destroy, :class_name => "HasRelationship::Relationship",
109
+ :conditions => through_conditions
110
+ has_many resource_name, :through => relationship_through, :readonly => false, :class_name => class_name,
111
+ :source => :relation1, :source_type => base_class,
112
+ :conditions => ["relationships.relation1_type = ? and relationships.relationship in (?)", base_class, relationships], :select => options[:select]
99
113
  end
100
114
  end
101
115
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: has-relationship
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
4
+ hash: 17
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 6
10
- version: 0.0.6
9
+ - 7
10
+ version: 0.0.7
11
11
  platform: ruby
12
12
  authors:
13
13
  - Andrew Hunter
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-04-27 00:00:00 -04:00
18
+ date: 2011-04-28 00:00:00 -04:00
19
19
  default_executable:
20
20
  dependencies: []
21
21