Permalink
Browse files

fixed for new address system

  • Loading branch information...
1 parent fdd4338 commit 98caaa7ba34f25f4dd1ba7e6d0e049df00c033be @damianogiacomello damianogiacomello committed Jul 30, 2013
@@ -1 +1,81 @@
-//= require admin/spree_core
+//= require jquery.validate/jquery.validate.min
+
+(function($){
+ $(document).ready(function(){
+ if($('.user_form_address').is('*')){
+
+ $('.user_form_address').validate();
+
+ var get_states = function(region){
+ country = $('div#' + region + 'country' + ' span#' + region + 'country-selection :only-child').val();
+ return state_mapper[country];
+ }
+
+ var update_state = function(region) {
+ states = get_states(region);
+
+ state_select = $('div#' + region + 'state select');
+ state_input = $('div#' + region + 'state input');
+
+ if(states) {
+ selected = parseInt(state_select.val());
+ state_select.html('');
+ states_with_blank = [["",""]].concat(states);
+ $.each(states_with_blank, function(pos,id_nm) {
+ var opt = $(document.createElement('option'))
+ .attr('value', id_nm[0])
+ .html(id_nm[1]);
+ if(selected==id_nm[0]){
+ opt.prop("selected", true);
+ }
+ state_select.append(opt);
+ });
+ state_select.prop("disabled", false).show();
+ state_input.hide().prop("disabled", true);
+
+ } else {
+ state_input.prop("disabled", false).show();
+ state_select.hide().prop("disabled", true);
+ }
+
+ };
+
+ $('div#bcountry select').change(function() { update_state('b'); });
+ $('div#scountry select').change(function() { update_state('s'); });
+ update_state('b');
+ update_state('s');
+
+ $('input#user_save_bill').click(function() {
+ if (($(this)).is(':checked')) {
+ $('#billing .inner').show();
+ $('#billing .inner input, #billing .inner select').prop('disabled', false);
+ if (get_states('s')) {
+ return $('span#bstate input').hide().prop('disabled', true);
+ } else {
+ return $('span#bstate select').hide().prop('disabled', true);
+ }
+ } else {
+ $('#billing .inner').hide();
+ return $('#billing .inner input, #billing .inner select').prop('disabled', true);
+ }
+ }).triggerHandler('click');
+
+ $('input#user_save_ship').click(function() {
+ if (($(this)).is(':checked')) {
+ $('#shipping .inner').show();
+ $('#shipping .inner input, #shipping .inner select').prop('disabled', false);
+ if (get_states('s')) {
+ return $('span#bstate input').hide().prop('disabled', true);
+ } else {
+ return $('span#bstate select').hide().prop('disabled', true);
+ }
+ } else {
+ $('#shipping .inner').hide();
+ return $('#shipping .inner input, #shipping .inner select').prop('disabled', true);
+ }
+ }).triggerHandler('click');
+
+ }
+
+ });
+})(jQuery);
@@ -1,3 +1 @@
-/*
- *= require admin/spree_core
-*/
+
@@ -1,10 +1,15 @@
Spree::Address.class_eval do
attr_accessible :tax_id_number, :vat_number
+ validates_format_of :tax_id_number, :with => /^[A-Z]{6}[A-Z0-9]{2}[A-Z][A-Z0-9]{2}[A-Z][A-Z0-9]{3}[A-Z]$/i, :if => :fiscal_code_present?
validates :vat_number, :length => {:maximum => 11}, :if => Proc.new {|a| !a.vat_number.blank? }
- validates :tax_id_number, :length => {:in => 11..16}, :if => Proc.new {|a| !a.tax_id_number.blank? }
+ validates :tax_id_number, :length => {:in => 11..16}, :if => :fiscal_code_present?
def empty?
attributes.except('id', 'created_at', 'updated_at', 'order_id', 'country_id', 'tax_id_number', 'vat_number').all? { |_, v| v.nil? }
end
+
+ def fiscal_code_present?
+ !tax_id_number.blank?
+ end
end
@@ -1,14 +1,12 @@
Spree::User.class_eval do
- attr_accessible :use_billing, :bill_address_attributes, :ship_address_attributes,
- :ship_address, :bill_address, :privacy
+ attr_accessible :bill_address_attributes, :ship_address_attributes,
+ :ship_address, :bill_address, :privacy, :save_ship, :save_bill
- attr_accessor :use_billing, :delete_ship_address, :delete_bill_address
+ attr_accessor :save_ship, :save_bill, :delete_ship_address, :delete_bill_address
accepts_nested_attributes_for :bill_address, :allow_destroy => true
accepts_nested_attributes_for :ship_address, :allow_destroy => true
- before_validation :clone_billing_address, :if => :use_billing?
-
after_update :removed_bill_address, :if => :delete_bill?
after_update :removed_ship_address, :if => :delete_ship?
@@ -17,8 +15,10 @@
def update_with_password(params = {})
params.delete(:password) if params[:password].blank?
params.delete(:password_confirmation) if params[:password_confirmation].blank?
- params.delete(:bill_address_attributes) if empty_address?(params[:bill_address_attributes])
- params.delete(:ship_address_attributes) if empty_address?(params[:ship_address_attributes])
+ if params[:bill_address_attributes] || params[:ship_address_attributes]
+ params.delete(:bill_address_attributes) if empty_address?(params[:bill_address_attributes])
+ params.delete(:ship_address_attributes) if empty_address?(params[:ship_address_attributes])
+ end
update_attributes(params)
end
@@ -29,15 +29,6 @@ def privacy_required?
end
end
- def clone_billing_address
- if bill_address and self.ship_address.nil?
- self.ship_address = bill_address.clone
- else
- self.ship_address.attributes = bill_address.attributes.except('id', 'updated_at', 'created_at')
- end
- true
- end
-
def removed_ship_address
if self.ship_address
self.ship_address.destroy
@@ -49,10 +40,6 @@ def removed_bill_address
self.bill_address.destroy
end
end
-
- def use_billing?
- @use_billing == true || @use_billing == "true" || @use_billing == "1"
- end
def delete_bill?
@delete_bill_address == true || @delete_bill_address == "true" || @delete_bill_address == "1"
@@ -4,7 +4,7 @@ class InstallGenerator < Rails::Generators::Base
def add_javascripts
append_file "app/assets/javascripts/store/all.js", "//= require store/spree_address_book\n"
- #append_file "app/assets/javascripts/admin/all.js", "//= require admin/spree_address_book\n"
+ append_file "app/assets/javascripts/admin/all.js", "//= require admin/spree_address_book\n"
end
def add_stylesheets

0 comments on commit 98caaa7

Please sign in to comment.