Permalink
Browse files

Elminated unneeded config options. Updated HTML to match spree core. …

…More specs passing.

Renamed address helper.
  • Loading branch information...
1 parent 7c2501f commit fc990f9d48b4c994b57f578feb716dccb699cbdd @romul romul committed Nov 5, 2012
View
2 app/controllers/spree/addresses_controller.rb
@@ -1,5 +1,5 @@
class Spree::AddressesController < Spree::BaseController
- helper Spree::AddressesHelper
+ helper Spree::AddressBookHelper
rescue_from ActiveRecord::RecordNotFound, :with => :render_404
load_and_authorize_resource
View
2 app/controllers/spree/checkout_controller_decorator.rb
@@ -1,5 +1,5 @@
Spree::CheckoutController.class_eval do
- helper Spree::AddressesHelper
+ helper Spree::AddressBookHelper
after_filter :normalize_addresses, :only => :update
before_filter :set_addresses, :only => :update
View
20 app/helpers/spree/addresses_helper.rb → app/helpers/spree/address_book_helper.rb
@@ -1,17 +1,21 @@
-module Spree::AddressesHelper
+module Spree::AddressBookHelper
def address_field(form, method, id_prefix = "b", &handler)
- content_tag :p, :id => [id_prefix, method].join('_'), :class => "field" do
+ # stay in line with the default spree method of p#id naming
+ # this ensures that JS state selection, and other future checkout.js things, will keep working
+ id_prefix = id_prefix[0] if id_prefix.length > 1
+
+ content_tag :p, :id => [id_prefix, method].join(''), :class => "field" do
if handler
handler.call
else
is_required = Spree::Address.required_fields.include?(method)
- separator = is_required ? '<span class="required">*</span><br />' : '<br />'
- form.label(method) + separator.html_safe +
+ separator = is_required ? '<span class="req">*</span><br />' : '<br />'
+ form.label(method) + separator.html_safe +
form.text_field(method, :class => is_required ? 'required' : nil)
end
end
end
-
+
def address_state(form, country)
country ||= Spree::Country.find(Spree::Config[:default_country_id])
have_states = !country.states.empty?
@@ -25,9 +29,9 @@ def address_state(form, country)
:class => !have_states ? "required" : "hidden",
:disabled => have_states)
].join.gsub('"', "'").gsub("\n", "")
-
- form.label(:state, t(:state)) + '<span class="required">*</span><br />'.html_safe +
+
+ form.label(:state, t(:state)) + '<span class="req">*</span><br />'.html_safe +
content_tag(:noscript, form.text_field(:state_name, :class => 'required')) +
javascript_tag("document.write(\"#{state_elements.html_safe}\");")
end
-end
+end
View
8 app/models/spree/address_book_configuration.rb
@@ -1,8 +0,0 @@
-module Spree
- class AddressBookConfiguration < Preferences::Configuration
- preference :disable_bill_address, :boolean, :default => false
- preference :alternative_bill_address_phone, :boolean, :default => false
- preference :alternative_ship_address_phone, :boolean, :default => false
- preference :alternative_address_phone, :boolean, :default => false
- end
-end
View
24 app/models/spree/address_decorator.rb
@@ -8,26 +8,32 @@ def self.required_fields
validator ? validator.attributes : []
end
- # can modify an address if it's not been used in an order
+ # TODO: look into if this is actually needed. I don't want to override methods unless it is really needed
+ # can modify an address if it's not been used in an order
+ def same_as?(other)
+ return false if other.nil?
+ attributes.except('id', 'updated_at', 'created_at', 'user_id') == other.attributes.except('id', 'updated_at', 'created_at', 'user_id')
+ end
+
+ # can modify an address if it's not been used in an order
def editable?
new_record? || (shipments.empty? && (Spree::Order.where("bill_address_id = ?", self.id).count + Spree::Order.where("bill_address_id = ?", self.id).count <= 1) && Spree::Order.complete.where("bill_address_id = ? OR ship_address_id = ?", self.id, self.id).count == 0)
end
-
+
def can_be_deleted?
shipments.empty? && Spree::Order.where("bill_address_id = ? OR ship_address_id = ?", self.id, self.id).count == 0
end
-
+
def to_s
"#{firstname} #{lastname}: #{zipcode}, #{country}, #{state || state_name}, #{city}, #{address1} #{address2}"
end
-
- def destroy_with_saving_used
+
+ # UPGRADE_CHECK if future versions of spree have a custom destroy function, this will break
+ def destroy
if can_be_deleted?
- destroy_without_saving_used
+ super
else
- update_attribute(:deleted_at, Time.now)
+ update_column :deleted_at, Time.now
end
end
- alias_method_chain :destroy, :saving_used
-
end
View
6 app/models/spree/order_decorator.rb
@@ -47,18 +47,20 @@ def ship_address_attributes=(attributes)
private
def update_or_create_address(attributes)
- address = nil
if attributes[:id]
address = Spree::Address.find(attributes[:id])
+ attributes.delete(:id)
+
if address && address.editable?
address.update_attributes(attributes)
else
attributes.delete(:id)
end
end
-
+
if !attributes[:id]
address = Spree::Address.new(attributes)
+ address.save
end
address
View
14 app/views/spree/addresses/_form.html.erb
@@ -1,16 +1,16 @@
<% ADDRESS_FIELDS.each do |field| %>
<% if field == "country" %>
- <%= address_form.label :country_id, t(field, :scope => [:activerecord, :attributes, :address]) %><span class="required">*</span><br />
- <span><%= address_form.collection_select :country_id, available_countries, :id, :name, {}, {:class => 'required'} %></span>
- <% elsif field == "state" %>
- <% if Spree::Config[:address_requires_state] %>
- <%= address_field(address_form, :state, address_name) { address_state(address_form, address.country) } %>
- <% end %>
+ <%= address_field(address_form, :country, address_name) do %>
+ <%= address_form.label :country_id, t(field, :scope => [:activerecord, :attributes, :address]) %><span class="req">*</span><br />
+ <span id="<%= address_name[0] %>country"><%= address_form.collection_select :country_id, available_countries, :id, :name, {}, {:class => 'required'} %></span>
+ <% end %>
+ <% elsif field == "state" && Spree::Config[:address_requires_state] %>
+ <%= address_field(address_form, :state, address_name) { address_state(address_form, address.country) } %>
<% else %>
<% next if field == "company" && !Spree::Config[:company] %>
<%= address_field(address_form, field.to_sym, address_name) %>
<% end %>
<% end %>
-<% if Spree::AddressBook::Config["alternative_#{address_name}_phone"] %>
+<% if Spree::Config["alternative_#{address_name[0] == 'b' ? 'billing' : 'shipping'}_phone"] %>
<%= address_field(address_form, :alternative_phone, address_name) %>
<% end %>
View
27 app/views/spree/checkout/_address.html.erb
@@ -7,23 +7,22 @@
<fieldset id="<%= address_type %>">
<legend><%= t(address_type + "_address")%></legend>
<% if address_type == 'shipping' && !Spree::AddressBook::Config[:disable_bill_address] %>
- <p class="field checkbox">
- <label for="order_use_billing" id="use_billing">
- <%= check_box_tag 'order[use_billing]', '1', (!(@order.bill_address.empty? && @order.ship_address.empty?) && @order.bill_address.eql?(@order.ship_address)) %> <%= t("use_billing_address") %>
- </label>
+ <p class="field checkbox" 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, t(:use_billing_address), :id => 'use_billing' %>
</p>
<% end %>
<% if @addresses.present? %>
- <div class="select_address">
- <p class="field">
- <% @addresses.each_with_index do |address, idx| %>
- <span id="<%= [address_type, dom_id(address)].join('_') %>">
- <label><%= form.radio_button "#{address_name}_id", address.id, :checked => (idx == 0) %> <%= address %></label> <%= link_to t(:edit), edit_address_path(address) %><br />
- </span>
- <% end %>
- <label><%= form.radio_button "#{address_name}_id", 0 %> <%= t('other_address') %></label>
- </p>
- </div>
+ <div class="select_address">
+ <p class="field">
+ <% @addresses.each_with_index do |address, idx| %>
+ <span id="<%= [address_type, dom_id(address)].join('_') %>">
+ <label><%= form.radio_button "#{address_name}_id", address.id, :checked => (idx == 0) %> <%= address %></label> <%= link_to t(:edit), edit_address_path(address) %><br />
+ </span>
+ <% end %>
+ <label><%= form.radio_button "#{address_name}_id", 0 %> <%= t('other_address') %></label>
+ </p>
+ </div>
<% end %>
<%= form.fields_for address_name do |address_form| %>
<div class="inner">
View
19 app/views/spree/users/_addresses.html.erb
@@ -1,22 +1,23 @@
+<h3><%= t(:shipping_addresses) %></h3>
<% if current_user.addresses.present? %>
-<h3><%= Spree::Address.model_name.human(:count => 2) %></h3>
<table id="user_addresses">
- <thead>
- <tr>
- <th><%= Spree::Address.model_name.human %></th>
- <th></th>
- </tr>
- </thead>
<tbody>
- <% current_user.addresses.each do |address| %>
+ <% current_user.addresses.each do |address| %>
<tr class="<%= cycle('even', 'odd') %>">
<td><%= address %></td>
<td>
<%= link_to t(:edit), edit_address_path(address) %>&nbsp;
<%= link_to t(:remove), address_path(address), :method => :delete, :confirm => t(:are_you_sure) %>
</td>
</tr>
- <% end %>
+ <% end %>
</tbody>
</table>
+<% else %>
+<div id="users-address-list-empty">
+<%= t(:no_shipping_addresses_on_file) %>
+</div>
<% end %>
+<div id="new_address_link">
+ <%= link_to t(:add_new_shipping_address), :controller => :addresses, :action => :new %>
+</div>
View
12 config/locales/en.yml
@@ -1,2 +1,14 @@
+# Sample localization file for English. Add more files in this directory for other locales.
+# See https://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points.
+
en:
other_address: "Other address"
+ add_new_shipping_address: "Add new shipping address"
+ new_shipping_address: "New Shipping Address"
+ no_shipping_addresses_on_file: "No shipping addresses on file"
+ shipping_addresses: "Shipping Addresses"
+ successfully_saved: "Saved successfully"
+ unsuccessfully_saved: "There was an error while trying to save your address."
+ successfully_updated: "Updated successfully"
+ unsuccessfully_updated: "There was an update while trying to update your address."
+ save: "Save"
View
5 lib/spree/address_book_configuration.rb
@@ -0,0 +1,5 @@
+module Spree
+ class AddressBookConfiguration < Preferences::Configuration
+ preference :disable_bill_address, :boolean, :default => false
+ end
+end
View
4 spec/support/checkout_with_product.rb
@@ -47,9 +47,7 @@ def fill_in_address(address, type = :bill)
fill_in I18n.t(:city), :with => address.city
fill_in I18n.t(:zip), :with => address.zipcode
fill_in I18n.t(:phone), :with => address.phone
-
- # TODO we are currently missing the alternative phone field, once this exists, this should be added back in
- # fill_in 'Alternative phone', :with => address.alternative_phone if Spree::Config[:alternative_billing_phone]
+ fill_in 'Alternative phone', :with => address.alternative_phone if Spree::Config[:alternative_billing_phone]
end
def expected_address_format(address)

1 comment on commit fc990f9

@romul

Oops, after few changes and rebasing author of commit was changed. Original author of main part of this commit: @iloveitaly

Please sign in to comment.