active_tools 0.0.50 → 0.0.51
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 +4 -4
- data/lib/active_tools/action_pack/action_view/field_name.rb +22 -0
- data/lib/active_tools/active_record/adaptive_belongs_to/adapter.rb +48 -32
- data/lib/active_tools/active_record/adaptive_belongs_to.rb +20 -15
- data/lib/active_tools/active_record/custom_counter_cache.rb +5 -0
- data/lib/active_tools/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3690b827d1bbd74263c304724e70dcee2edeb90d
|
4
|
+
data.tar.gz: a971e63143d76aeb518becfec2d20be3a4999f5e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cdc442425869c45f6d5a580e60cf4145f1bad3e99c5c411e0e4934870644cffa63638369f2b3548b3f55902e123261a229c52e78254142457c623756bb1247b6
|
7
|
+
data.tar.gz: be4c77d4de9e5e21cd737b9ff1282881c46a1072c4aa63f438e03d061d844d48cd1f41b319b9fbd49812550958f08c868786ce9435b61193ff7dc3c1707d6b6d
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module ActiveTools
|
2
|
+
module ActionPack
|
3
|
+
module ActionView
|
4
|
+
module FieldName
|
5
|
+
|
6
|
+
end
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
module OnLoadActionView
|
11
|
+
|
12
|
+
def field_name(builder, *args)
|
13
|
+
[object_name_to_field_id(builder.object_name), *args].join("_")
|
14
|
+
end
|
15
|
+
|
16
|
+
def object_name_to_field_id(val)
|
17
|
+
val.gsub(/\]\[|[^-a-zA-Z0-9:.]/, "_").sub(/_$/, "")
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
@@ -18,13 +18,14 @@ module ActiveTools
|
|
18
18
|
@update_if = @options[:update_if]
|
19
19
|
@destroy_if = @options[:destroy_if]
|
20
20
|
@uniq_by = Array(@options[:uniq_by]).map(&:to_s)
|
21
|
+
@target_process = @options[:target_process]
|
21
22
|
association.load_target
|
22
23
|
end
|
23
|
-
|
24
|
+
|
24
25
|
def klass
|
25
26
|
association.klass||reflection.class_name.constantize
|
26
27
|
end
|
27
|
-
|
28
|
+
|
28
29
|
def association
|
29
30
|
owner.association(assoc_name)
|
30
31
|
end
|
@@ -48,9 +49,9 @@ module ActiveTools
|
|
48
49
|
end
|
49
50
|
end
|
50
51
|
end
|
51
|
-
|
52
|
+
|
52
53
|
def try_nullify
|
53
|
-
if nullify_target?
|
54
|
+
if nullify_target?
|
54
55
|
store_backup!
|
55
56
|
self.target = nil
|
56
57
|
true
|
@@ -58,10 +59,10 @@ module ActiveTools
|
|
58
59
|
end
|
59
60
|
|
60
61
|
def try_commit
|
61
|
-
try_commit_existed ||
|
62
|
+
try_commit_existed || try_restore_refreshed_backup
|
62
63
|
end
|
63
64
|
|
64
|
-
def try_destroy
|
65
|
+
def try_destroy
|
65
66
|
try_destroy_backup
|
66
67
|
try_destroy_target
|
67
68
|
end
|
@@ -69,29 +70,25 @@ module ActiveTools
|
|
69
70
|
def template_attributes
|
70
71
|
attributes(@template, *@remote_attributes)
|
71
72
|
end
|
72
|
-
|
73
|
+
|
73
74
|
def target_attributes
|
74
75
|
attributes(target, *@remote_attributes)
|
75
76
|
end
|
76
77
|
|
77
|
-
def
|
78
|
+
def try_restore_refreshed_backup
|
78
79
|
if updateable_backup?
|
79
|
-
warn "Adaptive
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
@backup.reload
|
87
|
-
try_update
|
80
|
+
warn "Adaptive going to update: <#{@backup.class.name}: #{@backup.class.primary_key}: #{@backup.send(@backup.class.primary_key)}>"
|
81
|
+
@backup.attributes = template_attributes
|
82
|
+
if @backup.valid?
|
83
|
+
restore_backup!
|
84
|
+
true
|
85
|
+
else
|
86
|
+
false
|
88
87
|
end
|
89
|
-
restore_backup!
|
90
|
-
true
|
91
88
|
end
|
92
89
|
end
|
93
90
|
|
94
|
-
def try_commit_existed
|
91
|
+
def try_commit_existed
|
95
92
|
if @template.present? && @uniq_by.any? && (existed = detect_existed) && !(@backup.present? && same_records?(@backup, existed, :attributes => @uniq_by))
|
96
93
|
warn "Adaptive fetching existed <#{existed.class.name}: #{existed.class.primary_key}: #{existed.send(existed.class.primary_key)}>"
|
97
94
|
self.target = existed
|
@@ -99,10 +96,10 @@ module ActiveTools
|
|
99
96
|
@backup.mark_for_destruction
|
100
97
|
end
|
101
98
|
true
|
102
|
-
end
|
99
|
+
end
|
103
100
|
end
|
104
101
|
|
105
|
-
def try_destroy_backup
|
102
|
+
def try_destroy_backup
|
106
103
|
if destroyable_backup?
|
107
104
|
warn "Adaptive destroying backed up: <#{@backup.class.name}: #{@backup.class.primary_key}: #{@backup.send(@backup.class.primary_key)}>"
|
108
105
|
begin
|
@@ -117,9 +114,9 @@ module ActiveTools
|
|
117
114
|
end
|
118
115
|
end
|
119
116
|
|
120
|
-
def try_destroy_target
|
117
|
+
def try_destroy_target
|
121
118
|
if destroyable_target?
|
122
|
-
warn "Adaptive destroying target: <#{target.class.name}: #{target.class.primary_key}: #{target.send(target.class.primary_key)}>"
|
119
|
+
warn "Adaptive destroying target: <#{target.class.name}: #{target.class.primary_key}: #{target.send(target.class.primary_key)}>"
|
123
120
|
begin
|
124
121
|
target.destroy
|
125
122
|
rescue ::ActiveRecord::StaleObjectError
|
@@ -131,6 +128,25 @@ module ActiveTools
|
|
131
128
|
end
|
132
129
|
end
|
133
130
|
end
|
131
|
+
|
132
|
+
def target_process_do
|
133
|
+
@target_process.try(:call, target, owner)
|
134
|
+
end
|
135
|
+
|
136
|
+
def update_target_if_changed!
|
137
|
+
if target.changes.any?
|
138
|
+
warn "Adaptive updating: <#{target.class.name}: #{target.class.primary_key}: #{target.send(target.class.primary_key)}>"
|
139
|
+
begin
|
140
|
+
target.save
|
141
|
+
rescue ::ActiveRecord::StaleObjectError
|
142
|
+
target.reload
|
143
|
+
update_target_if_changed!
|
144
|
+
rescue ::ActiveRecord::StatementInvalid
|
145
|
+
target.reload
|
146
|
+
update_target_if_changed!
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
134
150
|
|
135
151
|
def clear!
|
136
152
|
@template = nil
|
@@ -138,7 +154,7 @@ module ActiveTools
|
|
138
154
|
end
|
139
155
|
|
140
156
|
private
|
141
|
-
|
157
|
+
|
142
158
|
def target_id
|
143
159
|
association.send(:target_id)
|
144
160
|
end
|
@@ -174,7 +190,7 @@ module ActiveTools
|
|
174
190
|
def destroyable_target?
|
175
191
|
target.try(:persisted?) && (!target.destroyed?||target.marked_for_destruction?) && @destroy_if.try(:call, target.reload, owner)
|
176
192
|
end
|
177
|
-
|
193
|
+
|
178
194
|
def attributes(object, *attrs)
|
179
195
|
array = attrs.map do |a|
|
180
196
|
begin
|
@@ -187,7 +203,7 @@ module ActiveTools
|
|
187
203
|
end
|
188
204
|
|
189
205
|
def create_template!
|
190
|
-
if target.nil? || @template.nil?
|
206
|
+
if target.nil? || @template.nil?
|
191
207
|
self.target = template
|
192
208
|
end
|
193
209
|
end
|
@@ -201,13 +217,13 @@ module ActiveTools
|
|
201
217
|
@backup = nil
|
202
218
|
end
|
203
219
|
end
|
204
|
-
|
220
|
+
|
205
221
|
def store_backup!
|
206
222
|
if target.try(:persisted?)
|
207
223
|
@backup ||= target
|
208
224
|
end
|
209
225
|
end
|
210
|
-
|
226
|
+
|
211
227
|
def same_records?(*args)
|
212
228
|
options = args.extract_options!
|
213
229
|
attributes = options[:attributes]
|
@@ -255,10 +271,10 @@ module ActiveTools
|
|
255
271
|
@template.tap do |t|
|
256
272
|
@init_proc.try(:call, t, owner)
|
257
273
|
end
|
258
|
-
end
|
274
|
+
end
|
259
275
|
end
|
260
276
|
|
261
277
|
end
|
262
278
|
end
|
263
|
-
|
264
|
-
end
|
279
|
+
|
280
|
+
end
|
@@ -4,12 +4,12 @@ module ActiveTools
|
|
4
4
|
module ActiveRecord
|
5
5
|
module AdaptiveBelongsTo
|
6
6
|
extend ::ActiveSupport::Concern
|
7
|
-
|
7
|
+
|
8
8
|
included do
|
9
9
|
end
|
10
10
|
|
11
11
|
module ClassMethods
|
12
|
-
|
12
|
+
|
13
13
|
def relation_options_under(*args)
|
14
14
|
path = args.extract_options!
|
15
15
|
local_attribute = args.first
|
@@ -36,15 +36,15 @@ module ActiveTools
|
|
36
36
|
end
|
37
37
|
{:outer_values => outer_values, :where_values => where_values}
|
38
38
|
end
|
39
|
-
|
39
|
+
|
40
40
|
class_eval do
|
41
41
|
define_method local_method do
|
42
42
|
self.class.send(local_method, self)
|
43
43
|
end
|
44
44
|
end
|
45
|
-
|
45
|
+
|
46
46
|
end
|
47
|
-
|
47
|
+
|
48
48
|
def adaptive_belongs_to(*args)
|
49
49
|
options = args.extract_options!
|
50
50
|
assoc_name = args.first
|
@@ -52,19 +52,24 @@ module ActiveTools
|
|
52
52
|
raise(ArgumentError, ":#{assoc_name} method doesn't look like an association accessor!")
|
53
53
|
end
|
54
54
|
adapter_name = "#{assoc_name}_adaptive"
|
55
|
-
|
55
|
+
|
56
56
|
raise(TypeError, "Option :attributes must be a Hash. #{options[:attributes].class} passed!") unless options[:attributes].is_a?(Hash)
|
57
57
|
attr_map = HashWithIndifferentAccess.new(options.delete(:attributes))
|
58
58
|
valid_options = Hash(options.delete(:valid_with)).symbolize_keys
|
59
59
|
valid_with assoc_name, valid_options.merge(:attributes => attr_map) #, :fit => true
|
60
|
-
|
60
|
+
|
61
61
|
class_attribute :adaptive_options unless defined?(adaptive_options)
|
62
62
|
self.adaptive_options ||= {}
|
63
63
|
self.adaptive_options[assoc_name.to_sym] = options.merge(:attr_map => attr_map)
|
64
|
-
|
64
|
+
|
65
65
|
class_eval <<-EOV
|
66
66
|
before_validation do
|
67
67
|
#{adapter_name}.try_nullify||#{adapter_name}.try_commit
|
68
|
+
#{adapter_name}.target_process_do
|
69
|
+
end
|
70
|
+
|
71
|
+
before_save do
|
72
|
+
#{adapter_name}.update_target_if_changed!
|
68
73
|
end
|
69
74
|
|
70
75
|
after_save do
|
@@ -75,7 +80,7 @@ module ActiveTools
|
|
75
80
|
after_destroy do
|
76
81
|
#{adapter_name}.try_destroy
|
77
82
|
end
|
78
|
-
|
83
|
+
|
79
84
|
def #{adapter_name}
|
80
85
|
@#{adapter_name} ||= ActiveTools::ActiveRecord::AdaptiveBelongsTo::Adapter.new(self, :#{assoc_name}, adaptive_options[:#{assoc_name}])
|
81
86
|
end
|
@@ -92,10 +97,10 @@ module ActiveTools
|
|
92
97
|
end
|
93
98
|
end
|
94
99
|
end
|
95
|
-
|
100
|
+
|
96
101
|
end
|
97
102
|
end
|
98
|
-
|
103
|
+
|
99
104
|
# def reload(*args)
|
100
105
|
# super.tap do |record|
|
101
106
|
# adaptive_options.keys.each do |assoc_name|
|
@@ -105,12 +110,12 @@ module ActiveTools
|
|
105
110
|
# end
|
106
111
|
# end
|
107
112
|
# end
|
108
|
-
|
113
|
+
|
109
114
|
end
|
110
115
|
end
|
111
|
-
|
116
|
+
|
112
117
|
module OnLoadActiveRecord
|
113
118
|
include ActiveRecord::AdaptiveBelongsTo
|
114
119
|
end
|
115
|
-
|
116
|
-
end
|
120
|
+
|
121
|
+
end
|
@@ -11,6 +11,11 @@ module ActiveTools
|
|
11
11
|
module ClassMethods
|
12
12
|
def custom_counter_cache_for(*args)
|
13
13
|
mapping = args.extract_options!
|
14
|
+
|
15
|
+
class_attribute :custom_counter_cache_options unless defined?(custom_counter_cache_options)
|
16
|
+
self.custom_counter_cache_options ||= {}
|
17
|
+
self.custom_counter_cache_options[:mapping] = mapping.with_indifferent_access
|
18
|
+
|
14
19
|
mapping.each do |assoc_name, value|
|
15
20
|
assoc_name = assoc_name.to_s
|
16
21
|
if assoc_name.last == "*"
|
data/lib/active_tools/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_tools
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.51
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Valery Kvon
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-08-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -63,6 +63,7 @@ files:
|
|
63
63
|
- lib/active_tools/action_pack/action_dispatch.rb
|
64
64
|
- lib/active_tools/action_pack/action_dispatch/flash_stack.rb
|
65
65
|
- lib/active_tools/action_pack/action_view.rb
|
66
|
+
- lib/active_tools/action_pack/action_view/field_name.rb
|
66
67
|
- lib/active_tools/action_pack/action_view/perform_as_tree.rb
|
67
68
|
- lib/active_tools/action_pack/action_view/tag_attributes.rb
|
68
69
|
- lib/active_tools/action_pack/action_view/uniq_content_for.rb
|