deferrable_actions 0.0.1
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.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +102 -0
- data/Rakefile +2 -0
- data/deferrable_actions.gemspec +19 -0
- data/lib/deferrable_actions/version.rb +3 -0
- data/lib/deferrable_actions.rb +47 -0
- data/spec/deferrable_actions_spec.rb +93 -0
- metadata +74 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
Copyright (c) 2012 bhserna
|
|
2
|
+
|
|
3
|
+
MIT License
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
6
|
+
a copy of this software and associated documentation files (the
|
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
11
|
+
the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be
|
|
14
|
+
included in all copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
# DeferrableActions
|
|
2
|
+
|
|
3
|
+
A very simple way to defer a controller action, and execute it in
|
|
4
|
+
another request.
|
|
5
|
+
|
|
6
|
+
## Installation
|
|
7
|
+
|
|
8
|
+
Add this line to your application's Gemfile:
|
|
9
|
+
|
|
10
|
+
gem 'deferrable_actions'
|
|
11
|
+
|
|
12
|
+
And then execute:
|
|
13
|
+
|
|
14
|
+
$ bundle
|
|
15
|
+
|
|
16
|
+
Or install it yourself as:
|
|
17
|
+
|
|
18
|
+
$ gem install deferrable_actions
|
|
19
|
+
|
|
20
|
+
## Usage
|
|
21
|
+
|
|
22
|
+
Include the module 'DefferrableActions' in the controller that you want
|
|
23
|
+
to defer actions, if you want, you can include the module in the
|
|
24
|
+
ApplicationController and all your controllers are going to have the
|
|
25
|
+
ability to defer actions.
|
|
26
|
+
|
|
27
|
+
```ruby
|
|
28
|
+
class ApplicationController < ActionController::Base
|
|
29
|
+
include DeferrableActions
|
|
30
|
+
|
|
31
|
+
# other stuff ...
|
|
32
|
+
end
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Example
|
|
36
|
+
|
|
37
|
+
The principal way that we use deferrable actions, is to defer actions
|
|
38
|
+
that need to authenticate the user in the middle of a POST request,
|
|
39
|
+
because we can't can redirect to a POST.
|
|
40
|
+
|
|
41
|
+
We use it like this:
|
|
42
|
+
|
|
43
|
+
```ruby
|
|
44
|
+
class TripsController < ApplicationController
|
|
45
|
+
def create
|
|
46
|
+
@trip = Trip.new(params[:trip])
|
|
47
|
+
|
|
48
|
+
if @trip.save
|
|
49
|
+
defer! :publish_trip!, trip_id: @trip.id
|
|
50
|
+
redirect_to auth_path
|
|
51
|
+
else
|
|
52
|
+
render :new
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
class SessionsController < ApplicationController
|
|
58
|
+
def create
|
|
59
|
+
# something ....
|
|
60
|
+
|
|
61
|
+
if defered_action?
|
|
62
|
+
execute_defered_action!
|
|
63
|
+
else
|
|
64
|
+
redirect_to root_url
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def failure
|
|
69
|
+
if defered_action?
|
|
70
|
+
on_failure_execute_defered_action!
|
|
71
|
+
else
|
|
72
|
+
redirect_to root_url
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
private
|
|
77
|
+
|
|
78
|
+
def publish_trip!(args)
|
|
79
|
+
trip = Trip.find(args[:trip_id])
|
|
80
|
+
trip.publish_by! current_user
|
|
81
|
+
|
|
82
|
+
redirect_to trip
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def on_failure_publish_trip!(args)
|
|
86
|
+
redirect_to trip_failed_path(args[:trip_id])
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## People
|
|
92
|
+
|
|
93
|
+
* Benito Serna @bhserna
|
|
94
|
+
* Jorge Gajón @gajon
|
|
95
|
+
|
|
96
|
+
## Contributing
|
|
97
|
+
|
|
98
|
+
1. Fork it
|
|
99
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
|
100
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
|
101
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
|
102
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
|
2
|
+
require File.expand_path('../lib/deferrable_actions/version', __FILE__)
|
|
3
|
+
|
|
4
|
+
Gem::Specification.new do |gem|
|
|
5
|
+
gem.authors = ["Benito Serna", "Jorge Gajon"]
|
|
6
|
+
gem.email = ["bhserna@gmail.com"]
|
|
7
|
+
gem.description = %q{A very simple way to defer a controller action, and execute it in another request.}
|
|
8
|
+
gem.summary = %q{Defer a controller action, and execute it later}
|
|
9
|
+
gem.homepage = "http://github.com/bhserna/deferrable_actions"
|
|
10
|
+
|
|
11
|
+
gem.files = `git ls-files`.split($\)
|
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
|
14
|
+
gem.name = "deferrable_actions"
|
|
15
|
+
gem.require_paths = ["lib"]
|
|
16
|
+
gem.version = DeferrableActions::VERSION
|
|
17
|
+
|
|
18
|
+
gem.add_development_dependency "rspec"
|
|
19
|
+
end
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
require "deferrable_actions/version"
|
|
2
|
+
|
|
3
|
+
module DeferrableActions
|
|
4
|
+
def defer!(method, args)
|
|
5
|
+
Action.store(session, method, args)
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
def defered_action?
|
|
9
|
+
Action.exists?(session)
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def execute_defered_action!
|
|
13
|
+
Action.new(self).execute! if defered_action?
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def on_failure_execute_defered_action!
|
|
17
|
+
Action.new(self).on_failure_execute! if defered_action?
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
class Action
|
|
21
|
+
attr_reader :controller, :action
|
|
22
|
+
|
|
23
|
+
def self.store(session, method, args)
|
|
24
|
+
session[:defered_action] = {
|
|
25
|
+
method: method,
|
|
26
|
+
args: args
|
|
27
|
+
}
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def self.exists?(session)
|
|
31
|
+
!!session[:defered_action]
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def initialize(controller)
|
|
35
|
+
@controller = controller
|
|
36
|
+
@action = controller.session.delete :defered_action
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def execute!
|
|
40
|
+
controller.send(action[:method], action[:args])
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def on_failure_execute!
|
|
44
|
+
controller.send("on_failure_#{action[:method]}", action[:args])
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
require_relative '../lib/deferrable_actions'
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
module DeferrableActions
|
|
5
|
+
class Controller
|
|
6
|
+
include DeferrableActions
|
|
7
|
+
|
|
8
|
+
def session
|
|
9
|
+
@session ||= {}
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
describe DeferrableActions do
|
|
15
|
+
let(:controller) { DeferrableActions::Controller.new }
|
|
16
|
+
|
|
17
|
+
describe '#defer!' do
|
|
18
|
+
it 'stores the action' do
|
|
19
|
+
DeferrableActions::Action.should_receive(:store).
|
|
20
|
+
with(controller.session, :my_action, arg_one: 'arg_one')
|
|
21
|
+
controller.defer!(:my_action, arg_one: 'arg_one')
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
describe '#defered_action?' do
|
|
26
|
+
it 'returns true if a defered action exists' do
|
|
27
|
+
controller.defer!(:my_action, arg_one: 'arg_one')
|
|
28
|
+
|
|
29
|
+
DeferrableActions::Action.should_receive(:exists?).with(controller.session)
|
|
30
|
+
controller.defered_action?
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
describe '#execute_defered_action!' do
|
|
35
|
+
it 'execute a defered action if the action exists' do
|
|
36
|
+
controller.defer!(:my_action, arg_one: 'arg_one')
|
|
37
|
+
|
|
38
|
+
controller.should_receive(:my_action).with arg_one: 'arg_one'
|
|
39
|
+
controller.execute_defered_action!
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
describe '#on_failure_execute_defered_action!' do
|
|
44
|
+
it 'execute a defered action if the action exists' do
|
|
45
|
+
controller.defer!(:my_action, arg_one: 'arg_one')
|
|
46
|
+
|
|
47
|
+
controller.should_receive(:on_failure_my_action).with arg_one: 'arg_one'
|
|
48
|
+
controller.on_failure_execute_defered_action!
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
module DeferrableActions
|
|
54
|
+
describe Action do
|
|
55
|
+
let(:controller) { Controller.new }
|
|
56
|
+
|
|
57
|
+
describe '.store' do
|
|
58
|
+
it 'stores a controller action, with its arguments' do
|
|
59
|
+
Action.store(controller.session, :my_action, arg_one: 'arg_one')
|
|
60
|
+
|
|
61
|
+
controller.session[:defered_action].should be_true
|
|
62
|
+
controller.session[:defered_action][:method].should eq :my_action
|
|
63
|
+
controller.session[:defered_action][:args][:arg_one].should eq 'arg_one'
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
describe '.exists?' do
|
|
68
|
+
it 'returns true if a defered action exists' do
|
|
69
|
+
Action.store(controller.session, :my_action, arg_one: 'arg_one')
|
|
70
|
+
|
|
71
|
+
Action.exists?(controller.session).should be_true
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
describe '#execute' do
|
|
76
|
+
it 'executes the deferred action' do
|
|
77
|
+
Action.store(controller.session, :my_action, arg_one: 'arg_one')
|
|
78
|
+
|
|
79
|
+
controller.should_receive(:my_action).with arg_one: 'arg_one'
|
|
80
|
+
Action.new(controller).execute!
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
describe '#on_failure_execute' do
|
|
85
|
+
it 'executes an action that has an "on_failure_" prefix to the deferred action' do
|
|
86
|
+
Action.store(controller.session, :my_action, arg_one: 'arg_one')
|
|
87
|
+
|
|
88
|
+
controller.should_receive(:on_failure_my_action).with arg_one: 'arg_one'
|
|
89
|
+
Action.new(controller).on_failure_execute!
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: deferrable_actions
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
prerelease:
|
|
6
|
+
platform: ruby
|
|
7
|
+
authors:
|
|
8
|
+
- Benito Serna
|
|
9
|
+
- Jorge Gajon
|
|
10
|
+
autorequire:
|
|
11
|
+
bindir: bin
|
|
12
|
+
cert_chain: []
|
|
13
|
+
date: 2012-12-13 00:00:00.000000000 Z
|
|
14
|
+
dependencies:
|
|
15
|
+
- !ruby/object:Gem::Dependency
|
|
16
|
+
name: rspec
|
|
17
|
+
requirement: !ruby/object:Gem::Requirement
|
|
18
|
+
none: false
|
|
19
|
+
requirements:
|
|
20
|
+
- - ! '>='
|
|
21
|
+
- !ruby/object:Gem::Version
|
|
22
|
+
version: '0'
|
|
23
|
+
type: :development
|
|
24
|
+
prerelease: false
|
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
26
|
+
none: false
|
|
27
|
+
requirements:
|
|
28
|
+
- - ! '>='
|
|
29
|
+
- !ruby/object:Gem::Version
|
|
30
|
+
version: '0'
|
|
31
|
+
description: A very simple way to defer a controller action, and execute it in another
|
|
32
|
+
request.
|
|
33
|
+
email:
|
|
34
|
+
- bhserna@gmail.com
|
|
35
|
+
executables: []
|
|
36
|
+
extensions: []
|
|
37
|
+
extra_rdoc_files: []
|
|
38
|
+
files:
|
|
39
|
+
- .gitignore
|
|
40
|
+
- Gemfile
|
|
41
|
+
- LICENSE
|
|
42
|
+
- README.md
|
|
43
|
+
- Rakefile
|
|
44
|
+
- deferrable_actions.gemspec
|
|
45
|
+
- lib/deferrable_actions.rb
|
|
46
|
+
- lib/deferrable_actions/version.rb
|
|
47
|
+
- spec/deferrable_actions_spec.rb
|
|
48
|
+
homepage: http://github.com/bhserna/deferrable_actions
|
|
49
|
+
licenses: []
|
|
50
|
+
post_install_message:
|
|
51
|
+
rdoc_options: []
|
|
52
|
+
require_paths:
|
|
53
|
+
- lib
|
|
54
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
55
|
+
none: false
|
|
56
|
+
requirements:
|
|
57
|
+
- - ! '>='
|
|
58
|
+
- !ruby/object:Gem::Version
|
|
59
|
+
version: '0'
|
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
61
|
+
none: false
|
|
62
|
+
requirements:
|
|
63
|
+
- - ! '>='
|
|
64
|
+
- !ruby/object:Gem::Version
|
|
65
|
+
version: '0'
|
|
66
|
+
requirements: []
|
|
67
|
+
rubyforge_project:
|
|
68
|
+
rubygems_version: 1.8.24
|
|
69
|
+
signing_key:
|
|
70
|
+
specification_version: 3
|
|
71
|
+
summary: Defer a controller action, and execute it later
|
|
72
|
+
test_files:
|
|
73
|
+
- spec/deferrable_actions_spec.rb
|
|
74
|
+
has_rdoc:
|