active_delegate 1.0.1 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +2 -0
- data/Gemfile.lock +15 -15
- data/Rakefile +2 -0
- data/lib/active_delegate.rb +3 -5
- data/lib/active_delegate/association/methods.rb +3 -6
- data/lib/active_delegate/associations.rb +3 -4
- data/lib/active_delegate/attribute/accessor.rb +4 -18
- data/lib/active_delegate/attribute/localize.rb +3 -4
- data/lib/active_delegate/attribute/methods.rb +13 -10
- data/lib/active_delegate/attribute/object.rb +3 -23
- data/lib/active_delegate/attributes.rb +6 -20
- data/lib/active_delegate/delegator.rb +5 -10
- data/lib/active_delegate/version.rb +3 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a0ff0702750360bd4c01ebc4075853ce241921ec038c864e6c3536934adfbcba
|
4
|
+
data.tar.gz: 3e18f2b5f05f886b1d5a25e7e2b46cc5fb511ccd90edbff242b543891512359c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 81f7b9b0b20bca2831c30c1e90ed4789cfcac4501b37816eb4ff6879a78da79875d67f7a9fd90a71c4d5a977a6eec4cfe376424c10e7c04cd4d4b283dca2ddaa
|
7
|
+
data.tar.gz: b6bef5e43efab6fa63b8f3f95952fc2e1f3df0cdd2f4af7cefdad6e73e18edddb2b8ebd13d78130bce3b3dfb443e086f23445b48a72fb9c4eb71c9d56e2734dc
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,31 +1,31 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
active_delegate (0.1
|
4
|
+
active_delegate (1.0.1)
|
5
5
|
activerecord (~> 5.0)
|
6
|
-
i18n (~> 0.8)
|
7
6
|
|
8
7
|
GEM
|
9
8
|
remote: https://rubygems.org/
|
10
9
|
specs:
|
11
|
-
activemodel (5.
|
12
|
-
activesupport (= 5.
|
13
|
-
activerecord (5.
|
14
|
-
activemodel (= 5.
|
15
|
-
activesupport (= 5.
|
16
|
-
arel (
|
17
|
-
activesupport (5.
|
10
|
+
activemodel (5.2.1)
|
11
|
+
activesupport (= 5.2.1)
|
12
|
+
activerecord (5.2.1)
|
13
|
+
activemodel (= 5.2.1)
|
14
|
+
activesupport (= 5.2.1)
|
15
|
+
arel (>= 9.0)
|
16
|
+
activesupport (5.2.1)
|
18
17
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
19
|
-
i18n (
|
18
|
+
i18n (>= 0.7, < 2)
|
20
19
|
minitest (~> 5.1)
|
21
20
|
tzinfo (~> 1.1)
|
22
|
-
arel (
|
21
|
+
arel (9.0.0)
|
23
22
|
concurrent-ruby (1.0.5)
|
24
|
-
i18n (
|
25
|
-
|
23
|
+
i18n (1.1.0)
|
24
|
+
concurrent-ruby (~> 1.0)
|
25
|
+
minitest (5.10.3)
|
26
26
|
rake (10.5.0)
|
27
27
|
thread_safe (0.3.6)
|
28
|
-
tzinfo (1.2.
|
28
|
+
tzinfo (1.2.5)
|
29
29
|
thread_safe (~> 0.1)
|
30
30
|
|
31
31
|
PLATFORMS
|
@@ -38,4 +38,4 @@ DEPENDENCIES
|
|
38
38
|
rake (~> 10.0)
|
39
39
|
|
40
40
|
BUNDLED WITH
|
41
|
-
1.
|
41
|
+
1.16.6
|
data/Rakefile
CHANGED
data/lib/active_delegate.rb
CHANGED
@@ -1,15 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'active_record'
|
2
4
|
require 'active_delegate/version'
|
3
5
|
|
6
|
+
# Defines delegation methods and loads modules
|
4
7
|
module ActiveDelegate
|
5
8
|
extend ActiveSupport::Concern
|
6
9
|
|
7
|
-
# Autoload modules
|
8
10
|
autoload :Associations, 'active_delegate/associations'
|
9
11
|
autoload :Attributes, 'active_delegate/attributes'
|
10
12
|
|
11
13
|
class_methods do
|
12
|
-
# Delegate associations
|
13
14
|
def delegate_associations(*args)
|
14
15
|
options = args.extract_options!
|
15
16
|
options = options.reverse_merge(only: args)
|
@@ -17,13 +18,11 @@ module ActiveDelegate
|
|
17
18
|
Associations.new(self, options).call
|
18
19
|
end
|
19
20
|
|
20
|
-
# Delegate association
|
21
21
|
def delegate_association(association, options = {})
|
22
22
|
options = options.merge(only: association)
|
23
23
|
Associations.new(self, options).call
|
24
24
|
end
|
25
25
|
|
26
|
-
# Delegate attributes
|
27
26
|
def delegate_attributes(*args)
|
28
27
|
options = args.extract_options!
|
29
28
|
options = options.reverse_merge(only: args).except(:cast_type, :alias)
|
@@ -31,7 +30,6 @@ module ActiveDelegate
|
|
31
30
|
Attributes.new(self, options).call
|
32
31
|
end
|
33
32
|
|
34
|
-
# Delegate attribute
|
35
33
|
def delegate_attribute(attribute, cast_type, options = {})
|
36
34
|
options = options.except(:only, :except)
|
37
35
|
options = options.merge(only: attribute, cast_type: cast_type)
|
@@ -1,15 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module ActiveDelegate
|
2
4
|
module Association
|
5
|
+
# Generates association method names
|
3
6
|
class Methods
|
4
7
|
attr_reader :association_name, :association_class
|
5
8
|
|
6
|
-
# Initialize association methods
|
7
9
|
def initialize(association_name, association_class)
|
8
10
|
@association_name = association_name
|
9
11
|
@association_class = association_class
|
10
12
|
end
|
11
13
|
|
12
|
-
# Get delegatable methods
|
13
14
|
def delegatable
|
14
15
|
delegatable = suffixed + prefixed
|
15
16
|
delegatable & association_class.instance_methods
|
@@ -17,22 +18,18 @@ module ActiveDelegate
|
|
17
18
|
|
18
19
|
private
|
19
20
|
|
20
|
-
# Get method prefixes
|
21
21
|
def prefixes
|
22
22
|
['build_']
|
23
23
|
end
|
24
24
|
|
25
|
-
# Get method suffixes
|
26
25
|
def suffixes
|
27
26
|
['', '=', '_attributes', '_attributes=']
|
28
27
|
end
|
29
28
|
|
30
|
-
# Get prefixed methods
|
31
29
|
def prefixed
|
32
30
|
prefixes.map { |s| :"#{s}#{attribute}" }
|
33
31
|
end
|
34
32
|
|
35
|
-
# Get suffixed methods
|
36
33
|
def suffixed
|
37
34
|
suffixes.map { |s| :"#{attribute}#{s}" }
|
38
35
|
end
|
@@ -1,24 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'active_delegate/delegator'
|
2
4
|
require 'active_delegate/association/methods'
|
3
5
|
|
4
6
|
module ActiveDelegate
|
7
|
+
# Delegates associations to an associated model
|
5
8
|
class Associations < Delegator
|
6
|
-
# Get all associations
|
7
9
|
def association_reflections
|
8
10
|
association_class.reflect_on_all_associations
|
9
11
|
end
|
10
12
|
|
11
|
-
# Get singular model association names
|
12
13
|
def association_names
|
13
14
|
association_reflections.map(&:name)
|
14
15
|
end
|
15
16
|
|
16
|
-
# Get delegatable associations
|
17
17
|
def delegatable_associations
|
18
18
|
delegation_args(association_names)
|
19
19
|
end
|
20
20
|
|
21
|
-
# Delegate associations
|
22
21
|
def call
|
23
22
|
delegatable_associations.each do |association|
|
24
23
|
methods = Association::Methods.new(association, association_class)
|
@@ -1,87 +1,74 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module ActiveDelegate
|
2
4
|
module Attribute
|
5
|
+
# Reads, writes and type casts attribute value
|
3
6
|
class Accessor
|
4
7
|
attr_reader :record, :options
|
5
8
|
|
6
|
-
# Initialize attribute
|
7
9
|
def initialize(record, options = {})
|
8
10
|
@record = record
|
9
11
|
@options = options
|
10
12
|
end
|
11
13
|
|
12
|
-
# Get default value
|
13
14
|
def default_value
|
14
15
|
options[:default]
|
15
16
|
end
|
16
17
|
|
17
|
-
# Get association name
|
18
18
|
def association_name
|
19
19
|
options[:association].to_sym
|
20
20
|
end
|
21
21
|
|
22
|
-
# Get association attribute name
|
23
22
|
def attribute_name
|
24
23
|
options[:attribute].to_sym
|
25
24
|
end
|
26
25
|
|
27
|
-
# Get record attribute type
|
28
26
|
def read_type
|
29
27
|
options[:read_type]
|
30
28
|
end
|
31
29
|
|
32
|
-
# Get association attribute type
|
33
30
|
def write_type
|
34
31
|
options[:write_type]
|
35
32
|
end
|
36
33
|
|
37
|
-
# Check if should cast value
|
38
34
|
def type_cast?
|
39
|
-
|
35
|
+
read_type_caster.type != write_type_caster.type
|
40
36
|
end
|
41
37
|
|
42
|
-
# Get associated value
|
43
38
|
def association_record
|
44
39
|
record.send(association_name)
|
45
40
|
end
|
46
41
|
|
47
|
-
# Get association attribute value
|
48
42
|
def attribute_value(*args)
|
49
43
|
association_record.try(attribute_name, *args)
|
50
44
|
end
|
51
45
|
|
52
|
-
# Get record attribute type caster
|
53
46
|
def read_type_caster
|
54
47
|
lookup_type_caster(read_type)
|
55
48
|
end
|
56
49
|
|
57
|
-
# Get association attribute type caster
|
58
50
|
def write_type_caster
|
59
51
|
lookup_type_caster(write_type)
|
60
52
|
end
|
61
53
|
|
62
|
-
# Cast value for reading
|
63
54
|
def cast_read_value(value)
|
64
55
|
read_type_caster.cast(value)
|
65
56
|
end
|
66
57
|
|
67
|
-
# Cast value for writing
|
68
58
|
def cast_write_value(value)
|
69
59
|
write_type_caster.cast(value)
|
70
60
|
end
|
71
61
|
|
72
|
-
# Prepare association attribute value for writing
|
73
62
|
def normalize_value(value)
|
74
63
|
value = cast_read_value(value)
|
75
64
|
cast_write_value(value)
|
76
65
|
end
|
77
66
|
|
78
|
-
# Read and cast value
|
79
67
|
def read(*args)
|
80
68
|
value = attribute_value(*args) || default_value
|
81
69
|
type_cast? ? cast_read_value(value) : value
|
82
70
|
end
|
83
71
|
|
84
|
-
# Cast and write value
|
85
72
|
def write(value)
|
86
73
|
value = normalize_value(value) if type_cast?
|
87
74
|
association_record.send(:"#{attribute_name}=", value)
|
@@ -89,7 +76,6 @@ module ActiveDelegate
|
|
89
76
|
|
90
77
|
private
|
91
78
|
|
92
|
-
# Get type caster class
|
93
79
|
def lookup_type_caster(type_caster)
|
94
80
|
if type_caster.is_a?(Symbol)
|
95
81
|
ActiveRecord::Type.lookup(type_caster)
|
@@ -1,15 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module ActiveDelegate
|
2
4
|
module Attribute
|
5
|
+
# Generates localized attributes names
|
3
6
|
class Localize
|
4
7
|
attr_reader :attribute_name, :association_instance
|
5
8
|
|
6
|
-
# Initialize attribute methods
|
7
9
|
def initialize(attribute_name, association_class)
|
8
10
|
@attribute_name = attribute_name
|
9
11
|
@association_instance = association_class.new
|
10
12
|
end
|
11
13
|
|
12
|
-
# Get localized attributes
|
13
14
|
def attributes
|
14
15
|
localized = suffixes.map { |s| :"#{attribute_name}#{s}" }
|
15
16
|
localized & association_instance.methods
|
@@ -17,14 +18,12 @@ module ActiveDelegate
|
|
17
18
|
|
18
19
|
private
|
19
20
|
|
20
|
-
# Get localized method suffixes
|
21
21
|
def suffixes
|
22
22
|
@suffixes ||= I18n.available_locales.map do |locale|
|
23
23
|
"_#{normalize_locale(locale)}"
|
24
24
|
end
|
25
25
|
end
|
26
26
|
|
27
|
-
# Normalize locale
|
28
27
|
def normalize_locale(locale)
|
29
28
|
locale.to_s.downcase.sub('-', '_').to_s
|
30
29
|
end
|
@@ -1,44 +1,47 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module ActiveDelegate
|
2
4
|
module Attribute
|
5
|
+
# Generates attribute method names
|
3
6
|
class Methods
|
4
7
|
attr_reader :attribute_name, :association_instance
|
5
8
|
|
6
|
-
# Initialize attribute methods
|
7
9
|
def initialize(attribute_name, association_class)
|
8
10
|
@attribute_name = attribute_name
|
9
11
|
@association_instance = association_class.new
|
10
12
|
end
|
11
13
|
|
12
|
-
# Get accessor methods
|
13
14
|
def accessors
|
14
15
|
suffix_attribute(accessor_suffixes)
|
15
16
|
end
|
16
17
|
|
17
|
-
# Get dirty methods for attributes
|
18
18
|
def dirty
|
19
19
|
suffix_attribute(dirty_suffixes)
|
20
20
|
end
|
21
21
|
|
22
|
-
# Get delegatable methods
|
23
22
|
def delegatable
|
24
23
|
accessors + dirty
|
25
24
|
end
|
26
25
|
|
27
26
|
private
|
28
27
|
|
29
|
-
# Get accessor method suffixes
|
30
28
|
def accessor_suffixes
|
31
29
|
['', '=', '?']
|
32
30
|
end
|
33
31
|
|
34
|
-
|
35
|
-
|
36
|
-
@dirty_suffixes ||= Class.new do
|
32
|
+
def dirty_module
|
33
|
+
@dirty_module ||= Class.new do
|
37
34
|
include ::ActiveModel::Dirty
|
38
|
-
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def dirty_suffixes
|
39
|
+
@dirty_suffixes ||= begin
|
40
|
+
suffixes = dirty_module.attribute_method_matchers.map(&:suffix)
|
41
|
+
suffixes.select { |m| m.starts_with?('_') }
|
42
|
+
end
|
39
43
|
end
|
40
44
|
|
41
|
-
# Generate suffixed array of symbols
|
42
45
|
def suffix_attribute(suffixes)
|
43
46
|
delegatable = suffixes.map { |s| :"#{attribute_name}#{s}" }
|
44
47
|
delegatable & association_instance.methods
|
@@ -1,105 +1,89 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'active_delegate/attribute/methods'
|
2
4
|
require 'active_delegate/attribute/localize'
|
3
5
|
|
4
6
|
module ActiveDelegate
|
5
7
|
module Attribute
|
8
|
+
# Calculates attribute methods, aliases and options
|
6
9
|
class Object
|
7
10
|
attr_reader :attribute_name, :association_class, :options
|
8
11
|
|
9
|
-
# Initialize attribute methods
|
10
12
|
def initialize(attribute_name, association_class, options = {})
|
11
13
|
@attribute_name = attribute_name
|
12
14
|
@association_class = association_class
|
13
15
|
@options = options
|
14
16
|
end
|
15
17
|
|
16
|
-
# Check if should define attribute
|
17
18
|
def define?
|
18
19
|
options[:define] || in_option?(:define)
|
19
20
|
end
|
20
21
|
|
21
|
-
# Check if should localize attribute
|
22
22
|
def localize?
|
23
23
|
options[:localized] || in_option?(:localized)
|
24
24
|
end
|
25
25
|
|
26
|
-
# Check if should define attribute finders
|
27
26
|
def finder?
|
28
27
|
options[:finder] || in_option?(:finder)
|
29
28
|
end
|
30
29
|
|
31
|
-
# Check if should define attribute scopes
|
32
30
|
def scope?
|
33
31
|
options[:scope] || in_option?(:scope)
|
34
32
|
end
|
35
33
|
|
36
|
-
# Get attribute prefix
|
37
34
|
def prefix
|
38
35
|
options[:prefix]
|
39
36
|
end
|
40
37
|
|
41
|
-
# Get attribute default
|
42
38
|
def default
|
43
39
|
options.fetch :default, association_class.column_defaults[unprefixed.to_s]
|
44
40
|
end
|
45
41
|
|
46
|
-
# Get read type or fallback to write type
|
47
42
|
def read_type
|
48
43
|
options.fetch :cast_type, write_type
|
49
44
|
end
|
50
45
|
|
51
|
-
# Get write type from associated model
|
52
46
|
def write_type
|
53
47
|
association_class.type_for_attribute(unprefixed.to_s)
|
54
48
|
end
|
55
49
|
|
56
|
-
# Get unprefixed attribute
|
57
50
|
def unprefixed
|
58
51
|
remove_prefix(attribute_name)
|
59
52
|
end
|
60
53
|
|
61
|
-
# Get prefixed attribute
|
62
54
|
def prefixed
|
63
55
|
add_prefix(attribute_name)
|
64
56
|
end
|
65
57
|
|
66
|
-
# Check if attribute is prefixed
|
67
58
|
def prefixed?
|
68
59
|
unprefixed != prefixed
|
69
60
|
end
|
70
61
|
|
71
|
-
# Get aliased attribute
|
72
62
|
def aliased
|
73
63
|
options[:alias] || prefixed
|
74
64
|
end
|
75
65
|
|
76
|
-
# Check if attribute is aliased
|
77
66
|
def aliased?
|
78
67
|
prefixed != aliased
|
79
68
|
end
|
80
69
|
|
81
|
-
# Get method aliases
|
82
70
|
def aliases
|
83
71
|
return {} unless aliased?
|
84
72
|
Hash[delegatable_methods.map { |m| generate_alias(m) }]
|
85
73
|
end
|
86
74
|
|
87
|
-
# Get localized attributes
|
88
75
|
def localized
|
89
76
|
@localized ||= Localize.new(unprefixed, association_class).attributes
|
90
77
|
end
|
91
78
|
|
92
|
-
# Check if attributes has localized methods
|
93
79
|
def localized?
|
94
80
|
localized.any?
|
95
81
|
end
|
96
82
|
|
97
|
-
# Get delegatable attributes
|
98
83
|
def delegatable_attributes
|
99
84
|
@delegatable_attributes ||= [unprefixed, *localized].map { |a| add_prefix(a) }
|
100
85
|
end
|
101
86
|
|
102
|
-
# Get delegatable attribute methods
|
103
87
|
def delegatable_methods
|
104
88
|
@delegatable_methods ||= [unprefixed, *localized].flat_map do |method_name|
|
105
89
|
Methods.new(method_name, association_class).delegatable
|
@@ -108,17 +92,14 @@ module ActiveDelegate
|
|
108
92
|
|
109
93
|
private
|
110
94
|
|
111
|
-
# Prefix attribute
|
112
95
|
def add_prefix(attr_name)
|
113
96
|
prefix.present? ? :"#{prefix}_#{attr_name}" : attr_name
|
114
97
|
end
|
115
98
|
|
116
|
-
# Unprefix attribute
|
117
99
|
def remove_prefix(attr_name)
|
118
100
|
attr_name.to_s.sub("#{prefix}_", '').to_sym
|
119
101
|
end
|
120
102
|
|
121
|
-
# Generate alias method
|
122
103
|
def generate_alias(method_name)
|
123
104
|
old_name = method_name.to_s.sub(unprefixed.to_s, prefixed.to_s)
|
124
105
|
new_name = method_name.to_s.sub(unprefixed.to_s, aliased.to_s)
|
@@ -126,7 +107,6 @@ module ActiveDelegate
|
|
126
107
|
[new_name.to_sym, old_name.to_sym]
|
127
108
|
end
|
128
109
|
|
129
|
-
# Check if attribute is in option
|
130
110
|
def in_option?(key)
|
131
111
|
attribute_name.in? Array(options[key])
|
132
112
|
end
|
@@ -1,10 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'active_delegate/delegator'
|
2
4
|
require 'active_delegate/attribute/object'
|
3
5
|
require 'active_delegate/attribute/accessor'
|
4
6
|
|
5
7
|
module ActiveDelegate
|
8
|
+
# Delegates attributes to an associated model
|
6
9
|
class Attributes < Delegator
|
7
|
-
# Get default options
|
8
10
|
def default_options
|
9
11
|
{
|
10
12
|
except: [],
|
@@ -19,18 +21,15 @@ module ActiveDelegate
|
|
19
21
|
}
|
20
22
|
end
|
21
23
|
|
22
|
-
# Get attribute options
|
23
24
|
def attribute_options
|
24
25
|
keys = %i[cast_type default define alias localized finder scope]
|
25
26
|
options.select { |k, _v| k.in? keys }.merge(prefix: delegation_prefix)
|
26
27
|
end
|
27
28
|
|
28
|
-
# Get association table
|
29
29
|
def association_table
|
30
30
|
association_class.table_name
|
31
31
|
end
|
32
32
|
|
33
|
-
# Default excluded attributes
|
34
33
|
def excluded_attributes
|
35
34
|
excluded = %i[id created_at updated_at]
|
36
35
|
|
@@ -43,7 +42,6 @@ module ActiveDelegate
|
|
43
42
|
excluded
|
44
43
|
end
|
45
44
|
|
46
|
-
# Get delegatable attributes
|
47
45
|
def delegatable_attributes
|
48
46
|
attributes = delegation_args(association_class.attribute_names)
|
49
47
|
attributes -= excluded_attributes
|
@@ -57,7 +55,6 @@ module ActiveDelegate
|
|
57
55
|
attributes.reject { |a| model.has_attribute?(a.prefixed) }
|
58
56
|
end
|
59
57
|
|
60
|
-
# Delegate attributes
|
61
58
|
def call
|
62
59
|
redefine_build_association(association_name)
|
63
60
|
|
@@ -72,18 +69,16 @@ module ActiveDelegate
|
|
72
69
|
|
73
70
|
private
|
74
71
|
|
75
|
-
# Redefine build association method
|
76
72
|
def redefine_build_association(assoc_name)
|
77
73
|
model.class_eval do
|
78
|
-
class_eval <<-
|
74
|
+
class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
79
75
|
def #{assoc_name}
|
80
76
|
super || send(:build_#{assoc_name})
|
81
77
|
end
|
82
|
-
|
78
|
+
RUBY
|
83
79
|
end
|
84
80
|
end
|
85
81
|
|
86
|
-
# Redefine attribute accessor methods
|
87
82
|
def redefine_attribute_accessors(method_name, attribute)
|
88
83
|
attr_options = {
|
89
84
|
association: association_name,
|
@@ -102,18 +97,15 @@ module ActiveDelegate
|
|
102
97
|
end
|
103
98
|
end
|
104
99
|
|
105
|
-
# Delegate attribute methods
|
106
100
|
def delegate_methods(methods)
|
107
101
|
model.delegate(*methods, delegation_options)
|
108
102
|
end
|
109
103
|
|
110
|
-
# Define model method keeping old values
|
111
104
|
def define_model_method(method, *attributes)
|
112
105
|
attributes = model.try(method).to_a.concat(attributes).uniq
|
113
106
|
model.send(:define_singleton_method, method) { attributes }
|
114
107
|
end
|
115
108
|
|
116
|
-
# Store attribute names in model class methods
|
117
109
|
def define_model_class_methods(attribute)
|
118
110
|
method_name = :"#{association_table}_attribute_names"
|
119
111
|
define_model_method(method_name, attribute.prefixed)
|
@@ -122,11 +114,8 @@ module ActiveDelegate
|
|
122
114
|
define_model_method(method_name, attribute.prefixed) if attribute.localized?
|
123
115
|
end
|
124
116
|
|
125
|
-
# Define delegated attribute methods
|
126
117
|
def define_attribute_methods(attribute)
|
127
|
-
if attribute.define?
|
128
|
-
model.attribute(attribute.aliased, attribute.read_type)
|
129
|
-
end
|
118
|
+
model.attribute(attribute.aliased, attribute.read_type) if attribute.define?
|
130
119
|
|
131
120
|
attribute.delegatable_attributes.each do |method_name|
|
132
121
|
redefine_attribute_accessors(method_name, attribute)
|
@@ -137,7 +126,6 @@ module ActiveDelegate
|
|
137
126
|
end
|
138
127
|
end
|
139
128
|
|
140
|
-
# Define attribute finder methods
|
141
129
|
def define_attribute_finders(attr_method:, attr_column:, assoc_name:, table_name:)
|
142
130
|
model.send(:define_singleton_method, :"find_by_#{attr_method}") do |value|
|
143
131
|
joins(assoc_name).find_by(table_name => { attr_column => value })
|
@@ -148,7 +136,6 @@ module ActiveDelegate
|
|
148
136
|
end
|
149
137
|
end
|
150
138
|
|
151
|
-
# Define attribute scope methods
|
152
139
|
def define_attribute_scopes(attr_method:, attr_column:, assoc_name:, table_name:)
|
153
140
|
model.send(:define_singleton_method, :"with_#{attr_method}") do |*args|
|
154
141
|
joins(assoc_name).where(table_name => { attr_column => args })
|
@@ -159,7 +146,6 @@ module ActiveDelegate
|
|
159
146
|
end
|
160
147
|
end
|
161
148
|
|
162
|
-
# Define attribute finders and scopes
|
163
149
|
def define_attribute_queries(attribute)
|
164
150
|
attr_options = {
|
165
151
|
assoc_name: association_name,
|
@@ -1,14 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module ActiveDelegate
|
4
|
+
# Validates delegation target and sets basic options
|
2
5
|
class Delegator
|
3
6
|
attr_reader :model, :options
|
4
7
|
|
5
|
-
# Initialize delegator
|
6
8
|
def initialize(model, options)
|
7
9
|
@model = model
|
8
10
|
@options = default_options.merge(options.symbolize_keys)
|
9
11
|
end
|
10
12
|
|
11
|
-
# Get default options
|
12
13
|
def default_options
|
13
14
|
{
|
14
15
|
except: [],
|
@@ -18,12 +19,10 @@ module ActiveDelegate
|
|
18
19
|
}
|
19
20
|
end
|
20
21
|
|
21
|
-
# Get delegation options
|
22
22
|
def delegation_options
|
23
23
|
options.select { |k, _v| k.in? %i[to allow_nil prefix] }
|
24
24
|
end
|
25
25
|
|
26
|
-
# Get delegation arguments
|
27
26
|
def delegation_args(available = [])
|
28
27
|
included = Array(options[:only]).map(&:to_sym)
|
29
28
|
excluded = Array(options[:except]).map(&:to_sym)
|
@@ -34,25 +33,21 @@ module ActiveDelegate
|
|
34
33
|
available
|
35
34
|
end
|
36
35
|
|
37
|
-
# Get delegation prefix
|
38
36
|
def delegation_prefix
|
39
37
|
prefix = options[:prefix]
|
40
38
|
prefix == true ? association_name : prefix
|
41
39
|
end
|
42
40
|
|
43
|
-
# Get association name
|
44
41
|
def association_name
|
45
42
|
@options[:to]
|
46
43
|
end
|
47
44
|
|
48
|
-
# Get association reflection
|
49
45
|
def association_reflection
|
50
|
-
reflection =
|
46
|
+
reflection = model.reflect_on_association(association_name)
|
51
47
|
return reflection unless reflection.nil?
|
52
|
-
raise "#{
|
48
|
+
raise "#{model.name} don't have the association #{association_name}"
|
53
49
|
end
|
54
50
|
|
55
|
-
# Get model association class
|
56
51
|
def association_class
|
57
52
|
association_reflection.klass
|
58
53
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_delegate
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jonian Guveli
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-10-
|
11
|
+
date: 2018-10-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|