active_record-acts_as 2.3.1 → 2.4.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: 649359dfef206fea9b42d2b8ec4c7c303e7711bd
4
- data.tar.gz: bdb45afb463b19d4c203fbf33aaa7f7df83eb6b6
3
+ metadata.gz: 589ce7c704e9fbea72a54a837e5a6937a617abc7
4
+ data.tar.gz: ffd0002ce231c728cf4508c008c4b033c12ebdf9
5
5
  SHA512:
6
- metadata.gz: 65771a5b9157b116e2d1bf6c9e27bbd8ee0aba57543a99dff471ed02194ff39edbb85e7d165e562515a7631407979b07bb8e32ef416459ad77bb04f7e569eefa
7
- data.tar.gz: 8b86a5551db05eccf02dec335d48d2c10bc13198213a4b5839b4cdaf8a7444536bad7290cfcd83d9c95ab5f99bede21d7eca1ffd5827524cd0ee1ae1a057ac35
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.3.1...HEAD
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.respond_to?(method, include_private) || super
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.respond_to?(method)
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
 
@@ -1,6 +1,6 @@
1
1
  module ActiveRecord
2
2
  module ActsAs
3
- VERSION = "2.3.1"
3
+ VERSION = "2.4.0"
4
4
  end
5
5
  end
6
6
 
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 the class method returns a scope' do
418
- it 'works' do
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 the class methods returns anything else' do
427
- it 'works' do
428
- expect(Pen.test_class_method).to eq('test')
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
- def self.with_price_higher_than(price)
14
- where('price > ?', price)
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.test_class_method
18
- 'test'
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.3.1
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-15 00:00:00.000000000 Z
12
+ date: 2017-04-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: sqlite3