active_record-acts_as 2.1.1 → 2.2.0

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: 71fdceb0ce3b5629016ebb56a04e7663d1fbefdd
4
- data.tar.gz: 9b7bb83ea854a435bf8c6a0198c33e4fdd552e8e
3
+ metadata.gz: 12fa1c4f20906b7d2352269034b053732a66ff54
4
+ data.tar.gz: 7589a0647e201e2d3105344038e71aa1bb66508a
5
5
  SHA512:
6
- metadata.gz: ade1a531d8a638c8c14c0f7758f2790e09869463b5e8da0f7042e7043198b9401176b8a58e0ee453159997596ff0b9f62750b559b1253313aa86b56510a28a40
7
- data.tar.gz: 4a1665b6991794b478bde0d027199eee0d956c6c8a099c94c215c49250470907bb1c6493513c3ce43c8068d31277d181d72ebdee6c420ba8b6d34ea556975181
6
+ metadata.gz: e147a8073f32ee2c4ee4bb81c5f13e45e88f2057eac38c8c22b1a7165123a7a5d69550fb719626e76deb2598fa6a9c3b780861c4ff6b31b6f000fc4f33aa33c2
7
+ data.tar.gz: 378cf9642978a153cbf16a4c2339d21b9fde016724f540fb0df5dbc3be60f7a7bceca384c5d5098fd311ee18fb8f0e49f772fc9c86e8f9a6d155d18d61e18dd5
data/.travis.yml CHANGED
@@ -1,9 +1,9 @@
1
1
  language: ruby
2
2
  sudo: false
3
3
  rvm:
4
- - 2.2.6
5
- - 2.3.3
6
- - 2.4.0
4
+ - 2.2.7
5
+ - 2.3.4
6
+ - 2.4.1
7
7
  before_install:
8
8
  - gem update --system
9
9
  - gem update bundler
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.1.1] - 2017-03-22
8
+ ### Fixed
9
+ - Fix querying subclass with `where`, for `enum` (and possibly other) attributes the detection whether the attribute is defined on the superclass or subclass didn't work.
10
+
7
11
  ## [2.1.0] - 2017-03-17
8
12
  ### Added
9
13
  - Access superobjects from query on submodel by calling `.actables`
@@ -57,7 +61,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
57
61
  ### Fixed
58
62
  - Fixed `remove_actable` migration helper (https://github.com/hzamani/active_record-acts_as/pull/71, thanks to [nuclearpidgeon](https://github.com/nuclearpidgeon)!)
59
63
 
60
- [Unreleased]: https://github.com/krautcomputing/active_record-acts_as/compare/v2.1.0...HEAD
64
+ [Unreleased]: https://github.com/krautcomputing/active_record-acts_as/compare/v2.1.1...HEAD
65
+ [2.1.1]: https://github.com/krautcomputing/active_record-acts_as/compare/v2.1.0...v2.1.1
61
66
  [2.1.0]: https://github.com/krautcomputing/active_record-acts_as/compare/v2.0.9...v2.1.0
62
67
  [2.0.9]: https://github.com/krautcomputing/active_record-acts_as/compare/v2.0.8...v2.0.9
63
68
  [2.0.8]: https://github.com/krautcomputing/active_record-acts_as/compare/v2.0.7...v2.0.8
data/README.md CHANGED
@@ -172,6 +172,11 @@ acts_as :person, -> { includes(:friends) }
172
172
  Make sure you know what you are doing when overwriting `polymorphic` option.
173
173
 
174
174
 
175
+ ## Caveats
176
+
177
+ Multiple `acts_as` in the same class are not supported!
178
+
179
+
175
180
  ## Migrating from acts_as_relation
176
181
 
177
182
  Replace `acts_as_superclass` in models with `actable` and if you where using
@@ -18,6 +18,19 @@ module ActiveRecord
18
18
  def actables
19
19
  acting_as_model.where(actable_id: select(:id))
20
20
  end
21
+
22
+ def method_missing(method, *args, &block)
23
+ if acting_as_model.respond_to?(method)
24
+ result = acting_as_model.public_send(method, *args, &block)
25
+ if result.is_a?(ActiveRecord::Relation)
26
+ all.joins(acting_as_name.to_sym).merge(result)
27
+ else
28
+ result
29
+ end
30
+ else
31
+ super
32
+ end
33
+ end
21
34
  end
22
35
  end
23
36
  end
@@ -13,16 +13,6 @@ module ActiveRecord
13
13
  super || acting_as.changed? || (defined?(@_acting_as_changed) ? @_acting_as_changed : false)
14
14
  end
15
15
 
16
- def acting_as_foreign_key
17
- acting_as[acting_as_reflection.foreign_key]
18
- end
19
-
20
- # Is the superclass persisted to the database?
21
- def acting_as_persisted?
22
- return false if acting_as.nil?
23
- !acting_as.id.nil? && !acting_as_foreign_key.nil?
24
- end
25
-
26
16
  def actable_must_be_valid
27
17
  if validates_actable
28
18
  unless acting_as.valid?
@@ -96,7 +86,11 @@ module ActiveRecord
96
86
  end
97
87
 
98
88
  def respond_to?(name, include_private = false, as_original_class = false)
99
- as_original_class ? super(name, include_private) : super(name, include_private) || acting_as.respond_to?(name)
89
+ if as_original_class
90
+ super(name, include_private)
91
+ else
92
+ super(name, include_private) || acting_as.respond_to?(name)
93
+ end
100
94
  end
101
95
 
102
96
  def self_respond_to?(name, include_private = false)
@@ -110,26 +104,10 @@ module ActiveRecord
110
104
  end
111
105
 
112
106
  def method_missing(method, *args, &block)
113
- uses_superclass_for?(method) ? acting_as.send(method, *args, &block) : super
114
- end
115
-
116
- def uses_superclass_for?(method)
117
- responds_locally = self_respond_to?(method)
118
- if acting_as.respond_to?(method)
119
- if responds_locally
120
- false
121
- else
122
- # Only use getters if the superclass has
123
- # an instance that is linked to this class instance.
124
- if acting_as_persisted?
125
- true
126
- else
127
- responds_locally ? false : true
128
- end
129
- end
107
+ if !self_respond_to?(method) && acting_as.respond_to?(method)
108
+ acting_as.send(method, *args, &block)
130
109
  else
131
- # If the superclass doesn't have it, use this class's methods
132
- false
110
+ super
133
111
  end
134
112
  end
135
113
  end
@@ -1,6 +1,6 @@
1
1
  module ActiveRecord
2
2
  module ActsAs
3
- VERSION = "2.1.1"
3
+ VERSION = "2.2.0"
4
4
  end
5
5
  end
6
6
 
data/spec/acts_as_spec.rb CHANGED
@@ -86,17 +86,6 @@ RSpec.describe "ActiveRecord::Base model with #acts_as called" do
86
86
  end
87
87
  end
88
88
 
89
- describe "#acting_as_foreign_key" do
90
- let(:product_foreign_key_value) do
91
- pen.acting_as[pen.acting_as.actable_reflection.foreign_key]
92
- end
93
- it "return acts_as foreign key value" do
94
- pen.save
95
- expect(pen.acting_as_foreign_key).to eq(product_foreign_key_value)
96
- expect(pen.acting_as_foreign_key).to eq(pen.id)
97
- end
98
- end
99
-
100
89
  describe "#acting_as=" do
101
90
  it "sets acts_as model" do
102
91
  product = Product.new(name: 'new product', price: 0.99)
@@ -370,6 +359,14 @@ RSpec.describe "ActiveRecord::Base model with #acts_as called" do
370
359
  end
371
360
  end
372
361
 
362
+ describe '#respond.to?' do
363
+ it 'returns true for instance methods of the supermodel and submodel' do
364
+ red_pen = Pen.create!(name: 'red pen', price: 0.8, color: 'red')
365
+ expect(red_pen.respond_to?(:pen_instance_method)).to eq(true)
366
+ expect(red_pen.respond_to?(:present)).to eq(true)
367
+ end
368
+ end
369
+
373
370
  describe ".actables" do
374
371
  before(:each) { clear_database }
375
372
 
@@ -385,6 +382,25 @@ RSpec.describe "ActiveRecord::Base model with #acts_as called" do
385
382
  end
386
383
  end
387
384
 
385
+ context 'class methods' do
386
+ before(:each) { clear_database }
387
+
388
+ context 'when the class method returns a scope' do
389
+ it 'works' do
390
+ cheap_pen = Pen.create!(name: 'cheap pen', price: 0.5, color: 'blue')
391
+ expensive_pen = Pen.create!(name: 'expensive pen', price: 1, color: 'red')
392
+
393
+ expect(Pen.with_price_higher_than(0.5).to_a).to eq([expensive_pen])
394
+ end
395
+ end
396
+
397
+ context 'when the class methods returns anything else' do
398
+ it 'works' do
399
+ expect(Pen.test_class_method).to eq('test')
400
+ end
401
+ end
402
+ end
403
+
388
404
  context "Querying" do
389
405
  before(:each) { clear_database }
390
406
 
data/spec/models.rb CHANGED
@@ -10,6 +10,14 @@ class Product < ActiveRecord::Base
10
10
  validates_presence_of :name, :price
11
11
  store :settings, accessors: [:global_option]
12
12
 
13
+ def self.with_price_higher_than(price)
14
+ where('price > ?', price)
15
+ end
16
+
17
+ def self.test_class_method
18
+ 'test'
19
+ end
20
+
13
21
  def present
14
22
  "#{name} - $#{price}"
15
23
  end
@@ -35,6 +43,9 @@ class Pen < ActiveRecord::Base
35
43
  belongs_to :pen_collection, touch: true
36
44
 
37
45
  validates_presence_of :color
46
+
47
+ def pen_instance_method
48
+ end
38
49
  end
39
50
 
40
51
  class Buyer < ActiveRecord::Base
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.1.1
4
+ version: 2.2.0
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-03-22 00:00:00.000000000 Z
12
+ date: 2017-04-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: sqlite3
@@ -184,7 +184,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
184
184
  version: '0'
185
185
  requirements: []
186
186
  rubyforge_project:
187
- rubygems_version: 2.6.8
187
+ rubygems_version: 2.6.11
188
188
  signing_key:
189
189
  specification_version: 4
190
190
  summary: Simulate multi-table inheritance for activerecord models