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

Add store promotion rule #2552

Merged
merged 1 commit into from
Feb 24, 2018
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<div class="field">
<label><%= t("spree.store_rule.choose_stores" ) %></label>
<%= select_tag "#{param_prefix}[store_ids][]",
options_from_collection_for_select(Spree::Store.all, :id, :name, promotion_rule.store_ids),
multiple: true, class: "select2 fullwidth" %>
</div>
20 changes: 20 additions & 0 deletions core/app/models/spree/promotion/rules/store.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module Spree
class Promotion
module Rules
class Store < PromotionRule
has_many :promotion_rule_stores, class_name: "Spree::PromotionRuleStore",
foreign_key: :promotion_rule_id,
dependent: :destroy
has_many :stores, through: :promotion_rule_stores, class_name: "Spree::Store"

def applicable?(promotable)
promotable.is_a?(Spree::Order)
end

def eligible?(order, _options = {})
stores.none? || stores.include?(order.store)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it seems really weird to check stores.none? here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is a little odd. I'm not sure why it was originally written this way in solidus_multi_domain, but I figured it would be best to keep it the same.

end
end
end
end
end
8 changes: 8 additions & 0 deletions core/app/models/spree/promotion_rule_store.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module Spree
class PromotionRuleStore < Spree::Base
self.table_name = "spree_promotion_rules_stores"

belongs_to :promotion_rule, class_name: "Spree::PromotionRule"
belongs_to :store, class_name: "Spree::Store"
end
end
4 changes: 4 additions & 0 deletions core/config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,8 @@ en:
form_text: "Apply this promotion to users whose last order was more than X days ago: "
spree/promotion/rules/user_role:
description: Order includes User with specified Role(s)
spree/promotion/rules/store:
description: Available only to the specified stores
spree/promotion_category:
name: Name
spree/property:
Expand Down Expand Up @@ -1966,6 +1968,8 @@ en:
user_has_no_store_credits: "User does not have any available store credit"
store_credit_category:
default: Default
store_rule:
choose_stores: Choose Stores
street_address: Street Address
street_address_2: Street Address (cont'd)
subtotal: Subtotal
Expand Down
10 changes: 10 additions & 0 deletions core/db/migrate/20180202190713_create_promotion_rule_stores.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreatePromotionRuleStores < ActiveRecord::Migration[5.1]
def change
create_table :spree_promotion_rules_stores do |t|
t.references :store, foreign_key: { to_table: "spree_stores" }
t.references :promotion_rule, foreign_key: { to_table: "spree_promotion_rules" }

t.timestamps
end
end
end
1 change: 1 addition & 0 deletions core/lib/spree/app_configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,7 @@ def environment
Spree::Promotion::Rules::OptionValue
Spree::Promotion::Rules::FirstRepeatPurchaseSince
Spree::Promotion::Rules::UserRole
Spree::Promotion::Rules::Store
]

promos.actions = %w[
Expand Down
33 changes: 33 additions & 0 deletions core/spec/models/spree/promotion/rules/store_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require "rails_helper"

RSpec.describe Spree::Promotion::Rules::Store, type: :model do
let(:rule) { described_class.new }

context "#eligible?(order)" do
let(:order) { Spree::Order.new }

it "is eligible if no stores are provided" do
expect(rule).to be_eligible(order)
end

it "is eligible if stores include the order's store" do
default_store = Spree::Store.new(name: "Default")
other_store = Spree::Store.new(name: "Other")

rule.stores = [default_store, other_store]
order.store = default_store

expect(rule).to be_eligible(order)
end

it "is not eligible if order is placed in a different store" do
default_store = Spree::Store.new(name: "Default")
other_store = Spree::Store.new(name: "Other")

rule.stores = [other_store]
order.store = default_store

expect(rule).not_to be_eligible(order)
end
end
end