Skip to content

Commit

Permalink
not sure what i'm even doing here.
Browse files Browse the repository at this point in the history
  • Loading branch information
ajb committed Jan 21, 2014
1 parent 0740afd commit 57a178e
Show file tree
Hide file tree
Showing 15 changed files with 315 additions and 324 deletions.
5 changes: 5 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ GEM
simplecov-html (~> 0.7.1)
simplecov-html (0.7.1)
slop (3.4.6)
spring (1.1.0)
spring-commands-rspec (1.0.1)
spring (>= 0.9.1)
sprockets (2.10.1)
hike (~> 1.2)
multi_json (~> 1.0)
Expand Down Expand Up @@ -199,5 +202,7 @@ DEPENDENCIES
launchy
rinku
rspec-rails
spring
spring-commands-rspec
terminal-notifier-guard
thin
6 changes: 6 additions & 0 deletions app/models/formbuilder/response_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,15 @@ def normalize_response(value, all_responses); end;

def validate_response(value); end;

def before_destroy(entry); end;

def options_array
Array(self.field_options['options']).map { |o| o['label'] }
end

def sortable_value(value)
value[0..10] # do we really need to sort more than the first 10 characters of a string?
end

end
end
4 changes: 4 additions & 0 deletions app/models/formbuilder/response_field_address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ def audit_response(value, all_responses)
end
end

def sortable_value(value)
"#{value['street']} #{value['city']} #{value['state']} #{value['zipcode']} #{value['country']}"
end

private
def country_options(selected_country)
all_countries.map do |k, v|
Expand Down
10 changes: 10 additions & 0 deletions app/models/formbuilder/response_field_checkboxes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,15 @@ def render_entry(value, opts = {})
"""
end

def sortable_value(value)
nil # see :normalize_response for override
end

def normalize_response(value, all_responses)
options_array.each do |option_label|
all_responses["#{self.id}_sortable_values_#{option_label}"] = value[option_label]
end
end

end
end
5 changes: 5 additions & 0 deletions app/models/formbuilder/response_field_date.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,10 @@ def validate_response(value)
end
end

def sortable_value(value)
['year', 'month', 'day'].each { |x| return 0 unless value[x].try(:present?) }
DateTime.new(value['year'].to_i, value['month'].to_i, value['day'].to_i).to_i rescue 0
end

end
end
8 changes: 8 additions & 0 deletions app/models/formbuilder/response_field_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,13 @@ def audit_response(value, all_responses)
all_responses["#{self.id}_filename"] = record.read_attribute(:upload)
end

def sortable_value(value)
value ? 1 : 0
end

def before_destroy(entry)
entry.remove_entry_attachments(entry.responses[self.id.to_s])
end

end
end
4 changes: 4 additions & 0 deletions app/models/formbuilder/response_field_price.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,9 @@ def validate_response(value)
end
end

def sortable_value(value)
"#{value['dollars'] || '0'}.#{value['cents'] || '0'}".to_f
end

end
end
6 changes: 6 additions & 0 deletions app/models/formbuilder/response_field_time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,11 @@ def validate_response(value)
end
end

def sortable_value(value)
hours = value['hours'].to_i
hours += 12 if value['am_pm'] && value['am_pm'] == 'PM'
(hours*60*60) + (value['minutes'].to_i * 60) + value['seconds'].to_i
end

end
end
1 change: 1 addition & 0 deletions config/spring.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Spring.application_root = './spec/dummy'
2 changes: 2 additions & 0 deletions formbuilder-rb.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ Gem::Specification.new do |s|
s.add_development_dependency 'guard-rspec'
s.add_development_dependency 'launchy'
s.add_development_dependency 'rspec-rails'
s.add_development_dependency 'spring'
s.add_development_dependency 'spring-commands-rspec'
s.add_development_dependency 'terminal-notifier-guard'
s.add_development_dependency 'thin'
s.add_development_dependency 'coveralls'
Expand Down
117 changes: 29 additions & 88 deletions lib/formbuilder/entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,22 @@ def save_response(raw_value, response_field, response_field_params = {})
values

when "file"
# if the file is already uploaded and we're not uploading another,
# be sure to keep it
# if the file is already uploaded and we're not uploading another, be sure to keep it
if raw_value.blank?
if old_responses && old_responses[response_field.id.to_s]
old_responses[response_field.id.to_s]
end
old_responses.try(:[], response_field.id.to_s)
else
remove_entry_attachment(responses[response_field.id.to_s]) if responses
attachment = EntryAttachment.create(upload: raw_value)
attachment.id
remove_entry_attachments(responses.try(:[], response_field.id.to_s))
EntryAttachment.create(upload: raw_value).id
end
# when "filebucket"
# # if the file is already uploaded and we're not uploading another, be sure to keep it
# if raw_value.blank?
# old_responses.try(:[], response_field.id.to_s)
# else
# remove_entry_attachments(Array(responses.try(:[], response_field.id.to_s)))
# EntryAttachment.create(upload: raw_value).id
# end

when "radio"
# Save 'other' value
responses["#{response_field.id}_other"] = raw_value == 'Other' ?
Expand All @@ -129,64 +134,42 @@ def save_response(raw_value, response_field, response_field_params = {})
calculate_sortable_value(response_field, value)
end

self.responses_will_change! # hack to make sure column is marked as dirty
self.responses_will_change!
end

def destroy_response(response_field)
case response_field.field_type
when "file"
self.remove_entry_attachment(responses[response_field.id.to_s])
end

response_field.before_destroy(self)
id = response_field.id.to_s
new_responses = self.responses.reject { |k, v| k.in?([id, "#{id}_sortable_value"]) }
self.responses = new_responses

self.responses_will_change! # hack to make sure column is marked as dirty
self.responses = self.responses.reject { |k, v| k.in?([id, "#{id}_sortable_value"]) }
self.responses_will_change!
end

def remove_entry_attachment(entry_attachment_id)
return unless entry_attachment_id.present?
EntryAttachment.where(id: entry_attachment_id).first.try(:destroy)
def remove_entry_attachments(entry_attachment_ids)
return unless entry_attachment_ids.present?

entry_attachment_ids.to_s.split(',').each do |x|
EntryAttachment.find_by(id: x).try(:destroy)
end
end

def error_for(response_field)
(self.errors.messages[:"responses_#{response_field.id}"] || [])[0]
Array(self.errors.messages[:"responses_#{response_field.id}"]).first
end

def calculate_responses_text
return unless self.respond_to?(:"responses_text=")
selected_responses = self.responses.select { |k, v| Integer(k) rescue nil }
self.responses_text = selected_responses.values.join(' ')
self.responses_text = self.responses.select { |k, v| Integer(k) rescue nil }.values.join(' ')
end

# useful when migrating
def calculate_sortable_values
response_fieldable.input_fields.each do |response_field|
calculate_sortable_value(response_field, response_value(response_field))
end

self.responses_will_change! # hack to make sure column is marked as dirty
end

def calculate_additional_info
response_fieldable.input_fields.each do |response_field|
value = response_value(response_field)
next unless value.present?

case response_field.field_type
when 'address'
begin
coords = Geocoder.coordinates("#{value['street']} #{value['city']} #{value['state']} " +
"#{value['zipcode']} #{value['country']}")
self.responses["#{response_field.id}_x"] = coords[0]
self.responses["#{response_field.id}_y"] = coords[1]
rescue
self.responses["#{response_field.id}_x"] = nil
self.responses["#{response_field.id}_y"] = nil
end
if (x = response_value(response_field)).present?
self.responses["#{response_field.id}_sortable_value"] = response_field.sortable_value(x)
end
end

self.responses_will_change!
end

# Normalizations get run before validation.
Expand All @@ -209,47 +192,5 @@ def audit_responses
self.responses_will_change!
end

def audit_responses!
audit_responses
self.save(validate: false)
end

def normalize_responses!
normalize_responses
self.save(validate: false)
end

def calculate_sortable_value(response_field, value)
return unless value.present?

self.responses["#{response_field.id}_sortable_value"] = case response_field.field_type
when "date"
['year', 'month', 'day'].each { |x| return 0 unless value[x] && !value[x].blank? }
DateTime.new(value['year'].to_i, value['month'].to_i, value['day'].to_i).to_i rescue 0
when "time"
hours = value['hours'].to_i
hours += 12 if value['am_pm'] && value['am_pm'] == 'PM'
(hours*60*60) + (value['minutes'].to_i * 60) + value['seconds'].to_i
when "file"
value ? 1 : 0
when "checkboxes"
calculate_sortable_value_for_checkboxes(response_field, value)
return nil
when "price"
"#{value['dollars'] || '0'}.#{value['cents'] || '0'}".to_f
when "address"
"#{value['street']} #{value['city']} #{value['state']} #{value['zipcode']} #{value['country']}"
else
# do we really need to sort more than the first 10 characters of a string?
value[0..10]
end
end

def calculate_sortable_value_for_checkboxes(response_field, value)
(response_field.field_options['options'] || []).each do |option|
self.responses["#{response_field.id}_sortable_values_#{option['label']}"] = value[option['label']]
end
end

end
end
2 changes: 1 addition & 1 deletion spec/dummy/app/controllers/test_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def show_form

def post_form
@entry.save_responses(params[:response_fields], @form.response_fields.not_admin_only)
@entry.save(validate: false)
@entry.save(skip_validation: true)
redirect_to :back
end

Expand Down
2 changes: 1 addition & 1 deletion spec/features/submitting_an_entry_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

subject { page }
let!(:form) { FactoryGirl.create(:kitchen_sink_form) }
let!(:entry) { e = Entry.new(form: form); e.save(validate: false); e }
let!(:entry) { e = Entry.new(form: form); e.save(skip_validation: true); e }

before do
visit test_form_path(form.id, entry.id)
Expand Down
Loading

0 comments on commit 57a178e

Please sign in to comment.