stepper 0.0.4 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +2 -2
- data/VERSION +1 -1
- data/lib/stepper/controllers/controller_additions.rb +62 -11
- data/lib/stepper/controllers/controller_resource.rb +8 -5
- data/lib/stepper/helper/action_view_additions.rb +1 -1
- data/lib/stepper/models/active_record_additions.rb +12 -25
- data/stepper.gemspec +8 -3
- data/test/controllers/controller_additions_test.rb +1 -1
- data/test/controllers/controller_create_test.rb +29 -0
- data/test/controllers/controller_invalid_params_test.rb +31 -0
- data/test/controllers/controller_resource_test.rb +2 -2
- data/test/controllers/controller_test.rb +7 -109
- data/test/controllers/controller_update_test.rb +45 -0
- data/test/controllers/redirect_test.rb +33 -0
- data/test/helpers/helper_test.rb +2 -2
- data/test/models/assigns_test.rb +4 -3
- data/test/rails_app/app/controllers/orders_controller.rb +14 -0
- data/test/rails_app/app/models/big_company.rb +1 -1
- data/test/rails_app/app/models/order.rb +3 -0
- data/test/rails_app/config/routes.rb +7 -1
- metadata +29 -24
- data/test/models/sti_test.rb +0 -11
data/README.rdoc
CHANGED
@@ -39,14 +39,14 @@ Setup validation for each step if necessary, method should have name like <tt>va
|
|
39
39
|
Now Your model ready for multistep form!
|
40
40
|
=== Configuring controller
|
41
41
|
|
42
|
-
Stepper use +update+, +create+ and +
|
42
|
+
Stepper use +update+, +create+, +new+ and +next_step+ actions.
|
43
43
|
|
44
44
|
For controller you need just add +has_steps+ method:
|
45
45
|
class CompaniesController < ApplicationController
|
46
46
|
has_steps
|
47
47
|
end
|
48
48
|
|
49
|
-
And you should have +show+ action because stepper redirects to it when last step finished.
|
49
|
+
And you should have +show+ action because stepper redirects to it when last step finished by default. For more options see method documentation.
|
50
50
|
=== Configuring view
|
51
51
|
|
52
52
|
Add stepper helper method into the form in view that rendered by new action:
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0
|
1
|
+
0.1.0
|
@@ -21,6 +21,20 @@ module Stepper
|
|
21
21
|
# has_steps
|
22
22
|
# end
|
23
23
|
# In this case resource will be loaded or built into +@company+ variable
|
24
|
+
#
|
25
|
+
# You can setup redirection for each +save+, +previous_step+, +next_step+ and +finish+ step to other action than default,
|
26
|
+
# options should have +after+ prefix:
|
27
|
+
#
|
28
|
+
# class CompaniesController < ApplicationController
|
29
|
+
# has_steps :redirect_to => { :after_save => {:action => :new} }
|
30
|
+
# end
|
31
|
+
#
|
32
|
+
# You can set proc that will be executed for current controller:
|
33
|
+
#
|
34
|
+
# class CompaniesController < ApplicationController
|
35
|
+
# has_steps :redirect_to => { :after_finish => proc{|controller, resource| controller.show_companies_url(resource)} }
|
36
|
+
# end
|
37
|
+
|
24
38
|
def has_steps(*args)
|
25
39
|
include InstanceMethods
|
26
40
|
stepper_resource_class.add_before_filter(self, *args)
|
@@ -67,29 +81,66 @@ module Stepper
|
|
67
81
|
# controller +new+ action
|
68
82
|
# it supports only html responce format for now
|
69
83
|
def new
|
70
|
-
|
84
|
+
|
85
|
+
end
|
86
|
+
|
87
|
+
# controller +new+ action
|
88
|
+
# it supports only html responce format for now
|
89
|
+
def next_step
|
90
|
+
if @_stepper_resource_instance.last_step?
|
91
|
+
redirect_to :action => :show, :id => @_stepper_resource_instance.id
|
92
|
+
else
|
93
|
+
render :action => :new
|
94
|
+
end
|
71
95
|
end
|
72
96
|
|
73
97
|
protected
|
98
|
+
|
99
|
+
# default redirection actions
|
100
|
+
def default_redirection
|
101
|
+
{
|
102
|
+
:next_step => {:action => "next_step", :id => @_stepper_resource_instance.id},
|
103
|
+
:previous_step => {:action => "next_step"},
|
104
|
+
:save => {:action => "index"},
|
105
|
+
:finish => {:action => "show", :id => @_stepper_resource_instance.id}
|
106
|
+
}
|
107
|
+
end
|
108
|
+
|
74
109
|
# redirects to controller actions depends of commit value
|
75
110
|
# save -> index
|
76
111
|
# previous_step -> new
|
77
112
|
# next_step -> new
|
78
113
|
# finish -> show
|
114
|
+
|
79
115
|
def redirect_steps
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
116
|
+
options, response_status = redirect_steps_options
|
117
|
+
redirect_to options, response_status
|
118
|
+
end
|
119
|
+
|
120
|
+
def redirect_steps_options
|
121
|
+
case params[:commit]
|
122
|
+
when t('stepper.save')
|
123
|
+
[ extract_redirect_params(:save), {}]
|
124
|
+
when t('stepper.previous_step')
|
125
|
+
[ extract_redirect_params(:previous_step), {}]
|
126
|
+
when t('stepper.next_step')
|
127
|
+
[ extract_redirect_params(:next_step), {}]
|
128
|
+
when t('stepper.finish')
|
129
|
+
[ extract_redirect_params(:finish), {}]
|
130
|
+
else
|
131
|
+
raise Stepper::StepperException.new("Unknown commit: #{params[:commit]}")
|
90
132
|
end
|
91
133
|
end
|
92
134
|
|
135
|
+
def extract_redirect_params(option)
|
136
|
+
redirection = @_stepper_redirect_to["after_#{option.to_s}".to_sym]
|
137
|
+
if redirection.is_a?(Proc)
|
138
|
+
redirection.call(self, @_stepper_resource_instance)
|
139
|
+
else
|
140
|
+
redirection
|
141
|
+
end || default_redirection[option]
|
142
|
+
end
|
143
|
+
|
93
144
|
# removes from params resource name, commit and id
|
94
145
|
def sanitized_params
|
95
146
|
params.except(@_stepper_name, :commit, :id)
|
@@ -1,6 +1,6 @@
|
|
1
1
|
module Stepper
|
2
2
|
class ControllerResource
|
3
|
-
# Sets up before filter in +controller_class+ for +create+, +update+ and +
|
3
|
+
# Sets up before filter in +controller_class+ for +create+, +update+, +new+ and +next_step+ actions.
|
4
4
|
# First argument can be name of resource.
|
5
5
|
# For example we have +CompaniesController+ and want to load or build resource to +@my_company+ variable:
|
6
6
|
# add_before_filter CompanyController, :my_company
|
@@ -10,8 +10,10 @@ module Stepper
|
|
10
10
|
# In this case resource will be loaded or built into +@company+ variable
|
11
11
|
#
|
12
12
|
def self.add_before_filter(controller_class, *args)
|
13
|
-
resource_name = args.first
|
14
|
-
|
13
|
+
resource_name = args.first if args.first.is_a?(Symbol) or args.first.is_a?(String)
|
14
|
+
options = args.extract_options!
|
15
|
+
controller_class.send(:before_filter, :only => [:create, :update, :new, :next_step]) do |controller|
|
16
|
+
controller.instance_variable_set :@_stepper_redirect_to, options[:redirect_to] || {}
|
15
17
|
controller_resource = controller.class.stepper_resource_class.new(controller, resource_name)
|
16
18
|
controller.instance_variable_set :@_stepper_resource_instance, controller_resource.load_resource
|
17
19
|
controller.instance_variable_set :@_stepper_name, controller_resource.name
|
@@ -26,10 +28,12 @@ module Stepper
|
|
26
28
|
|
27
29
|
def load_resource
|
28
30
|
self.resource_instance ||= load_resource_instance
|
31
|
+
self.resource_instance.attributes = @params[name] unless @params[name].blank?
|
32
|
+
self.resource_instance
|
29
33
|
end
|
30
34
|
|
31
35
|
def load_resource_instance
|
32
|
-
if @params[:action]
|
36
|
+
if ['create', 'new'].include? @params[:action]
|
33
37
|
resource = resource_class.new(@params[name] || {})
|
34
38
|
else
|
35
39
|
resource = unless @params[:id].blank?
|
@@ -37,7 +41,6 @@ module Stepper
|
|
37
41
|
else
|
38
42
|
resource_class.new
|
39
43
|
end
|
40
|
-
resource.attributes = @params[name] unless @params[name].blank?
|
41
44
|
end
|
42
45
|
resource
|
43
46
|
end
|
@@ -20,7 +20,7 @@ module Stepper
|
|
20
20
|
# If you want to have other partial for buttons than add partial to: +app/views/stepper/_fields.html.erb+
|
21
21
|
def stepper(form)
|
22
22
|
resource = self.instance_variable_get :@_stepper_resource_instance
|
23
|
-
current_step_column = resource.
|
23
|
+
current_step_column = resource.stepper_current_step_column
|
24
24
|
self.render(:partial => "stepper/fields",
|
25
25
|
:locals => { :f => form,
|
26
26
|
:resource => resource,
|
@@ -33,7 +33,7 @@ module Stepper
|
|
33
33
|
#check options
|
34
34
|
raise Stepper::StepperException.new("Options for has_steps must be in a hash.") unless options.is_a? Hash
|
35
35
|
options.each do |key, value|
|
36
|
-
unless [:current_step_column, :steps
|
36
|
+
unless [:current_step_column, :steps].include? key
|
37
37
|
raise Stepper::StepperException.new("Unknown option for has_steps: #{key.inspect} => #{value.inspect}.")
|
38
38
|
end
|
39
39
|
end
|
@@ -41,14 +41,11 @@ module Stepper
|
|
41
41
|
raise Stepper::StepperException.new(":steps condition can't be blank") if options[:steps].blank?
|
42
42
|
|
43
43
|
#set current step column
|
44
|
-
class_attribute :
|
45
|
-
self.
|
44
|
+
class_attribute :stepper_current_step_column, :instance_writer => false
|
45
|
+
self.stepper_current_step_column = options[:current_step_column] || :current_step
|
46
46
|
|
47
|
-
class_attribute :
|
48
|
-
|
49
|
-
self._stepper_steps = []
|
50
|
-
self._stepper_steps = self.try(:superclass).try(:_stepper_steps) if options[:inherit]
|
51
|
-
self._stepper_steps += options[:steps]
|
47
|
+
class_attribute :stepper_options, :instance_writer => false
|
48
|
+
self.stepper_options = options
|
52
49
|
|
53
50
|
self.validate :current_step_validation
|
54
51
|
|
@@ -57,18 +54,15 @@ module Stepper
|
|
57
54
|
end
|
58
55
|
|
59
56
|
module InstanceMethods
|
60
|
-
|
61
|
-
# returns name of current step column
|
62
|
-
def stepper_current_step_column
|
63
|
-
self.class._stepper_current_step_column
|
64
|
-
end
|
65
|
-
|
66
|
-
# returns defined steps array
|
67
57
|
def stepper_steps
|
68
|
-
self.
|
58
|
+
self.stepper_options[:steps]
|
69
59
|
end
|
70
60
|
|
71
|
-
|
61
|
+
unless self.respond_to? :steps
|
62
|
+
define_method :steps do
|
63
|
+
self.stepper_steps
|
64
|
+
end
|
65
|
+
end
|
72
66
|
|
73
67
|
# returns index of current step in steps array
|
74
68
|
def stepper_current_step_index
|
@@ -80,26 +74,19 @@ module Stepper
|
|
80
74
|
self.send(self.stepper_current_step_column)
|
81
75
|
end
|
82
76
|
|
83
|
-
alias_method :current_step, :stepper_current_step unless self.respond_to? :current_step
|
84
|
-
|
85
77
|
# sets up name of current step
|
86
|
-
# TODO: reject names that doesn't exists in steps array
|
87
78
|
def stepper_current_step=(step)
|
88
|
-
self.send("#{self.stepper_current_step_column}=", step)
|
79
|
+
self.send("#{self.stepper_current_step_column.to_s}=", step)
|
89
80
|
end
|
90
81
|
|
91
|
-
alias_method :current_step=, :stepper_current_step= unless self.respond_to? :current_step=
|
92
|
-
|
93
82
|
# Use to check current step or given step is last step
|
94
83
|
# last_step?("address")
|
95
|
-
# TODO: reject names that doesn't exists in steps array
|
96
84
|
def last_step?(step = stepper_current_step)
|
97
85
|
step == self.stepper_steps.last
|
98
86
|
end
|
99
87
|
|
100
88
|
# Use to check current step or given step is first step
|
101
89
|
# first_step?("address")
|
102
|
-
# TODO: reject names that doesn't exists in steps array
|
103
90
|
def first_step?(step = stepper_current_step)
|
104
91
|
(step == stepper_steps.first) or stepper_current_step.blank? && step.blank?
|
105
92
|
end
|
data/stepper.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{stepper}
|
8
|
-
s.version = "0.0
|
8
|
+
s.version = "0.1.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = [%q{Anton Versal}]
|
12
|
-
s.date = %q{2011-10-
|
12
|
+
s.date = %q{2011-10-08}
|
13
13
|
s.description = %q{Stepper is multistep form (wizard) solution for Rails 3. Stepper allows you to split up your large form into series of pages that users can navigate through to complete the form and save it state.}
|
14
14
|
s.email = %q{ant.ver@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -36,24 +36,29 @@ Gem::Specification.new do |s|
|
|
36
36
|
"lib/stepper/railtie.rb",
|
37
37
|
"stepper.gemspec",
|
38
38
|
"test/controllers/controller_additions_test.rb",
|
39
|
+
"test/controllers/controller_create_test.rb",
|
40
|
+
"test/controllers/controller_invalid_params_test.rb",
|
39
41
|
"test/controllers/controller_resource_test.rb",
|
40
42
|
"test/controllers/controller_test.rb",
|
43
|
+
"test/controllers/controller_update_test.rb",
|
44
|
+
"test/controllers/redirect_test.rb",
|
41
45
|
"test/helper.rb",
|
42
46
|
"test/helpers/helper_test.rb",
|
43
47
|
"test/integration/steps_test.rb",
|
44
48
|
"test/models/assigns_test.rb",
|
45
49
|
"test/models/instance_test.rb",
|
46
50
|
"test/models/models_test.rb",
|
47
|
-
"test/models/sti_test.rb",
|
48
51
|
"test/models/validation_test.rb",
|
49
52
|
"test/rails_app/Rakefile",
|
50
53
|
"test/rails_app/app/controllers/application_controller.rb",
|
51
54
|
"test/rails_app/app/controllers/companies_controller.rb",
|
55
|
+
"test/rails_app/app/controllers/orders_controller.rb",
|
52
56
|
"test/rails_app/app/helpers/application_helper.rb",
|
53
57
|
"test/rails_app/app/mailers/.gitkeep",
|
54
58
|
"test/rails_app/app/models/.gitkeep",
|
55
59
|
"test/rails_app/app/models/big_company.rb",
|
56
60
|
"test/rails_app/app/models/company.rb",
|
61
|
+
"test/rails_app/app/models/order.rb",
|
57
62
|
"test/rails_app/app/models/users.rb",
|
58
63
|
"test/rails_app/app/views/companies/_step1_step.html.erb",
|
59
64
|
"test/rails_app/app/views/companies/_step2_step.html.erb",
|
@@ -9,7 +9,7 @@ class ControllerAdditionsTest < ActiveSupport::TestCase
|
|
9
9
|
|
10
10
|
test "should has_steps setup before filter which passes call to ControllerResource" do
|
11
11
|
Stepper::ControllerResource.stubs(:new).with(@controller, nil).stubs(:load_resource)
|
12
|
-
@controller_class.stubs(:before_filter).with(:only => [:create, :update, :new])
|
12
|
+
@controller_class.stubs(:before_filter).with(:only => [:create, :update, :new, :next_step])
|
13
13
|
@controller_class.has_steps
|
14
14
|
end
|
15
15
|
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require "helper"
|
2
|
+
class CompaniesCreateControllerTest < ActionController::TestCase
|
3
|
+
tests CompaniesController
|
4
|
+
|
5
|
+
setup do
|
6
|
+
Company.expects(:new).with({'name' => 'Hina'}).returns(mock_company)
|
7
|
+
mock_company.expects(:attributes=).with({'name' => 'Hina'}).returns(true)
|
8
|
+
mock_company.expects(:save).returns(true)
|
9
|
+
mock_company.stubs(:id).returns(1)
|
10
|
+
end
|
11
|
+
|
12
|
+
test "should redirect to next step if commit 'Next step'" do
|
13
|
+
mock_company.stubs(:stepper_current_step).returns("step1")
|
14
|
+
post(:create, {:company => {:name => "Hina"}, :commit => "Next step"})
|
15
|
+
assert_response :redirect
|
16
|
+
assert_redirected_to "http://test.host/companies/1/next_step"
|
17
|
+
end
|
18
|
+
|
19
|
+
test "should redirect to index if commit 'Finish later'" do
|
20
|
+
post(:create, {:company => {:name => "Hina"}, :commit => "Finish later"})
|
21
|
+
assert_response :redirect
|
22
|
+
assert_redirected_to "http://test.host/companies"
|
23
|
+
end
|
24
|
+
|
25
|
+
protected
|
26
|
+
def mock_company(stubs={})
|
27
|
+
@mock_company ||= mock(stubs)
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require "helper"
|
2
|
+
class CompaniesInvalidParamsControllerTest < ActionController::TestCase
|
3
|
+
tests CompaniesController
|
4
|
+
|
5
|
+
setup do
|
6
|
+
@controller.expects(:render).at_least_once
|
7
|
+
end
|
8
|
+
|
9
|
+
test "should create action render to new action if object.save returns false" do
|
10
|
+
Company.expects(:new).with({'name' => 'Hina'}).returns(mock_company)
|
11
|
+
mock_company.expects(:attributes=).with({'name' => 'Hina'}).returns(true)
|
12
|
+
mock_company.expects(:save).returns(false)
|
13
|
+
mock_company.expects(:previous_step!)
|
14
|
+
post(:create, {:company => {:name => "Hina"}, :commit => "Next step"})
|
15
|
+
assert_response :success
|
16
|
+
end
|
17
|
+
|
18
|
+
test "should update action redirect to new action if object.save returns false" do
|
19
|
+
Company.expects(:find).with('1').returns(mock_company)
|
20
|
+
mock_company.expects(:attributes=).with({"name" => "Hina"}).returns(true)
|
21
|
+
mock_company.expects(:save).returns(false)
|
22
|
+
mock_company.expects(:previous_step!)
|
23
|
+
post(:update, {:company => {:name => "Hina"}, :id => 1, :commit => "Next step"})
|
24
|
+
assert_response :success
|
25
|
+
end
|
26
|
+
|
27
|
+
protected
|
28
|
+
def mock_company(stubs={})
|
29
|
+
@mock_company ||= mock(stubs)
|
30
|
+
end
|
31
|
+
end
|
@@ -10,7 +10,7 @@ class ControllerResourceTest < ActiveSupport::TestCase
|
|
10
10
|
|
11
11
|
test "should load resource into instance variable if params[:id] is specified" do
|
12
12
|
Company.stubs(:find).with(1).returns(mock_company(:id => 1))
|
13
|
-
@params.merge!(:action => "
|
13
|
+
@params.merge!(:action => "next_step", :id => mock_company.id)
|
14
14
|
resource = Stepper::ControllerResource.new(@controller)
|
15
15
|
resource.load_resource
|
16
16
|
assert_equal @controller.instance_variable_get(:@company), mock_company
|
@@ -18,7 +18,7 @@ class ControllerResourceTest < ActiveSupport::TestCase
|
|
18
18
|
|
19
19
|
test "should build resource and load into instance variable if params[:id] is not specified" do
|
20
20
|
Company.stubs(:new).returns(mock_company)
|
21
|
-
@params.merge!(:action => "
|
21
|
+
@params.merge!(:action => "some_action")
|
22
22
|
resource = Stepper::ControllerResource.new(@controller)
|
23
23
|
resource.load_resource
|
24
24
|
assert_equal @controller.instance_variable_get(:@company), mock_company
|
@@ -5,6 +5,7 @@ class CompaniesControllerTest < ActionController::TestCase
|
|
5
5
|
|
6
6
|
test "should raise error if commit is unknown" do
|
7
7
|
Company.expects(:new).with({'name' => 'Hina'}).returns(mock_company)
|
8
|
+
mock_company.expects(:attributes=).with({'name' => 'Hina'}).returns(true)
|
8
9
|
mock_company.expects(:save).returns(true)
|
9
10
|
mock_company.stubs(:id).returns(1)
|
10
11
|
assert_raise Stepper::StepperException do
|
@@ -13,24 +14,24 @@ class CompaniesControllerTest < ActionController::TestCase
|
|
13
14
|
end
|
14
15
|
|
15
16
|
test "should assign resource if params[:id] exists" do
|
16
|
-
@controller.
|
17
|
+
@controller.stubs(:render)
|
17
18
|
|
18
19
|
Company.expects(:find).with('1').returns(mock_company(:last_step? => false))
|
19
|
-
get :
|
20
|
+
get :next_step, :id => 1
|
20
21
|
assert_response :success
|
21
22
|
assert_equal assigns(:company), mock_company
|
22
23
|
end
|
23
24
|
|
24
25
|
test "should get existing assigns" do
|
25
|
-
@controller.
|
26
|
+
@controller.stubs(:render)
|
26
27
|
@controller.instance_variable_set(:@company, mock_company(:last_step? => false))
|
27
|
-
get :
|
28
|
+
get :next_step, :id => 1
|
28
29
|
assert_equal assigns(:company), mock_company
|
29
30
|
end
|
30
31
|
|
31
|
-
test "
|
32
|
+
test "next_step action should redirect to show if company on at the last step" do
|
32
33
|
@controller.instance_variable_set(:@company, mock_company(:last_step? => true, :id => "1"))
|
33
|
-
get :
|
34
|
+
get :next_step, :id => 1
|
34
35
|
assert_response :redirect
|
35
36
|
assert_redirected_to "http://test.host/companies/1"
|
36
37
|
end
|
@@ -41,106 +42,3 @@ class CompaniesControllerTest < ActionController::TestCase
|
|
41
42
|
end
|
42
43
|
end
|
43
44
|
|
44
|
-
class CompaniesCreateControllerTest < ActionController::TestCase
|
45
|
-
tests CompaniesController
|
46
|
-
|
47
|
-
setup do
|
48
|
-
Company.expects(:new).with({'name' => 'Hina'}).returns(mock_company)
|
49
|
-
mock_company.expects(:save).returns(true)
|
50
|
-
mock_company.stubs(:id).returns(1)
|
51
|
-
end
|
52
|
-
|
53
|
-
test "should redirect to next step if commit 'Next step'" do
|
54
|
-
mock_company.stubs(:stepper_current_step).returns("step1")
|
55
|
-
post(:create, {:company => {:name => "Hina"}, :commit => "Next step"})
|
56
|
-
assert_response :redirect
|
57
|
-
assert_redirected_to "http://test.host/companies/new?id=1"
|
58
|
-
assert_equal flash[:notice], "Step Step1 was successfully created."
|
59
|
-
end
|
60
|
-
|
61
|
-
test "should redirect to index if commit 'Finish later'" do
|
62
|
-
post(:create, {:company => {:name => "Hina"}, :commit => "Finish later"})
|
63
|
-
assert_response :redirect
|
64
|
-
assert_redirected_to "http://test.host/companies"
|
65
|
-
end
|
66
|
-
|
67
|
-
protected
|
68
|
-
def mock_company(stubs={})
|
69
|
-
@mock_company ||= mock(stubs)
|
70
|
-
end
|
71
|
-
end
|
72
|
-
|
73
|
-
class CompaniesUpdateControllerTest < ActionController::TestCase
|
74
|
-
tests CompaniesController
|
75
|
-
|
76
|
-
setup do
|
77
|
-
Company.expects(:find).with('1').returns(mock_company)
|
78
|
-
mock_company.expects(:attributes=).with({'code' => '23'}).returns(true)
|
79
|
-
mock_company.expects(:save).returns(true)
|
80
|
-
mock_company.stubs(:id).returns(1)
|
81
|
-
end
|
82
|
-
|
83
|
-
test "should redirect to next step if commit 'Next step'" do
|
84
|
-
mock_company.stubs(:stepper_current_step).returns("step2")
|
85
|
-
put(:update, {:company => {:code => "23"}, :commit => "Next step", :id => 1})
|
86
|
-
assert_response :redirect
|
87
|
-
assert_redirected_to "http://test.host/companies/new?id=1"
|
88
|
-
assert_equal flash[:notice], "Step Step2 was successfully created."
|
89
|
-
end
|
90
|
-
|
91
|
-
test "should redirect to index if commit 'Finish later'" do
|
92
|
-
mock_company.stubs(:previous_step!)
|
93
|
-
put(:update, {:company => {:code => "23"}, :commit => "Finish later", :id => 1})
|
94
|
-
assert_response :redirect
|
95
|
-
assert_redirected_to "http://test.host/companies"
|
96
|
-
end
|
97
|
-
|
98
|
-
test "should redirect to previous step if commit 'Previous step'" do
|
99
|
-
mock_company.expects(:previous_step!).returns(mock_company).at_least(2)
|
100
|
-
mock_company.stubs(:current_step).returns("step2")
|
101
|
-
put(:update, {:company => {:code => "23"}, :commit => "Previous step", :id => 1})
|
102
|
-
assert_response :redirect
|
103
|
-
assert_redirected_to "http://test.host/companies/new?id=1"
|
104
|
-
end
|
105
|
-
|
106
|
-
test "should redirect to show if commit 'Finish form'" do
|
107
|
-
put(:update, {:company => {:code => "23"}, :commit => "Finish form", :id => 1})
|
108
|
-
assert_response :redirect
|
109
|
-
assert_redirected_to "http://test.host/companies/1"
|
110
|
-
end
|
111
|
-
|
112
|
-
protected
|
113
|
-
def mock_company(stubs={})
|
114
|
-
@mock_company ||= mock(stubs)
|
115
|
-
end
|
116
|
-
end
|
117
|
-
|
118
|
-
class CompaniesInvalidParamsControllerTest < ActionController::TestCase
|
119
|
-
tests CompaniesController
|
120
|
-
|
121
|
-
setup do
|
122
|
-
@controller.expects(:render).at_least_once
|
123
|
-
end
|
124
|
-
|
125
|
-
test "should create action render to new action if object.save returns false" do
|
126
|
-
Company.expects(:new).with({'name' => 'Hina'}).returns(mock_company)
|
127
|
-
mock_company.expects(:save).returns(false)
|
128
|
-
mock_company.expects(:previous_step!)
|
129
|
-
post(:create, {:company => {:name => "Hina"}, :commit => "Next step"})
|
130
|
-
assert_response :success
|
131
|
-
end
|
132
|
-
|
133
|
-
test "should update action redirect to new action if object.save returns false" do
|
134
|
-
Company.expects(:find).with('1').returns(mock_company)
|
135
|
-
mock_company.expects(:attributes=).with({"name" => "Hina"}).returns(true)
|
136
|
-
mock_company.expects(:save).returns(false)
|
137
|
-
mock_company.expects(:previous_step!)
|
138
|
-
post(:update, {:company => {:name => "Hina"}, :id => 1, :commit => "Next step"})
|
139
|
-
assert_response :success
|
140
|
-
end
|
141
|
-
|
142
|
-
protected
|
143
|
-
def mock_company(stubs={})
|
144
|
-
@mock_company ||= mock(stubs)
|
145
|
-
end
|
146
|
-
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require "helper"
|
2
|
+
|
3
|
+
class CompaniesUpdateControllerTest < ActionController::TestCase
|
4
|
+
tests CompaniesController
|
5
|
+
|
6
|
+
setup do
|
7
|
+
Company.expects(:find).with('1').returns(mock_company)
|
8
|
+
mock_company.expects(:attributes=).with({'code' => '23'}).returns(true)
|
9
|
+
mock_company.expects(:save).returns(true)
|
10
|
+
mock_company.stubs(:id).returns(1)
|
11
|
+
end
|
12
|
+
|
13
|
+
test "should redirect to next step if commit 'Next step'" do
|
14
|
+
mock_company.stubs(:stepper_current_step).returns("step2")
|
15
|
+
put(:update, {:company => {:code => "23"}, :commit => "Next step", :id => 1})
|
16
|
+
assert_response :redirect
|
17
|
+
assert_redirected_to "http://test.host/companies/1/next_step"
|
18
|
+
end
|
19
|
+
|
20
|
+
test "should redirect to index if commit 'Finish later'" do
|
21
|
+
mock_company.stubs(:previous_step!)
|
22
|
+
put(:update, {:company => {:code => "23"}, :commit => "Finish later", :id => 1})
|
23
|
+
assert_response :redirect
|
24
|
+
assert_redirected_to "http://test.host/companies"
|
25
|
+
end
|
26
|
+
|
27
|
+
test "should redirect to previous step if commit 'Previous step'" do
|
28
|
+
mock_company.expects(:previous_step!).returns(mock_company).at_least(2)
|
29
|
+
mock_company.stubs(:current_step).returns("step2")
|
30
|
+
put(:update, {:company => {:code => "23"}, :commit => "Previous step", :id => 1})
|
31
|
+
assert_response :redirect
|
32
|
+
assert_redirected_to "http://test.host/companies/1/next_step"
|
33
|
+
end
|
34
|
+
|
35
|
+
test "should redirect to show if commit 'Finish form'" do
|
36
|
+
put(:update, {:company => {:code => "23"}, :commit => "Finish form", :id => 1})
|
37
|
+
assert_response :redirect
|
38
|
+
assert_redirected_to "http://test.host/companies/1"
|
39
|
+
end
|
40
|
+
|
41
|
+
protected
|
42
|
+
def mock_company(stubs={})
|
43
|
+
@mock_company ||= mock(stubs)
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class RedirectControllerTest < ActionController::TestCase
|
4
|
+
tests OrdersController
|
5
|
+
|
6
|
+
setup do
|
7
|
+
Order.expects(:find).with('1').returns(mock_order)
|
8
|
+
mock_order.expects(:attributes=).with({'code' => '23'}).returns(true)
|
9
|
+
mock_order.expects(:save).returns(true)
|
10
|
+
mock_order.stubs(:id).returns(1)
|
11
|
+
end
|
12
|
+
|
13
|
+
test "should redirect to :action => :index if commit 'Save'" do
|
14
|
+
mock_order.stubs(:stepper_current_step).returns("step2")
|
15
|
+
mock_order.stubs(:previous_step!)
|
16
|
+
put(:update, {:order => {:code => "23"}, :commit => "Finish later", :id => 1})
|
17
|
+
assert_response :redirect
|
18
|
+
assert_redirected_to "http://test.host/orders"
|
19
|
+
end
|
20
|
+
|
21
|
+
test "should redirect to show if commit 'Finish' and option is Proc" do
|
22
|
+
mock_order.stubs(:stepper_current_step).returns("step3")
|
23
|
+
put(:update, {:order => {:code => "23"}, :commit => "Finish form", :id => 1})
|
24
|
+
assert_response :redirect
|
25
|
+
assert_redirected_to "http://test.host/orders/1"
|
26
|
+
end
|
27
|
+
|
28
|
+
protected
|
29
|
+
def mock_order(stubs={})
|
30
|
+
@mock_order ||= mock(stubs)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
data/test/helpers/helper_test.rb
CHANGED
@@ -14,7 +14,7 @@ class StepperButtonsTest < ActionController::IntegrationTest
|
|
14
14
|
|
15
15
|
test "second step should have 'finish later', 'previous step' and 'next step' buttons" do
|
16
16
|
company = Company.create!(:name => "My company", :my_step => "step1")
|
17
|
-
get
|
17
|
+
get next_step_company_path(:id => company.id)
|
18
18
|
|
19
19
|
assert_select "li.next_step" do
|
20
20
|
assert_select "input[value='Next step']"
|
@@ -31,7 +31,7 @@ class StepperButtonsTest < ActionController::IntegrationTest
|
|
31
31
|
|
32
32
|
test "last step should have 'finish later', 'previous step' and 'finish' buttons" do
|
33
33
|
company = Company.create!(:name => "My company", :code => "04108", :my_step => "step2")
|
34
|
-
get
|
34
|
+
get next_step_company_path(:id => company.id)
|
35
35
|
|
36
36
|
assert_select "li.finish" do
|
37
37
|
assert_select "input[value='Finish form']"
|
data/test/models/assigns_test.rb
CHANGED
@@ -2,14 +2,15 @@ require 'helper'
|
|
2
2
|
class AssignsModelTest < ActiveSupport::TestCase
|
3
3
|
|
4
4
|
test "should assign default column" do
|
5
|
-
assert_equal User.
|
5
|
+
assert_equal User.stepper_current_step_column, :current_step
|
6
6
|
end
|
7
7
|
|
8
8
|
test "should assign column from options" do
|
9
|
-
assert_equal Company.
|
9
|
+
assert_equal Company.stepper_current_step_column, :my_step
|
10
10
|
end
|
11
11
|
|
12
12
|
test "should assign default steps" do
|
13
|
-
|
13
|
+
company = Company.new
|
14
|
+
assert_equal company.stepper_steps, ["step1", "step2", "step3"]
|
14
15
|
end
|
15
16
|
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
class OrdersController < ApplicationController
|
2
|
+
has_steps :redirect_to => {
|
3
|
+
:after_save => {:action => :index},
|
4
|
+
:after_finish => proc { |controller, resource| controller.order_url(resource.id) }
|
5
|
+
}
|
6
|
+
|
7
|
+
def show
|
8
|
+
|
9
|
+
end
|
10
|
+
|
11
|
+
def index
|
12
|
+
|
13
|
+
end
|
14
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: stepper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-10-
|
12
|
+
date: 2011-10-08 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
16
|
-
requirement: &
|
16
|
+
requirement: &70157641092880 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: 3.1.0
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70157641092880
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: ruby-debug19
|
27
|
-
requirement: &
|
27
|
+
requirement: &70157641090560 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70157641090560
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: sqlite3
|
38
|
-
requirement: &
|
38
|
+
requirement: &70157641082780 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70157641082780
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: shoulda
|
49
|
-
requirement: &
|
49
|
+
requirement: &70157641081340 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ! '>='
|
@@ -54,10 +54,10 @@ dependencies:
|
|
54
54
|
version: '0'
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *70157641081340
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: bundler
|
60
|
-
requirement: &
|
60
|
+
requirement: &70157641078200 !ruby/object:Gem::Requirement
|
61
61
|
none: false
|
62
62
|
requirements:
|
63
63
|
- - ~>
|
@@ -65,10 +65,10 @@ dependencies:
|
|
65
65
|
version: 1.0.0
|
66
66
|
type: :development
|
67
67
|
prerelease: false
|
68
|
-
version_requirements: *
|
68
|
+
version_requirements: *70157641078200
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: jeweler
|
71
|
-
requirement: &
|
71
|
+
requirement: &70157641076460 !ruby/object:Gem::Requirement
|
72
72
|
none: false
|
73
73
|
requirements:
|
74
74
|
- - ~>
|
@@ -76,10 +76,10 @@ dependencies:
|
|
76
76
|
version: 1.6.4
|
77
77
|
type: :development
|
78
78
|
prerelease: false
|
79
|
-
version_requirements: *
|
79
|
+
version_requirements: *70157641076460
|
80
80
|
- !ruby/object:Gem::Dependency
|
81
81
|
name: rcov
|
82
|
-
requirement: &
|
82
|
+
requirement: &70157641064820 !ruby/object:Gem::Requirement
|
83
83
|
none: false
|
84
84
|
requirements:
|
85
85
|
- - ! '>='
|
@@ -87,10 +87,10 @@ dependencies:
|
|
87
87
|
version: '0'
|
88
88
|
type: :development
|
89
89
|
prerelease: false
|
90
|
-
version_requirements: *
|
90
|
+
version_requirements: *70157641064820
|
91
91
|
- !ruby/object:Gem::Dependency
|
92
92
|
name: mocha
|
93
|
-
requirement: &
|
93
|
+
requirement: &70157641062940 !ruby/object:Gem::Requirement
|
94
94
|
none: false
|
95
95
|
requirements:
|
96
96
|
- - ! '>='
|
@@ -98,10 +98,10 @@ dependencies:
|
|
98
98
|
version: '0'
|
99
99
|
type: :development
|
100
100
|
prerelease: false
|
101
|
-
version_requirements: *
|
101
|
+
version_requirements: *70157641062940
|
102
102
|
- !ruby/object:Gem::Dependency
|
103
103
|
name: capybara
|
104
|
-
requirement: &
|
104
|
+
requirement: &70157641061660 !ruby/object:Gem::Requirement
|
105
105
|
none: false
|
106
106
|
requirements:
|
107
107
|
- - ! '>='
|
@@ -109,10 +109,10 @@ dependencies:
|
|
109
109
|
version: '0'
|
110
110
|
type: :development
|
111
111
|
prerelease: false
|
112
|
-
version_requirements: *
|
112
|
+
version_requirements: *70157641061660
|
113
113
|
- !ruby/object:Gem::Dependency
|
114
114
|
name: launchy
|
115
|
-
requirement: &
|
115
|
+
requirement: &70157641059620 !ruby/object:Gem::Requirement
|
116
116
|
none: false
|
117
117
|
requirements:
|
118
118
|
- - ! '>='
|
@@ -120,7 +120,7 @@ dependencies:
|
|
120
120
|
version: '0'
|
121
121
|
type: :development
|
122
122
|
prerelease: false
|
123
|
-
version_requirements: *
|
123
|
+
version_requirements: *70157641059620
|
124
124
|
description: Stepper is multistep form (wizard) solution for Rails 3. Stepper allows
|
125
125
|
you to split up your large form into series of pages that users can navigate through
|
126
126
|
to complete the form and save it state.
|
@@ -150,24 +150,29 @@ files:
|
|
150
150
|
- lib/stepper/railtie.rb
|
151
151
|
- stepper.gemspec
|
152
152
|
- test/controllers/controller_additions_test.rb
|
153
|
+
- test/controllers/controller_create_test.rb
|
154
|
+
- test/controllers/controller_invalid_params_test.rb
|
153
155
|
- test/controllers/controller_resource_test.rb
|
154
156
|
- test/controllers/controller_test.rb
|
157
|
+
- test/controllers/controller_update_test.rb
|
158
|
+
- test/controllers/redirect_test.rb
|
155
159
|
- test/helper.rb
|
156
160
|
- test/helpers/helper_test.rb
|
157
161
|
- test/integration/steps_test.rb
|
158
162
|
- test/models/assigns_test.rb
|
159
163
|
- test/models/instance_test.rb
|
160
164
|
- test/models/models_test.rb
|
161
|
-
- test/models/sti_test.rb
|
162
165
|
- test/models/validation_test.rb
|
163
166
|
- test/rails_app/Rakefile
|
164
167
|
- test/rails_app/app/controllers/application_controller.rb
|
165
168
|
- test/rails_app/app/controllers/companies_controller.rb
|
169
|
+
- test/rails_app/app/controllers/orders_controller.rb
|
166
170
|
- test/rails_app/app/helpers/application_helper.rb
|
167
171
|
- test/rails_app/app/mailers/.gitkeep
|
168
172
|
- test/rails_app/app/models/.gitkeep
|
169
173
|
- test/rails_app/app/models/big_company.rb
|
170
174
|
- test/rails_app/app/models/company.rb
|
175
|
+
- test/rails_app/app/models/order.rb
|
171
176
|
- test/rails_app/app/models/users.rb
|
172
177
|
- test/rails_app/app/views/companies/_step1_step.html.erb
|
173
178
|
- test/rails_app/app/views/companies/_step2_step.html.erb
|
@@ -218,7 +223,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
218
223
|
version: '0'
|
219
224
|
segments:
|
220
225
|
- 0
|
221
|
-
hash:
|
226
|
+
hash: 1432706558096763112
|
222
227
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
223
228
|
none: false
|
224
229
|
requirements:
|
data/test/models/sti_test.rb
DELETED
@@ -1,11 +0,0 @@
|
|
1
|
-
require 'helper'
|
2
|
-
class StiModelTest < ActiveSupport::TestCase
|
3
|
-
setup do
|
4
|
-
@big_company = BigCompany.new
|
5
|
-
end
|
6
|
-
|
7
|
-
test "should get steps from parent class" do
|
8
|
-
assert_equal @big_company.stepper_steps, ["step1", "step2", "step3", "step4", "step5", "step6"]
|
9
|
-
end
|
10
|
-
|
11
|
-
end
|