active_record-acts_as 5.1.0 → 5.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
  SHA256:
3
- metadata.gz: 532e3effe05da886dea3162ea29263ecce12c459e321bcd4fb870554a0227053
4
- data.tar.gz: 0e0a4d871dd0bd2be5e2521ddd36abbe9d8038b1bc3c046401abf6ac1c727bbd
3
+ metadata.gz: 2ed68e0f493ab0bca2e81e2a1f0fc1a37a9b97b125336e74f76c1a773794b6bc
4
+ data.tar.gz: 18934a6d17d87d3f5befeb57acdd11cbfbf0771e80ad95fafdb87819d0c1152a
5
5
  SHA512:
6
- metadata.gz: 0477314dde10d98e065be763b4555c3f9915909f129be5b53e5385d41c739dc3482ad4ee4ddc504b60d1e69fd57f36326a995d7cc3d3ab9a86a9a7b4d32ecb25
7
- data.tar.gz: 3607a37917ee5fcd6eca4f0a3df7f14a55252e4777faeadb736bb76312bfe0c2dd372779ccf353d8f883472d41504917ce6c1d95deaf20998763db28fe41f1a8
6
+ metadata.gz: '009d65fc100af5440fb62c5a9caefeaeda1c5473676d3d2a30c81450ae015780d7ad74226c3b76a0ab7b1c08b18db54edf092519a49a2cc507dc0a617cc2a093'
7
+ data.tar.gz: 9bacb126798c42cdada4db64e958425db014bbe59c9f5c3fd088472a70222b98afa20cd77816e622b8dacac61901680cb083975acfe422713d4771a0b0dada66
data/CHANGELOG.md CHANGED
@@ -5,6 +5,15 @@ 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
7
  ## [Unreleased] -
8
+ ## [5.2.0] - 2023-09-03
9
+
10
+ ### Added
11
+ - Add `.exists?` support to seamlessly check in both the model and super model
12
+ (@chaadow)
13
+
14
+ ### Fixed
15
+ - Fix `.actables` to restrict based on type. (@Taeir)
16
+
8
17
  ## [5.1.0] - 2023-06-29
9
18
  - Use Github actions instead of travis.yml
10
19
  - Add support for ruby 2.7 3.0 and 3.1
@@ -16,7 +16,7 @@ module ActiveRecord
16
16
  end
17
17
 
18
18
  def actables
19
- acting_as_model.where(actable_id: select(:id))
19
+ acting_as_model.where(actable_id: select(:id), actable_type: model_name.name)
20
20
  end
21
21
 
22
22
  def respond_to_missing?(method, include_private = false)
@@ -26,6 +26,23 @@ module ActiveRecord
26
26
  end
27
27
  end
28
28
 
29
+ module ExistsConcern
30
+ extend ActiveSupport::Concern
31
+
32
+ included do
33
+ alias :_original_exists? :exists?
34
+
35
+ # We redefine the method only when this module is included
36
+ # to have acces to the original exists? method
37
+ def exists?(...)
38
+ return super unless acting_as?
39
+
40
+ joins(acting_as_name.to_sym)._original_exists?(...)
41
+ end
42
+ end
43
+
44
+ end
45
+
29
46
  module ScopeForCreate
30
47
  def scope_for_create(attributes = nil)
31
48
  return super() unless acting_as?
@@ -41,3 +58,7 @@ module ActiveRecord
41
58
  Relation.send(:prepend, ActsAs::QueryMethods)
42
59
  Relation.send(:prepend, ActsAs::ScopeForCreate)
43
60
  end
61
+
62
+ ActiveSupport.on_load(:active_record) do
63
+ ActiveRecord::Relation.include(ActiveRecord::ActsAs::ExistsConcern)
64
+ end
@@ -1,6 +1,6 @@
1
1
  module ActiveRecord
2
2
  module ActsAs
3
- VERSION = "5.1.0"
3
+ VERSION = "5.2.0"
4
4
  end
5
5
  end
6
6
 
data/spec/acts_as_spec.rb CHANGED
@@ -300,25 +300,27 @@ RSpec.describe "ActiveRecord::Base model with #acts_as called" do
300
300
  end
301
301
 
302
302
  context "errors" do
303
+ let(:error_message) { ActiveRecord.version >= Gem::Version.new('7.1.0.alpha') ? "can’t be blank" : "can't be blank"}
304
+
303
305
  context 'when validates_actable is set to true' do
304
306
  it "combines supermodel and submodel errors" do
305
307
  pen = Pen.new
306
308
  expect(pen).to be_invalid
307
309
  expect(pen.errors.to_hash).to eq(
308
- name: ["can't be blank"],
309
- price: ["can't be blank"],
310
- color: ["can't be blank"]
310
+ name: [error_message],
311
+ price: [error_message],
312
+ color: [error_message]
311
313
  )
312
314
  pen.name = 'testing'
313
315
  expect(pen).to be_invalid
314
316
  expect(pen.errors.to_hash).to eq(
315
- price: ["can't be blank"],
316
- color: ["can't be blank"]
317
+ price: [error_message],
318
+ color: [error_message]
317
319
  )
318
320
  pen.color = 'red'
319
321
  expect(pen).to be_invalid
320
322
  expect(pen.errors.to_hash).to eq(
321
- price: ["can't be blank"]
323
+ price: [error_message]
322
324
  )
323
325
  pen.price = 0.8
324
326
  expect(pen).to be_valid
@@ -329,9 +331,7 @@ RSpec.describe "ActiveRecord::Base model with #acts_as called" do
329
331
  it "unless validates_actable is set to false" do
330
332
  pen = IsolatedPen.new
331
333
  expect(pen).to be_invalid
332
- expect(pen.errors.to_hash).to eq(
333
- color: ["can't be blank"]
334
- )
334
+ expect(pen.errors.to_hash).to eq( { color: [error_message] })
335
335
  pen.color = 'red'
336
336
  expect(pen).to be_valid
337
337
  end
@@ -487,6 +487,13 @@ RSpec.describe "ActiveRecord::Base model with #acts_as called" do
487
487
  @black_pen.buyers.create! name: 'John'
488
488
  end
489
489
 
490
+ describe 'exists?' do
491
+ it 'checks on both model and supermodel' do
492
+ expect(Pen.exists?(name: 'red pen')).to be_truthy
493
+ expect(Pen.exists?(name: 'red pen', price: 0.8)).to be_truthy
494
+ end
495
+ end
496
+
490
497
  describe '.where and .where!' do
491
498
  it 'respects supermodel attributes' do
492
499
  conditions = { price: 0.8 }
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: 5.1.0
4
+ version: 5.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hassan Zamani
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2023-06-29 00:00:00.000000000 Z
13
+ date: 2023-09-03 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: sqlite3