passive_record 0.4.11 → 0.4.12

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1df1e784cf91a0992f637e1d716f80e480735de6
4
- data.tar.gz: c42d72d513f9b2970ac550fac9ba4bf6cf17cf5f
3
+ metadata.gz: ca6a576aa3a31b7353bc0c5eb63341f499e8ae44
4
+ data.tar.gz: b341ffd2c74c73110f91c6de4f409cfe8705ffb1
5
5
  SHA512:
6
- metadata.gz: 2ac681f63db9780ce9604356125fe2d3e76efc029958e258c098da5a35599db0b871aa6fabe393dce3c6310d01bcd6f85758f7a63633ea695ad612f9d7905fd7
7
- data.tar.gz: b91ffa6952369232fd89ae1c88b18cedcd6274a554c52845ebc523fda6bb32aaa641dcec8eb3f6191eec141a8b16be165812f4828d9175d636b47083147fc070
6
+ metadata.gz: 11bb9c9f548185b19bf87c62264266d4529161f5b198f0b62e1bf85fd3dd8b1c63036a97b2d0ff818a6538a2f07e97303fe04d227012fff56ecb2cc0c64afe90
7
+ data.tar.gz: 99105eb8a1b8fbd8d50b166deaa98f7a671ab730736684d89a3c3e386a4fba242360f45fa1792d3be857adfa60d56c8ba9ac6838fb36620529f2877217a91a4d
@@ -82,14 +82,15 @@ module PassiveRecord
82
82
  end
83
83
 
84
84
  def has_many(collection_name_sym, opts={})
85
- target_class_name = opts.delete(:class_name) { (collection_name_sym.to_s).split('_').map(&:capitalize).join }
85
+ target_class_name = opts.delete(:class_name) { (collection_name_sym.to_s).split('_').map(&:capitalize).join.singularize }
86
86
  habtm = opts.delete(:habtm) { false }
87
87
 
88
88
  association = nil
89
89
  if opts.key?(:through)
90
90
  through_class_collection_name = opts.delete(:through)
91
91
 
92
- through_class_name = (through_class_collection_name.to_s).split('_').map(&:capitalize).join
92
+ through_class_name = (through_class_collection_name.to_s).split('_').map(&:capitalize).join.singularize
93
+ # binding.pry if through_class_collection_name == :blogs
93
94
  base_association = associations.detect { |assn| assn.child_class_name == through_class_name rescue false }
94
95
 
95
96
  association = HasManyThroughAssociation.new(
@@ -180,8 +181,8 @@ module PassiveRecord
180
181
  intended_module = module_name.constantize
181
182
 
182
183
  if (intended_module.const_get(inverse_habtm_join_class_name) rescue false)
183
- has_many inverse_habtm_join_class_name.underscore.pluralize.to_sym
184
- has_many collection_name_sym, :through => inverse_habtm_join_class_name.underscore.pluralize.to_sym, habtm: true
184
+ has_many inverse_habtm_join_class_name.underscore.to_sym
185
+ has_many collection_name_sym, :through => inverse_habtm_join_class_name.underscore.to_sym, habtm: true
185
186
  else
186
187
  auto_collection_sym = self.name.split('::').last.underscore.pluralize.to_sym
187
188
  eval <<-ruby
@@ -191,8 +192,8 @@ module PassiveRecord
191
192
  belongs_to :#{auto_collection_sym.to_s.singularize} # belongs_to :user
192
193
  end # end
193
194
  ruby
194
- has_many habtm_join_class_name.underscore.pluralize.to_sym
195
- has_many(collection_name_sym, :through => habtm_join_class_name.underscore.pluralize.to_sym, habtm: true)
195
+ has_many habtm_join_class_name.underscore.to_sym
196
+ has_many(collection_name_sym, :through => habtm_join_class_name.underscore.to_sym, habtm: true)
196
197
  end
197
198
  end
198
199
  end
@@ -79,6 +79,7 @@ module PassiveRecord
79
79
  end
80
80
 
81
81
  def intermediary_relation
82
+ # binding.pry
82
83
  @intermediary_relation ||= association.base_association.to_relation(parent_model)
83
84
  end
84
85
 
@@ -100,11 +101,29 @@ module PassiveRecord
100
101
  { association.through_class.to_s.singularize.to_sym => conds }
101
102
  end
102
103
  elsif intermediary_relation.association.is_a?(HasManyAssociation) # normal has many?
103
- intermediary_key = if association.habtm
104
+ intermediary_key = if association.is_a?(HasManyThroughAssociation)
105
+ ch = association.child_class_name.constantize
106
+ inverse_assn = ch.associations.detect { |assn|
107
+ if assn.is_a?(HasManyAssociation) || assn.is_a?(HasManyThroughAssociation)
108
+ assn.child_class_name == association.base_association.child_class_name
109
+ else # belongs to...
110
+ assn.parent_class_name == association.base_association.child_class_name
111
+ end
112
+ }
113
+
114
+ if inverse_assn.nil?
115
+ association.through_class.to_s.singularize.to_sym
116
+ elsif inverse_assn.is_a?(HasManyAssociation) || inverse_assn.is_a?(HasManyThroughAssociation)
117
+ inverse_assn.children_name_sym
118
+ else
119
+ inverse_assn.target_name_symbol
120
+ end
121
+ elsif association.habtm
104
122
  association.base_association.children_name_sym
105
123
  else
106
- intermediary_relation.association.children_name_sym.to_s.singularize.to_sym
124
+ association.base_association.children_name_sym.to_s.singularize.to_sym
107
125
  end
126
+
108
127
  nested_conds = { intermediary_key => { parent_model_id_field.to_sym => parent_model.id } }
109
128
 
110
129
  if nested_association.is_a?(HasManyThroughAssociation)
@@ -1,4 +1,4 @@
1
1
  module PassiveRecord
2
2
  # passive_record version
3
- VERSION = "0.4.11"
3
+ VERSION = "0.4.12"
4
4
  end
data/notes.md CHANGED
@@ -40,3 +40,37 @@ Post.where(blog: { feed: { channel: { stream: 890 }}})
40
40
  Post.where(published_at: 1.day.ago...Time.now, blog: { feed: { channel: { stream: { network_id: 1234 }}}})
41
41
 
42
42
 
43
+ ---------
44
+
45
+
46
+
47
+ Okay, so in this case we are trying to do `feed.posts.recent` ....
48
+
49
+ => #<struct PassiveRecord::Associations::HasManyThroughAssociation
50
+ parent_class=Feed,
51
+ child_class_name="Post",
52
+ target_name_symbol=:posts,
53
+ through_class=:blogs,
54
+ base_association=#<struct PassiveRecord::Associations::HasManyAssociation parent_class=Feed, child_class_name="Blog", children_name_sym=:blogs>,
55
+ habtm=false>
56
+
57
+ We are constructing a query on `Post`, and want to say {blog: {feed_id: ...}} ...
58
+
59
+ The through_class is :blogs which needs to be singularized
60
+
61
+ ---
62
+
63
+ In the other case we are just trying to do `user.resources.where ...` ...
64
+
65
+ => #<struct PassiveRecord::Associations::HasManyThroughAssociation
66
+ parent_class=User,
67
+ child_class_name="Resource",
68
+ target_name_symbol=:resources,
69
+ through_class=:resource_allocations,
70
+ base_association=#<struct PassiveRecord::Associations::HasManyAssociation parent_class=User, child_class_name="ResourceAllocation", children_name_sym=:resource_allocations>,
71
+ habtm=false>
72
+
73
+ We are constructing a query on `Resource` ...
74
+
75
+ We need to check from the perspective of `Resource` what the relation is to allocations...?
76
+
@@ -562,6 +562,27 @@ describe "passive record models" do
562
562
  end
563
563
  end
564
564
 
565
+ context 'manual habtm' do
566
+ let!(:resource) { Resource.create }
567
+ let!(:user) { User.create }
568
+
569
+ it 'should permit relations' do
570
+ expect(user.resources).to be_empty
571
+ expect(resource.users).to be_empty
572
+
573
+ ResourceAllocation.create(user: user, resource: resource)
574
+
575
+ expect(user.resources).to include(resource)
576
+ expect(resource.users).to include(user)
577
+ end
578
+
579
+ it 'should permit querying' do
580
+ ResourceAllocation.create(user: user, resource: resource)
581
+ # binding.pry
582
+ expect(user.resources.where.all).to include(resource)
583
+ end
584
+ end
585
+
565
586
  context 'direct habtm' do
566
587
  before(:each) { PassiveRecord.drop_all }
567
588
  let!(:user) { User.create roles: [role] }
@@ -92,13 +92,28 @@ end
92
92
  class User < Model
93
93
  has_many :friendships
94
94
  has_many :friends, :through => :friendships
95
+
95
96
  has_and_belongs_to_many :roles
97
+
98
+ has_many :resource_allocations
99
+ has_many :resources, :through => :resource_allocations
96
100
  end
97
101
 
98
102
  class Role < Model
99
103
  has_and_belongs_to_many :users
100
104
  end
101
105
 
106
+ class ResourceAllocation < Model
107
+ belongs_to :user
108
+ belongs_to :resource
109
+ end
110
+
111
+ class Resource < Model
112
+ # TODO why can't we use a custom class name here???
113
+ has_many :resource_allocations #, class_name: "ResourceAllocation"
114
+ has_many :users, through: :resource_allocations
115
+ end
116
+
102
117
  ###
103
118
  #
104
119
 
@@ -161,7 +176,6 @@ class Post < Model
161
176
  end
162
177
  end
163
178
 
164
- # whoa, we're reopening user here -- this might not be expected
165
179
  class Author < Model
166
180
  has_many :posts
167
181
  has_many :comments
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: passive_record
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.11
4
+ version: 0.4.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joseph Weissman
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-07-11 00:00:00.000000000 Z
11
+ date: 2016-07-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport