active_delegate 1.0.2 → 1.0.3

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: f0d5996473a59885a272818e9654d9163978b34d0a280f068078811ab6faa6b9
4
+ data.tar.gz: cc625328747d9108c7c42a85714a3be6829f8dd0be44736be2c1d6484f41d9c2
5
5
  SHA512:
6
- metadata.gz: 81f7b9b0b20bca2831c30c1e90ed4789cfcac4501b37816eb4ff6879a78da79875d67f7a9fd90a71c4d5a977a6eec4cfe376424c10e7c04cd4d4b283dca2ddaa
7
- data.tar.gz: b6bef5e43efab6fa63b8f3f95952fc2e1f3df0cdd2f4af7cefdad6e73e18edddb2b8ebd13d78130bce3b3dfb443e086f23445b48a72fb9c4eb71c9d56e2734dc
6
+ metadata.gz: 3aaa1cd551c0d1758441b5365532bf57de54c8ab0d0bd369be80f3c540a6744cbba758a41f52c430905252d2ef25731709ad19126eb7d78758aa53b50f34d0cd
7
+ data.tar.gz: f73de9365f2ac2201555b3e19be8ddb3817a4db18dffab719d2a0f29b747c518e2e38bd2a0952772a3e7895cb40ea301656e50a07d73d6f8d545de56c4e48e7c
@@ -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
 
@@ -86,7 +97,11 @@ module ActiveDelegate
86
97
 
87
98
  def delegatable_methods
88
99
  @delegatable_methods ||= [unprefixed, *localized].flat_map do |method_name|
89
- Methods.new(method_name, association_class).delegatable
100
+ methods = Methods.new(
101
+ method_name, association_class, writer: writer?, dirty: dirty?
102
+ )
103
+
104
+ methods.delegatable
90
105
  end
91
106
  end
92
107
 
@@ -97,7 +112,7 @@ module ActiveDelegate
97
112
  end
98
113
 
99
114
  def remove_prefix(attr_name)
100
- attr_name.to_s.sub("#{prefix}_", '').to_sym
115
+ prefix.present? ? attr_name.to_s.sub("#{prefix}_", '').to_sym : attr_name
101
116
  end
102
117
 
103
118
  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,10 +94,17 @@ 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
@@ -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.3'
5
5
  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.2
4
+ version: 1.0.3
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 00:00:00.000000000 Z
11
+ date: 2019-02-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -108,8 +108,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
108
108
  - !ruby/object:Gem::Version
109
109
  version: '0'
110
110
  requirements: []
111
- rubyforge_project:
112
- rubygems_version: 2.7.7
111
+ rubygems_version: 3.0.2
113
112
  signing_key:
114
113
  specification_version: 4
115
114
  summary: Delegate ActiveRecord model attributes and associations