|
|
#Problem: I'm using a form to generate a csv for booking periods,
#the form is populated from the pricing table which includes booking
#periods and prices -- but the table is updated via csv import, so
#the users can edit prices and periods separately.
#When I render back to the form on failure, I'm getting the existing
#values used to populate the 'edit' form and not the user's changes.
LINK: http://tinyurl.com/35r8qa
#controller. def csv is the method that re-renders form on failure:
class BookingPeriodsController < ApplicationController
def edit
prices = Price.find(:all, :conditions => {:band => 1})
populate_dates(prices)
end
def validate_date
start_date = 'invalid';end_date = 'invalid'
notice = 'you have an invalid date' #bah!
queries = [params[:date_0], params[:date_1]]
dates = [start_date, end_date]
queries.each_with_index do |string,i|
dates[i] = parse_date(string)
end
if dates[0].class == Date && dates[1].class == Date
if dates[0] > dates[1]
notice = 'start date after end date...'
else
notice = 'looks good.'
end
end
dates.map! {|x| x.strftime("%A %d %B %Y") if x.class == Date}
response = {:start_date => dates[0], :end_date => dates[1], :notice => notice}
render :json => response.to_json
end
def find_days
string = params[:period]
query = string.gsub(/in/){}.split(/\s+/)
if query.size > 2
year = query[2]
else
year = Date.today.year
end
month = query[1]
day = query[0].singularize.titleize
start_date = Date.parse("#{month} #{year}")
end_date = start_date.end_of_month
dates0 = DateRanger::Dates.new({:start => start_date, :end => end_date})
dates1 = DateRanger::Dates.new({:start => start_date.advance(:months => 1), :end => end_date.advance(:months => 1)})
@search_month = dates0.every(day)
@next_month = dates1.every(day)
render :layout => false
end
def csv
errors = []
csv = File.new("#{RAILS_ROOT}/lib/csv/periods.csv","w+")
params[:dates].each {|dates|
s = dates[1][:date_0]
e = dates[1][:date_1]
unless s.size < 1 || e.size < 1
start_date = parse_date(s)
end_date = parse_date(e)
error = dates[0].to_i
errors << error if start_date.nil?
errors << error if end_date.nil?
begin;errors << error if start_date > end_date;rescue;end
csv.puts "#{start_date},#{end_date}"
end
}
csv.close
#Rake::Task["bookings:bookables"].invoke
if errors.any?
prices = Price.find(:all, :conditions => {:band => 1})
populate_dates(prices)
flash[:errors] = errors
flash[:notice] = 'There are problems with some of your dates...'
render :action => 'edit'
else
render :text => errors
end
end
private
private
def parse_date(string)
begin
if (string =~ /first|second|third|fourth|last/) == 0
query = string.gsub(/in/){}.downcase.split(/\s+/)
if query[0] == 'last'
week = -1
else
week = DateRanger::ORDINALS.index(query[0])
end
day = query[1].titleize
begin
month = Date.parse("#{query[3]}/#{query[2]}")
range = DateRanger::Dates.new({:start => month, :end => month.end_of_month})
date = range.every(day,week).first
rescue
end
else
date = Date.parse(string)
end
rescue
end
date
end
def populate_dates(values)
@dates = []
year = Date.today.year
values.each do |value|
date = []
if value.start_date.year > year
date << value.start_date.strftime("%d %b %Y")
else
date << value.start_date.strftime("%d %b")
end
if value.end_date.year > year
date << value.end_date.strftime("%d %b %Y")
else
date << value.end_date.strftime("%d %b")
end
@dates << date
end
end
end
#view form partial
#errors
=flash[:notice]
-form_tag({:action => :csv}, :id => 'booking_periods_form') do
%p
=label("start_date","Start date")
%p
=label("end_date","End date")
%p
=submit_tag 'save', :class => 'submit'
-@dates.each_with_index do |date,i|
%span{:class => "#{date_errors(i)} #{cycle('','even')}"}
%p.clear
=text_field_tag("dates[#{i}_range][date_0]", date[0], {:maxlength => 50, :size => 22, :class => 'start'})
%p
=text_field_tag("dates[#{i}_range][date_1]", date[1], {:maxlength => 50, :size => 22, :class => 'end'})
-10.times do |i|
%span{:class => "#{date_errors(i+@dates.size)} #{cycle('','even')}"}
%p.clear
=text_field_tag("dates[#{i+@dates.size}_range][date_0]",nil, {:maxlength => 50, :size => 22, :class => 'start'})
%p
=text_field_tag("dates[#{i+@dates.size}_range][date_1]",nil, {:maxlength => 50, :size => 22, :class => 'end'})
|