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

Extract Legacy Promotion System: Move ActiveRecord Models and Factories #5634

Merged
merged 16 commits into from
Jun 21, 2024
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
34 changes: 3 additions & 31 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ Layout/EmptyLinesAroundAttributeAccessor:
Exclude:
- "core/app/models/spree/address/state_validator.rb"
- "core/app/models/spree/order_updater.rb"
- "legacy_promotions/app/models/spree/promotion/order_adjustments_recalculator.rb"
- "core/app/models/spree/stock_quantities.rb"
- "core/app/models/spree/variant.rb"
- "core/lib/spree/app_configuration.rb"
Expand All @@ -107,14 +106,6 @@ Layout/EmptyLinesAroundBlockBody:
Exclude:
- "core/spec/models/spree/order/checkout_spec.rb"

# Offense count: 1
# This cop supports safe autocorrection (--autocorrect).
# Configuration parameters: EnforcedStyle.
# SupportedStyles: empty_lines, empty_lines_except_namespace, empty_lines_special, no_empty_lines, beginning_only, ending_only
Layout/EmptyLinesAroundClassBody:
Exclude:
- "legacy_promotions/app/models/spree/promotion/order_adjustments_recalculator.rb"

# Offense count: 4
# This cop supports safe autocorrection (--autocorrect).
# Configuration parameters: AllowForAlignment, AllowBeforeTrailingComments, ForceEqualSignAlignment.
Expand Down Expand Up @@ -280,13 +271,6 @@ Lint/UnusedMethodArgument:
Naming/MethodParameterName:
Exclude:

# Offense count: 1
# This cop supports safe autocorrection (--autocorrect).
# Configuration parameters: SafeMultiline.
Performance/DeletePrefix:
Exclude:
- "legacy_promotions/app/models/spree/promotion_handler/page.rb"

# Offense count: 3
# This cop supports safe autocorrection (--autocorrect).
Performance/RegexpMatch:
Expand Down Expand Up @@ -329,7 +313,7 @@ Rails/ApplicationRecord:
- "core/db/migrate/20170412103617_transform_tax_rate_category_relation.rb"
- "core/db/migrate/20180322142651_add_amount_remaining_to_store_credit_events.rb"
- "core/db/migrate/20180710170104_create_spree_store_credit_reasons_table.rb"
- "core/db/migrate/20190106184413_remove_code_from_spree_promotions.rb"
- "legacy_promotions/db/migrate/20190106184413_remove_code_from_spree_promotions.rb"
mamhoff marked this conversation as resolved.
Show resolved Hide resolved
- "core/spec/lib/calculated_adjustments_spec.rb"
- "core/spec/models/spree/validations/db_maximum_length_validator_spec.rb"
- "core/spec/models/spree/wallet_payment_source_spec.rb"
Expand All @@ -343,7 +327,6 @@ Rails/Blank:
- "core/app/models/spree/line_item.rb"
- "core/app/models/spree/order.rb"
- "core/app/models/spree/order_contents.rb"
- "core/app/models/spree/promotion/rules/user_logged_in.rb"
- "core/app/models/spree/reimbursement_type/exchange.rb"
- "core/app/models/spree/wallet_payment_source.rb"
- "core/app/models/spree/zone.rb"
Expand Down Expand Up @@ -431,12 +414,6 @@ Rails/OutputSafety:
- "core/app/helpers/spree/products_helper.rb"
- "core/lib/spree/money.rb"

# Offense count: 1
# This cop supports unsafe autocorrection (--autocorrect-all).
Rails/Pick:
Exclude:
- "core/spec/models/spree/promotion/rules/option_value_spec.rb"

# Offense count: 1
# This cop supports safe autocorrection (--autocorrect).
Rails/Presence:
Expand All @@ -453,7 +430,6 @@ Rails/Present:
- "api/app/controllers/spree/api/taxons_controller.rb"
- "backend/app/helpers/spree/admin/stock_movements_helper.rb"
- "core/app/models/concerns/spree/ordered_property_value_list.rb"
- "core/app/models/spree/promotion/rules/first_order.rb"
- "core/app/models/spree/stock/availability_validator.rb"
- "core/lib/spree/core/search/base.rb"
- "core/spec/models/spree/stock/availability_validator_spec.rb"
Expand All @@ -475,8 +451,8 @@ Rails/ReflectionClassName:
Exclude:
- "core/app/models/spree/credit_card.rb"
- "core/app/models/spree/order.rb"
- "core/app/models/spree/promotion/rules/user.rb"
- "core/app/models/spree/promotion_rule_user.rb"
- "legacy_promotions/app/models/spree/promotion/rules/user.rb"
- "legacy_promotions/app/models/spree/promotion_rule_user.rb"
- "core/app/models/spree/role_user.rb"
- "core/app/models/spree/store_credit.rb"
- "core/app/models/spree/user_address.rb"
Expand Down Expand Up @@ -568,7 +544,6 @@ Style/CommentAnnotation:
- "backend/app/controllers/spree/admin/products_controller.rb"
- "backend/app/controllers/spree/admin/resource_controller.rb"
- "core/app/models/spree/payment_method/store_credit.rb"
- "core/app/models/spree/promotion.rb"
- "core/lib/spree/permission_sets/user_management.rb"
- "core/lib/spree/testing_support/rake.rb"
- "core/spec/models/spree/variant/scopes_spec.rb"
Expand Down Expand Up @@ -660,7 +635,6 @@ Style/RedundantAssignment:
# This cop supports safe autocorrection (--autocorrect).
Style/RedundantBegin:
Exclude:
- "legacy_promotions/app/models/spree/promotion_handler/coupon.rb"
- "core/app/models/spree/user_last_url_storer/rules/authentication_rule.rb"
- "core/spec/models/spree/stock/simple_coordinator_spec.rb"

Expand Down Expand Up @@ -729,7 +703,6 @@ Style/RedundantReturn:
Style/RedundantSelf:
Exclude:
- "core/app/models/spree/product.rb"
- "core/app/models/spree/promotion_code.rb"

# Offense count: 1
# This cop supports unsafe autocorrection (--autocorrect-all).
Expand All @@ -749,7 +722,6 @@ Style/SafeNavigation:
# Configuration parameters: AllowModifier.
Style/SoleNestedConditional:
Exclude:
- "core/app/models/spree/promotion/rules/first_order.rb"
- "core/lib/spree/core/controller_helpers/order.rb"

# Offense count: 13
Expand Down
55 changes: 0 additions & 55 deletions core/app/models/spree/adjustment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,12 @@ class Adjustment < Spree::Base
belongs_to :adjustable, polymorphic: true, touch: true, optional: true
belongs_to :source, polymorphic: true, optional: true
belongs_to :order, class_name: 'Spree::Order', inverse_of: :all_adjustments, optional: true
belongs_to :promotion_code, class_name: 'Spree::PromotionCode', optional: true
belongs_to :adjustment_reason, class_name: 'Spree::AdjustmentReason', inverse_of: :adjustments, optional: true

validates :adjustable, presence: true
validates :order, presence: true
validates :label, presence: true
validates :amount, numericality: true
validates :promotion_code, presence: true, if: :require_promotion_code?

scope :not_finalized, -> { where(finalized: false) }
scope :finalized, -> { where(finalized: true) }
Expand Down Expand Up @@ -89,58 +87,5 @@ def tax?
def cancellation?
source_type == 'Spree::UnitCancel'
end

# Recalculate and persist the amount from this adjustment's source based on
# the adjustable ({Order}, {Shipment}, or {LineItem})
#
# If the adjustment has no source (such as when created manually from the
# admin) or is closed, this is a noop.
#
# @return [BigDecimal] New amount of this adjustment
def recalculate
if finalized? && !tax?
return amount
end

# If the adjustment has no source, do not attempt to re-calculate the
# amount.
# Some scenarios where this happens:
# - Adjustments that are manually created via the admin backend
# - PromotionAction adjustments where the PromotionAction was deleted
# after the order was completed.
if source.present?
self.amount = source.compute_amount(adjustable)

if promotion?
self.eligible = calculate_eligibility
end

# Persist only if changed
# This is only not a save! to avoid the extra queries to load the order
# (for validations) and to touch the adjustment.
update_columns(eligible: eligible, amount: amount, updated_at: Time.current) if changed?
end
amount
end
deprecate :recalculate, deprecator: Spree.deprecator

# Calculates based on attached promotion (if this is a promotion
# adjustment) whether this promotion is still eligible.
# @api private
# @return [true,false] Whether this adjustment is eligible
def calculate_eligibility
if !finalized? && source && promotion?
source.promotion.eligible?(adjustable, promotion_code: promotion_code)
else
eligible?
end
end
deprecate :calculate_eligibility, deprecator: Spree.deprecator

private

def require_promotion_code?
promotion? && !source.promotion.apply_automatically && source.promotion.codes.any?
end
end
end
2 changes: 0 additions & 2 deletions core/app/models/spree/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,6 @@ def states
foreign_key: :order_id,
dependent: :destroy,
inverse_of: :order
has_many :order_promotions, class_name: 'Spree::OrderPromotion', dependent: :destroy
has_many :promotions, through: :order_promotions

# Payments
has_many :payments, dependent: :destroy, inverse_of: :order
Expand Down
4 changes: 0 additions & 4 deletions core/app/models/spree/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ class Product < Spree::Base
self.product_option_types = []
self.product_properties = []
self.classifications.destroy_all
self.product_promotion_rules = []
end

has_many :product_option_types, dependent: :destroy, inverse_of: :product
Expand All @@ -31,9 +30,6 @@ class Product < Spree::Base
has_many :classifications, dependent: :delete_all, inverse_of: :product
has_many :taxons, through: :classifications, before_remove: :remove_taxon

has_many :product_promotion_rules, dependent: :destroy
has_many :promotion_rules, through: :product_promotion_rules

belongs_to :tax_category, class_name: 'Spree::TaxCategory', optional: true
belongs_to :shipping_category, class_name: 'Spree::ShippingCategory', inverse_of: :products, optional: true

Expand Down
103 changes: 0 additions & 103 deletions core/db/migrate/20160101010000_solidus_one_four.rb
Original file line number Diff line number Diff line change
Expand Up @@ -411,15 +411,6 @@ def up
t.index ["product_id"], name: "index_spree_product_option_types_on_product_id"
end

create_table "spree_product_promotion_rules", force: :cascade do |t|
t.integer "product_id"
t.integer "promotion_rule_id"
t.datetime "created_at", precision: 6
t.datetime "updated_at", precision: 6
t.index ["product_id"], name: "index_products_promotion_rules_on_product_id"
t.index ["promotion_rule_id"], name: "index_products_promotion_rules_on_promotion_rule_id"
end

create_table "spree_product_properties", force: :cascade do |t|
t.string "value"
t.integer "product_id"
Expand Down Expand Up @@ -463,100 +454,6 @@ def up
t.index ["taxon_id"], name: "index_spree_products_taxons_on_taxon_id"
end

create_table "spree_promotion_action_line_items", force: :cascade do |t|
t.integer "promotion_action_id"
t.integer "variant_id"
t.integer "quantity", default: 1
t.datetime "created_at", precision: 6
t.datetime "updated_at", precision: 6
t.index ["promotion_action_id"], name: "index_spree_promotion_action_line_items_on_promotion_action_id"
t.index ["variant_id"], name: "index_spree_promotion_action_line_items_on_variant_id"
end

create_table "spree_promotion_actions", force: :cascade do |t|
t.integer "promotion_id"
t.integer "position"
t.string "type"
t.datetime "deleted_at"
t.text "preferences"
t.datetime "created_at", precision: 6
t.datetime "updated_at", precision: 6
t.index ["deleted_at"], name: "index_spree_promotion_actions_on_deleted_at"
t.index ["id", "type"], name: "index_spree_promotion_actions_on_id_and_type"
t.index ["promotion_id"], name: "index_spree_promotion_actions_on_promotion_id"
end

create_table "spree_promotion_categories", force: :cascade do |t|
t.string "name"
t.datetime "created_at", precision: 6
t.datetime "updated_at", precision: 6
t.string "code"
end

create_table "spree_promotion_codes", force: :cascade do |t|
t.integer "promotion_id", null: false
t.string "value", null: false
t.datetime "created_at", precision: 6
t.datetime "updated_at", precision: 6
t.index ["promotion_id"], name: "index_spree_promotion_codes_on_promotion_id"
t.index ["value"], name: "index_spree_promotion_codes_on_value", unique: true
end

create_table "spree_promotion_rule_taxons", force: :cascade do |t|
t.integer "taxon_id"
t.integer "promotion_rule_id"
t.datetime "created_at", precision: 6
t.datetime "updated_at", precision: 6
t.index ["promotion_rule_id"], name: "index_spree_promotion_rule_taxons_on_promotion_rule_id"
t.index ["taxon_id"], name: "index_spree_promotion_rule_taxons_on_taxon_id"
end

create_table "spree_promotion_rules", force: :cascade do |t|
t.integer "promotion_id"
t.integer "product_group_id"
t.string "type"
t.datetime "created_at", precision: 6
t.datetime "updated_at", precision: 6
t.string "code"
t.text "preferences"
t.index ["product_group_id"], name: "index_promotion_rules_on_product_group_id"
t.index ["promotion_id"], name: "index_spree_promotion_rules_on_promotion_id"
end

create_table "spree_promotion_rules_users", force: :cascade do |t|
t.integer "user_id"
t.integer "promotion_rule_id"
t.datetime "created_at", precision: 6
t.datetime "updated_at", precision: 6
t.index ["promotion_rule_id"], name: "index_promotion_rules_users_on_promotion_rule_id"
t.index ["user_id"], name: "index_promotion_rules_users_on_user_id"
end

create_table "spree_promotions", force: :cascade do |t|
t.string "description"
t.datetime "expires_at"
t.datetime "starts_at"
t.string "name"
t.string "type"
t.integer "usage_limit"
t.string "match_policy", default: "all"
t.string "code"
t.boolean "advertise", default: false
t.string "path"
t.datetime "created_at", precision: 6
t.datetime "updated_at", precision: 6
t.integer "promotion_category_id"
t.integer "per_code_usage_limit"
t.boolean "apply_automatically", default: false
t.index ["advertise"], name: "index_spree_promotions_on_advertise"
t.index ["apply_automatically"], name: "index_spree_promotions_on_apply_automatically"
t.index ["code"], name: "index_spree_promotions_on_code"
t.index ["expires_at"], name: "index_spree_promotions_on_expires_at"
t.index ["id", "type"], name: "index_spree_promotions_on_id_and_type"
t.index ["promotion_category_id"], name: "index_spree_promotions_on_promotion_category_id"
t.index ["starts_at"], name: "index_spree_promotions_on_starts_at"
end

create_table "spree_properties", force: :cascade do |t|
t.string "name"
t.string "presentation", null: false
Expand Down

This file was deleted.

12 changes: 0 additions & 12 deletions core/db/migrate/20180202190713_create_promotion_rule_stores.rb

This file was deleted.

This file was deleted.

Loading