Created
September 30, 2010 20:51
-
-
Save sbeam/605301 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Spree::DropdownVariantsByOption::OrdersController | |
JS_ESCAPE_MAP = { '\\' => '\\\\', '</' => '<\/', "\r\n" => '\n', "\n" => '\n', "\r" => '\n', '"' => '\\"', "'" => "\\'" } | |
def self.included(target) | |
target.class_eval do | |
create.before << :add_variants_from_option_values | |
create.rescues ActiveRecord::RecordNotFound | |
create.wants.js | |
create.failure.wants.js { render :js => "alert('#{format_js_error}')" } | |
end | |
end | |
private | |
def add_variants_from_option_values | |
quantity = params[:quantity].to_i | |
return if params[:option_values_primary].nil? or quantity < 1 | |
# Combine primary and non-primary option values to locate the correct variant | |
option_values = params[:option_values_primary].merge(params[:option_values] || {}) | |
if option_values and params[:product_id] | |
@variant = Variant.find_by_option_values(params[:product_id], option_values) | |
logger.debug ">>>>>>>>>>>>>>>>>>>>>>>>"[email protected] | |
raise t('not_all_options_specified') if @variant.length > 1 | |
@object.dropdown_variants_by_option_add_variant(@variant.first, quantity) | |
end | |
end | |
def format_js_error | |
message = "The following error(s) have occurred when trying to add the item to the cart: " | |
@order.errors.each_full {|m| message += "#{m} "} | |
message.gsub(/(\\|<\/|\r\n|[\n\r"'])/) { JS_ESCAPE_MAP[$1] } | |
message | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment