acts_as_multipart_form 0.0.13 → 0.0.14
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +4 -0
- data/README.rdoc +2 -1
- data/VERSION +1 -1
- data/acts_as_multipart_form.gemspec +2 -2
- data/app/models/multipart_form/in_progress_form.rb +2 -1
- data/lib/acts_as_multipart_form/multipart_form_in_controller.rb +20 -13
- data/spec/in_progress_form_spec.rb +2 -1
- data/spec/multipart_form_in_controller_spec.rb +16 -17
- metadata +3 -3
data/CHANGELOG
CHANGED
data/README.rdoc
CHANGED
@@ -55,7 +55,8 @@ This is currently only used for validations. If this is set, you can use a vali
|
|
55
55
|
== Add the route
|
56
56
|
|
57
57
|
resources :people do
|
58
|
-
match 'edit', to: 'people#hire_form', :via => [:get, :post]
|
58
|
+
match 'edit/:id', to: 'people#hire_form', :via => [:get, :post]
|
59
|
+
match 'new', to: 'people#hire_form', :via => [:get, :post] # optional
|
59
60
|
|
60
61
|
== Setting up your form
|
61
62
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.14
|
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "acts_as_multipart_form"
|
8
|
-
s.version = "0.0.
|
8
|
+
s.version = "0.0.14"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Jeremiah Hemphill", "Ethan Pemble"]
|
12
|
-
s.date = "2012-05-
|
12
|
+
s.date = "2012-05-16"
|
13
13
|
s.description = "Multipart forms using custom routes"
|
14
14
|
s.email = "jeremiah@cloudspace.com"
|
15
15
|
s.extra_rdoc_files = [
|
@@ -14,7 +14,8 @@ module MultipartForm
|
|
14
14
|
|
15
15
|
attr_accessible :form_subject, :form_subject_id, :form_subject_type, :form_name, :last_completed_step, :completed
|
16
16
|
|
17
|
-
validates_presence_of :
|
17
|
+
validates_presence_of :form_subject_id
|
18
|
+
validates_presence_of :form_subject_type
|
18
19
|
validates_presence_of :form_name
|
19
20
|
validates_presence_of :last_completed_step
|
20
21
|
validates_inclusion_of :completed, :in => [true, false]
|
@@ -93,7 +93,7 @@ module ActsAsMultipartForm
|
|
93
93
|
def load_multipart_form_index_links(form_name, form_subjects)
|
94
94
|
@multipart_form_index_parts = {}
|
95
95
|
form_subjects.each do |form_subject|
|
96
|
-
in_progress_form =
|
96
|
+
in_progress_form = find_or_initialize_multipart_in_progress_form(form_name, form_subject)
|
97
97
|
@multipart_form_index_parts[form_subject.id] = {}
|
98
98
|
@multipart_form_index_parts[form_subject.id][:parts] = get_available_multipart_form_parts(form_name, in_progress_form.last_completed_step)
|
99
99
|
@multipart_form_index_parts[form_subject.id][:completed] = in_progress_form.completed
|
@@ -110,10 +110,10 @@ module ActsAsMultipartForm
|
|
110
110
|
form_name = params[:action].to_sym
|
111
111
|
form_subject_id = params[:id]
|
112
112
|
|
113
|
-
@form_subject =
|
113
|
+
@form_subject = find_or_initialize_multipart_form_subject(form_name, form_subject_id)
|
114
114
|
params[:id] = @form_subject.id
|
115
115
|
|
116
|
-
in_progress_form =
|
116
|
+
in_progress_form = find_or_initialize_multipart_in_progress_form(form_name, @form_subject)
|
117
117
|
|
118
118
|
# set the part based on the params or in progress form
|
119
119
|
if params[:multipart_form_part]
|
@@ -131,7 +131,12 @@ module ActsAsMultipartForm
|
|
131
131
|
if(part.match(/_update$/))
|
132
132
|
if(result && result[:valid])
|
133
133
|
completed = redirect_to_next_multipart_form_part(form_name, @form_subject, part)
|
134
|
-
in_progress_form
|
134
|
+
# added form_subject_id in case it was not set when the in_progress_form was created (JH 5-15-2012)
|
135
|
+
# this would happen on a new page, but not an edit page
|
136
|
+
in_progress_form.update_attributes({
|
137
|
+
:form_subject_id => @form_subject.id,
|
138
|
+
:last_completed_step => part,
|
139
|
+
:completed => completed })
|
135
140
|
else
|
136
141
|
# render the previous page but stay on this page so we keep the errors
|
137
142
|
part = get_previous_multipart_form_part(form_name, part)
|
@@ -231,39 +236,41 @@ module ActsAsMultipartForm
|
|
231
236
|
# Given a form name and a form subject id, it creates the form subject
|
232
237
|
# The form subject is defined by the id and the multipart form's model attribute
|
233
238
|
#
|
234
|
-
# The subject is
|
235
|
-
# This is not a good situation but I don't know how to ensure the form subject gets saved otherwise
|
239
|
+
# The subject is initialized with no values and it is not saved
|
236
240
|
#
|
237
241
|
# @param [Symbol] form_name The name of the multipart form
|
238
242
|
# @param [Integer] form_dubject_id The id of the form subject (could be nil)
|
239
|
-
# @return [FormSubject] The form subject
|
240
|
-
def
|
243
|
+
# @return [FormSubject] The form subject in the database or a new form subject instance
|
244
|
+
def find_or_initialize_multipart_form_subject(form_name, form_subject_id)
|
241
245
|
# find or create the form subject
|
242
246
|
model = self.multipart_forms[form_name][:model]
|
243
247
|
if form_subject_id
|
244
248
|
form_subject = model.constantize.find(form_subject_id)
|
245
249
|
else
|
246
250
|
form_subject = model.constantize.new()
|
247
|
-
form_subject.save(:validate => false)
|
248
251
|
end
|
249
252
|
return form_subject
|
250
253
|
end
|
251
254
|
|
252
255
|
# Returns the InProgressForm object when given the form name and subject
|
253
|
-
#
|
256
|
+
#
|
257
|
+
# Initializes but does not asve the InPorgressForm object if it doesn't exist for the given form name/form subject pair
|
254
258
|
#
|
255
259
|
# @param [Symbol] form_name The name of the multipart form
|
256
260
|
# @param [FormSubject] form_subject The multipart form's subject
|
257
261
|
# @returns [InProgressForm] The associated or new in progress form object
|
258
|
-
def
|
262
|
+
def find_or_initialize_multipart_in_progress_form(form_name, form_subject)
|
259
263
|
# find or create the in progress form
|
260
264
|
# not sure why the polymorphic relationship isn't working here
|
261
265
|
in_progress_form = MultipartForm::InProgressForm.where(
|
262
266
|
:form_subject_id => form_subject.id,
|
263
267
|
:form_subject_type => form_subject.class.to_s,
|
264
268
|
:form_name => form_name.to_s).first
|
265
|
-
|
266
|
-
|
269
|
+
|
270
|
+
# if the form subject is a new_record, in_progress_form should be nil
|
271
|
+
# trying to stop weird edge cases from killing me (JH 5-15-2012)
|
272
|
+
if form_subject.new_record? || !in_progress_form
|
273
|
+
in_progress_form = MultipartForm::InProgressForm.new(
|
267
274
|
:form_subject_id => form_subject.id,
|
268
275
|
:form_subject_type => form_subject.class.to_s,
|
269
276
|
:form_name => form_name.to_s,
|
@@ -25,7 +25,8 @@ describe MultipartForm::InProgressForm do
|
|
25
25
|
end
|
26
26
|
|
27
27
|
it "should require a form subject" do
|
28
|
-
@ip_form.should have(1).error_on(:
|
28
|
+
@ip_form.should have(1).error_on(:form_subject_id)
|
29
|
+
@ip_form.should have(1).error_on(:form_subject_type)
|
29
30
|
end
|
30
31
|
|
31
32
|
it "should require a form name" do
|
@@ -155,11 +155,11 @@ describe ActsAsMultipartForm::MultipartFormInController do
|
|
155
155
|
@ipf = mock_model(MultipartForm::InProgressForm)
|
156
156
|
@ipf.stub!(:last_completed_step)
|
157
157
|
@ipf.stub!(:completed).and_return(true)
|
158
|
-
@controller.stub!(:
|
158
|
+
@controller.stub!(:find_or_initialize_multipart_in_progress_form).and_return(@ipf)
|
159
159
|
end
|
160
160
|
|
161
|
-
it "should call
|
162
|
-
@controller.should_receive(:
|
161
|
+
it "should call find_or_initialize_multipart_in_progress_form" do
|
162
|
+
@controller.should_receive(:find_or_initialize_multipart_in_progress_form)
|
163
163
|
@controller.load_multipart_form_index_links(@form_name, @form_subjects)
|
164
164
|
end
|
165
165
|
|
@@ -216,8 +216,8 @@ describe ActsAsMultipartForm::MultipartFormInController do
|
|
216
216
|
:in_progress_form_id => ipf.id
|
217
217
|
}
|
218
218
|
@controller.stub!(:params).and_return(params)
|
219
|
-
@controller.stub!(:
|
220
|
-
@controller.stub!(:
|
219
|
+
@controller.stub!(:find_or_initialize_multipart_form_subect)
|
220
|
+
@controller.stub!(:find_or_initialize_multipart_in_progress_form).and_return(ipf)
|
221
221
|
@controller.should_receive(:job_info)
|
222
222
|
@controller.multipart_form_handler
|
223
223
|
end
|
@@ -240,16 +240,16 @@ describe ActsAsMultipartForm::MultipartFormInController do
|
|
240
240
|
}
|
241
241
|
@controller.stub!(:params).and_return(params)
|
242
242
|
ipf = MultipartForm::InProgressForm.new
|
243
|
-
@controller.stub!(:
|
243
|
+
@controller.stub!(:find_or_initialize_multipart_in_progress_form).and_return(ipf)
|
244
244
|
@controller.stub!(:person_info_update).and_return({:valid => true})
|
245
245
|
@controller.stub!(:redirect_to_next_multipart_form_part).and_return(false)
|
246
|
-
ipf.should_receive(:update_attributes).with(:completed => false, :last_completed_step => :person_info_update)
|
246
|
+
ipf.should_receive(:update_attributes).with(:form_subject_id => nil, :completed => false, :last_completed_step => :person_info_update)
|
247
247
|
|
248
248
|
@controller.multipart_form_handler
|
249
249
|
end
|
250
250
|
end
|
251
251
|
|
252
|
-
describe "
|
252
|
+
describe "find_or_initialize_multipart_form_subject method" do
|
253
253
|
before(:each) do
|
254
254
|
@controller = PeopleController.new
|
255
255
|
@controller.multipart_forms[:hire_form][:model] = "Person"
|
@@ -257,23 +257,22 @@ describe ActsAsMultipartForm::MultipartFormInController do
|
|
257
257
|
|
258
258
|
it "should find the form subject on a valid id" do
|
259
259
|
Person.should_receive(:find)
|
260
|
-
@controller.
|
260
|
+
@controller.find_or_initialize_multipart_form_subject(:hire_form, 1)
|
261
261
|
end
|
262
262
|
|
263
263
|
it "should create the form subject if there is no id given" do
|
264
264
|
p = mock_model(Person)
|
265
265
|
Person.should_receive(:new).and_return(p)
|
266
|
-
|
267
|
-
@controller.find_or_create_multipart_form_subject(:hire_form, nil)
|
266
|
+
@controller.find_or_initialize_multipart_form_subject(:hire_form, nil)
|
268
267
|
end
|
269
268
|
|
270
269
|
it "should return an error or nil if the id is invalid" do
|
271
270
|
Person.should_receive(:find).and_return(nil)
|
272
|
-
@controller.
|
271
|
+
@controller.find_or_initialize_multipart_form_subject(:hire_form, 1).should be_nil
|
273
272
|
end
|
274
273
|
end
|
275
274
|
|
276
|
-
describe "
|
275
|
+
describe "find_or_initialize_multipart_in_progress_form method" do
|
277
276
|
before(:each) do
|
278
277
|
@controller = PeopleController.new
|
279
278
|
@form_subject = mock_model(Person)
|
@@ -282,17 +281,17 @@ describe ActsAsMultipartForm::MultipartFormInController do
|
|
282
281
|
|
283
282
|
it "should find the in progress form if it exists" do
|
284
283
|
MultipartForm::InProgressForm.should_receive(:where).and_return([@ipf])
|
285
|
-
@controller.
|
284
|
+
@controller.find_or_initialize_multipart_in_progress_form(:hire_form, @form_subject).should == @ipf
|
286
285
|
end
|
287
286
|
|
288
287
|
it "should create an in progress form if it doesn't exist" do
|
289
|
-
MultipartForm::InProgressForm.should_receive(:
|
290
|
-
@controller.
|
288
|
+
MultipartForm::InProgressForm.should_receive(:new).and_return(@ipf)
|
289
|
+
@controller.find_or_initialize_multipart_in_progress_form(:hire_form, @form_subject).should == @ipf
|
291
290
|
end
|
292
291
|
|
293
292
|
# maybe refactor so it doesn't hit the database
|
294
293
|
it "should create an in progress form with a last_completed_step initially set to 'none'" do
|
295
|
-
@controller.
|
294
|
+
@controller.find_or_initialize_multipart_in_progress_form(:hire_form, @form_subject).last_completed_step.should == "none"
|
296
295
|
end
|
297
296
|
end
|
298
297
|
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: acts_as_multipart_form
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.0.
|
5
|
+
version: 0.0.14
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Jeremiah Hemphill
|
@@ -11,7 +11,7 @@ autorequire:
|
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
13
|
|
14
|
-
date: 2012-05-
|
14
|
+
date: 2012-05-16 00:00:00 Z
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
name: rails
|
@@ -258,7 +258,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
258
258
|
requirements:
|
259
259
|
- - ">="
|
260
260
|
- !ruby/object:Gem::Version
|
261
|
-
hash:
|
261
|
+
hash: 1464765102833044920
|
262
262
|
segments:
|
263
263
|
- 0
|
264
264
|
version: "0"
|