-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #75 from friendlycart/goodies
Goodies!
- Loading branch information
Showing
19 changed files
with
269 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
promotions/app/models/concerns/solidus_friendly_promotions/actions/order_level_action.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# frozen_string_literal: true | ||
|
||
module SolidusFriendlyPromotions | ||
module Actions | ||
module OrderLevelAction | ||
def can_discount?(_) | ||
false | ||
end | ||
|
||
def level | ||
:order | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
promotions/app/models/solidus_friendly_promotions/actions/create_discounted_item.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# frozen_string_literal: true | ||
|
||
module SolidusFriendlyPromotions | ||
module Actions | ||
class CreateDiscountedItem < PromotionAction | ||
include OrderLevelAction | ||
preference :variant_id, :integer | ||
preference :quantity, :integer, default: 1 | ||
|
||
def perform(order) | ||
line_item = find_item(order) || create_item(order) | ||
line_item.current_discounts << discount(line_item) | ||
end | ||
|
||
def remove_from(order) | ||
line_item = find_item(order) | ||
order.line_items.destroy(line_item) | ||
end | ||
|
||
private | ||
|
||
def find_item(order) | ||
order.line_items.detect { |line_item| line_item.managed_by_order_action == self } | ||
end | ||
|
||
def create_item(order) | ||
order.line_items.create!(quantity: preferred_quantity, variant: variant, managed_by_order_action: self) | ||
end | ||
|
||
def variant | ||
Spree::Variant.find(preferred_variant_id) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
...idus_friendly_promotions/admin/promotion_actions/actions/_create_discounted_item.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<%= fields_for param_prefix, promotion_action do |form| %> | ||
<div class="field"> | ||
<%= form.label :preferred_variant_id %> | ||
<%= form.text_field :preferred_variant_id, class: "variant_autocomplete fullwidth" %> | ||
<%= form.label :preferred_quantity %> | ||
<%= form.number_field :preferred_quantity, class: "fullwidth" %> | ||
</div> | ||
<% end %> | ||
|
||
<%= render( | ||
"solidus_friendly_promotions/admin/promotion_actions/actions/calculator_fields", | ||
promotion_action: promotion_action, | ||
param_prefix: param_prefix, | ||
form: form | ||
) %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
promotions/db/migrate/20231104135812_add_managed_by_order_action_to_line_items.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class AddManagedByOrderActionToLineItems < ActiveRecord::Migration[7.0] | ||
def change | ||
add_reference :spree_line_items, :managed_by_order_action, foreign_key: {to_table: :friendly_promotion_actions, null: true} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
promotions/spec/models/solidus_friendly_promotions/actions/create_discounted_item_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# frozen_string_literal: true | ||
|
||
require "spec_helper" | ||
|
||
RSpec.describe SolidusFriendlyPromotions::Actions::CreateDiscountedItem do | ||
it { is_expected.to respond_to(:preferred_variant_id) } | ||
|
||
describe "#perform" do | ||
let(:order) { create(:order_with_line_items) } | ||
let(:promotion) { create(:friendly_promotion) } | ||
let(:action) { SolidusFriendlyPromotions::Actions::CreateDiscountedItem.new(preferred_variant_id: goodie.id, calculator: hundred_percent, promotion: promotion) } | ||
let(:hundred_percent) { SolidusFriendlyPromotions::Calculators::Percent.new(preferred_percent: 100) } | ||
let(:goodie) { create(:variant) } | ||
subject { action.perform(order) } | ||
|
||
it "creates a line item with a hundred percent discount" do | ||
expect { subject }.to change { order.line_items.count }.by(1) | ||
created_item = order.line_items.detect { |line_item| line_item.managed_by_order_action == action } | ||
expect(created_item.discountable_amount).to be_zero | ||
end | ||
|
||
it "never calls the order recalculator" do | ||
expect(order).not_to receive(:recalculate) | ||
end | ||
end | ||
end |
Oops, something went wrong.