#Ajax e-book submission
def ebook_submission
@product = Product.find_by_name(params[:name])
if request.xhr?
#Okay, we've got an ajax request
#First we'll 'validate' our input...
if !params[:customer_name].nil? and params[:email].match(/^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/)
#Alright, we have a valid e-mail and the name field isn't blank - let's post to 1sc
url = URI.parse('http://www.mcssl.com/app/contactsave.asp')
req = Net::HTTP::Post.new(url.path)
req.set_form_data("merchantid" => "101156",
"ARThankyouURL" => "http://#{request.env['HTTP_X_FORWARDED_SERVER']}/ebook_thanks",
"copyarresponse" => "1",
"custom" => "0",
"defaultar" => "#{@product.ebook_autoresponder}",
"allowmulti" => "0",
"visiblefields" => "Name,Email1",
"requiredfields" => "Email1",
"Name" => params[:customer_name],
"Email1" => params[:email]
)
res = Net::HTTP.new(url.host, url.port).start {|http| http.request(req) }
logger.info(res.body)
case res
when Net::HTTPRedirection
render :update do |page|
page << "$('ebook_form').hide();"
page << "$('ebook_response').show();"
end
else
#not sure what the hell went wrong... render an error
page.replace_html "ebook_topper", "We're Sorry!
There Has Been An Error."
page.replace_html "ebook_title", "Please try again later."
end
else
render :update do |page|
page.replace_html "ebook_topper", "We're Sorry!
There Has Been An Error."
page.replace_html "ebook_title", "You must enter
A name and
valid e-mail!"
end
end
end
#Some shipping code...
def mark_as_shipped
#By default, all orders should be marked as 'to be shipped'
orders_to_ship = Array.new
for k,v in params[:to_be_shipped]
if v == "yes"
orders_to_ship << Order.find(k)
end
end
orders_to_ship.each {|order|
#Set the order status code appropriately, but we won't do so yet!
order.shipped_on = Time.now
order.order_status_code_id = 5
for line_item in order.line_items
line_item.reduce_inventory
end
order.save!
logger.info("Order #{order.order_number} HAS BEEN MARKED AS SHIPPED")
}
@orders = orders_to_ship
end
end
def update_offer
@product = Product.find_by_name(params[:name])
@offer = Offer.find(params[:id])
if @offer.update_attributes(params[:offer])
expire_fragment(@product.name.to_s) unless !read_fragment(@product.name.to_s)
responds_to_parent do
render :update do |page|
page.replace_html "offer_#{params[:id]}_row", {:partial => 'offers/row', :locals => {:offer => @offer, :product => @product}}
page.replace_html 'offers_flash_notice', "The offering has been successfully updated."
page.delay(1) do page.visual_effect :appear, "offers_flash_notice" end
page.delay(1) do page.visual_effect :highlight, 'offers_flash_notice' end
page.delay(2) do page.visual_effect :fade, 'offers_flash_notice' end
end
end
else
respond_to_parent do
render :update do |page|
for error in @offer.errors
page << "new Effect.Pulsate($$('#offer_#{@offer.id}_row form table tbody tr td #offer_#{error[0]}_notice')[0].update('This field #{error[1]}'), {pulses: 2});"
end
end
end
end
end
#Add a testimonial, nifty custom error checking...
def add_testimonial
@product = Product.find_by_name(params[:name])
@testimonial = Testimonial.new(params[:testimonial])
@testimonial.product = @product
if @testimonial.valid?
@testimonial.save!
expire_fragment(@product.name.to_s) unless !read_fragment(@product.name.to_s)
responds_to_parent do
render :update do |page|
page << "$('create_new_test').innerHTML = ''"
page[:no_tests].hide unless @product.testimonials.length > 1
page.visual_effect :slide_up, 'new_test', :queue => 'end'
page.insert_html :bottom, 'tests_list', {:partial => 'testimonials/row', :locals => { :test => @testimonial, :product => @product, :with_row => "yes" } }
page.replace_html 'tests_flash_notice', "Your testimonial has been successfully created."
page.delay(1) do page.visual_effect :appear, "tests_flash_notice" end
page.delay(1) do page.visual_effect :highlight, 'tests_flash_notice' end
page.delay(2) do page.visual_effect :fade, 'tests_flash_notice' end
page << "$('create_new_test').innerHTML = 'Create A Testimonial'"
page << "Form.reset('new_test_form');"
end
end
else
responds_to_parent do
render :update do |page|
for error in @testimonial.errors
page << "new Effect.Pulsate($$('#new_test form table tbody tr td #testimonial_#{error[0]}_notice')[0].update('This field #{error[1]}'), {pulses: 2});"
end
end
end
end
end
#self-referential has-and-belongs-to-many relationships with single table inheritance (yahrly)
class SubProduct < ActiveRecord::Base
belongs_to :product
belongs_to :virtual_product
after_save :update_parent_quantity, :update_parent_status
after_update :update_parent_quantity, :update_parent_status
after_destroy :update_parent_quantity, :update_parent_status
def update_parent_quantity
#Cater to the lowest common denominator for virtual product quantities...
#I.E. if we contain 2 of ProductX which has 600 in stock,
#and 3 of ProductY which has 30 in stock, we effectively only have
#10 of this virtual item in stock
the_numerology_quiz = Array.new
for sub_product in self.virtual_product.sub_products
the_products_quantity = sub_product.product.quantity
the_number_contained = sub_product.number_contained
our_magic_number = the_products_quantity / the_number_contained
the_numerology_quiz << our_magic_number
end
self.virtual_product.quantity = the_numerology_quiz.min
self.virtual_product.update_without_callbacks
end
def update_parent_status
#alright, werd, the parent exists, let's get some work done
#instantiate a variable in the outer loop
the_unconfirmed_subproducts = Array.new
for sub_product in self.virtual_product.sub_products
if sub_product.product.product_status_code_id < 2
the_unconfirmed_subproducts << sub_product
end
end
if the_unconfirmed_subproducts.length > 0
#We have unconfirmed subproducts. Set the parent status code to 1, save it forcibly, and cease
#execution.
self.virtual_product.product_status_code_id = 1
self.virtual_product.update_without_callbacks
else
self.virtual_product.product_status_code_id = 2
end
self.virtual_product.update_weight
end
def update_parent
# This will be called from the inner loop of an iterator on @product.sub_products
# as an after_save/update callback. Or maybe some other way, who knows?
end
end