has-relationship 0.0.3 → 0.0.4

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.3
1
+ 0.0.4
@@ -4,7 +4,7 @@ module HasRelationship
4
4
  types = [types] unless types.kind_of?(Array)
5
5
 
6
6
  types.each do |type|
7
- class_name = type.to_s.classify
7
+ class_name = options[:class_name] ? options[:class_name] : type.to_s.classify
8
8
 
9
9
  # default relationship: Class1_Class2, where Class1 is the class of this class,
10
10
  # Class2 is the class of that this class has a relationship with
@@ -26,7 +26,8 @@ module HasRelationship
26
26
  end
27
27
 
28
28
  relationship = options[:relationship] if options[:relationship]
29
- relationship_through = options[:singular] ? "relationship_through_#{relationship.to_s.tableize.singularize}".to_sym : "relationship_through_#{relationship.to_s.tableize}".to_sym
29
+ relationship = [relationship] unless relationship.kind_of?(Array)
30
+ relationship_through = options[:singular] ? "relationship_through_#{relationship.join("_").to_s.tableize.singularize}".to_sym : "relationship_through_#{relationship.join("_").to_s.tableize}".to_sym
30
31
 
31
32
  HasRelationship::Relationship.class_eval do
32
33
  belongs_to "relation2_#{class_name.downcase}".to_sym, :class_name => class_name, :foreign_key => "relation2_id"
@@ -39,10 +40,10 @@ module HasRelationship
39
40
  class_eval do
40
41
  if options[:singular]
41
42
  has_one relationship_through, :as => :relation1, :dependent => :destroy, :class_name => "HasRelationship::Relationship", :conditions => {:relationship => relationship}
42
- 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 = ?", class_name, relationship]
43
+ 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, relationship], :select => options[:select]
43
44
  else
44
45
  has_many relationship_through, :as => :relation1, :dependent => :destroy, :class_name => "HasRelationship::Relationship", :conditions => {:relationship => relationship}
45
- has_many resource_name, :through => relationship_through, :class_name => class_name, :source => "relation2_#{class_name.downcase}".to_sym, :conditions => ["relationships.relation2_type = ? and relationships.relationship = ?", class_name, relationship]
46
+ has_many resource_name, :through => relationship_through, :class_name => class_name, :source => "relation2_#{class_name.downcase}".to_sym, :conditions => ["relationships.relation2_type = ? and relationships.relationship in (?)", class_name, relationship], :select => options[:select]
46
47
  end
47
48
  end
48
49
  end
@@ -52,7 +53,7 @@ module HasRelationship
52
53
  types = [types] unless types.kind_of?(Array)
53
54
 
54
55
  types.each do |type|
55
- class_name = type.to_s.classify
56
+ class_name = options[:class_name] ? options[:class_name] : type.to_s.classify
56
57
 
57
58
  # default relationship: Class2_Class1, where Class1 is the class of this class,
58
59
  # Class2 is the class of that this class has a relationship with
@@ -73,7 +74,9 @@ module HasRelationship
73
74
  end
74
75
 
75
76
  relationship = options[:relationship] if options[:relationship]
76
- relationship_through = options[:singular] ? "inverse_relationship_through_#{relationship.to_s.tableize.singularize}".to_sym : "inverse_relationship_through_#{relationship.to_s.tableize}".to_sym
77
+ relationship = [relationship] unless relationship.kind_of?(Array)
78
+
79
+ relationship_through = options[:singular] ? "inverse_relationship_through_#{relationship.join("_").to_s.tableize.singularize}".to_sym : "inverse_relationship_through_#{relationship.join("_").to_s.tableize}".to_sym
77
80
 
78
81
  HasRelationship::Relationship.class_eval do
79
82
  belongs_to "relation1_#{class_name.downcase}".to_sym, :class_name => class_name, :foreign_key => "relation1_id"
@@ -86,10 +89,10 @@ module HasRelationship
86
89
  class_eval do
87
90
  if options[:singular]
88
91
  has_one relationship_through, :as => :relation2, :dependent => :destroy, :class_name => "HasRelationship::Relationship", :conditions => {:relationship => relationship}
89
- 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 = ?", class_name, relationship]
92
+ 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, relationship], :select => options[:select]
90
93
  else
91
94
  has_many relationship_through, :as => :relation2, :dependent => :destroy, :class_name => "HasRelationship::Relationship", :conditions => {:relationship => relationship}
92
- has_many resource_name, :through => relationship_through, :class_name => class_name, :source => "relation1_#{class_name.downcase}".to_sym, :conditions => ["relationships.relation1_type = ? and relationships.relationship = ?", class_name, relationship]
95
+ has_many 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, relationship], :select => options[:select]
93
96
  end
94
97
  end
95
98
  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: 25
4
+ hash: 23
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 3
10
- version: 0.0.3
9
+ - 4
10
+ version: 0.0.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - Andrew Hunter