Skip to content

Commit

Permalink
Merge pull request #3502 from MassimilianoLattanzio/disable-codes-on-…
Browse files Browse the repository at this point in the history
…apply-automatically-promo

Disable codes on apply automatically promo
  • Loading branch information
aldesantis authored Apr 21, 2020
2 parents f38a7a4 + 03a3a9f commit 4e497d9
Show file tree
Hide file tree
Showing 13 changed files with 78 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ def index

def new
@promotion = Spree::Promotion.accessible_by(current_ability, :read).find(params[:promotion_id])
@promotion_code = @promotion.promotion_codes.build
if @promotion.apply_automatically
flash[:error] = t('activerecord.errors.models.spree/promotion_code.attributes.base.disallowed_with_apply_automatically')
redirect_to admin_promotion_promotion_codes_url(@promotion)
else
@promotion_code = @promotion.promotion_codes.build
end
end

def create
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<% content_for :page_actions do %>
<li>
<% if can?(:create, Spree::PromotionCode) %>
<% if can?(:create, Spree::PromotionCode) && !@promotion.apply_automatically? %>
<%= link_to t('spree.create_promotion_code'), new_admin_promotion_promotion_code_path(promotion_id: @promotion.id), class: 'btn btn-primary' %>
<% end %>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<div class="form-check">
<label class="form-check-label">
<%= radio_button_tag('activation_type', 'auto', activation_type == 'auto') %>
<%= t('.auto') %>
<%= t('.auto') %> <%= f.field_hint :promo_code_will_be_disabled %>
</label>
</div>
<div class="form-check">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,11 @@
expect(flash[:error]).not_to be_nil
expect(Spree::PromotionCode.where(promotion: promotion).count).to eql(3)
end

it "can't create a new code on promotions that apply automatically" do
apply_automatically_promotion = create(:promotion, apply_automatically: true)
get :new, params: { promotion_id: apply_automatically_promotion.id }
expect(response).to redirect_to(spree.admin_promotion_promotion_codes_path(apply_automatically_promotion))
expect(flash[:error]).not_to be_nil
end
end
2 changes: 1 addition & 1 deletion core/app/models/spree/adjustment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def calculate_eligibility
private

def require_promotion_code?
promotion? && source.promotion.codes.any?
promotion? && !source.promotion.apply_automatically && source.promotion.codes.any?
end

def repair_adjustments_associations_on_create
Expand Down
2 changes: 1 addition & 1 deletion core/app/models/spree/order_promotion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class OrderPromotion < Spree::Base
private

def require_promotion_code?
promotion && promotion.codes.any?
promotion && !promotion.apply_automatically && promotion.codes.any?
end
end
end
5 changes: 2 additions & 3 deletions core/app/models/spree/promotion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Promotion < Spree::Base
validates :usage_limit, numericality: { greater_than: 0, allow_nil: true }
validates :per_code_usage_limit, numericality: { greater_than_or_equal_to: 0, allow_nil: true }
validates :description, length: { maximum: 255 }
validate :apply_automatically_disallowed_with_codes_or_paths
validate :apply_automatically_disallowed_with_paths

before_save :normalize_blank_values

Expand Down Expand Up @@ -257,10 +257,9 @@ def match_all?
match_policy == "all"
end

def apply_automatically_disallowed_with_codes_or_paths
def apply_automatically_disallowed_with_paths
return unless apply_automatically

errors.add(:apply_automatically, :disallowed_with_code) if codes.any?
errors.add(:apply_automatically, :disallowed_with_path) if path.present?
end

Expand Down
5 changes: 5 additions & 0 deletions core/app/models/spree/promotion_code.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class Spree::PromotionCode < Spree::Base

validates :value, presence: true, uniqueness: { allow_blank: true, case_sensitive: true }
validates :promotion, presence: true
validate :promotion_not_apply_automatically, on: :create

before_save :normalize_code

Expand Down Expand Up @@ -37,6 +38,10 @@ def usage_limit
promotion.per_code_usage_limit
end

def promotion_not_apply_automatically
errors.add(:base, :disallowed_with_apply_automatically) if promotion.apply_automatically
end

private

def normalize_code
Expand Down
7 changes: 6 additions & 1 deletion core/config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -479,8 +479,11 @@ en:
spree/promotion:
attributes:
apply_automatically:
disallowed_with_code: Disallowed for promotions with a code
disallowed_with_path: Disallowed for promotions with a path
spree/promotion_code:
attributes:
base:
disallowed_with_apply_automatically: Could not create promotion code on promotion that apply automatically
spree/refund:
attributes:
amount:
Expand Down Expand Up @@ -1406,6 +1409,8 @@ en:
is specified, the promotion will never expire.
starts_at: This determines when the promotion can be applied to orders. <br/>
If no value is specified, the promotion will be immediately available.
promo_code_will_be_disabled: Selecting this option, promo codes will be disabled for this promotion
because all its rules / actions will be applied automatically to all orders.
spree/stock_location:
active: 'This determines whether stock from this location can be used when
building packages.<br/> Default: Checked'
Expand Down
17 changes: 17 additions & 0 deletions core/spec/models/spree/adjustment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,23 @@
it { is_expected.to include("can't be blank") }
end
end

context "when the adjustment is a promotion that apply automatically adjustment" do
let(:adjustment) { build(:adjustment, source: promotion.actions.first) }
let(:promotion) { create(:promotion, :with_order_adjustment, apply_automatically: true) }

context "when the promotion does not have a code" do
it { is_expected.to be_blank }
end

context "when the promotion has a code" do
let!(:promotion_code) do
promotion.codes << build(:promotion_code, promotion: promotion)
end

it { is_expected.to be_blank }
end
end
end

describe 'repairing adjustment associations' do
Expand Down
24 changes: 24 additions & 0 deletions core/spec/models/spree/order_promotion_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,28 @@
it { is_expected.to include("can't be blank") }
end
end

describe "promotion code presence error on promotion that apply automatically" do
subject do
order_promotion.promotion.apply_automatically = true
order_promotion.promotion.save!
order_promotion.valid?
order_promotion.errors[:promotion_code]
end

let(:order_promotion) { build(:order_promotion) }
let(:promotion) { order_promotion.promotion }

context "when the promotion does not have a code" do
it { is_expected.to be_blank }
end

context "when the promotion has a code" do
let!(:promotion_code) do
promotion.codes << build(:promotion_code, promotion: promotion)
end

it { is_expected.to be_blank }
end
end
end
7 changes: 7 additions & 0 deletions core/spec/models/spree/promotion_code_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -203,4 +203,11 @@
}.to change{ order.reload.state }.from("confirm").to("address")
end
end

it "cannot create promotion code on apply automatically promotion" do
promotion = create(:promotion, apply_automatically: true)
expect {
create(:promotion_code, promotion: promotion)
}.to raise_error ActiveRecord::RecordInvalid, "Validation failed: Could not create promotion code on promotion that apply automatically"
end
end
6 changes: 0 additions & 6 deletions core/spec/models/spree/promotion_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,6 @@
expect(subject).to be_valid
end

it "invalidates the promotion when it has a code" do
subject.codes.build(value: "foo")
expect(subject).to_not be_valid
expect(subject.errors).to include(:apply_automatically)
end

it "invalidates the promotion when it has a path" do
subject.path = "foo"
expect(subject).to_not be_valid
Expand Down

0 comments on commit 4e497d9

Please sign in to comment.