active_record-acts_as 2.3.1 → 2.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -1
- data/lib/active_record/acts_as/class_methods.rb +2 -2
- data/lib/active_record/acts_as/relation.rb +13 -0
- data/lib/active_record/acts_as/version.rb +1 -1
- data/spec/acts_as_spec.rb +14 -5
- data/spec/models.rb +6 -4
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 589ce7c704e9fbea72a54a837e5a6937a617abc7
|
4
|
+
data.tar.gz: ffd0002ce231c728cf4508c008c4b033c12ebdf9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a4ddd42dca8305b4b6e10d6045455b54192834004e0289cb1a1073ed99f409342f075467b8c4a63dc812ace6e31b3cd09692ad3a4538cc08af683409833eeeff
|
7
|
+
data.tar.gz: 46701f3ca1f4f00783ef1e2a58763752e5a7ff5d67f20e33ea2ac78a20f170c92ec83750d62f57687e7c1dc6336739fb6083b64deb0e350ecddc88d25380b7ef
|
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.0] - 2017-04-16
|
8
|
+
### Changed
|
9
|
+
- Don't make all supermodel class methods callable by submodel, only scopes. Add `callable_by_submodel` to supermodel so users can make their own class methods callable by submodels.
|
10
|
+
|
7
11
|
## [2.3.1] - 2017-04-15
|
8
12
|
### Fixed
|
9
13
|
- Make calling supermodel class methods work through relations/associations as well
|
@@ -80,7 +84,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
|
|
80
84
|
### Fixed
|
81
85
|
- Fixed `remove_actable` migration helper (https://github.com/hzamani/active_record-acts_as/pull/71, thanks to [nuclearpidgeon](https://github.com/nuclearpidgeon)!)
|
82
86
|
|
83
|
-
[Unreleased]: https://github.com/krautcomputing/active_record-acts_as/compare/v2.
|
87
|
+
[Unreleased]: https://github.com/krautcomputing/active_record-acts_as/compare/v2.4.0...HEAD
|
88
|
+
[2.4.0]: https://github.com/krautcomputing/active_record-acts_as/compare/v2.3.1...v2.4.0
|
84
89
|
[2.3.1]: https://github.com/krautcomputing/active_record-acts_as/compare/v2.3.0...v2.3.1
|
85
90
|
[2.3.0]: https://github.com/krautcomputing/active_record-acts_as/compare/v2.2.1...v2.3.0
|
86
91
|
[2.2.1]: https://github.com/krautcomputing/active_record-acts_as/compare/v2.2.0...v2.2.1
|
@@ -20,11 +20,11 @@ module ActiveRecord
|
|
20
20
|
end
|
21
21
|
|
22
22
|
def respond_to_missing?(method, include_private = false)
|
23
|
-
acting_as_model.
|
23
|
+
acting_as_model.methods_callable_by_submodel.include?(method) || super
|
24
24
|
end
|
25
25
|
|
26
26
|
def method_missing(method, *args, &block)
|
27
|
-
if acting_as_model.
|
27
|
+
if acting_as_model.methods_callable_by_submodel.include?(method)
|
28
28
|
result = acting_as_model.public_send(method, *args, &block)
|
29
29
|
if result.is_a?(ActiveRecord::Relation)
|
30
30
|
all.joins(acting_as_name.to_sym).merge(result)
|
@@ -83,6 +83,19 @@ module ActiveRecord
|
|
83
83
|
|
84
84
|
cattr_reader(:actable_reflection) { reflections.stringify_keys[name.to_s] }
|
85
85
|
|
86
|
+
def self.methods_callable_by_submodel
|
87
|
+
@methods_callable_by_submodel ||= Set.new
|
88
|
+
end
|
89
|
+
|
90
|
+
def self.callable_by_submodel(method)
|
91
|
+
@methods_callable_by_submodel ||= Set.new
|
92
|
+
@methods_callable_by_submodel << method
|
93
|
+
end
|
94
|
+
|
95
|
+
def self.scope(*)
|
96
|
+
callable_by_submodel super
|
97
|
+
end
|
98
|
+
|
86
99
|
alias_method :specific, name
|
87
100
|
end
|
88
101
|
|
data/spec/acts_as_spec.rb
CHANGED
@@ -414,18 +414,27 @@ RSpec.describe "ActiveRecord::Base model with #acts_as called" do
|
|
414
414
|
context 'class methods' do
|
415
415
|
before(:each) { clear_database }
|
416
416
|
|
417
|
-
context 'when
|
418
|
-
it '
|
417
|
+
context 'when they are defined via `scope`' do
|
418
|
+
it 'can be called from the submodel' do
|
419
419
|
cheap_pen = Pen.create!(name: 'cheap pen', price: 0.5, color: 'blue')
|
420
420
|
expensive_pen = Pen.create!(name: 'expensive pen', price: 1, color: 'red')
|
421
421
|
|
422
|
+
expect(Product.with_price_higher_than(0.5).to_a).to eq([expensive_pen.acting_as])
|
422
423
|
expect(Pen.with_price_higher_than(0.5).to_a).to eq([expensive_pen])
|
423
424
|
end
|
424
425
|
end
|
425
426
|
|
426
|
-
context 'when
|
427
|
-
it '
|
428
|
-
expect(
|
427
|
+
context 'when they are not defined via `scope` but made callable by submodel' do
|
428
|
+
it 'can be called from the submodel' do
|
429
|
+
expect(Product.class_method_callable_by_submodel).to eq('class_method_callable_by_submodel')
|
430
|
+
expect(Pen.class_method_callable_by_submodel).to eq('class_method_callable_by_submodel')
|
431
|
+
end
|
432
|
+
end
|
433
|
+
|
434
|
+
context 'when they are neither defined via `scope` nor made callable by submodel' do
|
435
|
+
it 'cannot be called from the submodel' do
|
436
|
+
expect(Product.class_method).to eq('class_method')
|
437
|
+
expect { Pen.class_method }.to raise_error(NoMethodError)
|
429
438
|
end
|
430
439
|
end
|
431
440
|
end
|
data/spec/models.rb
CHANGED
@@ -10,12 +10,14 @@ class Product < ActiveRecord::Base
|
|
10
10
|
validates_presence_of :name, :price
|
11
11
|
store :settings, accessors: [:global_option]
|
12
12
|
|
13
|
-
|
14
|
-
|
13
|
+
scope :with_price_higher_than, ->(price) { where('price > ?', price) }
|
14
|
+
|
15
|
+
def self.class_method
|
16
|
+
'class_method'
|
15
17
|
end
|
16
18
|
|
17
|
-
def self.
|
18
|
-
'
|
19
|
+
callable_by_submodel def self.class_method_callable_by_submodel
|
20
|
+
'class_method_callable_by_submodel'
|
19
21
|
end
|
20
22
|
|
21
23
|
def present
|
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
|
+
version: 2.4.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-04-
|
12
|
+
date: 2017-04-16 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: sqlite3
|