Skip to content

Commit

Permalink
Merge pull request solidusio#2557 from adammathys/store-shipping-methods
Browse files Browse the repository at this point in the history
Add association between stores and shipping
  • Loading branch information
gmacdougall authored Feb 21, 2018
2 parents b3f1d12 + d304dea commit 1f4cd58
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 0 deletions.
8 changes: 8 additions & 0 deletions backend/app/views/spree/admin/shipping_methods/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@
<% end %>
</div>

<div class="col-5">
<%= f.field_container :store_ids do %>
<%= f.label :store_ids, plural_resource_name(Spree::Store) %>
<%= f.collection_select :store_ids, Spree::Store.all, :id, :name, {}, multiple: true, class: "select2 fullwidth" %>
<%= error_message_on :shipping_method, :store_ids %>
<% end %>
</div>

<div data-hook="admin_shipping_method_form_tracking_url_field" class="col-10">
<%= f.field_container :tracking_url do %>
<%= f.label :tracking_url %><br />
Expand Down
8 changes: 8 additions & 0 deletions core/app/models/spree/shipping_method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,18 @@ class ShippingMethod < Spree::Base
has_many :shipping_method_stock_locations, dependent: :destroy, class_name: "Spree::ShippingMethodStockLocation"
has_many :stock_locations, through: :shipping_method_stock_locations

has_many :store_shipping_methods, inverse_of: :shipping_method
has_many :stores, through: :store_shipping_methods

validates :name, presence: true

validate :at_least_one_shipping_category

scope :available_to_store, ->(store) do
raise ArgumentError, "You must provide a store" if store.nil?
store.shipping_methods.empty? ? all : where(id: store.shipping_method_ids)
end

# @param shipping_category_ids [Array<Integer>] ids of desired shipping categories
# @return [ActiveRecord::Relation] shipping methods which are associated
# with all of the provided shipping categories
Expand Down
1 change: 1 addition & 0 deletions core/app/models/spree/stock/estimator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def calculate_shipping_rates(package)

def shipping_methods(package)
package.shipping_methods
.available_to_store(package.shipment.order.store)
.available_for_address(package.shipment.order.ship_address)
.includes(:calculator)
.to_a
Expand Down
4 changes: 4 additions & 0 deletions core/app/models/spree/store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ module Spree
class Store < Spree::Base
has_many :store_payment_methods, inverse_of: :store
has_many :payment_methods, through: :store_payment_methods

has_many :store_shipping_methods, inverse_of: :store
has_many :shipping_methods, through: :store_shipping_methods

has_many :orders, class_name: "Spree::Order"

validates :code, presence: true, uniqueness: { allow_blank: true }
Expand Down
6 changes: 6 additions & 0 deletions core/app/models/spree/store_shipping_method.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module Spree
class StoreShippingMethod < Spree::Base
belongs_to :store, inverse_of: :store_shipping_methods
belongs_to :shipping_method, inverse_of: :store_shipping_methods
end
end
10 changes: 10 additions & 0 deletions core/db/migrate/20180202222641_create_store_shipping_methods.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateStoreShippingMethods < ActiveRecord::Migration[5.1]
def change
create_table :spree_store_shipping_methods do |t|
t.references :store, foreign_key: { to_table: "spree_stores" }
t.references :shipping_method, foreign_key: { to_table: "spree_shipping_methods" }

t.timestamps
end
end
end
28 changes: 28 additions & 0 deletions core/spec/models/spree/stock/estimator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,34 @@ module Stock
end
end

context "excludes shipping methods from other stores" do
before{ Spree::ShippingMethod.all.each(&:really_destroy!) }

let!(:other_method) do
create(
:shipping_method,
cost: 0.00,
stores: [build(:store, name: "Other")]
)
end

let!(:main_method) do
create(
:shipping_method,
cost: 5.00,
stores: [order.store]
)
end

it "does not return the other rate at all" do
expect(subject.shipping_rates(package).map(&:shipping_method_id)).to eq([main_method.id])
end

it "doesn't select the other rate even if it's more affordable" do
expect(subject.shipping_rates(package).map(&:selected)).to eq [true]
end
end

context "includes tax adjustments if applicable" do
let(:zone) { create(:zone, countries: [order.tax_address.country])}

Expand Down

0 comments on commit 1f4cd58

Please sign in to comment.