with_action 1.0.0
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/.bundle/config +2 -0
- data/Gemfile +2 -0
- data/Gemfile.lock +49 -0
- data/MIT-LICENSE +20 -0
- data/README +59 -0
- data/Rakefile +24 -0
- data/lib/with_action.rb +7 -0
- data/lib/with_action/version.rb +3 -0
- data/lib/with_action/with_action.rb +40 -0
- data/test/with_action_test.rb +77 -0
- data/with_action.gemspec +25 -0
- metadata +124 -0
data/.bundle/config
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
with_action (1.0.0)
|
5
|
+
actionpack
|
6
|
+
activesupport
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: http://rubygems.org/
|
10
|
+
specs:
|
11
|
+
abstract (1.0.0)
|
12
|
+
actionpack (3.0.1)
|
13
|
+
activemodel (= 3.0.1)
|
14
|
+
activesupport (= 3.0.1)
|
15
|
+
builder (~> 2.1.2)
|
16
|
+
erubis (~> 2.6.6)
|
17
|
+
i18n (~> 0.4.1)
|
18
|
+
rack (~> 1.2.1)
|
19
|
+
rack-mount (~> 0.6.12)
|
20
|
+
rack-test (~> 0.5.4)
|
21
|
+
tzinfo (~> 0.3.23)
|
22
|
+
activemodel (3.0.1)
|
23
|
+
activesupport (= 3.0.1)
|
24
|
+
builder (~> 2.1.2)
|
25
|
+
i18n (~> 0.4.1)
|
26
|
+
activesupport (3.0.1)
|
27
|
+
builder (2.1.2)
|
28
|
+
erubis (2.6.6)
|
29
|
+
abstract (>= 1.0.0)
|
30
|
+
i18n (0.4.1)
|
31
|
+
mocha (0.9.9)
|
32
|
+
rake
|
33
|
+
rack (1.2.1)
|
34
|
+
rack-mount (0.6.13)
|
35
|
+
rack (>= 1.0.0)
|
36
|
+
rack-test (0.5.6)
|
37
|
+
rack (>= 1.0)
|
38
|
+
rake (0.8.7)
|
39
|
+
tzinfo (0.3.23)
|
40
|
+
|
41
|
+
PLATFORMS
|
42
|
+
ruby
|
43
|
+
|
44
|
+
DEPENDENCIES
|
45
|
+
actionpack
|
46
|
+
activesupport
|
47
|
+
bundler (>= 1.0.0)
|
48
|
+
mocha
|
49
|
+
with_action!
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2007 [name of plugin creator]
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
= WithAction
|
2
|
+
|
3
|
+
A respond_to style helper for doing different actions based on which request parameters are passed. Specifically, it is helpful if you want to use multiple form buttons on a page, such as "Save", "Save and Continue Editing", and "Cancel". with_action executes different blocks based on what the presence of request parameters.
|
4
|
+
|
5
|
+
def create
|
6
|
+
with_action do |a|
|
7
|
+
a.cancel { redirect_to articles_path }
|
8
|
+
a.any do
|
9
|
+
@article = Article.new(params[:article])
|
10
|
+
if @article.save
|
11
|
+
a.save { redirect_to article_path(@article) }
|
12
|
+
a.edit { redirect_to article_path(@article) }
|
13
|
+
a.approve do
|
14
|
+
@article.approve!
|
15
|
+
redirect_to article_path(@article)
|
16
|
+
end
|
17
|
+
else
|
18
|
+
render :action => 'new'
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
A block is invoked if a parameter with the same name exists and is not blank. Here is an example of a form to submit to this action:
|
25
|
+
|
26
|
+
<%= submit_tag 'Save', :name => 'save' %>
|
27
|
+
<%= submit_tag 'Save & Continue Editing', :name => 'edit' %>
|
28
|
+
<%= submit_tag 'Save & Approve', :name => 'approve' %>
|
29
|
+
<%= submit_tag 'Cancel', :name => 'cancel' %>
|
30
|
+
|
31
|
+
If an @any@ block is present and no parameter that matches one of the other blocks, it is called by default, otherwise the first block will be called. The @any@ block is the only one that can have nesting and be called multiple times.
|
32
|
+
|
33
|
+
If a block is not passed to the action, then a method with the same name is called on the controller:
|
34
|
+
|
35
|
+
def update
|
36
|
+
with_action do |a|
|
37
|
+
a.publish
|
38
|
+
a.reject
|
39
|
+
a.any { redirect_to root_path }
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def publish
|
44
|
+
# …
|
45
|
+
end
|
46
|
+
|
47
|
+
def reject
|
48
|
+
# …
|
49
|
+
end
|
50
|
+
|
51
|
+
Which can be abbreviated as:
|
52
|
+
|
53
|
+
def update
|
54
|
+
with_action(:publish, :reject) do |a|
|
55
|
+
a.any { redirect_to root_path }
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
(c) Copyright 2007 Brandon Keepers (brandon@opensoul.org)
|
data/Rakefile
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'bundler'
|
2
|
+
Bundler::GemHelper.install_tasks
|
3
|
+
|
4
|
+
require 'rake/testtask'
|
5
|
+
require 'rake/rdoctask'
|
6
|
+
|
7
|
+
desc 'Default: run unit tests.'
|
8
|
+
task :default => :test
|
9
|
+
|
10
|
+
desc 'Test the with_action plugin.'
|
11
|
+
Rake::TestTask.new(:test) do |t|
|
12
|
+
t.libs << 'lib'
|
13
|
+
t.pattern = 'test/**/*_test.rb'
|
14
|
+
t.verbose = true
|
15
|
+
end
|
16
|
+
|
17
|
+
desc 'Generate documentation for the with_action plugin.'
|
18
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
19
|
+
rdoc.rdoc_dir = 'rdoc'
|
20
|
+
rdoc.title = 'WithAction'
|
21
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
22
|
+
rdoc.rdoc_files.include('README')
|
23
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
24
|
+
end
|
data/lib/with_action.rb
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
module WithAction
|
2
|
+
def with_action(*actions)
|
3
|
+
responder = ActionResponder.new(self)
|
4
|
+
yield responder if block_given?
|
5
|
+
actions.each {|a| responder.send(a) }
|
6
|
+
responder.respond
|
7
|
+
end
|
8
|
+
|
9
|
+
class ActionResponder
|
10
|
+
def initialize(controller)
|
11
|
+
@controller = controller
|
12
|
+
@order, @responses = [], {}
|
13
|
+
end
|
14
|
+
|
15
|
+
def respond
|
16
|
+
action = @order.detect do |a|
|
17
|
+
!@controller.params[a].blank? || !@controller.params["#{a}.x"].blank?
|
18
|
+
end
|
19
|
+
action ||= @order.include?(:any) ? :any : @order.first
|
20
|
+
@responses[action].call if @responses[action]
|
21
|
+
end
|
22
|
+
|
23
|
+
def any(&block)
|
24
|
+
method_missing(:any) do
|
25
|
+
# reset
|
26
|
+
@order, @responses = [], {}
|
27
|
+
block.call.tap do
|
28
|
+
respond
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def method_missing(action, &block)
|
34
|
+
@order << action
|
35
|
+
block ||= lambda { @controller.send(action) }
|
36
|
+
@responses[action] = block
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'rubygems'
|
3
|
+
require 'active_support'
|
4
|
+
require 'mocha'
|
5
|
+
require File.dirname(__FILE__) + '/../lib/with_action'
|
6
|
+
|
7
|
+
class WithActionTest < Test::Unit::TestCase
|
8
|
+
include WithAction
|
9
|
+
|
10
|
+
def setup
|
11
|
+
@params = {}
|
12
|
+
@responder = WithAction::ActionResponder.new(self)
|
13
|
+
end
|
14
|
+
|
15
|
+
def params
|
16
|
+
@params
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_calls_second_with_two_responses
|
20
|
+
@params[:save] = true
|
21
|
+
with_action do |a|
|
22
|
+
a.cancel { fail('cancel should not get called') }
|
23
|
+
a.save { @executed = :save }
|
24
|
+
end
|
25
|
+
assert_equal :save, @executed
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_does_not_call_any_on_match
|
29
|
+
@params[:cancel] = true
|
30
|
+
with_action do |a|
|
31
|
+
a.cancel { @executed = :cancel }
|
32
|
+
a.any { fail('any should not get called') }
|
33
|
+
end
|
34
|
+
assert_equal :cancel, @executed
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_any
|
38
|
+
@params[:bar] = true
|
39
|
+
with_action do |a|
|
40
|
+
a.foo { raise('foo should not get called') }
|
41
|
+
a.any do
|
42
|
+
a.bar { @executed = :bar }
|
43
|
+
end
|
44
|
+
end
|
45
|
+
assert_equal :bar, @executed
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_defaults_to_any_block
|
49
|
+
with_action do |a|
|
50
|
+
a.foo { raise('foo should not get called') }
|
51
|
+
a.any { @executed = :any }
|
52
|
+
end
|
53
|
+
assert_equal :any, @executed
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_defaults_to_first_without_any_block
|
57
|
+
with_action do |a|
|
58
|
+
a.foo { @executed = :foo }
|
59
|
+
a.bar { fail('this should never get executed') }
|
60
|
+
end
|
61
|
+
assert_equal :foo, @executed
|
62
|
+
end
|
63
|
+
|
64
|
+
def test_calls_method_without_block
|
65
|
+
self.expects(:foo)
|
66
|
+
with_action do |a|
|
67
|
+
a.foo
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def test_takes_arguments_and_calls_method
|
72
|
+
self.expects(:bar)
|
73
|
+
@params[:bar] = true
|
74
|
+
with_action(:foo, :bar)
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
data/with_action.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path("../lib/with_action/version", __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = "with_action"
|
6
|
+
s.version = WithAction::VERSION
|
7
|
+
s.platform = Gem::Platform::RUBY
|
8
|
+
s.authors = ["Brandon Keepers", "Daniel Morrison"]
|
9
|
+
s.email = ['info@collectiveidea.com']
|
10
|
+
s.homepage = "http://rubygems.org/gems/with_action"
|
11
|
+
s.summary = "A respond_to style helper for Rails controllers for doing different actions based on which request parameters are passed."
|
12
|
+
s.description = 'A respond_to style helper for doing different actions based on which request parameters are passed. Specifically, it is helpful if you want to use multiple form buttons on a page, such as "Save", "Save and Continue Editing", and "Cancel". with_action executes different blocks based on what the presence of request parameters.'
|
13
|
+
|
14
|
+
s.required_rubygems_version = ">= 1.3.6"
|
15
|
+
s.rubyforge_project = "with_action"
|
16
|
+
|
17
|
+
s.add_dependency "actionpack"
|
18
|
+
|
19
|
+
s.add_development_dependency "bundler", ">= 1.0.0"
|
20
|
+
s.add_development_dependency "mocha"
|
21
|
+
|
22
|
+
s.files = `git ls-files`.split("\n")
|
23
|
+
s.executables = `git ls-files`.split("\n").map{|f| f =~ /^bin\/(.*)/ ? $1 : nil}.compact
|
24
|
+
s.require_path = 'lib'
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,124 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: with_action
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 23
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
- 0
|
10
|
+
version: 1.0.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Brandon Keepers
|
14
|
+
- Daniel Morrison
|
15
|
+
autorequire:
|
16
|
+
bindir: bin
|
17
|
+
cert_chain: []
|
18
|
+
|
19
|
+
date: 2010-10-26 00:00:00 -04:00
|
20
|
+
default_executable:
|
21
|
+
dependencies:
|
22
|
+
- !ruby/object:Gem::Dependency
|
23
|
+
name: actionpack
|
24
|
+
prerelease: false
|
25
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
26
|
+
none: false
|
27
|
+
requirements:
|
28
|
+
- - ">="
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
hash: 3
|
31
|
+
segments:
|
32
|
+
- 0
|
33
|
+
version: "0"
|
34
|
+
type: :runtime
|
35
|
+
version_requirements: *id001
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: bundler
|
38
|
+
prerelease: false
|
39
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
hash: 23
|
45
|
+
segments:
|
46
|
+
- 1
|
47
|
+
- 0
|
48
|
+
- 0
|
49
|
+
version: 1.0.0
|
50
|
+
type: :development
|
51
|
+
version_requirements: *id002
|
52
|
+
- !ruby/object:Gem::Dependency
|
53
|
+
name: mocha
|
54
|
+
prerelease: false
|
55
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
56
|
+
none: false
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
hash: 3
|
61
|
+
segments:
|
62
|
+
- 0
|
63
|
+
version: "0"
|
64
|
+
type: :development
|
65
|
+
version_requirements: *id003
|
66
|
+
description: A respond_to style helper for doing different actions based on which request parameters are passed. Specifically, it is helpful if you want to use multiple form buttons on a page, such as "Save", "Save and Continue Editing", and "Cancel". with_action executes different blocks based on what the presence of request parameters.
|
67
|
+
email:
|
68
|
+
- info@collectiveidea.com
|
69
|
+
executables: []
|
70
|
+
|
71
|
+
extensions: []
|
72
|
+
|
73
|
+
extra_rdoc_files: []
|
74
|
+
|
75
|
+
files:
|
76
|
+
- .bundle/config
|
77
|
+
- Gemfile
|
78
|
+
- Gemfile.lock
|
79
|
+
- MIT-LICENSE
|
80
|
+
- README
|
81
|
+
- Rakefile
|
82
|
+
- lib/with_action.rb
|
83
|
+
- lib/with_action/version.rb
|
84
|
+
- lib/with_action/with_action.rb
|
85
|
+
- test/with_action_test.rb
|
86
|
+
- with_action.gemspec
|
87
|
+
has_rdoc: true
|
88
|
+
homepage: http://rubygems.org/gems/with_action
|
89
|
+
licenses: []
|
90
|
+
|
91
|
+
post_install_message:
|
92
|
+
rdoc_options: []
|
93
|
+
|
94
|
+
require_paths:
|
95
|
+
- lib
|
96
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ">="
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
hash: 3
|
102
|
+
segments:
|
103
|
+
- 0
|
104
|
+
version: "0"
|
105
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
106
|
+
none: false
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
hash: 23
|
111
|
+
segments:
|
112
|
+
- 1
|
113
|
+
- 3
|
114
|
+
- 6
|
115
|
+
version: 1.3.6
|
116
|
+
requirements: []
|
117
|
+
|
118
|
+
rubyforge_project: with_action
|
119
|
+
rubygems_version: 1.3.7
|
120
|
+
signing_key:
|
121
|
+
specification_version: 3
|
122
|
+
summary: A respond_to style helper for Rails controllers for doing different actions based on which request parameters are passed.
|
123
|
+
test_files: []
|
124
|
+
|