active_tools 0.0.6 → 0.0.10
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/active_tools.gemspec +2 -2
- data/lib/active_tools/action_pack/action_controller/path_helper.rb +10 -23
- data/lib/active_tools/action_pack/action_view/tag_attributes.rb +4 -1
- data/lib/active_tools/action_pack/action_view.rb +1 -0
- data/lib/active_tools/active_record/adaptive_belongs_to/adapter.rb +38 -24
- data/lib/active_tools/active_record/adaptive_belongs_to.rb +11 -21
- data/lib/active_tools/active_record/custom_counter_cache/instance_methods.rb +1 -1
- data/lib/active_tools/activerecord.rb +1 -0
- data/lib/active_tools/misc/script_flow.rb +22 -55
- data/lib/active_tools/misc.rb +1 -1
- data/lib/active_tools/version.rb +1 -1
- metadata +14 -14
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 51b06ad3dec14fa1f094a6024d64ca9ed4b50ed5
|
4
|
+
data.tar.gz: 7a599525d1dbefef2de63aca1be53497c0ee8407
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5d609f04f1b350636e47bf4107f438f1a34a654ca5a1e57e8276d322870d7df28df98acd35d3d6409af68871e63b7a63813c0a78b7c9d8e5ad19a01507581a04
|
7
|
+
data.tar.gz: c392bec6ca96f1b3ea97b27623c16ec88ed837557de8443b7faff5fdccb7914689457df20825a54569f133f03a6dda6555493c116aa010f0d7764f6a5c460cfc
|
data/active_tools.gemspec
CHANGED
@@ -14,8 +14,8 @@ Gem::Specification.new do |gem|
|
|
14
14
|
|
15
15
|
gem.rubyforge_project = "active_tools"
|
16
16
|
|
17
|
-
gem.
|
18
|
-
gem.add_development_dependency "rspec"
|
17
|
+
gem.add_runtime_dependency "rails", "~> 4"
|
18
|
+
gem.add_development_dependency "rspec", "~> 0"
|
19
19
|
|
20
20
|
gem.files = `git ls-files`.split($/)
|
21
21
|
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
@@ -1,3 +1,6 @@
|
|
1
|
+
require 'active_tools/action_pack/action_controller/path_helper/complex_helpers'
|
2
|
+
require 'active_tools/action_pack/action_controller/path_helper/http_referer'
|
3
|
+
|
1
4
|
module ActiveTools
|
2
5
|
module ActionPack
|
3
6
|
module ActionController
|
@@ -5,31 +8,10 @@ module ActiveTools
|
|
5
8
|
extend ::ActiveSupport::Concern
|
6
9
|
|
7
10
|
included do
|
8
|
-
|
11
|
+
include ComplexHelpers
|
12
|
+
helper_method :path?, :action?, :controller?, :current_action, :current_controller, :http_referer
|
9
13
|
end
|
10
14
|
|
11
|
-
def path?(controller, action = nil)
|
12
|
-
controller?(controller) && action?(action)
|
13
|
-
end
|
14
|
-
|
15
|
-
def action?(action)
|
16
|
-
actions = case action
|
17
|
-
when Array then action.collect {|c| c.to_s}
|
18
|
-
when String, Symbol then Array.wrap(action.to_s)
|
19
|
-
else nil
|
20
|
-
end
|
21
|
-
actions.blank? ? true : current_action.in?(actions)
|
22
|
-
end
|
23
|
-
|
24
|
-
def controller?(controller)
|
25
|
-
controllers = case controller
|
26
|
-
when Array then controller.collect {|c| c.to_s}
|
27
|
-
when String, Symbol then Array.wrap(controller.to_s)
|
28
|
-
else nil
|
29
|
-
end
|
30
|
-
controllers.blank? ? true : current_controller.in?(controllers)
|
31
|
-
end
|
32
|
-
|
33
15
|
def current_action
|
34
16
|
request.path_parameters[:action]
|
35
17
|
end
|
@@ -37,6 +19,11 @@ module ActiveTools
|
|
37
19
|
def current_controller
|
38
20
|
request.path_parameters[:controller]
|
39
21
|
end
|
22
|
+
|
23
|
+
def http_referer(environment = {})
|
24
|
+
@http_referer ||= HttpReferer.new(request, environment)
|
25
|
+
end
|
26
|
+
|
40
27
|
end
|
41
28
|
end
|
42
29
|
end
|
@@ -3,7 +3,6 @@ module ActiveTools
|
|
3
3
|
module ActionView
|
4
4
|
module TagAttributes
|
5
5
|
class Collect
|
6
|
-
attr_reader :hash
|
7
6
|
def initialize(hash = nil)
|
8
7
|
@hash = HashWithIndifferentAccess.new {|h,k| h[k] = Array.new}
|
9
8
|
merge(hash) if hash
|
@@ -30,6 +29,10 @@ module ActiveTools
|
|
30
29
|
Hash[@hash.map {|k,v| [k, v.join(" ")]}]
|
31
30
|
end
|
32
31
|
|
32
|
+
def hash
|
33
|
+
@hash.symbolize_keys
|
34
|
+
end
|
35
|
+
|
33
36
|
private
|
34
37
|
|
35
38
|
def type_valid(object = nil)
|
@@ -12,6 +12,7 @@ module ActiveTools
|
|
12
12
|
@foreign_key = reflection.foreign_key
|
13
13
|
@remote_attributes = @options[:remote_attributes]
|
14
14
|
@init_proc = @options[:init_proc]
|
15
|
+
@nullify_if = @options[:nullify_if]
|
15
16
|
@update_if = @options[:update_if]
|
16
17
|
@destroy_if = @options[:destroy_if]
|
17
18
|
@uniq_by = Array(@options[:uniq_by]).map(&:to_s)
|
@@ -34,12 +35,19 @@ module ActiveTools
|
|
34
35
|
end
|
35
36
|
end
|
36
37
|
end
|
38
|
+
|
39
|
+
def try_nullify
|
40
|
+
if nullify?
|
41
|
+
store_backup!
|
42
|
+
self.target = nil
|
43
|
+
end
|
44
|
+
end
|
37
45
|
|
38
|
-
def try_commit
|
46
|
+
def try_commit
|
39
47
|
try_commit_existed || try_update
|
40
48
|
end
|
41
49
|
|
42
|
-
def try_destroy
|
50
|
+
def try_destroy
|
43
51
|
try_destroy_backup
|
44
52
|
try_destroy_target
|
45
53
|
end
|
@@ -47,7 +55,7 @@ module ActiveTools
|
|
47
55
|
def try_update
|
48
56
|
if updateable_backup?
|
49
57
|
begin
|
50
|
-
@backup.update(attributes(@template, *@remote_attributes))
|
58
|
+
@backup.update(attributes(@template, *@remote_attributes))
|
51
59
|
rescue ::ActiveRecord::StaleObjectError
|
52
60
|
@backup.reload
|
53
61
|
try_update
|
@@ -57,9 +65,11 @@ module ActiveTools
|
|
57
65
|
end
|
58
66
|
|
59
67
|
def try_commit_existed
|
60
|
-
if @template.present? && @uniq_by.any? && existed = detect_existed
|
68
|
+
if @template.present? && @uniq_by.any? && (existed = detect_existed)
|
61
69
|
self.target = existed
|
62
|
-
|
70
|
+
if updateable_backup?
|
71
|
+
@backup.mark_for_destruction
|
72
|
+
end
|
63
73
|
true
|
64
74
|
end
|
65
75
|
end
|
@@ -75,18 +85,7 @@ module ActiveTools
|
|
75
85
|
end
|
76
86
|
end
|
77
87
|
|
78
|
-
def
|
79
|
-
if updateable_backup?
|
80
|
-
begin
|
81
|
-
@backup.destroy
|
82
|
-
rescue ::ActiveRecord::StaleObjectError
|
83
|
-
@backup.reload
|
84
|
-
try_destroy_updateable_backup
|
85
|
-
end
|
86
|
-
end
|
87
|
-
end
|
88
|
-
|
89
|
-
def try_destroy_target(force = false)
|
88
|
+
def try_destroy_target
|
90
89
|
if destroyable_target?
|
91
90
|
begin
|
92
91
|
target.destroy
|
@@ -111,8 +110,8 @@ module ActiveTools
|
|
111
110
|
relation_options_call = "#{attribute}_relation_options"
|
112
111
|
if klass.respond_to?(relation_options_call)
|
113
112
|
values = @template.send(relation_options_call)
|
114
|
-
outer_values.
|
115
|
-
where_values.
|
113
|
+
outer_values.deep_merge!(values[:outer_values])
|
114
|
+
where_values.deep_merge!(values[:where_values])
|
116
115
|
else
|
117
116
|
where_values[attribute] = @template.send(attribute)
|
118
117
|
end
|
@@ -120,16 +119,20 @@ module ActiveTools
|
|
120
119
|
klass.includes(outer_values).where(where_values).limit(1).first
|
121
120
|
end
|
122
121
|
|
122
|
+
def nullify?
|
123
|
+
target.present? && @nullify_if.try(:call, (target.persisted? ? target.reload : target), owner)
|
124
|
+
end
|
125
|
+
|
123
126
|
def updateable_backup?
|
124
|
-
@backup.
|
127
|
+
@backup.try(:persisted?) && @update_if.try(:call, @backup.reload, owner)
|
125
128
|
end
|
126
129
|
|
127
130
|
def destroyable_backup?
|
128
|
-
@backup.
|
131
|
+
@backup.try(:persisted?) && (!@backup.destroyed?||@backup.marked_for_destruction?) && @destroy_if.try(:call, @backup.reload, owner)
|
129
132
|
end
|
130
133
|
|
131
134
|
def destroyable_target?
|
132
|
-
target.try(:persisted?) && !target.destroyed? && @destroy_if.try(:call, target)
|
135
|
+
target.try(:persisted?) && (!target.destroyed?||target.marked_for_destruction?) && @destroy_if.try(:call, target.reload, owner)
|
133
136
|
end
|
134
137
|
|
135
138
|
def attributes(object, *attrs)
|
@@ -144,6 +147,9 @@ module ActiveTools
|
|
144
147
|
|
145
148
|
def restore_backup!
|
146
149
|
if @backup
|
150
|
+
if @backup.marked_for_destruction?
|
151
|
+
@backup.instance_variable_set(:@marked_for_destruction, false)
|
152
|
+
end
|
147
153
|
self.target = @backup
|
148
154
|
@backup = nil
|
149
155
|
end
|
@@ -165,7 +171,15 @@ module ActiveTools
|
|
165
171
|
|
166
172
|
def target=(record)
|
167
173
|
if owner.persisted?
|
168
|
-
|
174
|
+
if Rails.version >= "4.1.0"
|
175
|
+
if record
|
176
|
+
association.send(:replace_keys, record)
|
177
|
+
else
|
178
|
+
association.send(:remove_keys)
|
179
|
+
end
|
180
|
+
else
|
181
|
+
association.send(:replace_keys, record)
|
182
|
+
end
|
169
183
|
association.set_inverse_instance(record)
|
170
184
|
association.instance_variable_set(:@updated, true) if record != @backup
|
171
185
|
association.target = record
|
@@ -184,7 +198,7 @@ module ActiveTools
|
|
184
198
|
target.dup
|
185
199
|
end
|
186
200
|
@template.tap do |t|
|
187
|
-
@init_proc.try(:call, t)
|
201
|
+
@init_proc.try(:call, t, owner)
|
188
202
|
end
|
189
203
|
end
|
190
204
|
end
|
@@ -1,3 +1,4 @@
|
|
1
|
+
require 'active_tools/core_extension/deep_merge'
|
1
2
|
require 'active_tools/active_record/adaptive_belongs_to/adapter'
|
2
3
|
module ActiveTools
|
3
4
|
module ActiveRecord
|
@@ -50,8 +51,8 @@ module ActiveTools
|
|
50
51
|
unless reflection = reflections[assoc_name]
|
51
52
|
raise(ArgumentError, ":#{assoc_name} method doesn't look like an association accessor!")
|
52
53
|
end
|
53
|
-
adapter_name = "#{assoc_name}
|
54
|
-
config_name = "#{assoc_name}
|
54
|
+
adapter_name = "#{assoc_name}_adaptive"
|
55
|
+
config_name = "#{assoc_name}_adaptive_options"
|
55
56
|
|
56
57
|
raise(TypeError, "Option :attributes must be a Hash. #{options[:attributes].class} passed!") unless options[:attributes].is_a?(Hash)
|
57
58
|
attr_map = options.delete(:attributes).with_indifferent_access
|
@@ -62,11 +63,15 @@ module ActiveTools
|
|
62
63
|
self.send("#{config_name}=", options.merge(:remote_attributes => attr_map.keys))
|
63
64
|
|
64
65
|
class_eval <<-EOV
|
65
|
-
|
66
|
+
before_validation do
|
67
|
+
#{adapter_name}.try_nullify
|
68
|
+
end
|
69
|
+
|
70
|
+
#{Rails.version >= "4.1.0" ? "after_validation" : "before_save"} do
|
66
71
|
#{adapter_name}.try_commit
|
67
72
|
end
|
68
73
|
|
69
|
-
|
74
|
+
after_save do
|
70
75
|
#{adapter_name}.try_destroy_backup
|
71
76
|
#{adapter_name}.clear!
|
72
77
|
end
|
@@ -86,28 +91,13 @@ module ActiveTools
|
|
86
91
|
define_method local_attribute do
|
87
92
|
send(adapter_name).read(remote_attribute)
|
88
93
|
end
|
89
|
-
|
94
|
+
|
90
95
|
define_method "#{local_attribute}=" do |value|
|
91
96
|
send(adapter_name).write(remote_attribute, value)
|
92
97
|
end
|
93
98
|
end
|
94
99
|
end
|
95
|
-
|
96
|
-
# attr_map.each do |remote_attribute, local_attribute|
|
97
|
-
# class_eval <<-EOV
|
98
|
-
# def #{local_attribute}_adaptive
|
99
|
-
# [{:#{assoc_name} => {}}, {:#{reflections[assoc_name].table_name} => {:#{remote_attribute} => #{local_attribute}}}]
|
100
|
-
# end
|
101
|
-
#
|
102
|
-
# def #{local_attribute}
|
103
|
-
# #{adapter_name}.read(:#{remote_attribute})
|
104
|
-
# end
|
105
|
-
#
|
106
|
-
# def #{local_attribute}=(value)
|
107
|
-
# #{adapter_name}.write(:#{remote_attribute}, value)
|
108
|
-
# end
|
109
|
-
# EOV
|
110
|
-
# end
|
100
|
+
|
111
101
|
end
|
112
102
|
end
|
113
103
|
end
|
@@ -27,7 +27,7 @@ module ActiveTools
|
|
27
27
|
foreign_key = reflection.foreign_key
|
28
28
|
if (@_after_create_custom_counter_called ||= false)
|
29
29
|
@_after_create_custom_counter_called = false
|
30
|
-
elsif send(:attribute_changed?, foreign_key) && !new_record? &&
|
30
|
+
elsif send(:attribute_changed?, foreign_key) && !new_record? && defined?(reflection.klass.to_s.camelize)
|
31
31
|
model = reflection.klass
|
32
32
|
foreign_key_was = attribute_was foreign_key
|
33
33
|
foreign_key = attribute foreign_key
|
@@ -1,55 +1,14 @@
|
|
1
|
+
require 'active_tools/misc/uniq_content'
|
2
|
+
|
1
3
|
module ActiveTools
|
2
4
|
module Misc
|
3
|
-
|
4
|
-
class Map
|
5
|
-
attr_reader :content
|
6
|
-
|
7
|
-
delegate :any?, :empty?, :to => :content
|
8
|
-
|
9
|
-
def initialize
|
10
|
-
@content = ::ActiveSupport::OrderedHash.new { |h,k| h[k] = ::ActiveSupport::SafeBuffer.new }
|
11
|
-
end
|
12
|
-
|
13
|
-
# Called by _layout_for to read stored values.
|
14
|
-
def get(key)
|
15
|
-
@content[key]
|
16
|
-
end
|
17
|
-
|
18
|
-
# Called by each renderer object to set the layout contents.
|
19
|
-
def set(key, value)
|
20
|
-
@content[key] = value
|
21
|
-
end
|
22
|
-
|
23
|
-
# Called by content_for
|
24
|
-
def append(key, value)
|
25
|
-
@content[key] << value
|
26
|
-
end
|
27
|
-
alias_method :append!, :append
|
28
|
-
|
29
|
-
def add_script(script)
|
30
|
-
set(script.hash, script)
|
31
|
-
end
|
32
|
-
|
33
|
-
def render
|
34
|
-
@content.values.join("\n").html_safe
|
35
|
-
end
|
36
|
-
|
37
|
-
end
|
38
|
-
end
|
5
|
+
DEFAULT_JS_FLOW_KEY = :_script_flow
|
39
6
|
end
|
40
7
|
|
41
8
|
module OnLoadActionController
|
42
|
-
included do
|
43
|
-
helper_method :script_flow
|
44
|
-
end
|
45
|
-
|
46
|
-
def script_flow
|
47
|
-
@script_flow ||= Misc::ScriptFlow::Map.new
|
48
|
-
end
|
49
|
-
|
50
9
|
def _render_template(options)
|
51
10
|
if lookup_context.rendered_format == :js
|
52
|
-
super +
|
11
|
+
super + uniq_content_storage.render_content(Misc::DEFAULT_JS_FLOW_KEY)
|
53
12
|
else
|
54
13
|
super
|
55
14
|
end
|
@@ -57,25 +16,33 @@ module ActiveTools
|
|
57
16
|
end
|
58
17
|
|
59
18
|
module OnLoadActionView
|
60
|
-
def script(
|
19
|
+
def script(*args, &block)
|
20
|
+
options = args.extract_options!
|
21
|
+
content = args.first
|
61
22
|
if content || block_given?
|
62
23
|
if block_given?
|
63
24
|
content = capture(&block)
|
64
25
|
end
|
65
26
|
if content
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
27
|
+
case request.format
|
28
|
+
when Mime::JS then
|
29
|
+
uniq_content_storage.append_content(content, Misc::DEFAULT_JS_FLOW_KEY)
|
30
|
+
nil
|
31
|
+
when Mime::HTML then
|
32
|
+
unless uniq_content_storage.remembered?(content, options[:volume])
|
33
|
+
flow = uniq_content_storage.remember(content, options[:volume])
|
34
|
+
options[:javascript_tag] == false ? flow : javascript_tag(flow, options)
|
35
|
+
end
|
36
|
+
end
|
73
37
|
end
|
74
38
|
end
|
75
39
|
end
|
76
40
|
|
77
|
-
def script_for(identifier,
|
78
|
-
|
41
|
+
def script_for(identifier, *args, &block)
|
42
|
+
options = args.extract_options!
|
43
|
+
content_for(identifier) do
|
44
|
+
script(args, options.merge(:volume => identifier), &block)
|
45
|
+
end
|
79
46
|
end
|
80
47
|
end
|
81
48
|
|
data/lib/active_tools/misc.rb
CHANGED
data/lib/active_tools/version.rb
CHANGED
metadata
CHANGED
@@ -1,41 +1,41 @@
|
|
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.10
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Valery Kvon
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2014-04-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '4'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '4'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rspec
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
41
|
description: Missing tools for Rails developers
|
@@ -45,9 +45,9 @@ executables: []
|
|
45
45
|
extensions: []
|
46
46
|
extra_rdoc_files: []
|
47
47
|
files:
|
48
|
-
- .gitignore
|
49
|
-
- .ruby-gemset
|
50
|
-
- .ruby-version
|
48
|
+
- ".gitignore"
|
49
|
+
- ".ruby-gemset"
|
50
|
+
- ".ruby-version"
|
51
51
|
- Gemfile
|
52
52
|
- LICENSE.txt
|
53
53
|
- README.md
|
@@ -102,17 +102,17 @@ require_paths:
|
|
102
102
|
- lib
|
103
103
|
required_ruby_version: !ruby/object:Gem::Requirement
|
104
104
|
requirements:
|
105
|
-
- -
|
105
|
+
- - ">="
|
106
106
|
- !ruby/object:Gem::Version
|
107
107
|
version: '0'
|
108
108
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
109
109
|
requirements:
|
110
|
-
- -
|
110
|
+
- - ">="
|
111
111
|
- !ruby/object:Gem::Version
|
112
112
|
version: '0'
|
113
113
|
requirements: []
|
114
114
|
rubyforge_project: active_tools
|
115
|
-
rubygems_version: 2.
|
115
|
+
rubygems_version: 2.2.2
|
116
116
|
signing_key:
|
117
117
|
specification_version: 4
|
118
118
|
summary: ActionDispatch, ActionController, ActiveModel, ActiveRecord, ActiveSupport,
|