batch_actions 0.0.1 → 0.0.2
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/Gemfile +1 -1
- data/lib/batch_actions.rb +8 -4
- data/lib/batch_actions/class_methods.rb +17 -17
- data/lib/batch_actions/version.rb +1 -1
- data/spec/batch_actions_spec.rb +71 -47
- data/spec/spec_helper.rb +2 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 55c9b7fa8c2b242f9b5390326e0452610e3077f4
|
4
|
+
data.tar.gz: a70cdebc35ad2a3d59d43681521fae8a542d7eba
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 74ae8d1efa6459edafbaedc32869e3c921d8b249184778b4cb487e8eddf433b4e50ed5556df2a14e353189998a050ce33cd89854f26571f705ec7feb8f760c4c
|
7
|
+
data.tar.gz: b7da68362cb4e6d5f6c7902693238bba991c04bdc0f3efa9e297ec4b7ff38c74d4f821ddec74e08e550822c16667ea6aef0f6815deb04334855a68267f192ad6
|
data/Gemfile
CHANGED
data/lib/batch_actions.rb
CHANGED
@@ -17,12 +17,16 @@ module BatchActions
|
|
17
17
|
allowed
|
18
18
|
end
|
19
19
|
|
20
|
-
def
|
21
|
-
|
20
|
+
def dispatch_batch
|
21
|
+
name = params[:batch_action]
|
22
22
|
|
23
|
-
|
23
|
+
allowed = batch_actions.detect { |action| action.to_s == name.to_s }
|
24
|
+
unless allowed
|
25
|
+
raise ActionController::RoutingError.new('batch action is not allowed')
|
26
|
+
end
|
24
27
|
|
25
|
-
|
28
|
+
self.status, headers, self.response_body = self.class.action(:"batch_#{name}").call(env)
|
29
|
+
self.headers.merge! headers
|
26
30
|
end
|
27
31
|
|
28
32
|
def self.included(base)
|
@@ -18,42 +18,42 @@ module BatchActions
|
|
18
18
|
if block_given?
|
19
19
|
apply = block
|
20
20
|
else
|
21
|
-
|
22
|
-
objects.each do |object|
|
23
|
-
object.send(keyword)
|
24
|
-
end
|
25
|
-
end
|
21
|
+
raise ArgumentError, "block must be specified"
|
26
22
|
end
|
27
23
|
|
28
24
|
if opts.include? :scope
|
29
25
|
scope = opts[:scope]
|
30
26
|
else
|
31
|
-
scope = ->(
|
32
|
-
model.where(:id =>
|
27
|
+
scope = ->(ids) do
|
28
|
+
model.where(:id => ids)
|
33
29
|
end
|
34
30
|
end
|
35
31
|
|
36
32
|
if opts.include? :if
|
37
33
|
condition = opts[:if]
|
38
34
|
else
|
39
|
-
condition = ->()
|
40
|
-
if self.respond_to? :can?
|
41
|
-
can? keyword, model
|
42
|
-
else
|
43
|
-
true
|
44
|
-
end
|
45
|
-
end
|
35
|
+
condition = ->() { true }
|
46
36
|
end
|
47
37
|
|
48
38
|
@batch_actions[keyword] = condition
|
49
39
|
|
40
|
+
raise ArgumentError, "unexpected number of arguments for scope" if scope.arity < 1 || scope.arity > 2
|
41
|
+
|
50
42
|
define_method(:"batch_#{keyword}") do
|
51
43
|
result = instance_exec(&condition)
|
52
44
|
|
53
|
-
raise
|
45
|
+
raise ActionController::RoutingError.new('batch action is not allowed') unless result
|
46
|
+
|
47
|
+
case scope.arity
|
48
|
+
when 1
|
49
|
+
objects = instance_exec(params[:ids], &scope)
|
50
|
+
|
51
|
+
when 2
|
52
|
+
objects = instance_exec(params[:ids], model.where(:id => params[:ids]), &scope)
|
53
|
+
|
54
|
+
end
|
54
55
|
|
55
|
-
|
56
|
-
apply.call(objects)
|
56
|
+
instance_exec(objects, &apply)
|
57
57
|
end
|
58
58
|
end
|
59
59
|
end
|
data/spec/batch_actions_spec.rb
CHANGED
@@ -6,7 +6,9 @@ describe BatchActions do
|
|
6
6
|
:ids => [ 1, 2 ]
|
7
7
|
) do
|
8
8
|
batch_model TestModel
|
9
|
-
batch_action :test1
|
9
|
+
batch_action :test1 do |list|
|
10
|
+
list.each &:test1
|
11
|
+
end
|
10
12
|
end
|
11
13
|
|
12
14
|
times = 0
|
@@ -22,6 +24,18 @@ describe BatchActions do
|
|
22
24
|
it "requires a model to be specified" do
|
23
25
|
expect do
|
24
26
|
mock_controller do
|
27
|
+
batch_action :test1 do |list|
|
28
|
+
list.each &:test1
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end.to raise_error(ArgumentError)
|
32
|
+
end
|
33
|
+
|
34
|
+
it "requires a block to be specified" do
|
35
|
+
expect do
|
36
|
+
mock_controller do
|
37
|
+
batch_model TestModel
|
38
|
+
|
25
39
|
batch_action :test1
|
26
40
|
end
|
27
41
|
end.to raise_error(ArgumentError)
|
@@ -33,8 +47,12 @@ describe BatchActions do
|
|
33
47
|
) do
|
34
48
|
batch_model TestModel
|
35
49
|
|
36
|
-
batch_action :test1
|
37
|
-
|
50
|
+
batch_action :test1 do |list|
|
51
|
+
list.each &:test1
|
52
|
+
end
|
53
|
+
batch_action :test2, :model => TestModel2 do |list|
|
54
|
+
list.each &:test2
|
55
|
+
end
|
38
56
|
end
|
39
57
|
|
40
58
|
TestModel.should_receive(:where).with({ :id => [ 1 ]}).and_call_original
|
@@ -51,42 +69,38 @@ describe BatchActions do
|
|
51
69
|
scope_called = false
|
52
70
|
|
53
71
|
instance = TestModel.new
|
54
|
-
instance.should_receive(:test1).and_return(nil)
|
72
|
+
instance.should_receive(:test1).exactly(2).times.and_return(nil)
|
55
73
|
|
56
74
|
ctrl = mock_controller(
|
57
75
|
:ids => [ 1 ]
|
58
76
|
) do
|
59
77
|
batch_model TestModel
|
60
78
|
|
61
|
-
batch_action
|
79
|
+
batch_action(:test1, :scope => ->(ids) do
|
62
80
|
scope_called = true
|
63
81
|
|
64
82
|
[ instance ]
|
83
|
+
end) do |list|
|
84
|
+
list.each &:test1
|
65
85
|
end
|
66
|
-
end
|
67
|
-
|
68
|
-
ctrl.batch_test1
|
69
|
-
|
70
|
-
scope_called.should be_true
|
71
|
-
end
|
72
86
|
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
ctrl = mock_controller(
|
77
|
-
:ids => [ 1 ]
|
78
|
-
) do
|
79
|
-
batch_model TestModel
|
87
|
+
batch_action(:test2, :scope => ->(ids, scope) do
|
88
|
+
scope_called = true
|
80
89
|
|
81
|
-
|
82
|
-
|
90
|
+
[ instance ]
|
91
|
+
end) do |list|
|
92
|
+
list.each &:test1
|
83
93
|
end
|
84
94
|
end
|
85
95
|
|
86
96
|
ctrl.batch_test1
|
97
|
+
scope_called.should be_true
|
98
|
+
|
99
|
+
scope_called = false
|
100
|
+
ctrl.batch_test2
|
101
|
+
scope_called.should be_true
|
102
|
+
|
87
103
|
|
88
|
-
block_called.should_not be_nil
|
89
|
-
block_called.length.should == 1
|
90
104
|
end
|
91
105
|
|
92
106
|
it "supports :if" do
|
@@ -95,7 +109,9 @@ describe BatchActions do
|
|
95
109
|
) do
|
96
110
|
batch_model TestModel
|
97
111
|
|
98
|
-
batch_action
|
112
|
+
batch_action(:test, :if => ->() { false }) do |list|
|
113
|
+
list.each &:test
|
114
|
+
end
|
99
115
|
end
|
100
116
|
|
101
117
|
expect { ctrl.batch_test1 }.to raise_error
|
@@ -106,9 +122,15 @@ describe BatchActions do
|
|
106
122
|
ctrl = mock_controller do
|
107
123
|
batch_model TestModel
|
108
124
|
|
109
|
-
batch_action :test1
|
110
|
-
|
111
|
-
|
125
|
+
batch_action :test1 do |list|
|
126
|
+
list.each &:test1
|
127
|
+
end
|
128
|
+
batch_action :test2 do |list|
|
129
|
+
list.each &:test2
|
130
|
+
end
|
131
|
+
batch_action(:test3, :if => ->() { false }) do |list|
|
132
|
+
list.each &:test3
|
133
|
+
end
|
112
134
|
end
|
113
135
|
|
114
136
|
ctrl.batch_actions.should == [ :test1, :test2 ]
|
@@ -117,39 +139,41 @@ describe BatchActions do
|
|
117
139
|
it "supports InheritedResources" do
|
118
140
|
expect do
|
119
141
|
mock_controller(:parent => InheritedResources::Base) do
|
120
|
-
batch_action
|
142
|
+
batch_action(:test1) do |list|
|
143
|
+
list.each &:test1
|
144
|
+
end
|
121
145
|
end
|
122
146
|
end.to_not raise_error
|
123
147
|
end
|
124
148
|
|
125
|
-
it "
|
126
|
-
ctrl = mock_controller do
|
127
|
-
batch_model TestModel
|
128
|
-
|
129
|
-
batch_action :test1
|
130
|
-
batch_action :test2
|
131
|
-
|
132
|
-
def can?(keyword, model)
|
133
|
-
keyword == :test1
|
134
|
-
end
|
135
|
-
end
|
136
|
-
|
137
|
-
ctrl.batch_actions.should == [ :test1 ]
|
138
|
-
end
|
139
|
-
|
140
|
-
it "implements batch_action" do
|
149
|
+
it "implements dispatch_batch" do
|
141
150
|
[ "test1", "test2" ].each do |action|
|
142
151
|
ctrl = mock_controller(
|
143
152
|
:ids => [ 1 ],
|
144
|
-
:
|
153
|
+
:batch_action => action
|
145
154
|
) do
|
146
155
|
batch_model TestModel
|
147
|
-
batch_action :test1
|
148
|
-
|
156
|
+
batch_action :test1 do |list|
|
157
|
+
list.each &:test1
|
158
|
+
end
|
159
|
+
batch_action :test2 do |list|
|
160
|
+
list.each &:test2
|
161
|
+
end
|
149
162
|
end
|
150
163
|
|
151
164
|
TestModel.any_instance.should_receive(action.to_sym).and_return(nil)
|
152
|
-
ctrl.
|
165
|
+
ctrl.dispatch_batch
|
153
166
|
end
|
154
167
|
end
|
168
|
+
|
169
|
+
it "throws on disallowed action in dispatch_batch" do
|
170
|
+
ctrl = mock_controller(
|
171
|
+
:batch_action => :test
|
172
|
+
) do
|
173
|
+
end
|
174
|
+
|
175
|
+
expect do
|
176
|
+
ctrl.dispatch_batch
|
177
|
+
end.to raise_error(ActionController::RoutingError)
|
178
|
+
end
|
155
179
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -2,6 +2,7 @@ require 'simplecov'
|
|
2
2
|
SimpleCov.start
|
3
3
|
|
4
4
|
require 'batch_actions'
|
5
|
+
require 'action_controller'
|
5
6
|
|
6
7
|
class TestModel
|
7
8
|
def self.where(query)
|
@@ -24,7 +25,7 @@ module InheritedResources
|
|
24
25
|
end
|
25
26
|
|
26
27
|
def mock_controller(params = {}, &block)
|
27
|
-
parent = params.delete(:parent) ||
|
28
|
+
parent = params.delete(:parent) || ActionController::Metal
|
28
29
|
|
29
30
|
mock_class = Class.new(parent) do
|
30
31
|
include BatchActions
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: batch_actions
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sergey Gridasov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-08-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -78,7 +78,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
78
78
|
version: '0'
|
79
79
|
requirements: []
|
80
80
|
rubyforge_project:
|
81
|
-
rubygems_version: 2.0.
|
81
|
+
rubygems_version: 2.0.3
|
82
82
|
signing_key:
|
83
83
|
specification_version: 4
|
84
84
|
summary: Batch action support for Rails.
|