passive_record 0.4.10 → 0.4.11

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: cd29b55c6736cf66a3dceed9de1b23d662f42803
4
- data.tar.gz: 1fc06588e4ae2262ee5c138ce388c881746c66c0
3
+ metadata.gz: 1df1e784cf91a0992f637e1d716f80e480735de6
4
+ data.tar.gz: c42d72d513f9b2970ac550fac9ba4bf6cf17cf5f
5
5
  SHA512:
6
- metadata.gz: 6b282a65d73fea26273adb072d74c3f56b828d753c468516ae8317330418c7161c1c0e55df1737a4e000ca7f1eedaa472cb146f35786459181f30bed39abbab6
7
- data.tar.gz: f8cb9799687f87993624ff521479224c836acca05e59ce1c5680012a473790ae7cb144e448ce1661eaba8dc5ec0c7e1f90b09ef2d89deb228b274bccda26d960
6
+ metadata.gz: 2ac681f63db9780ce9604356125fe2d3e76efc029958e258c098da5a35599db0b871aa6fabe393dce3c6310d01bcd6f85758f7a63633ea695ad612f9d7905fd7
7
+ data.tar.gz: b91ffa6952369232fd89ae1c88b18cedcd6274a554c52845ebc523fda6bb32aaa641dcec8eb3f6191eec141a8b16be165812f4828d9175d636b47083147fc070
@@ -83,6 +83,7 @@ module PassiveRecord
83
83
 
84
84
  def has_many(collection_name_sym, opts={})
85
85
  target_class_name = opts.delete(:class_name) { (collection_name_sym.to_s).split('_').map(&:capitalize).join }
86
+ habtm = opts.delete(:habtm) { false }
86
87
 
87
88
  association = nil
88
89
  if opts.key?(:through)
@@ -92,7 +93,7 @@ module PassiveRecord
92
93
  base_association = associations.detect { |assn| assn.child_class_name == through_class_name rescue false }
93
94
 
94
95
  association = HasManyThroughAssociation.new(
95
- self, target_class_name, collection_name_sym, through_class_collection_name, base_association)
96
+ self, target_class_name, collection_name_sym, through_class_collection_name, base_association, habtm)
96
97
 
97
98
  associate!(association)
98
99
 
@@ -180,7 +181,7 @@ module PassiveRecord
180
181
 
181
182
  if (intended_module.const_get(inverse_habtm_join_class_name) rescue false)
182
183
  has_many inverse_habtm_join_class_name.underscore.pluralize.to_sym
183
- has_many collection_name_sym, :through => 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
185
  else
185
186
  auto_collection_sym = self.name.split('::').last.underscore.pluralize.to_sym
186
187
  eval <<-ruby
@@ -191,7 +192,7 @@ module PassiveRecord
191
192
  end # end
192
193
  ruby
193
194
  has_many habtm_join_class_name.underscore.pluralize.to_sym
194
- has_many(collection_name_sym, :through => 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
196
  end
196
197
  end
197
198
  end
@@ -8,6 +8,10 @@ module PassiveRecord
8
8
  def target_name_symbol
9
9
  children_name_sym
10
10
  end
11
+
12
+ def habtm
13
+ false #@habtm ||= false
14
+ end #?
11
15
  end
12
16
 
13
17
  class HasManyRelation < HasOneRelation
@@ -1,6 +1,6 @@
1
1
  module PassiveRecord
2
2
  module Associations
3
- class HasManyThroughAssociation < Struct.new(:parent_class, :child_class_name, :target_name_symbol, :through_class, :base_association)
3
+ class HasManyThroughAssociation < Struct.new(:parent_class, :child_class_name, :target_name_symbol, :through_class, :base_association, :habtm)
4
4
  def to_relation(parent_model)
5
5
  HasManyThroughRelation.new(self, parent_model)
6
6
  end
@@ -91,12 +91,20 @@ module PassiveRecord
91
91
  end
92
92
 
93
93
  def intermediary_conditions
94
- if intermediary_relation.association.is_a?(HasManyThroughAssociation)
95
- # we are 'thru' a has_many thru at the same level, we should be able to recurse thru its #intermediary_conds
94
+ if intermediary_relation.is_a?(HasManyThroughRelation)
96
95
  conds = intermediary_relation.intermediary_conditions
97
- { association.through_class.to_s.singularize.to_sym => conds }
96
+
97
+ if nested_association.habtm
98
+ { association.through_class => conds }
99
+ else
100
+ { association.through_class.to_s.singularize.to_sym => conds }
101
+ end
98
102
  elsif intermediary_relation.association.is_a?(HasManyAssociation) # normal has many?
99
- intermediary_key = intermediary_relation.association.children_name_sym.to_s.singularize.to_sym
103
+ intermediary_key = if association.habtm
104
+ association.base_association.children_name_sym
105
+ else
106
+ intermediary_relation.association.children_name_sym.to_s.singularize.to_sym
107
+ end
100
108
  nested_conds = { intermediary_key => { parent_model_id_field.to_sym => parent_model.id } }
101
109
 
102
110
  if nested_association.is_a?(HasManyThroughAssociation)
@@ -1,4 +1,4 @@
1
1
  module PassiveRecord
2
2
  # passive_record version
3
- VERSION = "0.4.10"
3
+ VERSION = "0.4.11"
4
4
  end
@@ -342,6 +342,16 @@ describe "passive record models" do
342
342
  expect(network.comments.approved).not_to include(unapproved_comment)
343
343
  end
344
344
  end
345
+
346
+ describe 'should find related models a recursive has_many :thru a habtm' do
347
+ let!(:promoted_tag) { Post.first.create_tag(promoted: true) }
348
+ let!(:unpromoted_tag) { Post.last.create_tag(promoted: false) }
349
+
350
+ it 'should refine and restrict' do
351
+ expect(network.tags.promoted).to include(promoted_tag)
352
+ expect(network.tags.promoted).not_to include(unpromoted_tag)
353
+ end
354
+ end
345
355
  end
346
356
  end
347
357
  end
data/spec/spec_helper.rb CHANGED
@@ -106,6 +106,7 @@ class Network < Model
106
106
  has_many :streams
107
107
  has_many :posts, :through => :streams
108
108
  has_many :comments, :through => :posts
109
+ has_many :tags, :through => :posts
109
110
  end
110
111
 
111
112
  class Stream < Model
@@ -131,11 +132,18 @@ class Blog < Model
131
132
  belongs_to :feed
132
133
  end
133
134
 
135
+ class Tag < Model
136
+ has_and_belongs_to_many :posts
137
+ attr_accessor :promoted
138
+ def self.promoted; where(promoted: true) end
139
+ end
140
+
134
141
  class Post < Model
135
142
  belongs_to :author
136
143
  belongs_to :blog
137
144
  has_many :comments
138
145
  has_many :commenters, :through => :comments, :class_name => "Author"
146
+ has_and_belongs_to_many :tags
139
147
 
140
148
  attr_accessor :active, :published_at
141
149
  before_create { @published_at = Time.now }
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.10
4
+ version: 0.4.11
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-10 00:00:00.000000000 Z
11
+ date: 2016-07-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport