From 8c322c1a0f4777a1724ff6b7eb0c34ccbc036e66 Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Tue, 1 Sep 2020 13:49:17 +0100 Subject: [PATCH 01/12] Move shared address_form partial to the only place where it is used, order customer_details --- .../customer_details}/_address_form.html.erb | 0 app/views/spree/admin/orders/customer_details/_form.html.haml | 4 ++-- 2 files changed, 2 insertions(+), 2 deletions(-) rename app/views/spree/admin/{shared => orders/customer_details}/_address_form.html.erb (100%) diff --git a/app/views/spree/admin/shared/_address_form.html.erb b/app/views/spree/admin/orders/customer_details/_address_form.html.erb similarity index 100% rename from app/views/spree/admin/shared/_address_form.html.erb rename to app/views/spree/admin/orders/customer_details/_address_form.html.erb diff --git a/app/views/spree/admin/orders/customer_details/_form.html.haml b/app/views/spree/admin/orders/customer_details/_form.html.haml index 3cfb029474..eebfe7a14c 100644 --- a/app/views/spree/admin/orders/customer_details/_form.html.haml +++ b/app/views/spree/admin/orders/customer_details/_form.html.haml @@ -11,13 +11,13 @@ %fieldset.no-border-bottom %legend{:align => "center"}= Spree.t(:billing_address) = f.fields_for :bill_address do |ba_form| - = render :partial => 'spree/admin/shared/address_form', :locals => { :f => ba_form, :name => Spree.t(:billing_address), :use_billing => false } + = render :partial => 'address_form', :locals => { :f => ba_form, :name => Spree.t(:billing_address), :use_billing => false } .omega.six.columns{"data-hook" => "ship_address_wrapper"} %fieldset.no-border-bottom %legend{:align => "center"}= Spree.t(:shipping_address) = f.fields_for :ship_address do |sa_form| - = render :partial => 'spree/admin/shared/address_form', :locals => { :f => sa_form, :name => Spree.t(:shipping_address), :use_billing => true } + = render :partial => 'address_form', :locals => { :f => sa_form, :name => Spree.t(:shipping_address), :use_billing => true } .clear From 04f9c5ec5e67512f9dfc62a9f41991ddb6d03c6f Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Tue, 1 Sep 2020 13:53:19 +0100 Subject: [PATCH 02/12] Convert address form from erb to haml --- .../customer_details/_address_form.html.erb | 72 ------------------- .../customer_details/_address_form.html.haml | 53 ++++++++++++++ 2 files changed, 53 insertions(+), 72 deletions(-) delete mode 100644 app/views/spree/admin/orders/customer_details/_address_form.html.erb create mode 100644 app/views/spree/admin/orders/customer_details/_address_form.html.haml diff --git a/app/views/spree/admin/orders/customer_details/_address_form.html.erb b/app/views/spree/admin/orders/customer_details/_address_form.html.erb deleted file mode 100644 index 012c40d9e8..0000000000 --- a/app/views/spree/admin/orders/customer_details/_address_form.html.erb +++ /dev/null @@ -1,72 +0,0 @@ -<% if use_billing %> -
- - <%= check_box_tag 'order[use_billing]', '1', (!(@order.bill_address.empty? && @order.ship_address.empty?) && @order.bill_address.same_as?(@order.ship_address)) %> - <%= label_tag 'order[use_billing]', Spree.t(:use_billing_address) %> - -
-<% end %> - -<% is_shipping_address = name == Spree.t(:shipping_address) %> -<% shipping_or_billing = is_shipping_address ? 'shipping' : 'billing' %> -<% s_or_b = is_shipping_address ? 's' : 'b' %> - -
-
"> - <%= f.label :firstname, Spree.t(:first_name) + ':' %> - <%= f.text_field :firstname, :class => 'fullwidth' %> -
-
"> - <%= f.label :lastname, Spree.t(:last_name) + ':' %> - <%= f.text_field :lastname, :class => 'fullwidth' %> -
- <% if Spree::Config[:company] %> -
"> - <%= f.label :company, Spree.t(:company) + ':' %> - <%= f.text_field :company, :class => 'fullwidth' %> -
- <% end %> -
"> - <%= f.label :address1, Spree.t(:street_address) + ':' %> - <%= f.text_field :address1, :class => 'fullwidth' %> -
-
"> - <%= f.label :address2, Spree.t(:street_address_2) + ':' %> - <%= f.text_field :address2, :class => 'fullwidth' %> -
-
"> - <%= f.label :city, Spree.t(:city) + ':' %> - <%= f.text_field :city, :class => 'fullwidth' %> -
-
"> - <%= f.label :zipcode, Spree.t(:zip) + ':' %> - <%= f.text_field :zipcode, :class => 'fullwidth' %> -
-
"> - <%= f.label :country_id, Spree.t(:country) + ':' %> - - <%= f.collection_select :country_id, available_countries, :id, :name, {}, {:class => 'select2 fullwidth'} %> - -
-
"> - <%= f.label :state_id, Spree.t(:state) + ':' %> - - <%= f.text_field :state_name, - :style => "display: #{f.object.country.states.empty? ? 'block' : 'none' };", - :disabled => !f.object.country.states.empty?, :class => 'fullwidth state_name' %> - <%= f.collection_select :state_id, f.object.country.states.sort, :id, :name, {:include_blank => true}, {:class => 'select2 fullwidth', :style => "display: #{f.object.country.states.empty? ? 'none' : 'block' };", :disabled => f.object.country.states.empty?} %> - -
-
"> - <%= f.label :phone, Spree.t(:phone) + ':' %> - <%= f.phone_field :phone, :class => 'fullwidth' %> -
-
- -<% content_for :head do %> - <%= javascript_tag do -%> - $(document).ready(function(){ - $('span#<%= s_or_b %>country .select2').on('change', function() { update_state('<%= s_or_b %>'); }); - }); - <% end -%> -<% end %> diff --git a/app/views/spree/admin/orders/customer_details/_address_form.html.haml b/app/views/spree/admin/orders/customer_details/_address_form.html.haml new file mode 100644 index 0000000000..d1945166a6 --- /dev/null +++ b/app/views/spree/admin/orders/customer_details/_address_form.html.haml @@ -0,0 +1,53 @@ +- if use_billing + .field{:style => "position: absolute;margin-top: -15px;right: 0;"} + %span{"data-hook" => "use_billing"} + = check_box_tag 'order[use_billing]', '1', (!(@order.bill_address.empty? && @order.ship_address.empty?) && @order.bill_address.same_as?(@order.ship_address)) + = label_tag 'order[use_billing]', Spree.t(:use_billing_address) + +- is_shipping_address = name == Spree.t(:shipping_address) +- shipping_or_billing = is_shipping_address ? 'shipping' : 'billing' +- s_or_b = is_shipping_address ? 's' : 'b' + +%div{"data-hook" => "address_fields", :id => "#{is_shipping_address ? 'shipping' : 'billing'}", :style => "display: #{(use_billing (!(@order.bill_address.empty? @order.ship_address.empty?) @order.bill_address.eql?(@order.ship_address))) ? 'none' : 'block'}"} + %div{:class => "field #{"#{shipping_or_billing}-row"}"} + = f.label :firstname, Spree.t(:first_name) + ':' + = f.text_field :firstname, :class => 'fullwidth' + %div{:class => "field #{"#{shipping_or_billing}-row"}"} + = f.label :lastname, Spree.t(:last_name) + ':' + = f.text_field :lastname, :class => 'fullwidth' + - if Spree::Config[:company] + %div{:class => "field #{"#{shipping_or_billing}-row"}"} + = f.label :company, Spree.t(:company) + ':' + = f.text_field :company, :class => 'fullwidth' + %div{:class => "field #{"#{shipping_or_billing}-row"}"} + = f.label :address1, Spree.t(:street_address) + ':' + = f.text_field :address1, :class => 'fullwidth' + %div{:class => "field #{"#{shipping_or_billing}-row"}"} + = f.label :address2, Spree.t(:street_address_2) + ':' + = f.text_field :address2, :class => 'fullwidth' + %div{:class => "field #{"#{shipping_or_billing}-row"}"} + = f.label :city, Spree.t(:city) + ':' + = f.text_field :city, :class => 'fullwidth' + %div{:class => "field #{"#{shipping_or_billing}-row"}"} + = f.label :zipcode, Spree.t(:zip) + ':' + = f.text_field :zipcode, :class => 'fullwidth' + %div{:class => "field #{"#{shipping_or_billing}-row"}"} + = f.label :country_id, Spree.t(:country) + ':' + %span{:id => "#{s_or_b}country"} + = f.collection_select :country_id, available_countries, :id, :name, {}, {:class => 'select2 fullwidth'} + %div{:class => "field #{"#{shipping_or_billing}-row"}"} + = f.label :state_id, Spree.t(:state) + ':' + %span{:id => "#{s_or_b}state"} + = f.text_field :state_name, | + :style => "display: #{f.object.country.states.empty? ? 'block' : 'none' };", | + :disabled => !f.object.country.states.empty?, :class => 'fullwidth state_name' | + = f.collection_select :state_id, f.object.country.states.sort, :id, :name, {:include_blank => true}, {:class => 'select2 fullwidth', :style => "display: #{f.object.country.states.empty? ? 'none' : 'block' };", :disabled => f.object.country.states.empty?} + %div{:class => "field #{"#{shipping_or_billing}-row"}"} + = f.label :phone, Spree.t(:phone) + ':' + = f.phone_field :phone, :class => 'fullwidth' + +- content_for :head do + = javascript_tag do + $(document).ready(function(){ + $('span##{s_or_b}country .select2').on('change', function() { update_state('#{s_or_b}'); }); + }); From e4dee8a2fb085bcea7e536b509b8740f882ca5f2 Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Tue, 1 Sep 2020 13:57:57 +0100 Subject: [PATCH 03/12] Replace hash rockets --- .../customer_details/_address_form.html.haml | 54 +++++++++---------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/app/views/spree/admin/orders/customer_details/_address_form.html.haml b/app/views/spree/admin/orders/customer_details/_address_form.html.haml index d1945166a6..a976d3d027 100644 --- a/app/views/spree/admin/orders/customer_details/_address_form.html.haml +++ b/app/views/spree/admin/orders/customer_details/_address_form.html.haml @@ -1,6 +1,6 @@ - if use_billing - .field{:style => "position: absolute;margin-top: -15px;right: 0;"} - %span{"data-hook" => "use_billing"} + .field{style: "position: absolute;margin-top: -15px;right: 0;"} + %span = check_box_tag 'order[use_billing]', '1', (!(@order.bill_address.empty? && @order.ship_address.empty?) && @order.bill_address.same_as?(@order.ship_address)) = label_tag 'order[use_billing]', Spree.t(:use_billing_address) @@ -8,43 +8,43 @@ - shipping_or_billing = is_shipping_address ? 'shipping' : 'billing' - s_or_b = is_shipping_address ? 's' : 'b' -%div{"data-hook" => "address_fields", :id => "#{is_shipping_address ? 'shipping' : 'billing'}", :style => "display: #{(use_billing (!(@order.bill_address.empty? @order.ship_address.empty?) @order.bill_address.eql?(@order.ship_address))) ? 'none' : 'block'}"} - %div{:class => "field #{"#{shipping_or_billing}-row"}"} +%div{id: "#{is_shipping_address ? 'shipping' : 'billing'}", style: "display: #{(use_billing (!(@order.bill_address.empty? @order.ship_address.empty?) @order.bill_address.eql?(@order.ship_address))) ? 'none' : 'block'}"} + %div{class: "field #{"#{shipping_or_billing}-row"}"} = f.label :firstname, Spree.t(:first_name) + ':' - = f.text_field :firstname, :class => 'fullwidth' - %div{:class => "field #{"#{shipping_or_billing}-row"}"} + = f.text_field :firstname, class: 'fullwidth' + %div{class: "field #{"#{shipping_or_billing}-row"}"} = f.label :lastname, Spree.t(:last_name) + ':' - = f.text_field :lastname, :class => 'fullwidth' + = f.text_field :lastname, class: 'fullwidth' - if Spree::Config[:company] - %div{:class => "field #{"#{shipping_or_billing}-row"}"} + %div{class: "field #{"#{shipping_or_billing}-row"}"} = f.label :company, Spree.t(:company) + ':' - = f.text_field :company, :class => 'fullwidth' - %div{:class => "field #{"#{shipping_or_billing}-row"}"} + = f.text_field :company, class: 'fullwidth' + %div{class: "field #{"#{shipping_or_billing}-row"}"} = f.label :address1, Spree.t(:street_address) + ':' - = f.text_field :address1, :class => 'fullwidth' - %div{:class => "field #{"#{shipping_or_billing}-row"}"} + = f.text_field :address1, class: 'fullwidth' + %div{class: "field #{"#{shipping_or_billing}-row"}"} = f.label :address2, Spree.t(:street_address_2) + ':' - = f.text_field :address2, :class => 'fullwidth' - %div{:class => "field #{"#{shipping_or_billing}-row"}"} + = f.text_field :address2, class: 'fullwidth' + %div{class: "field #{"#{shipping_or_billing}-row"}"} = f.label :city, Spree.t(:city) + ':' - = f.text_field :city, :class => 'fullwidth' - %div{:class => "field #{"#{shipping_or_billing}-row"}"} + = f.text_field :city, class: 'fullwidth' + %div{class: "field #{"#{shipping_or_billing}-row"}"} = f.label :zipcode, Spree.t(:zip) + ':' - = f.text_field :zipcode, :class => 'fullwidth' - %div{:class => "field #{"#{shipping_or_billing}-row"}"} + = f.text_field :zipcode, class: 'fullwidth' + %div{class: "field #{"#{shipping_or_billing}-row"}"} = f.label :country_id, Spree.t(:country) + ':' - %span{:id => "#{s_or_b}country"} - = f.collection_select :country_id, available_countries, :id, :name, {}, {:class => 'select2 fullwidth'} - %div{:class => "field #{"#{shipping_or_billing}-row"}"} + %span{id: "#{s_or_b}country"} + = f.collection_select :country_id, available_countries, :id, :name, {}, {class: 'select2 fullwidth'} + %div{class: "field #{"#{shipping_or_billing}-row"}"} = f.label :state_id, Spree.t(:state) + ':' - %span{:id => "#{s_or_b}state"} + %span{id: "#{s_or_b}state"} = f.text_field :state_name, | - :style => "display: #{f.object.country.states.empty? ? 'block' : 'none' };", | - :disabled => !f.object.country.states.empty?, :class => 'fullwidth state_name' | - = f.collection_select :state_id, f.object.country.states.sort, :id, :name, {:include_blank => true}, {:class => 'select2 fullwidth', :style => "display: #{f.object.country.states.empty? ? 'none' : 'block' };", :disabled => f.object.country.states.empty?} - %div{:class => "field #{"#{shipping_or_billing}-row"}"} + style: "display: #{f.object.country.states.empty? ? 'block' : 'none' };", | + disabled: !f.object.country.states.empty?, class: 'fullwidth state_name' | + = f.collection_select :state_id, f.object.country.states.sort, :id, :name, {include_blank: true}, {class: 'select2 fullwidth', style: "display: #{f.object.country.states.empty? ? 'none' : 'block' };", disabled: f.object.country.states.empty?} + %div{class: "field #{"#{shipping_or_billing}-row"}"} = f.label :phone, Spree.t(:phone) + ':' - = f.phone_field :phone, :class => 'fullwidth' + = f.phone_field :phone, class: 'fullwidth' - content_for :head do = javascript_tag do From 56fb09c0066afae33b4d463500ab92e404443f05 Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Tue, 1 Sep 2020 13:58:57 +0100 Subject: [PATCH 04/12] Remove unused class --- .../customer_details/_address_form.html.haml | 21 +++++++++---------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/app/views/spree/admin/orders/customer_details/_address_form.html.haml b/app/views/spree/admin/orders/customer_details/_address_form.html.haml index a976d3d027..96636ff20f 100644 --- a/app/views/spree/admin/orders/customer_details/_address_form.html.haml +++ b/app/views/spree/admin/orders/customer_details/_address_form.html.haml @@ -5,44 +5,43 @@ = label_tag 'order[use_billing]', Spree.t(:use_billing_address) - is_shipping_address = name == Spree.t(:shipping_address) -- shipping_or_billing = is_shipping_address ? 'shipping' : 'billing' - s_or_b = is_shipping_address ? 's' : 'b' %div{id: "#{is_shipping_address ? 'shipping' : 'billing'}", style: "display: #{(use_billing (!(@order.bill_address.empty? @order.ship_address.empty?) @order.bill_address.eql?(@order.ship_address))) ? 'none' : 'block'}"} - %div{class: "field #{"#{shipping_or_billing}-row"}"} + %div{class: "field"} = f.label :firstname, Spree.t(:first_name) + ':' = f.text_field :firstname, class: 'fullwidth' - %div{class: "field #{"#{shipping_or_billing}-row"}"} + %div{class: "field"} = f.label :lastname, Spree.t(:last_name) + ':' = f.text_field :lastname, class: 'fullwidth' - if Spree::Config[:company] - %div{class: "field #{"#{shipping_or_billing}-row"}"} + %div{class: "field"} = f.label :company, Spree.t(:company) + ':' = f.text_field :company, class: 'fullwidth' - %div{class: "field #{"#{shipping_or_billing}-row"}"} + %div{class: "field"} = f.label :address1, Spree.t(:street_address) + ':' = f.text_field :address1, class: 'fullwidth' - %div{class: "field #{"#{shipping_or_billing}-row"}"} + %div{class: "field"} = f.label :address2, Spree.t(:street_address_2) + ':' = f.text_field :address2, class: 'fullwidth' - %div{class: "field #{"#{shipping_or_billing}-row"}"} + %div{class: "field"} = f.label :city, Spree.t(:city) + ':' = f.text_field :city, class: 'fullwidth' - %div{class: "field #{"#{shipping_or_billing}-row"}"} + %div{class: "field"} = f.label :zipcode, Spree.t(:zip) + ':' = f.text_field :zipcode, class: 'fullwidth' - %div{class: "field #{"#{shipping_or_billing}-row"}"} + %div{class: "field"} = f.label :country_id, Spree.t(:country) + ':' %span{id: "#{s_or_b}country"} = f.collection_select :country_id, available_countries, :id, :name, {}, {class: 'select2 fullwidth'} - %div{class: "field #{"#{shipping_or_billing}-row"}"} + %div{class: "field"} = f.label :state_id, Spree.t(:state) + ':' %span{id: "#{s_or_b}state"} = f.text_field :state_name, | style: "display: #{f.object.country.states.empty? ? 'block' : 'none' };", | disabled: !f.object.country.states.empty?, class: 'fullwidth state_name' | = f.collection_select :state_id, f.object.country.states.sort, :id, :name, {include_blank: true}, {class: 'select2 fullwidth', style: "display: #{f.object.country.states.empty? ? 'none' : 'block' };", disabled: f.object.country.states.empty?} - %div{class: "field #{"#{shipping_or_billing}-row"}"} + %div{class: "field"} = f.label :phone, Spree.t(:phone) + ':' = f.phone_field :phone, class: 'fullwidth' From 1a39a55009e761c5b94587b1ef94eda7428588c8 Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Tue, 1 Sep 2020 14:06:15 +0100 Subject: [PATCH 05/12] Fix problem of converting from erb to haml --- .../admin/orders/customer_details/_address_form.html.haml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/views/spree/admin/orders/customer_details/_address_form.html.haml b/app/views/spree/admin/orders/customer_details/_address_form.html.haml index 96636ff20f..e5d0d443f1 100644 --- a/app/views/spree/admin/orders/customer_details/_address_form.html.haml +++ b/app/views/spree/admin/orders/customer_details/_address_form.html.haml @@ -7,7 +7,9 @@ - is_shipping_address = name == Spree.t(:shipping_address) - s_or_b = is_shipping_address ? 's' : 'b' -%div{id: "#{is_shipping_address ? 'shipping' : 'billing'}", style: "display: #{(use_billing (!(@order.bill_address.empty? @order.ship_address.empty?) @order.bill_address.eql?(@order.ship_address))) ? 'none' : 'block'}"} +- display_style = (use_billing && (!(@order.bill_address.empty? && @order.ship_address.empty?) && @order.bill_address.eql?(@order.ship_address))) ? 'none' : 'block' + +%div{id: "#{is_shipping_address ? 'shipping' : 'billing'}", style: "display: #{display_style}"} %div{class: "field"} = f.label :firstname, Spree.t(:first_name) + ':' = f.text_field :firstname, class: 'fullwidth' From 46623242f752681655b1c78b84114ffc390bcb70 Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Tue, 1 Sep 2020 14:28:45 +0100 Subject: [PATCH 06/12] Make use_billing checkbox work again, missign js code from spree --- .../admin/spree/orders/use_billing.js | 17 +++++++++++++++++ .../customer_details/_address_form.html.haml | 2 +- .../orders/customer_details/_form.html.haml | 1 + 3 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 app/assets/javascripts/admin/spree/orders/use_billing.js diff --git a/app/assets/javascripts/admin/spree/orders/use_billing.js b/app/assets/javascripts/admin/spree/orders/use_billing.js new file mode 100644 index 0000000000..9c79c0d906 --- /dev/null +++ b/app/assets/javascripts/admin/spree/orders/use_billing.js @@ -0,0 +1,17 @@ +$(document).ready(function() { + var order_use_billing_input = $('input#order_use_billing'); + + var order_use_billing = function () { + if (!order_use_billing_input.is(':checked')) { + $('#shipping').show(); + } else { + $('#shipping').hide(); + } + }; + + order_use_billing_input.click(function() { + order_use_billing(); + }); + + order_use_billing(); +}); \ No newline at end of file diff --git a/app/views/spree/admin/orders/customer_details/_address_form.html.haml b/app/views/spree/admin/orders/customer_details/_address_form.html.haml index e5d0d443f1..606de2a600 100644 --- a/app/views/spree/admin/orders/customer_details/_address_form.html.haml +++ b/app/views/spree/admin/orders/customer_details/_address_form.html.haml @@ -7,7 +7,7 @@ - is_shipping_address = name == Spree.t(:shipping_address) - s_or_b = is_shipping_address ? 's' : 'b' -- display_style = (use_billing && (!(@order.bill_address.empty? && @order.ship_address.empty?) && @order.bill_address.eql?(@order.ship_address))) ? 'none' : 'block' +- display_style = (use_billing && (!(@order.bill_address.empty? && @order.ship_address.empty?) && @order.bill_address == @order.ship_address)) ? 'none' : 'block' %div{id: "#{is_shipping_address ? 'shipping' : 'billing'}", style: "display: #{display_style}"} %div{class: "field"} diff --git a/app/views/spree/admin/orders/customer_details/_form.html.haml b/app/views/spree/admin/orders/customer_details/_form.html.haml index eebfe7a14c..5ac4e15a52 100644 --- a/app/views/spree/admin/orders/customer_details/_form.html.haml +++ b/app/views/spree/admin/orders/customer_details/_form.html.haml @@ -26,3 +26,4 @@ - content_for :head do = javascript_include_tag 'admin/spree/orders/address_states.js' + = javascript_include_tag 'admin/spree/orders/use_billing.js' From 41d797489de57319a558eaaed4922e22d8219673 Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Tue, 1 Sep 2020 14:52:18 +0100 Subject: [PATCH 07/12] Make onchange code simpler --- .../admin/orders/customer_details/_address_form.html.haml | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/app/views/spree/admin/orders/customer_details/_address_form.html.haml b/app/views/spree/admin/orders/customer_details/_address_form.html.haml index 606de2a600..d804946766 100644 --- a/app/views/spree/admin/orders/customer_details/_address_form.html.haml +++ b/app/views/spree/admin/orders/customer_details/_address_form.html.haml @@ -35,7 +35,7 @@ %div{class: "field"} = f.label :country_id, Spree.t(:country) + ':' %span{id: "#{s_or_b}country"} - = f.collection_select :country_id, available_countries, :id, :name, {}, {class: 'select2 fullwidth'} + = f.collection_select :country_id, available_countries, :id, :name, {}, {class: 'select2 fullwidth', onchange: "update_state('#{s_or_b}')"} %div{class: "field"} = f.label :state_id, Spree.t(:state) + ':' %span{id: "#{s_or_b}state"} @@ -46,9 +46,3 @@ %div{class: "field"} = f.label :phone, Spree.t(:phone) + ':' = f.phone_field :phone, class: 'fullwidth' - -- content_for :head do - = javascript_tag do - $(document).ready(function(){ - $('span##{s_or_b}country .select2').on('change', function() { update_state('#{s_or_b}'); }); - }); From ffa0d202befb4293d7b22cf56b595723585a3122 Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Tue, 1 Sep 2020 15:46:22 +0100 Subject: [PATCH 08/12] Add states api endpoint to support existing code on the customer details page when user changes country --- .../admin/spree/orders/address_states.js | 3 +- app/controllers/api/states_controller.rb | 34 +++++++++ app/serializers/api/state_serializer.rb | 2 +- app/views/spree/admin/shared/_routes.html.erb | 3 +- config/routes/api.rb | 2 + .../controllers/api/states_controller_spec.rb | 76 +++++++++++++++++++ 6 files changed, 116 insertions(+), 4 deletions(-) create mode 100644 app/controllers/api/states_controller.rb create mode 100644 spec/controllers/api/states_controller_spec.rb diff --git a/app/assets/javascripts/admin/spree/orders/address_states.js b/app/assets/javascripts/admin/spree/orders/address_states.js index 5542e5656a..845c501f7f 100644 --- a/app/assets/javascripts/admin/spree/orders/address_states.js +++ b/app/assets/javascripts/admin/spree/orders/address_states.js @@ -4,8 +4,7 @@ var update_state = function(region) { var state_select = $('span#' + region + 'state select.select2'); var state_input = $('span#' + region + 'state input.state_name'); - $.get(Spree.routes.states_search + "?country_id=" + country, function(data) { - var states = data["states"] + $.get(Spree.routes.states_search + "?country_id=" + country, function(states) { if (states.length > 0) { state_select.html(''); var states_with_blank = [{name: '', id: ''}].concat(states); diff --git a/app/controllers/api/states_controller.rb b/app/controllers/api/states_controller.rb new file mode 100644 index 0000000000..708e40d277 --- /dev/null +++ b/app/controllers/api/states_controller.rb @@ -0,0 +1,34 @@ +module Api + class StatesController < Api::BaseController + respond_to :json + + skip_authorization_check + + def index + @states = scope.ransack(params[:q]).result. + includes(:country).order('name ASC') + + if params[:page] || params[:per_page] + @states = @states.page(params[:page]).per(params[:per_page]) + end + + render json: @states, each_serializer: Api::StateSerializer, status: :ok + end + + def show + @state = scope.find(params[:id]) + render json: @state, serializer: Api::StateSerializer, status: :ok + end + + private + + def scope + if params[:country_id] + @country = Spree::Country.find(params[:country_id]) + @country.states + else + Spree::State.all + end + end + end +end diff --git a/app/serializers/api/state_serializer.rb b/app/serializers/api/state_serializer.rb index 38ad444701..2862c24468 100644 --- a/app/serializers/api/state_serializer.rb +++ b/app/serializers/api/state_serializer.rb @@ -1,3 +1,3 @@ class Api::StateSerializer < ActiveModel::Serializer - attributes :id, :name, :abbr + attributes :id, :name, :abbr, :country_id end diff --git a/app/views/spree/admin/shared/_routes.html.erb b/app/views/spree/admin/shared/_routes.html.erb index ba3bbda3fe..15d562e4ef 100644 --- a/app/views/spree/admin/shared/_routes.html.erb +++ b/app/views/spree/admin/shared/_routes.html.erb @@ -2,6 +2,7 @@ Spree.routes = <%== { :variants_search => spree.admin_search_variants_path(:format => 'json'), :taxons_search => main_app.api_taxons_path(:format => 'json'), - :orders_api => main_app.api_orders_path + :orders_api => main_app.api_orders_path, + :states_search => main_app.api_states_path(:format => 'json') }.to_json %>; diff --git a/config/routes/api.rb b/config/routes/api.rb index 1147076112..f50a9c4763 100644 --- a/config/routes/api.rb +++ b/config/routes/api.rb @@ -61,6 +61,8 @@ Openfoodnetwork::Application.routes.draw do post '/product_images/:product_id', to: 'product_images#update_product_image' + resources :states, :only => [:index, :show] + resources :taxons, :only => [:index] resources :taxonomies do diff --git a/spec/controllers/api/states_controller_spec.rb b/spec/controllers/api/states_controller_spec.rb new file mode 100644 index 0000000000..3badf4a518 --- /dev/null +++ b/spec/controllers/api/states_controller_spec.rb @@ -0,0 +1,76 @@ +require 'spec_helper' + +module Api + describe StatesController do + render_views + + let!(:state) { create(:state, :name => "Victoria") } + let(:attributes) { [:id, :name, :abbr, :country_id] } + let(:current_user) { create(:user) } + + before do + allow(controller).to receive(:spree_current_user) { current_user } + end + + it "gets all states" do + api_get :index + expect(json_response.first.symbolize_keys.keys).to include(*attributes) + expect(json_response.map { |state| state[:name] }).to include(state.name) + end + + context "pagination" do + before do + Spree::State.should_receive(:all).and_return(@scope = double) + @scope.stub_chain(:ransack, :result, :includes, :order).and_return(@scope) + end + + it "does not paginate states results when asked not to do so" do + @scope.should_not_receive(:page) + @scope.should_not_receive(:per) + api_get :index + end + + it "paginates when page parameter is passed through" do + @scope.should_receive(:page).with(1).and_return(@scope) + @scope.should_receive(:per).with(nil) + api_get :index, :page => 1 + end + + it "paginates when per_page parameter is passed through" do + @scope.should_receive(:page).with(nil).and_return(@scope) + @scope.should_receive(:per).with(25) + api_get :index, :per_page => 25 + end + end + + + context "with two states" do + before { create(:state, :name => "New South Wales") } + + it "gets all states for a country" do + country = create(:country, :states_required => true) + state.country = country + state.save + + api_get :index, :country_id => country.id + expect(json_response.first.symbolize_keys.keys).to include(*attributes) + expect(json_response.count).to eq 1 + end + + it "can view all states" do + api_get :index + expect(json_response.first.symbolize_keys.keys).to include(*attributes) + end + + it 'can query the results through a paramter' do + api_get :index, :q => { :name_cont => 'Vic' } + expect(json_response.first['name']).to eq("Victoria") + end + end + + it "can view a state" do + api_get :show, :id => state.id + expect(json_response.symbolize_keys.keys).to include(*attributes) + end + end +end From ed114f4c4c830f2161d30255deb9f1a0d85da503 Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Tue, 1 Sep 2020 15:52:36 +0100 Subject: [PATCH 09/12] Fix rubocop issues --- app/controllers/api/states_controller.rb | 26 +++++++++++++------ .../controllers/api/states_controller_spec.rb | 21 ++++++++------- 2 files changed, 29 insertions(+), 18 deletions(-) diff --git a/app/controllers/api/states_controller.rb b/app/controllers/api/states_controller.rb index 708e40d277..0ad0b991d4 100644 --- a/app/controllers/api/states_controller.rb +++ b/app/controllers/api/states_controller.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Api class StatesController < Api::BaseController respond_to :json @@ -5,14 +7,7 @@ module Api skip_authorization_check def index - @states = scope.ransack(params[:q]).result. - includes(:country).order('name ASC') - - if params[:page] || params[:per_page] - @states = @states.page(params[:page]).per(params[:per_page]) - end - - render json: @states, each_serializer: Api::StateSerializer, status: :ok + render json: states, each_serializer: Api::StateSerializer, status: :ok end def show @@ -30,5 +25,20 @@ module Api Spree::State.all end end + + def states + states = scope.ransack(params[:q]).result. + includes(:country).order('name ASC') + + if pagination? + states = states.page(params[:page]).per(params[:per_page]) + end + + states + end + + def pagination? + params[:page] || params[:per_page] + end end end diff --git a/spec/controllers/api/states_controller_spec.rb b/spec/controllers/api/states_controller_spec.rb index 3badf4a518..f8372ea3bf 100644 --- a/spec/controllers/api/states_controller_spec.rb +++ b/spec/controllers/api/states_controller_spec.rb @@ -1,10 +1,12 @@ +# frozen_string_literal: true + require 'spec_helper' module Api describe StatesController do render_views - let!(:state) { create(:state, :name => "Victoria") } + let!(:state) { create(:state, name: "Victoria") } let(:attributes) { [:id, :name, :abbr, :country_id] } let(:current_user) { create(:user) } @@ -33,26 +35,25 @@ module Api it "paginates when page parameter is passed through" do @scope.should_receive(:page).with(1).and_return(@scope) @scope.should_receive(:per).with(nil) - api_get :index, :page => 1 + api_get :index, page: 1 end it "paginates when per_page parameter is passed through" do @scope.should_receive(:page).with(nil).and_return(@scope) @scope.should_receive(:per).with(25) - api_get :index, :per_page => 25 + api_get :index, per_page: 25 end end - context "with two states" do - before { create(:state, :name => "New South Wales") } + before { create(:state, name: "New South Wales") } it "gets all states for a country" do - country = create(:country, :states_required => true) - state.country = country + country = create(:country, states_required: true) + state.country = country state.save - api_get :index, :country_id => country.id + api_get :index, country_id: country.id expect(json_response.first.symbolize_keys.keys).to include(*attributes) expect(json_response.count).to eq 1 end @@ -63,13 +64,13 @@ module Api end it 'can query the results through a paramter' do - api_get :index, :q => { :name_cont => 'Vic' } + api_get :index, q: { name_cont: 'Vic' } expect(json_response.first['name']).to eq("Victoria") end end it "can view a state" do - api_get :show, :id => state.id + api_get :show, id: state.id expect(json_response.symbolize_keys.keys).to include(*attributes) end end From 88d90a37e8f8ae1a11b018c1f645ca3cdd38c213 Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Tue, 1 Sep 2020 15:53:18 +0100 Subject: [PATCH 10/12] Run transpec --- spec/controllers/api/states_controller_spec.rb | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/spec/controllers/api/states_controller_spec.rb b/spec/controllers/api/states_controller_spec.rb index f8372ea3bf..81313c3153 100644 --- a/spec/controllers/api/states_controller_spec.rb +++ b/spec/controllers/api/states_controller_spec.rb @@ -22,25 +22,25 @@ module Api context "pagination" do before do - Spree::State.should_receive(:all).and_return(@scope = double) - @scope.stub_chain(:ransack, :result, :includes, :order).and_return(@scope) + expect(Spree::State).to receive(:all).and_return(@scope = double) + allow(@scope).to receive_message_chain(:ransack, :result, :includes, :order).and_return(@scope) end it "does not paginate states results when asked not to do so" do - @scope.should_not_receive(:page) - @scope.should_not_receive(:per) + expect(@scope).not_to receive(:page) + expect(@scope).not_to receive(:per) api_get :index end it "paginates when page parameter is passed through" do - @scope.should_receive(:page).with(1).and_return(@scope) - @scope.should_receive(:per).with(nil) + expect(@scope).to receive(:page).with(1).and_return(@scope) + expect(@scope).to receive(:per).with(nil) api_get :index, page: 1 end it "paginates when per_page parameter is passed through" do - @scope.should_receive(:page).with(nil).and_return(@scope) - @scope.should_receive(:per).with(25) + expect(@scope).to receive(:page).with(nil).and_return(@scope) + expect(@scope).to receive(:per).with(25) api_get :index, per_page: 25 end end From 5716ea8611e32ed43b0cce192860fc041a70c838 Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Tue, 1 Sep 2020 15:55:46 +0100 Subject: [PATCH 11/12] Fix rubocop issue --- app/assets/javascripts/admin/spree/orders/use_billing.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/assets/javascripts/admin/spree/orders/use_billing.js b/app/assets/javascripts/admin/spree/orders/use_billing.js index 9c79c0d906..20d1d96668 100644 --- a/app/assets/javascripts/admin/spree/orders/use_billing.js +++ b/app/assets/javascripts/admin/spree/orders/use_billing.js @@ -14,4 +14,4 @@ $(document).ready(function() { }); order_use_billing(); -}); \ No newline at end of file +}); From ffac38d93442685d53025fbea6c6f85f44687ced Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Tue, 1 Sep 2020 16:06:49 +0100 Subject: [PATCH 12/12] Remove option to enter state_name for countries without states Thisis not supported in OFN --- .../admin/spree/orders/address_states.js | 29 +++++++------------ .../customer_details/_address_form.html.haml | 5 +--- 2 files changed, 11 insertions(+), 23 deletions(-) diff --git a/app/assets/javascripts/admin/spree/orders/address_states.js b/app/assets/javascripts/admin/spree/orders/address_states.js index 845c501f7f..ef1cf6b367 100644 --- a/app/assets/javascripts/admin/spree/orders/address_states.js +++ b/app/assets/javascripts/admin/spree/orders/address_states.js @@ -1,26 +1,17 @@ var update_state = function(region) { var country = $('span#' + region + 'country .select2').select2('val'); - var state_select = $('span#' + region + 'state select.select2'); - var state_input = $('span#' + region + 'state input.state_name'); $.get(Spree.routes.states_search + "?country_id=" + country, function(states) { - if (states.length > 0) { - state_select.html(''); - var states_with_blank = [{name: '', id: ''}].concat(states); - $.each(states_with_blank, function(pos,state) { - var opt = $(document.createElement('option')) - .attr('value', state.id) - .html(state.name); - state_select.append(opt); - }); - state_select.prop("disabled", false).show(); - state_select.select2(); - state_input.hide().prop("disabled", true); - - } else { - state_input.prop("disabled", false).show(); - state_select.select2('destroy').hide(); - } + state_select.html(''); + var states_with_blank = [{name: '', id: ''}].concat(states); + $.each(states_with_blank, function(pos,state) { + var opt = $(document.createElement('option')) + .attr('value', state.id) + .html(state.name); + state_select.append(opt); + }); + state_select.prop("disabled", false).show(); + state_select.select2(); }) }; diff --git a/app/views/spree/admin/orders/customer_details/_address_form.html.haml b/app/views/spree/admin/orders/customer_details/_address_form.html.haml index d804946766..18bd0970b6 100644 --- a/app/views/spree/admin/orders/customer_details/_address_form.html.haml +++ b/app/views/spree/admin/orders/customer_details/_address_form.html.haml @@ -39,10 +39,7 @@ %div{class: "field"} = f.label :state_id, Spree.t(:state) + ':' %span{id: "#{s_or_b}state"} - = f.text_field :state_name, | - style: "display: #{f.object.country.states.empty? ? 'block' : 'none' };", | - disabled: !f.object.country.states.empty?, class: 'fullwidth state_name' | - = f.collection_select :state_id, f.object.country.states.sort, :id, :name, {include_blank: true}, {class: 'select2 fullwidth', style: "display: #{f.object.country.states.empty? ? 'none' : 'block' };", disabled: f.object.country.states.empty?} + = f.collection_select :state_id, f.object.country.states.sort, :id, :name, {include_blank: true}, {class: 'select2 fullwidth', disabled: f.object.country.states.empty?} %div{class: "field"} = f.label :phone, Spree.t(:phone) + ':' = f.phone_field :phone, class: 'fullwidth'