spree_address_book 0.70.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. data/.gitignore +1 -2
  2. data/.rspec +3 -0
  3. data/Gemfile +6 -0
  4. data/Gemfile.lock +228 -0
  5. data/Rakefile +10 -57
  6. data/Versionfile +1 -0
  7. data/app/assets/javascripts/store/spree_address_book.js +53 -0
  8. data/app/assets/stylesheets/store/spree_address_book.scss +12 -0
  9. data/app/controllers/{addresses_controller.rb → spree/addresses_controller.rb} +1 -1
  10. data/app/controllers/{checkout_controller_decorator.rb → spree/checkout_controller_decorator.rb} +4 -2
  11. data/app/helpers/checkout_helper_decorator.rb +4 -4
  12. data/app/models/address_decorator.rb +4 -4
  13. data/app/models/order_decorator.rb +7 -7
  14. data/{lib → app/models/spree}/address_ability.rb +2 -2
  15. data/app/models/spree/address_book_configuration.rb +8 -0
  16. data/app/models/user_decorator.rb +1 -1
  17. data/app/overrides/views_decorator.rb +2 -2
  18. data/app/views/spree/addresses/_form.html.erb +13 -0
  19. data/app/views/{addresses → spree/addresses}/destroy.js.erb +0 -0
  20. data/app/views/{addresses → spree/addresses}/edit.html.erb +1 -1
  21. data/app/views/spree/checkout/_address.html.erb +45 -0
  22. data/app/views/spree/users/_addresses.html.erb +22 -0
  23. data/config/routes.rb +1 -1
  24. data/db/migrate/20110302102208_add_user_id_and_deleted_at_to_addresses.rb +8 -2
  25. data/features/support/env.rb +3 -22
  26. data/lib/generators/spree_address_book/install_generator.rb +8 -0
  27. data/lib/spree_address_book.rb +17 -11
  28. data/spec/controllers/spree/checkout_controller_spec.rb +52 -0
  29. data/spec/models/address_spec.rb +2 -2
  30. data/spec/models/order_spec.rb +21 -0
  31. data/spec/spec_helper.rb +15 -10
  32. data/spree_address_book.gemspec +8 -3
  33. metadata +85 -20
  34. data/app/assets/javascripts/store/checkout.js +0 -73
  35. data/app/views/addresses/_form.html.erb +0 -15
  36. data/app/views/checkout/_address.html.erb +0 -85
  37. data/app/views/users/_addresses.html.erb +0 -20
@@ -1,18 +1,18 @@
1
- Address.class_eval do
1
+ Spree::Address.class_eval do
2
2
  belongs_to :user
3
3
 
4
4
  def self.required_fields
5
- validator = Address.validators.find{|v| v.kind_of?(ActiveModel::Validations::PresenceValidator)}
5
+ validator = Spree::Address.validators.find{|v| v.kind_of?(ActiveModel::Validations::PresenceValidator)}
6
6
  validator ? validator.attributes : []
7
7
  end
8
8
 
9
9
  # can modify an address if it's not been used in an order
10
10
  def editable?
11
- new_record? || (shipments.empty? && (Order.where("bill_address_id = ?", self.id).count + Order.where("bill_address_id = ?", self.id).count <= 1) && Order.complete.where("bill_address_id = ? OR ship_address_id = ?", self.id, self.id).count == 0)
11
+ 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)
12
12
  end
13
13
 
14
14
  def can_be_deleted?
15
- shipments.empty? && Order.where("bill_address_id = ? OR ship_address_id = ?", self.id, self.id).count == 0
15
+ shipments.empty? && Spree::Order.where("bill_address_id = ? OR ship_address_id = ?", self.id, self.id).count == 0
16
16
  end
17
17
 
18
18
  def to_s
@@ -1,12 +1,12 @@
1
- Order.class_eval do
1
+ Spree::Order.class_eval do
2
2
  attr_accessible :bill_address_id, :ship_address_id
3
- before_validation :clone_shipping_address, :if => "Spree::Config[:disable_bill_address]"
3
+ before_validation :clone_shipping_address, :if => "Spree::AddressBook::Config[:disable_bill_address]"
4
4
 
5
5
  def clone_shipping_address
6
6
  if self.ship_address
7
7
  self.bill_address = self.ship_address
8
8
  end
9
- true
9
+ true
10
10
  end
11
11
 
12
12
  def clone_billing_address
@@ -17,7 +17,7 @@ Order.class_eval do
17
17
  end
18
18
 
19
19
  def bill_address_id=(id)
20
- address = Address.find(id)
20
+ address = Spree::Address.where(:id => id).first
21
21
  if address && address.user_id == self.user_id
22
22
  self["bill_address_id"] = address.id
23
23
  self.bill_address.reload
@@ -31,7 +31,7 @@ Order.class_eval do
31
31
  end
32
32
 
33
33
  def ship_address_id=(id)
34
- address = Address.find(id)
34
+ address = Spree::Address.where(:id => id).first
35
35
  if address && address.user_id == self.user_id
36
36
  self["ship_address_id"] = address.id
37
37
  self.ship_address.reload
@@ -49,7 +49,7 @@ Order.class_eval do
49
49
  def update_or_create_address(attributes)
50
50
  address = nil
51
51
  if attributes[:id]
52
- address = Address.find(attributes[:id])
52
+ address = Spree::Address.find(attributes[:id])
53
53
  if address && address.editable?
54
54
  address.update_attributes(attributes)
55
55
  else
@@ -58,7 +58,7 @@ Order.class_eval do
58
58
  end
59
59
 
60
60
  if !attributes[:id]
61
- address = Address.new(attributes)
61
+ address = Spree::Address.new(attributes)
62
62
  address.save
63
63
  end
64
64
 
@@ -1,8 +1,8 @@
1
- class AddressAbility
1
+ class Spree::AddressAbility
2
2
  include CanCan::Ability
3
3
 
4
4
  def initialize(user)
5
- can :manage, Address do |address|
5
+ can :manage, Spree::Address do |address|
6
6
  address.user == user
7
7
  end
8
8
  end
@@ -0,0 +1,8 @@
1
+ module Spree
2
+ class AddressBookConfiguration < Preferences::Configuration
3
+ preference :disable_bill_address, :boolean, :default => false
4
+ preference :alternative_bill_address_phone, :boolean, :default => false
5
+ preference :alternative_ship_address_phone, :boolean, :default => false
6
+ preference :alternative_address_phone, :boolean, :default => false
7
+ end
8
+ end
@@ -1,3 +1,3 @@
1
- User.class_eval do
1
+ Spree::User.class_eval do
2
2
  has_many :addresses, :conditions => {:deleted_at => nil}, :order => "updated_at DESC"
3
3
  end
@@ -1,6 +1,6 @@
1
1
  Deface::Override.new(
2
- :virtual_path => "users/show",
2
+ :virtual_path => "spree/users/show",
3
3
  :name => "address_book_account_my_orders",
4
4
  :insert_after => "[data-hook='account_my_orders'], #account_my_orders[data-hook]",
5
- :partial => "users/addresses",
5
+ :partial => "spree/users/addresses",
6
6
  :disabled => false)
@@ -0,0 +1,13 @@
1
+ <% ADDRESS_FIELDS.each do |field| %>
2
+ <% if field == "country" %>
3
+ <%= address_form.label :country_id, t(field, :scope => [:activerecord, :attributes, :address]) %><span class="req">*</span><br />
4
+ <span><%= address_form.collection_select :country_id, available_countries, :id, :name, {}, {:class => 'required'} %></span>
5
+ <% elsif field == "state" && Spree::Config[:address_requires_state] %>
6
+ <%= address_field(address_form, :state, address_name) { address_state(address_form, address.country) } %>
7
+ <% else %>
8
+ <%= address_field(address_form, field.to_sym, address_name) %>
9
+ <% end %>
10
+ <% end %>
11
+ <% if Spree::AddressBook::Config["alternative_#{address_name}_phone"] %>
12
+ <%= address_field(address_form, :alternative_phone, address_name) %>
13
+ <% end %>
@@ -7,7 +7,7 @@
7
7
  <%= form_for @address do |f| %>
8
8
  <fieldset>
9
9
  <div class="inner">
10
- <%= render :partial => 'addresses/form', :locals => {
10
+ <%= render :partial => 'spree/addresses/form', :locals => {
11
11
  :address_name => 'address',
12
12
  :address_form => f,
13
13
  :address => @address
@@ -0,0 +1,45 @@
1
+ <% @addresses = current_user ? current_user.addresses : [] %>
2
+
3
+ <% ['billing', 'shipping'].each do |address_type|
4
+ next if address_type == 'billing' && Spree::AddressBook::Config[:disable_bill_address]
5
+ address_name = "#{address_type[0...4]}_address" %>
6
+ <div class="columns alpha six" data-hook="<%= address_type %>_fieldset_wrapper">
7
+ <fieldset id="<%= address_type %>">
8
+ <legend><%= t(address_type + "_address")%></legend>
9
+ <% if address_type == 'shipping' && !Spree::AddressBook::Config[:disable_bill_address] %>
10
+ <p class="field checkbox">
11
+ <label for="order_use_billing" id="use_billing">
12
+ <%= 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") %>
13
+ </label>
14
+ </p>
15
+ <% end %>
16
+ <div class="select_address">
17
+ <p class="field">
18
+ <% if @addresses.present? %>
19
+ <% @addresses.each_with_index do |address, idx| %>
20
+ <span id="<%= [address_type, dom_id(address)].join('_') %>">
21
+ <label><%= form.radio_button "#{address_name}_id", address.id, :checked => (idx == 0) %> <%= address %></label> <%= link_to t(:edit), edit_address_path(address) %><br />
22
+ </span>
23
+ <% end %>
24
+ <label><%= form.radio_button "#{address_name}_id", 0 %> <%= t('other_address') %></label>
25
+ <% end %>
26
+ </p>
27
+ </div>
28
+ <%= form.fields_for address_name do |address_form| %>
29
+ <div class="inner">
30
+ <p class="field">&nbsp;</p>
31
+ <%= render :partial => 'spree/addresses/form', :locals => {
32
+ :address_name => address_name,
33
+ :address_form => address_form,
34
+ :address => Spree::Address.default
35
+ } %>
36
+ </div>
37
+ <% end %>
38
+ </fieldset>
39
+ </div>
40
+ <% end %>
41
+
42
+ <hr class="space" />
43
+ <div class="form-buttons">
44
+ <input type="submit" class="continue button primary" value="<%=t("save_and_continue") %>" />
45
+ </div>
@@ -0,0 +1,22 @@
1
+ <% if current_user.addresses.present? %>
2
+ <h3><%= Spree::Address.model_name.human(:count => 2) %></h3>
3
+ <table id="user_addresses">
4
+ <thead>
5
+ <tr>
6
+ <th><%= Spree::Address.model_name.human %></th>
7
+ <th></th>
8
+ </tr>
9
+ </thead>
10
+ <tbody>
11
+ <% current_user.addresses.each do |address| %>
12
+ <tr class="<%= cycle('even', 'odd') %>">
13
+ <td><%= address %></td>
14
+ <td>
15
+ <%= link_to t(:edit), edit_address_path(address) %>&nbsp;
16
+ <%= link_to t(:remove), address_path(address), :method => :delete, :confirm => t(:are_you_sure) %>
17
+ </td>
18
+ </tr>
19
+ <% end %>
20
+ </tbody>
21
+ </table>
22
+ <% end %>
data/config/routes.rb CHANGED
@@ -1,3 +1,3 @@
1
- Rails.application.routes.draw do
1
+ Spree::Core::Engine.routes.prepend do
2
2
  resources :addresses, :only => [:edit, :update, :destroy]
3
3
  end
@@ -1,15 +1,21 @@
1
1
  class AddUserIdAndDeletedAtToAddresses < ActiveRecord::Migration
2
2
  def self.up
3
- change_table :addresses do |t|
3
+ change_table addresses_table_name do |t|
4
4
  t.integer :user_id
5
5
  t.datetime :deleted_at
6
6
  end
7
7
  end
8
8
 
9
9
  def self.down
10
- change_table :addresses do |t|
10
+ change_table addresses_table_name do |t|
11
11
  t.remove :deleted_at
12
12
  t.remove :user_id
13
13
  end
14
14
  end
15
+
16
+ private
17
+
18
+ def self.addresses_table_name
19
+ table_exists?('addresses') ? :addresses : :spree_addresses
20
+ end
15
21
  end
@@ -1,22 +1,3 @@
1
- FEATURES_PATH = File.expand_path('../..', __FILE__)
2
-
3
- # load shared env with features
4
- require File.expand_path('../../../../spree/features/support/env', __FILE__)
5
-
6
- # load the rest of files for support and step definitions
7
- directories = [ File.expand_path('../../../../spree/features/support', __FILE__),
8
- File.expand_path('../../../../spree/features/step_definitions', __FILE__),
9
- File.expand_path('../../../spec/factories', __FILE__) ]
10
-
11
- files = directories.map do |dir|
12
- Dir["#{dir}/**/*.rb"]
13
- end.flatten.uniq
14
-
15
- files.each do |path|
16
- if path !~ /env.rb$/
17
- fp = File.expand_path(path)
18
- #puts fp
19
- load(fp)
20
- end
21
- end
22
-
1
+ require File.expand_path("../../../spec/dummy/config/environment.rb", __FILE__)
2
+ require 'spree/core/testing_support/factories'
3
+ require 'spree/core/testing_support/env'
@@ -1,6 +1,14 @@
1
1
  module SpreeAddressBook
2
2
  module Generators
3
3
  class InstallGenerator < Rails::Generators::Base
4
+ def add_javascripts
5
+ append_file "app/assets/javascripts/store/all.js", "//= require store/spree_address_book\n"
6
+ end
7
+
8
+ def add_stylesheets
9
+ inject_into_file "app/assets/stylesheets/store/all.css", " *= require store/spree_address_book\n", :before => /\*\//, :verbose => true
10
+ end
11
+
4
12
  def add_migrations
5
13
  run 'bundle exec rake railties:install:migrations FROM=spree_address_book'
6
14
  end
@@ -1,18 +1,24 @@
1
1
  require 'spree_core'
2
2
 
3
- module SpreeAddressBook
4
- class Engine < Rails::Engine
5
- engine_name 'spree_address_book'
6
-
7
- config.autoload_paths += %W(#{config.root}/lib)
3
+ module Spree
4
+ module AddressBook
5
+ class Engine < Rails::Engine
6
+ engine_name 'spree_address_book'
7
+
8
+ initializer "spree.advanced_cart.environment", :before => :load_config_initializers do |app|
9
+ Spree::AddressBook::Config = Spree::AddressBookConfiguration.new
10
+ end
11
+
12
+ config.autoload_paths += %W(#{config.root}/lib)
8
13
 
9
- def self.activate
10
- Dir.glob(File.join(File.dirname(__FILE__), "../app/**/*_decorator*.rb")) do |c|
11
- Rails.env.production? ? require(c) : load(c)
14
+ def self.activate
15
+ Dir.glob(File.join(File.dirname(__FILE__), "../app/**/*_decorator*.rb")) do |c|
16
+ Rails.env.production? ? require(c) : load(c)
17
+ end
18
+ Spree::Ability.register_ability(Spree::AddressAbility)
12
19
  end
13
- Ability.register_ability(AddressAbility)
14
- end
15
20
 
16
- config.to_prepare &method(:activate).to_proc
21
+ config.to_prepare &method(:activate).to_proc
22
+ end
17
23
  end
18
24
  end
@@ -0,0 +1,52 @@
1
+ require 'spec_helper'
2
+
3
+ describe Spree::CheckoutController do
4
+
5
+ before(:each) do
6
+ user = Factory(:user)
7
+ @address = Factory(:address, :user => user)
8
+
9
+ @order = Factory(:order, :bill_address_id => nil, :ship_address_id => nil)
10
+ @order.add_variant(Factory(:product).master, 1)
11
+ @order.user = user
12
+ @order.shipping_method = Factory(:shipping_method)
13
+ @address.user = @order.user
14
+ @order.save
15
+
16
+ controller.stub :current_order => @order
17
+ controller.stub :current_user => @order.user
18
+ end
19
+
20
+ describe "on address step" do
21
+ it "set equal address ids" do
22
+ put_address_to_order('bill_address_id' => @address.id, 'ship_address_id' => @address.id)
23
+ @order.bill_address.should be_present
24
+ @order.ship_address.should be_present
25
+ @order.bill_address_id.should == @address.id
26
+ @order.bill_address_id.should == @order.ship_address_id
27
+ end
28
+
29
+ it "set bill_address_id and use_billing" do
30
+ put_address_to_order(:bill_address_id => @address.id, :use_billing => true)
31
+ @order.bill_address.should be_present
32
+ @order.ship_address.should be_present
33
+ @order.bill_address_id.should == @address.id
34
+ @order.bill_address_id.should == @order.ship_address_id
35
+ end
36
+
37
+ it "set address attributes" do
38
+ put_address_to_order(:bill_address_attributes => @address.clone.attributes, :ship_address_attributes => @address.clone.attributes)
39
+ @order.bill_address_id.should_not == nil
40
+ @order.ship_address_id.should_not == nil
41
+ @order.bill_address_id.should == @order.ship_address_id
42
+ end
43
+ end
44
+
45
+ private
46
+
47
+ def put_address_to_order(params)
48
+ put :update, {:use_route => :spree, :state => "address", :order => params}
49
+ end
50
+
51
+
52
+ end
@@ -1,5 +1,5 @@
1
- require File.dirname(__FILE__) + '/../spec_helper'
1
+ require 'spec_helper'
2
2
 
3
- describe Address do
3
+ describe Spree::Address do
4
4
 
5
5
  end
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ describe Spree::Order do
4
+
5
+ describe "Create order with the same bill & ship addresses" do
6
+ it "should have equal ids when set ids" do
7
+ address = Factory(:address)
8
+ @order = Factory(:order, :bill_address_id => address.id, :ship_address_id => address.id)
9
+ @order.bill_address_id.should == @order.ship_address_id
10
+ end
11
+
12
+ it "should have equal ids when option use_billing is active" do
13
+ address = Factory(:address)
14
+ @order = Factory(:order, :use_billing => true,
15
+ :bill_address_id => address.id,
16
+ :ship_address_id => nil)
17
+ @order = @order.reload
18
+ @order.bill_address_id.should == @order.ship_address_id
19
+ end
20
+ end
21
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,14 +1,19 @@
1
- # This file is copied to ~/spec when you run 'ruby script/generate rspec'
2
- # from the project root directory.
3
- ENV["RAILS_ENV"] ||= 'test'
4
- require File.expand_path("../test_app/config/environment", __FILE__)
1
+ # Configure Rails Environment
2
+ ENV["RAILS_ENV"] ||= "test"
3
+
4
+ require File.expand_path("../dummy/config/environment.rb", __FILE__)
5
+
5
6
  require 'rspec/rails'
6
7
 
7
- # Requires supporting files with custom matchers and macros, etc,
8
- # in ./support/ and its subdirectories.
9
- Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
8
+ # Requires supporting ruby files with custom matchers and macros, etc,
9
+ # in spec/support/ and its subdirectories.
10
+ Dir[File.join(File.dirname(__FILE__), "support/**/*.rb")].each {|f| require f }
10
11
 
11
- require 'spree_core/testing_support/factories'
12
+ # Requires factories defined in spree_core
13
+ require 'spree/core/testing_support/factories'
14
+ require 'spree/core/testing_support/env'
15
+
16
+ require 'ffaker'
12
17
 
13
18
  RSpec.configure do |config|
14
19
  # == Mock Framework
@@ -20,6 +25,7 @@ RSpec.configure do |config|
20
25
  # config.mock_with :rr
21
26
  config.mock_with :rspec
22
27
 
28
+ # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
23
29
  config.fixture_path = "#{::Rails.root}/spec/fixtures"
24
30
 
25
31
  #config.include Devise::TestHelpers, :type => :controller
@@ -27,6 +33,5 @@ RSpec.configure do |config|
27
33
  # examples within a transaction, comment the following line or assign false
28
34
  # instead of true.
29
35
  config.use_transactional_fixtures = true
36
+
30
37
  end
31
-
32
- @configuration ||= AppConfiguration.find_or_create_by_name("Default configuration")