Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create migration #1626

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions backend/app/views/spree/admin/shipping_methods/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,6 @@
<% end %>
</div>

<div data-hook="admin_shipping_method_form_display_field" class="col-xs-5">
<%= f.field_container :display_on do %>
<%= f.label :display_on %><br />
<%= select(:shipping_method, :display_on, Spree::ShippingMethod::DISPLAY.collect { |display| [Spree.t(display), display == :both ? nil : display.to_s] }, {}, {:class => 'select2 fullwidth'}) %>
<%= error_message_on :shipping_method, :display_on %>
<% end %>
</div>

<div data-hook="admin_shipping_method_form_internal_name_field" class="col-xs-5">
<%= f.field_container :admin_name do %>
<%= f.label :admin_name %><br />
Expand Down Expand Up @@ -54,6 +46,17 @@
<%= error_message_on :shipping_method, :tracking_url %>
<% end %>
</div>

<div data-hook="admin_shipping_method_form_display_field" class="col-xs-10">
<div data-hook="available_to_users" class="field">
<%= label_tag nil, Spree::ShippingMethod.human_attribute_name(:available_to_users) %>
<%= f.check_box :available_to_users %>
</div>
<div data-hook="available_to_admin" class="field">
<%= label_tag nil, Spree::ShippingMethod.human_attribute_name(:available_to_admin) %>
<%= f.check_box :available_to_admin %>
</div>
</div>
</div>

<div class="row">
Expand Down
9 changes: 6 additions & 3 deletions backend/app/views/spree/admin/shipping_methods/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@
<col style="width: 20%">
<col style="width: 15%">
<col style="width: 40%">
<col style="width: 10%">
<col style="width: 5%">
<col style="width: 5%">
<col style="width: 15%">
</colgroup>
<thead>
<tr data-hook="admin_shipping_methods_index_headers">
<th><%= Spree::ShippingMethod.human_attribute_name(:name) %></th>
<th><%= Spree::Zone.model_name.human %></th>
<th><%= Spree::Calculator.model_name.human %></th>
<th><%= Spree::ShippingMethod.human_attribute_name(:display_on) %></th>
<th><%= Spree::ShippingMethod.human_attribute_name(:available_to_users) %></th>
<th><%= Spree::ShippingMethod.human_attribute_name(:available_to_admin) %></th>
<th data-hook="admin_shipping_methods_index_header_actions" class="actions"></th>
</tr>
</thead>
Expand All @@ -37,7 +39,8 @@
<td class="align-center"><%= shipping_method.admin_name + ' / ' if shipping_method.admin_name.present? %><%= shipping_method.name %></td>
<td class="align-center"><%= shipping_method.zones.collect(&:name).join(", ") if shipping_method.zones %></td>
<td class="align-center"><%= shipping_method.calculator.description %></td>
<td class="align-center"><%= shipping_method.display_on.blank? ? Spree.t(:both) : Spree.t(shipping_method.display_on.downcase.tr(" ", "_")) %></td>
<td class="align-center"><%= shipping_method.available_to_users ? Spree.t(:say_yes) : Spree.t(:say_no) %></td>
<td class="align-center"><%= shipping_method.available_to_admin ? Spree.t(:say_yes) : Spree.t(:say_no) %></td>
<td data-hook="admin_shipping_methods_index_row_actions" class="actions">
<% if can?(:update, shipping_method) %>
<%= link_to_edit shipping_method, :no_text => true %>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
expect(column_text(1)).to eq(shipping_method.name)
expect(column_text(2)).to eq(zone.name)
expect(column_text(3)).to eq("Flat rate")
expect(column_text(4)).to eq("Both")
expect(column_text(4)).to eq("Yes")
expect(column_text(5)).to eq("Yes")
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

let(:country) { create(:country, name: "Kangaland") }
let(:state) { create(:state, name: "Alabama", country: country) }
let!(:shipping_method) { create(:shipping_method, display_on: "front_end") }
let!(:shipping_method) { create(:shipping_method, available_to_users: true, available_to_admin: false) }
let!(:order) { create(:order, ship_address: ship_address, bill_address: bill_address, state: 'complete', completed_at: "2011-02-01 12:36:15") }
let!(:product) { create(:product_in_stock) }

Expand Down
6 changes: 4 additions & 2 deletions core/app/models/spree/shipping_method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ module Spree
class ShippingMethod < Spree::Base
acts_as_paranoid
include Spree::CalculatedAdjustments
DISPLAY = [:both, :front_end, :back_end]

has_many :shipping_method_categories, dependent: :destroy
has_many :shipping_categories, through: :shipping_method_categories
Expand All @@ -23,6 +22,9 @@ class ShippingMethod < Spree::Base

validate :at_least_one_shipping_category

scope :available_to_users, -> { where(available_to_users: true) }
scope :available_to_admin, -> { where(available_to_admin: true) }

# @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 Expand Up @@ -81,7 +83,7 @@ def build_tracking_url(tracking)

# Some shipping methods are only meant to be set via backend
def frontend?
display_on != "back_end"
available_to_users
end

private
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class AddAvailableToColumnsAndRemoveDisplayOnFromShippingMethods < ActiveRecord::Migration[5.0]
def up
add_column(:spree_shipping_methods, :available_to_users, :boolean, default: true)
add_column(:spree_shipping_methods, :available_to_admin, :boolean, default: true)
execute("UPDATE spree_shipping_methods "\
"SET available_to_users=#{quoted_false} "\
"WHERE NOT (display_on='front_end' OR display_on='' OR display_on IS NULL)")
execute("UPDATE spree_shipping_methods "\
"SET available_to_admin=#{quoted_false} "\
"WHERE NOT (display_on='back_end' OR display_on='' OR display_on IS NULL)")
remove_column(:spree_shipping_methods, :display_on)
end

def down
add_column(:spree_shipping_methods, :display_on, :string)
execute("UPDATE spree_shipping_methods "\
"SET display_on='' "\
"WHERE (available_to_users=#{quoted_true} AND available_to_admin=#{quoted_true})")
execute("UPDATE spree_shipping_methods "\
"SET display_on='front_end' "\
"WHERE (available_to_users=#{quoted_true} AND NOT available_to_admin=#{quoted_true})")
execute("UPDATE spree_shipping_methods "\
"SET display_on='back_end' "\
"WHERE (available_to_admin=#{quoted_true} AND NOT available_to_users=#{quoted_true})")
remove_column(:spree_shipping_methods, :available_to_users)
remove_column(:spree_shipping_methods, :available_to_admin)
end
end
2 changes: 1 addition & 1 deletion core/spec/models/spree/stock/estimator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ module Stock

context "involves backend only shipping methods" do
before{ Spree::ShippingMethod.destroy_all }
let!(:backend_method) { create(:shipping_method, display_on: "back_end", cost: 0.00) }
let!(:backend_method) { create(:shipping_method, available_to_admin: true, available_to_users: false, cost: 0.00) }
let!(:generic_method) { create(:shipping_method, cost: 5.00) }

it "does not return backend rates at all" do
Expand Down