acts_as_multipart_form 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +3 -0
- data/Gemfile +27 -0
- data/Gemfile.lock +157 -0
- data/LICENSE.txt +20 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +19 -0
- data/Rakefile +65 -0
- data/VERSION +1 -0
- data/acts_as_multipart_form.gemspec +163 -0
- data/app/controllers/multipart_form/in_progress_forms_controller.rb +0 -0
- data/app/models/multipart_form/in_progress_form.rb +20 -0
- data/app/views/multipart_form/_breadcrumb.html.erb +21 -0
- data/app/views/multipart_form/_index_links.html.erb +20 -0
- data/app/views/multipart_form/in_progress_form/index.html.erb +0 -0
- data/app/views/multipart_form/in_progress_form/index.html.haml +0 -0
- data/lib/acts_as_multipart_form.rb +2 -0
- data/lib/acts_as_multipart_form/config.rb +36 -0
- data/lib/acts_as_multipart_form/engine.rb +4 -0
- data/lib/acts_as_multipart_form/multipart_form_in_controller.rb +301 -0
- data/lib/acts_as_multipart_form/multipart_form_in_model.rb +105 -0
- data/lib/acts_as_multipart_form/railtie.rb +14 -0
- data/lib/generators/acts_as_multipart_form/install_generator.rb +44 -0
- data/lib/generators/acts_as_multipart_form/templates/config.rb +6 -0
- data/lib/generators/acts_as_multipart_form/templates/migrations/install_migration.rb.erb +17 -0
- data/spec/acts_as_multipart_form_spec.rb +7 -0
- data/spec/dummy/Rakefile +7 -0
- data/spec/dummy/app/controllers/application_controller.rb +3 -0
- data/spec/dummy/app/controllers/people_controller.rb +53 -0
- data/spec/dummy/app/helpers/application_helper.rb +2 -0
- data/spec/dummy/app/models/person.rb +5 -0
- data/spec/dummy/app/models/person_with_multiple_actsas.rb +5 -0
- data/spec/dummy/app/models/person_with_multiple_forms.rb +4 -0
- data/spec/dummy/app/views/layouts/application.html.erb +14 -0
- data/spec/dummy/app/views/people/_job_info.html.erb +8 -0
- data/spec/dummy/app/views/people/_person_info.html.erb +8 -0
- data/spec/dummy/app/views/people/hire_form.html.erb +10 -0
- data/spec/dummy/app/views/people/index.html.erb +16 -0
- data/spec/dummy/app/views/people/show.html.erb +0 -0
- data/spec/dummy/config.ru +4 -0
- data/spec/dummy/config/application.rb +45 -0
- data/spec/dummy/config/boot.rb +10 -0
- data/spec/dummy/config/database.yml +22 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/development.rb +26 -0
- data/spec/dummy/config/environments/production.rb +49 -0
- data/spec/dummy/config/environments/test.rb +35 -0
- data/spec/dummy/config/initializers/acts_as_multipart_form.rb +6 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/config/initializers/inflections.rb +10 -0
- data/spec/dummy/config/initializers/mime_types.rb +5 -0
- data/spec/dummy/config/initializers/secret_token.rb +7 -0
- data/spec/dummy/config/initializers/session_store.rb +8 -0
- data/spec/dummy/config/locales/en.yml +5 -0
- data/spec/dummy/config/routes.rb +66 -0
- data/spec/dummy/db/migrate/20110715180834_create_people.rb +13 -0
- data/spec/dummy/db/migrate/20110722130249_create_multipart_form_tables.rb +17 -0
- data/spec/dummy/db/schema.rb +31 -0
- data/spec/dummy/features/form_breadcrumb.feature +75 -0
- data/spec/dummy/features/form_submission.feature +23 -0
- data/spec/dummy/features/index_links.feature +56 -0
- data/spec/dummy/features/step_definitions/acts_as_multipart_form_steps.rb +30 -0
- data/spec/dummy/features/step_definitions/config_steps.rb +23 -0
- data/spec/dummy/features/step_definitions/web_steps.rb +214 -0
- data/spec/dummy/features/support/env.rb +15 -0
- data/spec/dummy/features/support/paths.rb +31 -0
- data/spec/dummy/features/support/selectors.rb +39 -0
- data/spec/dummy/public/404.html +26 -0
- data/spec/dummy/public/422.html +26 -0
- data/spec/dummy/public/500.html +26 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/dummy/public/javascripts/application.js +2 -0
- data/spec/dummy/public/javascripts/controls.js +965 -0
- data/spec/dummy/public/javascripts/dragdrop.js +974 -0
- data/spec/dummy/public/javascripts/effects.js +1123 -0
- data/spec/dummy/public/javascripts/prototype.js +6001 -0
- data/spec/dummy/public/javascripts/rails.js +191 -0
- data/spec/dummy/public/stylesheets/.gitkeep +0 -0
- data/spec/dummy/script/rails +6 -0
- data/spec/in_progress_form_spec.rb +43 -0
- data/spec/integration/navigation_spec.rb +9 -0
- data/spec/multipart_form_in_controller_integeration_spec.rb +23 -0
- data/spec/multipart_form_in_controller_spec.rb +360 -0
- data/spec/multipart_form_in_model_integration_spec.rb +30 -0
- data/spec/multipart_form_in_model_spec.rb +156 -0
- data/spec/spec_helper.rb +38 -0
- metadata +296 -0
@@ -0,0 +1,191 @@
|
|
1
|
+
(function() {
|
2
|
+
// Technique from Juriy Zaytsev
|
3
|
+
// http://thinkweb2.com/projects/prototype/detecting-event-support-without-browser-sniffing/
|
4
|
+
function isEventSupported(eventName) {
|
5
|
+
var el = document.createElement('div');
|
6
|
+
eventName = 'on' + eventName;
|
7
|
+
var isSupported = (eventName in el);
|
8
|
+
if (!isSupported) {
|
9
|
+
el.setAttribute(eventName, 'return;');
|
10
|
+
isSupported = typeof el[eventName] == 'function';
|
11
|
+
}
|
12
|
+
el = null;
|
13
|
+
return isSupported;
|
14
|
+
}
|
15
|
+
|
16
|
+
function isForm(element) {
|
17
|
+
return Object.isElement(element) && element.nodeName.toUpperCase() == 'FORM'
|
18
|
+
}
|
19
|
+
|
20
|
+
function isInput(element) {
|
21
|
+
if (Object.isElement(element)) {
|
22
|
+
var name = element.nodeName.toUpperCase()
|
23
|
+
return name == 'INPUT' || name == 'SELECT' || name == 'TEXTAREA'
|
24
|
+
}
|
25
|
+
else return false
|
26
|
+
}
|
27
|
+
|
28
|
+
var submitBubbles = isEventSupported('submit'),
|
29
|
+
changeBubbles = isEventSupported('change')
|
30
|
+
|
31
|
+
if (!submitBubbles || !changeBubbles) {
|
32
|
+
// augment the Event.Handler class to observe custom events when needed
|
33
|
+
Event.Handler.prototype.initialize = Event.Handler.prototype.initialize.wrap(
|
34
|
+
function(init, element, eventName, selector, callback) {
|
35
|
+
init(element, eventName, selector, callback)
|
36
|
+
// is the handler being attached to an element that doesn't support this event?
|
37
|
+
if ( (!submitBubbles && this.eventName == 'submit' && !isForm(this.element)) ||
|
38
|
+
(!changeBubbles && this.eventName == 'change' && !isInput(this.element)) ) {
|
39
|
+
// "submit" => "emulated:submit"
|
40
|
+
this.eventName = 'emulated:' + this.eventName
|
41
|
+
}
|
42
|
+
}
|
43
|
+
)
|
44
|
+
}
|
45
|
+
|
46
|
+
if (!submitBubbles) {
|
47
|
+
// discover forms on the page by observing focus events which always bubble
|
48
|
+
document.on('focusin', 'form', function(focusEvent, form) {
|
49
|
+
// special handler for the real "submit" event (one-time operation)
|
50
|
+
if (!form.retrieve('emulated:submit')) {
|
51
|
+
form.on('submit', function(submitEvent) {
|
52
|
+
var emulated = form.fire('emulated:submit', submitEvent, true)
|
53
|
+
// if custom event received preventDefault, cancel the real one too
|
54
|
+
if (emulated.returnValue === false) submitEvent.preventDefault()
|
55
|
+
})
|
56
|
+
form.store('emulated:submit', true)
|
57
|
+
}
|
58
|
+
})
|
59
|
+
}
|
60
|
+
|
61
|
+
if (!changeBubbles) {
|
62
|
+
// discover form inputs on the page
|
63
|
+
document.on('focusin', 'input, select, texarea', function(focusEvent, input) {
|
64
|
+
// special handler for real "change" events
|
65
|
+
if (!input.retrieve('emulated:change')) {
|
66
|
+
input.on('change', function(changeEvent) {
|
67
|
+
input.fire('emulated:change', changeEvent, true)
|
68
|
+
})
|
69
|
+
input.store('emulated:change', true)
|
70
|
+
}
|
71
|
+
})
|
72
|
+
}
|
73
|
+
|
74
|
+
function handleRemote(element) {
|
75
|
+
var method, url, params;
|
76
|
+
|
77
|
+
var event = element.fire("ajax:before");
|
78
|
+
if (event.stopped) return false;
|
79
|
+
|
80
|
+
if (element.tagName.toLowerCase() === 'form') {
|
81
|
+
method = element.readAttribute('method') || 'post';
|
82
|
+
url = element.readAttribute('action');
|
83
|
+
params = element.serialize();
|
84
|
+
} else {
|
85
|
+
method = element.readAttribute('data-method') || 'get';
|
86
|
+
url = element.readAttribute('href');
|
87
|
+
params = {};
|
88
|
+
}
|
89
|
+
|
90
|
+
new Ajax.Request(url, {
|
91
|
+
method: method,
|
92
|
+
parameters: params,
|
93
|
+
evalScripts: true,
|
94
|
+
|
95
|
+
onComplete: function(request) { element.fire("ajax:complete", request); },
|
96
|
+
onSuccess: function(request) { element.fire("ajax:success", request); },
|
97
|
+
onFailure: function(request) { element.fire("ajax:failure", request); }
|
98
|
+
});
|
99
|
+
|
100
|
+
element.fire("ajax:after");
|
101
|
+
}
|
102
|
+
|
103
|
+
function handleMethod(element) {
|
104
|
+
var method = element.readAttribute('data-method'),
|
105
|
+
url = element.readAttribute('href'),
|
106
|
+
csrf_param = $$('meta[name=csrf-param]')[0],
|
107
|
+
csrf_token = $$('meta[name=csrf-token]')[0];
|
108
|
+
|
109
|
+
var form = new Element('form', { method: "POST", action: url, style: "display: none;" });
|
110
|
+
element.parentNode.insert(form);
|
111
|
+
|
112
|
+
if (method !== 'post') {
|
113
|
+
var field = new Element('input', { type: 'hidden', name: '_method', value: method });
|
114
|
+
form.insert(field);
|
115
|
+
}
|
116
|
+
|
117
|
+
if (csrf_param) {
|
118
|
+
var param = csrf_param.readAttribute('content'),
|
119
|
+
token = csrf_token.readAttribute('content'),
|
120
|
+
field = new Element('input', { type: 'hidden', name: param, value: token });
|
121
|
+
form.insert(field);
|
122
|
+
}
|
123
|
+
|
124
|
+
form.submit();
|
125
|
+
}
|
126
|
+
|
127
|
+
|
128
|
+
document.on("click", "*[data-confirm]", function(event, element) {
|
129
|
+
var message = element.readAttribute('data-confirm');
|
130
|
+
if (!confirm(message)) event.stop();
|
131
|
+
});
|
132
|
+
|
133
|
+
document.on("click", "a[data-remote]", function(event, element) {
|
134
|
+
if (event.stopped) return;
|
135
|
+
handleRemote(element);
|
136
|
+
event.stop();
|
137
|
+
});
|
138
|
+
|
139
|
+
document.on("click", "a[data-method]", function(event, element) {
|
140
|
+
if (event.stopped) return;
|
141
|
+
handleMethod(element);
|
142
|
+
event.stop();
|
143
|
+
});
|
144
|
+
|
145
|
+
document.on("submit", function(event) {
|
146
|
+
var element = event.findElement(),
|
147
|
+
message = element.readAttribute('data-confirm');
|
148
|
+
if (message && !confirm(message)) {
|
149
|
+
event.stop();
|
150
|
+
return false;
|
151
|
+
}
|
152
|
+
|
153
|
+
var inputs = element.select("input[type=submit][data-disable-with]");
|
154
|
+
inputs.each(function(input) {
|
155
|
+
input.disabled = true;
|
156
|
+
input.writeAttribute('data-original-value', input.value);
|
157
|
+
input.value = input.readAttribute('data-disable-with');
|
158
|
+
});
|
159
|
+
|
160
|
+
var element = event.findElement("form[data-remote]");
|
161
|
+
if (element) {
|
162
|
+
handleRemote(element);
|
163
|
+
event.stop();
|
164
|
+
}
|
165
|
+
});
|
166
|
+
|
167
|
+
document.on("ajax:after", "form", function(event, element) {
|
168
|
+
var inputs = element.select("input[type=submit][disabled=true][data-disable-with]");
|
169
|
+
inputs.each(function(input) {
|
170
|
+
input.value = input.readAttribute('data-original-value');
|
171
|
+
input.removeAttribute('data-original-value');
|
172
|
+
input.disabled = false;
|
173
|
+
});
|
174
|
+
});
|
175
|
+
|
176
|
+
Ajax.Responders.register({
|
177
|
+
onCreate: function(request) {
|
178
|
+
var csrf_meta_tag = $$('meta[name=csrf-token]')[0];
|
179
|
+
|
180
|
+
if (csrf_meta_tag) {
|
181
|
+
var header = 'X-CSRF-Token',
|
182
|
+
token = csrf_meta_tag.readAttribute('content');
|
183
|
+
|
184
|
+
if (!request.options.requestHeaders) {
|
185
|
+
request.options.requestHeaders = {};
|
186
|
+
}
|
187
|
+
request.options.requestHeaders[header] = token;
|
188
|
+
}
|
189
|
+
}
|
190
|
+
});
|
191
|
+
})();
|
File without changes
|
@@ -0,0 +1,6 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
|
3
|
+
|
4
|
+
APP_PATH = File.expand_path('../../config/application', __FILE__)
|
5
|
+
require File.expand_path('../../config/boot', __FILE__)
|
6
|
+
require 'rails/commands'
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require_relative 'spec_helper'
|
2
|
+
|
3
|
+
describe MultipartForm::InProgressForm do
|
4
|
+
describe "validations" do
|
5
|
+
before(:each) do
|
6
|
+
@ip_form = MultipartForm::InProgressForm.new
|
7
|
+
@person = mock_model(Person)
|
8
|
+
@person.stub!(:model).and_return("Person")
|
9
|
+
@person.stub!(:id).and_return(1)
|
10
|
+
|
11
|
+
form_name = :person_form.to_s
|
12
|
+
|
13
|
+
@valid_attributes = {
|
14
|
+
:form_subject => @person,
|
15
|
+
:form_name => "hire_form",
|
16
|
+
:last_completed_step => "personal_info",
|
17
|
+
:completed => "false"
|
18
|
+
}
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should be valid" do
|
23
|
+
@ip_form.attributes = @valid_attributes
|
24
|
+
@ip_form.should be_valid
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should require a form subject" do
|
28
|
+
@ip_form.should have(1).error_on(:form_subject)
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should require a form name" do
|
32
|
+
@ip_form.should have(1).error_on(:form_name)
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should require a last completed step" do
|
36
|
+
@ip_form.should have(1).error_on(:last_completed_step)
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should require completed" do
|
40
|
+
@ip_form.should have(1).error_on(:completed)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require_relative 'spec_helper'
|
2
|
+
|
3
|
+
describe PeopleController do
|
4
|
+
describe "hire_form action" do
|
5
|
+
# no idea why I can't call get
|
6
|
+
# I am terrible at testing
|
7
|
+
def get_hire_form(input_params = nil)
|
8
|
+
params = input_params || { :in_progress_form_id => 1, :multipart_form_part => :person_info }
|
9
|
+
get :hire_form, params
|
10
|
+
end
|
11
|
+
|
12
|
+
#it "should call the form handler" do
|
13
|
+
# @controller.should_receive(:multipart_form_handler)
|
14
|
+
# get_hire_form
|
15
|
+
#end
|
16
|
+
|
17
|
+
#it "should save the multipart form part for the form so it is available for the view" do
|
18
|
+
# get_hire_form
|
19
|
+
# assigns[:multipart_form_part].should == :person_info
|
20
|
+
#end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
@@ -0,0 +1,360 @@
|
|
1
|
+
require_relative 'spec_helper'
|
2
|
+
|
3
|
+
describe ActsAsMultipartForm::MultipartFormInController do
|
4
|
+
describe "acts_as_multipart_form class method" do
|
5
|
+
before(:each) do
|
6
|
+
@controller = PeopleController.new
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should include instance methods" do
|
10
|
+
@controller.should be_a_kind_of (ActsAsMultipartForm::MultipartFormInController::InstanceMethods)
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should setup the multipart_forms hash" do
|
14
|
+
@controller.should respond_to :multipart_forms
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should save the parameters to multipart_forms hash" do
|
18
|
+
@controller.multipart_forms[:hire_form][:parts].should == [:person_info, :person_info_update, :job_info, :job_info_update]
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should set the model if it is not given" do
|
22
|
+
@controller.multipart_forms[:hire_form][:model].should == "Person"
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should set the form_route if it is not given" do
|
26
|
+
@controller.multipart_forms[:hire_form][:form_route].should == "person_hire_form"
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should set the show_route if it is not given" do
|
30
|
+
@controller.multipart_forms[:hire_form][:show_route].should == "person"
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
describe "get_next_multipart_form_part method" do
|
35
|
+
before(:each) do
|
36
|
+
@controller = PeopleController.new
|
37
|
+
@controller.multipart_forms[:hire_form][:parts] = [:person_info, :job_info]
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should respond to get_next_multipart_form_part" do
|
41
|
+
@controller.should respond_to :get_next_multipart_form_part
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should return the next part" do
|
45
|
+
@controller.get_next_multipart_form_part(:hire_form, :person_info).should == :job_info
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should return the current part if on the last part" do
|
49
|
+
@controller.get_next_multipart_form_part(:hire_form, :job_info).should == :job_info
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
describe "get_previous_multipart_form_part method" do
|
54
|
+
before(:each) do
|
55
|
+
@controller = PeopleController.new
|
56
|
+
@controller.multipart_forms[:hire_form][:parts] = [:person_info, :job_info]
|
57
|
+
end
|
58
|
+
|
59
|
+
it "should respond to get_previous_multipart_form_part" do
|
60
|
+
@controller.should respond_to :get_previous_multipart_form_part
|
61
|
+
end
|
62
|
+
|
63
|
+
it "should return the previous part" do
|
64
|
+
@controller.get_previous_multipart_form_part(:hire_form, :job_info).should == :person_info
|
65
|
+
end
|
66
|
+
|
67
|
+
it "Should return the current part if on the first part" do
|
68
|
+
@controller.get_previous_multipart_form_part(:hire_form, :person_info).should == :person_info
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
describe "last_multipart_form_part? method" do
|
73
|
+
before(:each) do
|
74
|
+
@controller = PeopleController.new
|
75
|
+
@controller.multipart_forms[:hire_form][:parts] = [:person_info, :job_info]
|
76
|
+
end
|
77
|
+
|
78
|
+
it "should respond to last_multipart_form_part?" do
|
79
|
+
@controller.should respond_to :last_multipart_form_part?
|
80
|
+
end
|
81
|
+
|
82
|
+
it "should return true if the parts match" do
|
83
|
+
@controller.last_multipart_form_part?(:hire_form, :job_info).should be_true
|
84
|
+
end
|
85
|
+
|
86
|
+
it "should return false if the parts don't match" do
|
87
|
+
@controller.last_multipart_form_part?(:hire_form, :person_info).should be_false
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
describe "first_multipart_form_part? method" do
|
92
|
+
before(:each) do
|
93
|
+
@controller = PeopleController.new
|
94
|
+
@controller.multipart_forms[:hire_form][:parts] = [:person_info, :job_info]
|
95
|
+
end
|
96
|
+
|
97
|
+
it "should respond to first_multipart_form_part?" do
|
98
|
+
@controller.should respond_to :first_multipart_form_part?
|
99
|
+
end
|
100
|
+
|
101
|
+
it "should return true if the parts match" do
|
102
|
+
@controller.first_multipart_form_part?(:hire_form, :person_info).should be_true
|
103
|
+
end
|
104
|
+
|
105
|
+
it "should return false if the parts don't match" do
|
106
|
+
@controller.first_multipart_form_part?(:hire_form, :job_info).should be_false
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
describe "multipart_form_action? method" do
|
111
|
+
before(:each) do
|
112
|
+
@controller = PeopleController.new
|
113
|
+
end
|
114
|
+
|
115
|
+
it "should respond_to multipart_form_action?" do
|
116
|
+
@controller.respond_to?(:multipart_form_action?).should be_true
|
117
|
+
end
|
118
|
+
|
119
|
+
it "should be true if the key is included" do
|
120
|
+
@controller.multipart_forms.stub!(:keys).and_return([:hire_form])
|
121
|
+
@controller.multipart_form_action?(:hire_form).should be_true
|
122
|
+
end
|
123
|
+
|
124
|
+
it "should be false if the key is not included" do
|
125
|
+
@controller.multipart_forms.stub!(:keys).and_return([:some_other_form])
|
126
|
+
@controller.multipart_form_action?(:hire_form).should be_false
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
describe "load_multipart_form_index_links method" do
|
131
|
+
before(:each) do
|
132
|
+
@controller = PeopleController.new
|
133
|
+
@form_name = :hire_form
|
134
|
+
@person = mock_model(Person)
|
135
|
+
@person.stub!(:id).and_return(34)
|
136
|
+
@form_subjects = [@person]
|
137
|
+
@ipf = mock_model(MultipartForm::InProgressForm)
|
138
|
+
@ipf.stub!(:last_completed_step)
|
139
|
+
@ipf.stub!(:completed).and_return(true)
|
140
|
+
@controller.stub!(:find_or_create_multipart_in_progress_form).and_return(@ipf)
|
141
|
+
end
|
142
|
+
|
143
|
+
it "should call find_or_create_multipart_in_progress_form" do
|
144
|
+
@controller.should_receive(:find_or_create_multipart_in_progress_form)
|
145
|
+
@controller.load_multipart_form_index_links(@form_name, @form_subjects)
|
146
|
+
end
|
147
|
+
|
148
|
+
it "should call get_available_multipart_form_parts" do
|
149
|
+
@controller.should_receive(:get_available_multipart_form_parts)
|
150
|
+
@controller.load_multipart_form_index_links(@form_name, @form_subjects)
|
151
|
+
end
|
152
|
+
|
153
|
+
it "should call completed for in_progress_form" do
|
154
|
+
@ipf.should_receive(:completed).and_return(true)
|
155
|
+
@controller.load_multipart_form_index_links(@form_name, @form_subjects)
|
156
|
+
end
|
157
|
+
|
158
|
+
it "should call last_completed_step for in_progress_form" do
|
159
|
+
@ipf.should_receive(:last_completed_step).and_return(:form_part)
|
160
|
+
@controller.load_multipart_form_index_links(@form_name, @form_subjects)
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
describe "multipart_form_handler method" do
|
165
|
+
before(:each) do
|
166
|
+
@controller = PeopleController.new
|
167
|
+
@default_params = {
|
168
|
+
:action => :hire_form,
|
169
|
+
:multipart_form_part => :person_info,
|
170
|
+
:in_progress_form_id => 100
|
171
|
+
}
|
172
|
+
@controller.stub!(:params).and_return(@default_params)
|
173
|
+
@controller.multipart_forms[:hire_form][:parts] = [:person_info, :person_info_update, :job_info, :job_info_update]
|
174
|
+
end
|
175
|
+
|
176
|
+
it "should respond to multipart_form_handler" do
|
177
|
+
@controller.should respond_to :multipart_form_handler
|
178
|
+
end
|
179
|
+
|
180
|
+
it "should call the multipart form part method" do
|
181
|
+
@controller.should_receive(:person_info)
|
182
|
+
@controller.multipart_form_handler
|
183
|
+
end
|
184
|
+
|
185
|
+
it "should not call the multipart form method if it is not a member of that form's parts" do
|
186
|
+
@controller.should_not_receive(:person_info)
|
187
|
+
@controller.multipart_forms[:hire_form][:parts] = [:job_info, :job_info_update]
|
188
|
+
@controller.multipart_form_handler
|
189
|
+
end
|
190
|
+
|
191
|
+
# this is not great
|
192
|
+
# should be refactored so it doesn't hit the database
|
193
|
+
it "should default to the last completed step of the in progress form is not set in params" do
|
194
|
+
ipf = MultipartForm::InProgressForm.new(:last_completed_step => "person_info_update")
|
195
|
+
ipf.save(:validate => false)
|
196
|
+
params = {
|
197
|
+
:action => :hire_form,
|
198
|
+
:in_progress_form_id => ipf.id
|
199
|
+
}
|
200
|
+
@controller.stub!(:params).and_return(params)
|
201
|
+
@controller.stub!(:find_or_create_multipart_form_subect)
|
202
|
+
@controller.stub!(:find_or_create_multipart_in_progress_form).and_return(ipf)
|
203
|
+
@controller.should_receive(:job_info)
|
204
|
+
@controller.multipart_form_handler
|
205
|
+
end
|
206
|
+
|
207
|
+
it "should default to the first part if no part is given and the in progress form does not have a completed part" do
|
208
|
+
params = {
|
209
|
+
:action => :hire_form,
|
210
|
+
:in_progress_form_id => 100
|
211
|
+
}
|
212
|
+
@controller.stub!(:params).and_return(params)
|
213
|
+
@controller.should_receive(:person_info)
|
214
|
+
@controller.multipart_form_handler
|
215
|
+
end
|
216
|
+
|
217
|
+
it "should maintain the completed part state in the database" do
|
218
|
+
params = {
|
219
|
+
:action => :hire_form,
|
220
|
+
:multipart_form_part => :person_info_update,
|
221
|
+
:in_progress_form_id => 100
|
222
|
+
}
|
223
|
+
@controller.stub!(:params).and_return(params)
|
224
|
+
ipf = MultipartForm::InProgressForm.new
|
225
|
+
@controller.stub!(:find_or_create_multipart_in_progress_form).and_return(ipf)
|
226
|
+
@controller.stub!(:person_info_update).and_return({:valid => true})
|
227
|
+
@controller.stub!(:redirect_to_next_multipart_form_part).and_return(false)
|
228
|
+
ipf.should_receive(:update_attributes).with(:completed => false, :last_completed_step => :person_info_update)
|
229
|
+
|
230
|
+
@controller.multipart_form_handler
|
231
|
+
end
|
232
|
+
end
|
233
|
+
|
234
|
+
describe "find_or_create_multipart_form_subject method" do
|
235
|
+
before(:each) do
|
236
|
+
@controller = PeopleController.new
|
237
|
+
@controller.multipart_forms[:hire_form][:model] = "Person"
|
238
|
+
end
|
239
|
+
|
240
|
+
it "should find the form subject on a valid id" do
|
241
|
+
Person.should_receive(:find)
|
242
|
+
@controller.find_or_create_multipart_form_subject(:hire_form, 1)
|
243
|
+
end
|
244
|
+
|
245
|
+
it "should create the form subject if there is no id given" do
|
246
|
+
p = mock_model(Person)
|
247
|
+
Person.should_receive(:new).and_return(p)
|
248
|
+
p.should_receive(:save).with(:validate => false)
|
249
|
+
@controller.find_or_create_multipart_form_subject(:hire_form, nil)
|
250
|
+
end
|
251
|
+
|
252
|
+
it "should return an error or nil if the id is invalid" do
|
253
|
+
Person.should_receive(:find).and_return(nil)
|
254
|
+
@controller.find_or_create_multipart_form_subject(:hire_form, 1).should be_nil
|
255
|
+
end
|
256
|
+
end
|
257
|
+
|
258
|
+
describe "find_or_create_multipart_in_progress_form method" do
|
259
|
+
before(:each) do
|
260
|
+
@controller = PeopleController.new
|
261
|
+
@form_subject = mock_model(Person)
|
262
|
+
@ipf = mock_model(MultipartForm::InProgressForm)
|
263
|
+
end
|
264
|
+
|
265
|
+
it "should find the in progress form if it exists" do
|
266
|
+
MultipartForm::InProgressForm.should_receive(:where).and_return([@ipf])
|
267
|
+
@controller.find_or_create_multipart_in_progress_form(:hire_form, @form_subject).should == @ipf
|
268
|
+
end
|
269
|
+
|
270
|
+
it "should create an in progress form if it doesn't exist" do
|
271
|
+
MultipartForm::InProgressForm.should_receive(:create).and_return(@ipf)
|
272
|
+
@controller.find_or_create_multipart_in_progress_form(:hire_form, @form_subject).should == @ipf
|
273
|
+
end
|
274
|
+
|
275
|
+
# maybe refactor so it doesn't hit the database
|
276
|
+
it "should create an in progress form with a last_completed_step initially set to 'none'" do
|
277
|
+
@controller.find_or_create_multipart_in_progress_form(:hire_form, @form_subject).last_completed_step.should == "none"
|
278
|
+
end
|
279
|
+
end
|
280
|
+
|
281
|
+
# marking these test pending for now becuase I don't know how to fix the issues with rspec and redirect_to
|
282
|
+
# they only break in rspec as far as I can tell
|
283
|
+
describe "redirect_to_next_multipart_form_part method" do
|
284
|
+
before(:each) do
|
285
|
+
@controller = PeopleController.new
|
286
|
+
@form_name = :hire_form
|
287
|
+
@form_subject = mock_model(Person)
|
288
|
+
@form_subject.stub!(:id).and_return(1)
|
289
|
+
@part = :person_info
|
290
|
+
end
|
291
|
+
|
292
|
+
it "should redirect to the view page if on the last step" do
|
293
|
+
pending
|
294
|
+
@controller.stub!(:last_multipart_form_part?).and_return(true)
|
295
|
+
@controller.redirect_to_next_multipart_form_part(@form_name, @form_subject, @part).should redirect_to("/people/1")
|
296
|
+
end
|
297
|
+
|
298
|
+
it "should return true if on the last step" do
|
299
|
+
pending
|
300
|
+
@controller.stub!(:last_multipart_form_part?).and_return(true)
|
301
|
+
@controller.redirect_to_next_multipart_form_part(@form_name, @form_subject, @part).should be_true
|
302
|
+
end
|
303
|
+
|
304
|
+
it "should redirect to the given route and path if not on the last step" do
|
305
|
+
pending
|
306
|
+
@controller.stub!(:last_multipart_form_part?).and_return(false)
|
307
|
+
@controller.redirect_to_next_multipart_form_part(@form_name, @form_subject, @part).should redirect_to("/people/hire_form/1/job_info")
|
308
|
+
end
|
309
|
+
|
310
|
+
it "should return false if not on the last step" do
|
311
|
+
@controller.stub!(:last_multipart_form_part?).and_return(false)
|
312
|
+
end
|
313
|
+
end
|
314
|
+
|
315
|
+
describe "get_available_multipart_form_parts method" do
|
316
|
+
before(:each) do
|
317
|
+
@controller = PeopleController.new
|
318
|
+
@form_name = :hire_form
|
319
|
+
@last_completed_part = :person_info_update
|
320
|
+
@controller.multipart_forms[@form_name][:parts] = [:person_info, :person_info_update, :job_info, :job_info_update]
|
321
|
+
end
|
322
|
+
|
323
|
+
it "should return a hash" do
|
324
|
+
ActsAsMultipartForm.config.stub!(:show_incomplete_parts).and_return(true)
|
325
|
+
@controller.get_available_multipart_form_parts(@form_name, @last_completed_part).should be_a_kind_of Array
|
326
|
+
end
|
327
|
+
|
328
|
+
it "should return the parts" do
|
329
|
+
ActsAsMultipartForm.config.stub!(:show_incomplete_parts).and_return(true)
|
330
|
+
results = @controller.get_available_multipart_form_parts(@form_name, @last_completed_part)
|
331
|
+
results[0][:name].should == :person_info
|
332
|
+
results[1][:name].should == :job_info
|
333
|
+
end
|
334
|
+
|
335
|
+
it "should not return the _update parts" do
|
336
|
+
ActsAsMultipartForm.config.stub!(:show_incomplete_parts).and_return(true)
|
337
|
+
results = @controller.get_available_multipart_form_parts(@form_name, @last_completed_part)
|
338
|
+
results.each do |result|
|
339
|
+
result[:name].should_not == :person_info_update
|
340
|
+
result[:name].should_not == :job_info_update
|
341
|
+
end
|
342
|
+
end
|
343
|
+
|
344
|
+
it "should not return future parts if the config is set to not show incomplete parts" do
|
345
|
+
ActsAsMultipartForm.config.stub!(:show_incomplete_parts).and_return(false)
|
346
|
+
results = @controller.get_available_multipart_form_parts(@form_name, @last_completed_part)
|
347
|
+
results[0][:name].should == :person_info
|
348
|
+
results.each do |result|
|
349
|
+
result[:name].should_not == :job_info
|
350
|
+
end
|
351
|
+
end
|
352
|
+
|
353
|
+
it "should return future parts if the config is set to show incomplete parts" do
|
354
|
+
ActsAsMultipartForm.config.stub!(:show_incomplete_parts).and_return(true)
|
355
|
+
results = @controller.get_available_multipart_form_parts(@form_name, @last_completed_part)
|
356
|
+
results[0][:name].should == :person_info
|
357
|
+
results[1][:name].should == :job_info
|
358
|
+
end
|
359
|
+
end
|
360
|
+
end
|