active_delegate 1.0.2 → 1.0.7

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
  SHA256:
3
- metadata.gz: a0ff0702750360bd4c01ebc4075853ce241921ec038c864e6c3536934adfbcba
4
- data.tar.gz: 3e18f2b5f05f886b1d5a25e7e2b46cc5fb511ccd90edbff242b543891512359c
3
+ metadata.gz: d85c2de28a990e8fb0e392262d2f0db2d63e31ddafb8a0326e33b4ab85290d5d
4
+ data.tar.gz: 86c12da318e53d570a90fdc7a7a0fd8d3af6612b41d161b023d0fd1f455a2f42
5
5
  SHA512:
6
- metadata.gz: 81f7b9b0b20bca2831c30c1e90ed4789cfcac4501b37816eb4ff6879a78da79875d67f7a9fd90a71c4d5a977a6eec4cfe376424c10e7c04cd4d4b283dca2ddaa
7
- data.tar.gz: b6bef5e43efab6fa63b8f3f95952fc2e1f3df0cdd2f4af7cefdad6e73e18edddb2b8ebd13d78130bce3b3dfb443e086f23445b48a72fb9c4eb71c9d56e2734dc
6
+ metadata.gz: 46c2b21fc4216a86b2fc448f1dfe3b45887765afc01b8c5150e578fd070a9f7db47a050edfb517f4d310d1748ae4967ac03bb3e720acd5d590d5fd81df90cc2b
7
+ data.tar.gz: 9bac2587b0324a2186d44689769345cb15f7f4e2a210bbed0e08206c1795c6e01a1ef0f9f9824c2a66d1104cef97e21733038301fc57b6ae6b325f9ca12952dc
@@ -27,11 +27,11 @@ module ActiveDelegate
27
27
  end
28
28
 
29
29
  def prefixed
30
- prefixes.map { |s| :"#{s}#{attribute}" }
30
+ prefixes.map { |s| :"#{s}#{association_name}" }
31
31
  end
32
32
 
33
33
  def suffixed
34
- suffixes.map { |s| :"#{attribute}#{s}" }
34
+ suffixes.map { |s| :"#{association_name}#{s}" }
35
35
  end
36
36
  end
37
37
  end
@@ -6,7 +6,9 @@ module ActiveDelegate
6
6
  class Methods
7
7
  attr_reader :attribute_name, :association_instance
8
8
 
9
- def initialize(attribute_name, association_class)
9
+ def initialize(attribute_name, association_class, writer:, dirty:)
10
+ @define_writer = writer
11
+ @define_dirty = dirty
10
12
  @attribute_name = attribute_name
11
13
  @association_instance = association_class.new
12
14
  end
@@ -26,7 +28,10 @@ module ActiveDelegate
26
28
  private
27
29
 
28
30
  def accessor_suffixes
29
- ['', '=', '?']
31
+ suffixes = ['', '?']
32
+ suffixes << '=' if @define_writer
33
+
34
+ suffixes
30
35
  end
31
36
 
32
37
  def dirty_module
@@ -36,6 +41,8 @@ module ActiveDelegate
36
41
  end
37
42
 
38
43
  def dirty_suffixes
44
+ return [] unless @define_dirty
45
+
39
46
  @dirty_suffixes ||= begin
40
47
  suffixes = dirty_module.attribute_method_matchers.map(&:suffix)
41
48
  suffixes.select { |m| m.starts_with?('_') }
@@ -16,19 +16,27 @@ module ActiveDelegate
16
16
  end
17
17
 
18
18
  def define?
19
- options[:define] || in_option?(:define)
19
+ dirty? && (in_option?(:define) || options[:define])
20
20
  end
21
21
 
22
22
  def localize?
23
- options[:localized] || in_option?(:localized)
23
+ in_option?(:localized) || options[:localized]
24
+ end
25
+
26
+ def writer?
27
+ in_option?(:writer) || options[:writer]
28
+ end
29
+
30
+ def dirty?
31
+ writer? && (in_option?(:dirty) || options[:dirty])
24
32
  end
25
33
 
26
34
  def finder?
27
- options[:finder] || in_option?(:finder)
35
+ in_option?(:finder) || options[:finder]
28
36
  end
29
37
 
30
38
  def scope?
31
- options[:scope] || in_option?(:scope)
39
+ in_option?(:scope) || options[:scope]
32
40
  end
33
41
 
34
42
  def prefix
@@ -69,10 +77,13 @@ module ActiveDelegate
69
77
 
70
78
  def aliases
71
79
  return {} unless aliased?
80
+
72
81
  Hash[delegatable_methods.map { |m| generate_alias(m) }]
73
82
  end
74
83
 
75
84
  def localized
85
+ return [] unless localize?
86
+
76
87
  @localized ||= Localize.new(unprefixed, association_class).attributes
77
88
  end
78
89
 
@@ -80,13 +91,21 @@ module ActiveDelegate
80
91
  localized.any?
81
92
  end
82
93
 
94
+ def accessible_attributes
95
+ @accessible_attributes ||= [unprefixed].map { |a| add_prefix(a) }
96
+ end
97
+
83
98
  def delegatable_attributes
84
99
  @delegatable_attributes ||= [unprefixed, *localized].map { |a| add_prefix(a) }
85
100
  end
86
101
 
87
102
  def delegatable_methods
88
103
  @delegatable_methods ||= [unprefixed, *localized].flat_map do |method_name|
89
- Methods.new(method_name, association_class).delegatable
104
+ methods = Methods.new(
105
+ method_name, association_class, writer: writer?, dirty: dirty?
106
+ )
107
+
108
+ methods.delegatable
90
109
  end
91
110
  end
92
111
 
@@ -97,7 +116,7 @@ module ActiveDelegate
97
116
  end
98
117
 
99
118
  def remove_prefix(attr_name)
100
- attr_name.to_s.sub("#{prefix}_", '').to_sym
119
+ prefix.present? ? attr_name.to_s.sub("#{prefix}_", '').to_sym : attr_name
101
120
  end
102
121
 
103
122
  def generate_alias(method_name)
@@ -11,6 +11,8 @@ module ActiveDelegate
11
11
  {
12
12
  except: [],
13
13
  only: [],
14
+ writer: true,
15
+ dirty: true,
14
16
  localized: false,
15
17
  define: true,
16
18
  finder: false,
@@ -22,7 +24,7 @@ module ActiveDelegate
22
24
  end
23
25
 
24
26
  def attribute_options
25
- keys = %i[cast_type default define alias localized finder scope]
27
+ keys = %i[cast_type default define alias writer dirty localized finder scope]
26
28
  options.select { |k, _v| k.in? keys }.merge(prefix: delegation_prefix)
27
29
  end
28
30
 
@@ -31,7 +33,11 @@ module ActiveDelegate
31
33
  end
32
34
 
33
35
  def excluded_attributes
34
- excluded = %i[id created_at updated_at]
36
+ excluded = %i[created_at updated_at]
37
+ excluded << association_reflection.active_record_primary_key.to_sym
38
+
39
+ for_key = association_reflection.foreign_key
40
+ excluded << for_key.to_sym if for_key.present?
35
41
 
36
42
  sti_col = association_class.inheritance_column
37
43
  excluded << sti_col.to_sym if sti_col.present?
@@ -88,17 +94,24 @@ module ActiveDelegate
88
94
  default: attribute.default
89
95
  }
90
96
 
97
+ redefine_reader(method_name, attr_options)
98
+ redefine_writer(method_name, attr_options) if attribute.writer?
99
+ end
100
+
101
+ def redefine_reader(method_name, attr_options)
91
102
  model.send(:redefine_method, method_name) do |*args|
92
103
  ActiveDelegate::Attribute::Accessor.new(self, attr_options).read(*args)
93
104
  end
105
+ end
94
106
 
107
+ def redefine_writer(method_name, attr_options)
95
108
  model.send(:redefine_method, :"#{method_name}=") do |value|
96
109
  ActiveDelegate::Attribute::Accessor.new(self, attr_options).write(value)
97
110
  end
98
111
  end
99
112
 
100
113
  def delegate_methods(methods)
101
- model.delegate(*methods, delegation_options)
114
+ model.delegate(*methods, **delegation_options)
102
115
  end
103
116
 
104
117
  def define_model_method(method, *attributes)
@@ -117,7 +130,7 @@ module ActiveDelegate
117
130
  def define_attribute_methods(attribute)
118
131
  model.attribute(attribute.aliased, attribute.read_type) if attribute.define?
119
132
 
120
- attribute.delegatable_attributes.each do |method_name|
133
+ attribute.accessible_attributes.each do |method_name|
121
134
  redefine_attribute_accessors(method_name, attribute)
122
135
  end
123
136
 
@@ -45,6 +45,7 @@ module ActiveDelegate
45
45
  def association_reflection
46
46
  reflection = model.reflect_on_association(association_name)
47
47
  return reflection unless reflection.nil?
48
+
48
49
  raise "#{model.name} don't have the association #{association_name}"
49
50
  end
50
51
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveDelegate
4
- VERSION = '1.0.2'
4
+ VERSION = '1.0.7'
5
5
  end
metadata CHANGED
@@ -1,43 +1,49 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_delegate
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonian Guveli
8
- autorequire:
9
- bindir: exe
8
+ autorequire:
9
+ bindir: bin
10
10
  cert_chain: []
11
- date: 2018-10-11 00:00:00.000000000 Z
11
+ date: 2020-06-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '5.0'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '7.0'
20
23
  type: :runtime
21
24
  prerelease: false
22
25
  version_requirements: !ruby/object:Gem::Requirement
23
26
  requirements:
24
- - - "~>"
27
+ - - ">="
25
28
  - !ruby/object:Gem::Version
26
29
  version: '5.0'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '7.0'
27
33
  - !ruby/object:Gem::Dependency
28
34
  name: bundler
29
35
  requirement: !ruby/object:Gem::Requirement
30
36
  requirements:
31
37
  - - "~>"
32
38
  - !ruby/object:Gem::Version
33
- version: '1.14'
39
+ version: '2.0'
34
40
  type: :development
35
41
  prerelease: false
36
42
  version_requirements: !ruby/object:Gem::Requirement
37
43
  requirements:
38
44
  - - "~>"
39
45
  - !ruby/object:Gem::Version
40
- version: '1.14'
46
+ version: '2.0'
41
47
  - !ruby/object:Gem::Dependency
42
48
  name: minitest
43
49
  requirement: !ruby/object:Gem::Requirement
@@ -58,14 +64,14 @@ dependencies:
58
64
  requirements:
59
65
  - - "~>"
60
66
  - !ruby/object:Gem::Version
61
- version: '10.0'
67
+ version: '13.0'
62
68
  type: :development
63
69
  prerelease: false
64
70
  version_requirements: !ruby/object:Gem::Requirement
65
71
  requirements:
66
72
  - - "~>"
67
73
  - !ruby/object:Gem::Version
68
- version: '10.0'
74
+ version: '13.0'
69
75
  description: Stores and retrieves delegatable data through attributes on an ActiveRecord
70
76
  class, with support for translatable attributes.
71
77
  email:
@@ -75,7 +81,6 @@ extensions: []
75
81
  extra_rdoc_files: []
76
82
  files:
77
83
  - Gemfile
78
- - Gemfile.lock
79
84
  - LICENSE.txt
80
85
  - README.md
81
86
  - Rakefile
@@ -93,7 +98,7 @@ homepage: https://github.com/hardpixel/active-delegate
93
98
  licenses:
94
99
  - MIT
95
100
  metadata: {}
96
- post_install_message:
101
+ post_install_message:
97
102
  rdoc_options: []
98
103
  require_paths:
99
104
  - lib
@@ -108,9 +113,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
108
113
  - !ruby/object:Gem::Version
109
114
  version: '0'
110
115
  requirements: []
111
- rubyforge_project:
112
- rubygems_version: 2.7.7
113
- signing_key:
116
+ rubygems_version: 3.1.2
117
+ signing_key:
114
118
  specification_version: 4
115
119
  summary: Delegate ActiveRecord model attributes and associations
116
120
  test_files: []
@@ -1,41 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- active_delegate (1.0.1)
5
- activerecord (~> 5.0)
6
-
7
- GEM
8
- remote: https://rubygems.org/
9
- specs:
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)
17
- concurrent-ruby (~> 1.0, >= 1.0.2)
18
- i18n (>= 0.7, < 2)
19
- minitest (~> 5.1)
20
- tzinfo (~> 1.1)
21
- arel (9.0.0)
22
- concurrent-ruby (1.0.5)
23
- i18n (1.1.0)
24
- concurrent-ruby (~> 1.0)
25
- minitest (5.10.3)
26
- rake (10.5.0)
27
- thread_safe (0.3.6)
28
- tzinfo (1.2.5)
29
- thread_safe (~> 0.1)
30
-
31
- PLATFORMS
32
- ruby
33
-
34
- DEPENDENCIES
35
- active_delegate!
36
- bundler (~> 1.14)
37
- minitest (~> 5.0)
38
- rake (~> 10.0)
39
-
40
- BUNDLED WITH
41
- 1.16.6