activerecord-delegation-expected_behaviour 1.0.0 → 1.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +5 -2
- data/lib/activerecord-delegation-expected_behaviour.rb +21 -8
- 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: fc9524166ef902464385e71aa84a89e4825439fe
|
4
|
+
data.tar.gz: '08f19408bf4d8ca070017ef3241af11b46cf3367'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
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
|
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.
|
6
|
+
VERSION = '1.1.1'
|
7
7
|
|
8
|
-
|
9
|
-
:
|
10
|
-
:
|
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
|
-
|
13
|
-
|
14
|
-
|
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.
|
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.
|
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-
|
11
|
+
date: 2017-04-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|