activerecord-delegation-expected_behaviour 1.0.0 → 1.1.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 879e4bcf5822aee3c6dda3aed6786da196a114dc
4
- data.tar.gz: a6bb723ac40a764b15c73fa809cd84fa389989b0
3
+ metadata.gz: fc9524166ef902464385e71aa84a89e4825439fe
4
+ data.tar.gz: '08f19408bf4d8ca070017ef3241af11b46cf3367'
5
5
  SHA512:
6
- metadata.gz: 408cb2ea634b2d24e4592abab60f2134f2c2b2e31e839d1265c0cf7021429f02842e382d28dd72413dbb18685e3b04ab33ae44ef61473f65b2d350e0225219c2
7
- data.tar.gz: 1ae9a946714b7a373b67486f003587f2a382fcb1a42f01268861c003f207dd698b59ced273c51e89ab38a91b9a0dd2d1068dc404ccfc512b2c3a0aa11beeafa5
6
+ metadata.gz: 1d0f4bd9c6313020137ec2f8006a6e30be9a53e709e82c3df8d7eec992efb95a1381ee8e67cb0c7aa838b65ea8399828f538ea9db0e74771bb21346061647704
7
+ data.tar.gz: 536e9fb52d82b7bfda121577fb301543b03158274f5ac9168e9bfa1159df9cfe7d22e821be264f0093dcd225a7d597b78c0eb20ed4290763cdb9fb96ae21d8ae
data/README.md CHANGED
@@ -4,11 +4,14 @@ Prefer some class methods over methods from Enumerable on ActiveRecord relations
4
4
 
5
5
  ## Rationale
6
6
 
7
- In Rails 5 you can no longer call some ActiveRecord class methods named like their Enumerable counterparts:
7
+ In Rails 5 you can no longer call some ActiveRecord class methods named
8
+ like their Enumerable counterparts:
8
9
 
9
10
  ```:encode_with, :to_ary, :join, :[], :&, :|, :+, :-, :sample, :reverse, :compact, :in_groups, :in_groups_of, :to_sentence, :to_formatted_s, :shuffle, :split, :index```
10
11
 
11
- This gem reinstates the expected behaviour, where you can always get the method from Enumerable by calling to_a on the relation.
12
+ This gem reinstates the expected behaviour, where you can always
13
+ get the method from Enumerable by calling to_a on the relation,
14
+ whilst being able to define it as class method on the relation model.
12
15
 
13
16
  See: https://github.com/rails/rails/issues/26949
14
17
 
@@ -3,19 +3,32 @@ require 'active_record'
3
3
  module ActiveRecord
4
4
  module Delegation
5
5
  module ExpectedBehaviour
6
- VERSION = '1.0.0'
6
+ VERSION = '1.1.1'
7
7
 
8
- UNEXPECTED_METHODS = [:encode_with, :to_ary, :join, :[], :&, :|, :+, :-,
9
- :sample, :reverse, :compact, :in_groups, :in_groups_of,
10
- :to_sentence, :to_formatted_s, :shuffle, :split, :index]
8
+ REDELEGATED_METHODS = [:encode_with, :to_ary, :join, :[], :&, :|, :+, :-, :sample,
9
+ :reverse, :compact, :in_groups, :in_groups_of, :to_sentence, :to_formatted_s,
10
+ :shuffle, :split, :index]
11
11
 
12
- def self.included(base)
13
- base.module_eval do
14
- UNEXPECTED_METHODS.each { |method| remove_method(method) }
12
+ REDELEGATED_METHODS.each do |method|
13
+ if method =~ /[^\]]=$/
14
+ define_method(method) { |arg| delegate_to_klass_or_records(method, arg) }
15
+ else
16
+ define_method(method) { |*args, &block| delegate_to_klass_or_records(method, *args, &block) }
15
17
  end
16
18
  end
19
+
20
+ protected
21
+
22
+ def delegate_to_klass_or_records(method, *args, &block)
23
+ if @klass.respond_to?(method)
24
+ scoping { @klass.public_send(method, *args, &block) }
25
+ else
26
+ records.public_send(method, *args, &block)
27
+ end
28
+ end
29
+
17
30
  end
18
31
  end
19
32
  end
20
33
 
21
- ActiveRecord::Delegation.include ActiveRecord::Delegation::ExpectedBehaviour
34
+ ActiveRecord::Delegation.prepend ActiveRecord::Delegation::ExpectedBehaviour
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-delegation-expected_behaviour
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthias Grosser
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-04-13 00:00:00.000000000 Z
11
+ date: 2017-04-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord