active_record-acts_as 2.4.1 → 2.4.2

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: b0bbaf8a07eceb6c10fd9e9fea3ece8a641aef69
4
- data.tar.gz: c5c9b6265ca7464402b6cf8955c28345deee417d
3
+ metadata.gz: d36aaa8f22e66b7d9fd5c2280f4548e9efe3b3fe
4
+ data.tar.gz: 9f30018fa7aaaffc40c8c915b0ed6cc7cd8abda3
5
5
  SHA512:
6
- metadata.gz: 195e8c8b7c28634bb882fdcd693341a2eb8bdad0092c1a43fda14414a55da85dda79b2b04ad5d8bd651e622a7380bc5731a082f9d996aa156968bba316639e64
7
- data.tar.gz: 5daea8a73f30a50e7471bbf521a00dbb9313576ec508ba518f24508c1a5262a28dfcae98acb6c389e05da2ed540dce7d5b091452f25444a8b687efae75c895e7
6
+ metadata.gz: 7b9a24160409f92c1bc8e1e81e85dcd2553c133804507bf1eb848aff3865a65a9bb173a0cc6f237d7877998761acd7d89fa838ff7484486e4a50ba188429d1e0
7
+ data.tar.gz: 672a6b0320fdaa101567546b887a652a993a61d1ccb0d130d3eb55c332c68b09750d2cceb5990aac57cfbc03fee4536c6d3c20b68f03dbdcac614b1ca56c014d
data/CHANGELOG.md CHANGED
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
4
4
  The format is based on [Keep a Changelog](http://keepachangelog.com/)
5
5
  and this project adheres to [Semantic Versioning](http://semver.org/).
6
6
 
7
+ ## [2.4.2] - 2017-04-20
8
+ ### Fixed
9
+ - Fix querying for conditions with hashes.
10
+
7
11
  ## [2.4.1] - 2017-04-19
8
12
  ### Fixed
9
13
  - Make ActiveRecord::Relation#where! work.
@@ -88,7 +92,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
88
92
  ### Fixed
89
93
  - Fixed `remove_actable` migration helper (https://github.com/hzamani/active_record-acts_as/pull/71, thanks to [nuclearpidgeon](https://github.com/nuclearpidgeon)!)
90
94
 
91
- [Unreleased]: https://github.com/krautcomputing/active_record-acts_as/compare/v2.4.1...HEAD
95
+ [Unreleased]: https://github.com/krautcomputing/active_record-acts_as/compare/v2.4.2...HEAD
96
+ [2.4.2]: https://github.com/krautcomputing/active_record-acts_as/compare/v2.4.1...v2.4.2
92
97
  [2.4.1]: https://github.com/krautcomputing/active_record-acts_as/compare/v2.4.0...v2.4.1
93
98
  [2.4.0]: https://github.com/krautcomputing/active_record-acts_as/compare/v2.3.1...v2.4.0
94
99
  [2.3.1]: https://github.com/krautcomputing/active_record-acts_as/compare/v2.3.0...v2.3.1
@@ -1,24 +1,27 @@
1
1
  module ActiveRecord
2
2
  module ActsAs
3
3
  module QueryMethods
4
- def where!(opts, *)
4
+ def where!(opts, *rest)
5
5
  if acting_as? && opts.is_a?(Hash)
6
- if table_name_opts = opts.delete(klass.table_name)
6
+ if table_name_opts = opts.delete(table_name)
7
7
  opts = opts.merge(table_name_opts)
8
8
  end
9
9
 
10
- # Filter out the conditions that should be
11
- # applied to the `acting_as_model`. Ignore
12
- # conditions that contain a dot or are attributes
13
- # of the submodel.
14
- opts, acts_as_opts = opts.stringify_keys.partition { |k, _| k =~ /\./ || column_names.include?(k.to_s) }.map(&:to_h)
10
+ # Filter out the conditions that should be applied to the `acting_as_model`, which are
11
+ # those that neither target specific tables explicitly (where the condition value
12
+ # is a hash or the condition key contains a dot) nor are attributes of the submodel.
13
+ opts, acts_as_opts = opts.stringify_keys.partition do |k, v|
14
+ v.is_a?(Hash) ||
15
+ k =~ /\./ ||
16
+ column_names.include?(k.to_s)
17
+ end.map(&:to_h)
15
18
 
16
19
  if acts_as_opts.any?
17
20
  opts[acting_as_model.table_name] = acts_as_opts
18
21
  end
19
22
  end
20
23
 
21
- super
24
+ super opts, *rest
22
25
  end
23
26
  end
24
27
 
@@ -1,6 +1,6 @@
1
1
  module ActiveRecord
2
2
  module ActsAs
3
- VERSION = "2.4.1"
3
+ VERSION = "2.4.2"
4
4
  end
5
5
  end
6
6
 
data/spec/acts_as_spec.rb CHANGED
@@ -440,46 +440,66 @@ RSpec.describe "ActiveRecord::Base model with #acts_as called" do
440
440
  end
441
441
 
442
442
  context "Querying" do
443
- before(:each) { clear_database }
443
+ before do
444
+ clear_database
445
+
446
+ @red_pen = Pen.create!(name: 'red pen', price: 0.8, color: 'red')
447
+ @blue_pen = Pen.create!(name: 'blue pen', price: 0.8, color: 'blue')
448
+ @black_pen = Pen.create!(name: 'black pen', price: 0.9, color: 'black')
444
449
 
445
- it "respects supermodel attributes in .where" do
446
- red_pen = Pen.create(name: 'red pen', price: 0.8, color: 'red')
447
- blue_pen = Pen.create(name: 'blue pen', price: 0.8, color: 'blue')
448
- black_pen = Pen.create(name: 'black pen', price: 0.9, color: 'black')
450
+ @red_pen.pen_caps.create! size: 'S'
451
+ @blue_pen.pen_caps.create! size: 'M'
452
+ @black_pen.pen_caps.create! size: 'M'
449
453
 
450
- expect(Pen.where(price: 0.8).to_a).to eq([red_pen, blue_pen])
454
+ @red_pen.buyers.create! name: 'Tim'
455
+ @blue_pen.buyers.create! name: 'Tim'
456
+ @black_pen.buyers.create! name: 'John'
451
457
  end
452
458
 
453
- it "respects supermodel attributes in .where!" do
454
- red_pen = Pen.create(name: 'red pen', price: 0.8, color: 'red')
455
- blue_pen = Pen.create(name: 'blue pen', price: 0.8, color: 'blue')
456
- black_pen = Pen.create(name: 'black pen', price: 0.9, color: 'black')
459
+ describe '.where and .where!' do
460
+ it 'respects supermodel attributes' do
461
+ conditions = { price: 0.8 }
457
462
 
458
- relation = Pen.all
459
- relation.where!(price: 0.8)
460
- expect(relation.to_a).to eq([red_pen, blue_pen])
461
- end
463
+ expect(Pen.where(conditions).to_a).to eq([@red_pen, @blue_pen])
464
+
465
+ relation = Pen.all
466
+ relation.where!(conditions)
467
+ expect(relation.to_a).to eq([@red_pen, @blue_pen])
468
+ end
469
+
470
+ it 'works with hashes' do
471
+ conditions = {
472
+ pen_caps: { size: 'M' },
473
+ buyers: { name: 'Tim' }
474
+ }
462
475
 
463
- it "respects supermodel attributes in .find_by" do
464
- red_pen = Pen.create(name: 'red pen', price: 0.8, color: 'red')
465
- blue_pen = Pen.create(name: 'blue pen', price: 0.8, color: 'blue')
466
- black_pen = Pen.create(name: 'black pen', price: 0.9, color: 'black')
476
+ expect(Pen.joins(:pen_caps, :buyers).where(conditions).to_a).to eq([@blue_pen])
467
477
 
468
- expect(Pen.find_by(name: 'red pen')).to eq(red_pen)
469
- expect(Pen.find_by(name: 'blue pen')).to eq(blue_pen)
470
- expect(Pen.find_by(name: 'black pen')).to eq(black_pen)
478
+ relation = Pen.joins(:pen_caps, :buyers)
479
+ relation.where!(conditions)
480
+ expect(relation.to_a).to eq([@blue_pen])
481
+ end
471
482
  end
472
483
 
473
- it "includes supermodel attributes in Relation.scope_for_create" do
474
- relation = Pen.where(name: 'new name', price: 1.4, color: 'red')
475
- expect(relation.scope_for_create.keys).to include('name')
476
- expect(relation.scope_for_create['name']).to eq('new name')
484
+ describe '.find_by' do
485
+ it 'respects supermodel attributes' do
486
+ expect(Pen.find_by(name: 'red pen')).to eq(@red_pen)
487
+ expect(Pen.find_by(name: 'blue pen')).to eq(@blue_pen)
488
+ expect(Pen.find_by(name: 'black pen')).to eq(@black_pen)
489
+ end
490
+
491
+ it 'works with specifying the table name' do
492
+ expect(Pen.find_by('pens.color' => 'red')).to eq(@red_pen)
493
+ end
477
494
  end
478
495
 
479
- it "works when the submodel table name is specified" do
480
- red_pen = Pen.create!(name: 'red pen', price: 0.8, color: 'red')
496
+ describe '.scope_for_create' do
497
+ it 'includes supermodel attributes' do
498
+ relation = Pen.where(name: 'new name', price: 1.4, color: 'red')
481
499
 
482
- expect(Pen.find_by('pens.color' => 'red')).to eq(red_pen)
500
+ expect(relation.scope_for_create.keys).to include('name')
501
+ expect(relation.scope_for_create['name']).to eq('new name')
502
+ end
483
503
  end
484
504
  end
485
505
 
data/spec/models.rb CHANGED
@@ -121,10 +121,12 @@ def initialize_schema
121
121
  end
122
122
 
123
123
  create_table :buyers do |t|
124
+ t.string :name
124
125
  t.integer :product_id
125
126
  end
126
127
 
127
128
  create_table :pen_caps do |t|
129
+ t.string :size
128
130
  t.integer :pen_id
129
131
  end
130
132
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_record-acts_as
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.4.1
4
+ version: 2.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hassan Zamani
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2017-04-19 00:00:00.000000000 Z
12
+ date: 2017-04-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: sqlite3