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

[pull] main from solidusio:main #400

Merged
merged 14 commits into from
Mar 7, 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
3 changes: 3 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ commands:
- run:
name: "Run Sample Tests"
command: ./bin/build-ci sample
- run:
name: "Run Legacy Promotion Tests"
command: ./bin/build-ci legacy_promotions

- store_artifacts:
path: /tmp/test-artifacts
Expand Down
2 changes: 1 addition & 1 deletion admin/app/views/layouts/solidus_admin/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

<div class="fixed inset-x-0 bottom-3 flex items-center justify-center flex-col gap-3 pointer-events-none" role="alert">
<% flash.each do |key, message| %>
<%= render component("ui/toast").new(text: message, scheme: key == :error ? :error : :default) %>
<%= render component("ui/toast").new(text: message, scheme: key.to_sym == :error ? :error : :default) %>
<% end %>
</div>
</body>
Expand Down
2 changes: 1 addition & 1 deletion api/app/controllers/spree/api/promotions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def show
private

def load_promotion
@promotion = Spree::Promotion.with_coupon_code(params[:id]) || Spree::Promotion.find(params[:id])
@promotion = Spree::Config.promotions.promotion_finder_class.by_code_or_id(params[:id])
end
end
end
Expand Down
36 changes: 24 additions & 12 deletions api/spec/requests/spree/api/promotions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,38 @@ module Spree::Api
stub_authentication!
end

let(:promotion) { create :promotion, code: '10off' }
let(:found_promotion) do
OpenStruct.new(
id: 1,
name: 'Test Promotion',
description: 'Promotion for testing purposes',
path: '/api/promotions/test-promo',
starts_at: 1.day.ago,
expires_at: 1.day.from_now,
type: "something",
usage_limit: 100,
advertise: false,
)
end

describe 'GET #show' do
subject { get spree.api_promotion_path(id) }
subject { get spree.api_promotion_path("1") }

context 'when admin' do
sign_in_as_admin!

context 'when finding by id' do
let(:id) { promotion.id }

it_behaves_like "a JSON response"
end

context 'when finding by code' do
let(:id) { promotion.codes.first.value }
context 'when finding by a promotion' do
before do
allow(Spree::Config.promotions.promotion_finder_class).to receive(:by_code_or_id).and_return(found_promotion)
end

it_behaves_like "a JSON response"
end

context 'when id does not exist' do
let(:id) { 'argh' }
before do
allow(Spree::Config.promotions.promotion_finder_class).to receive(:by_code_or_id).and_raise(ActiveRecord::RecordNotFound)
end

it 'should be 404' do
subject
Expand All @@ -55,7 +65,9 @@ module Spree::Api
end

context 'when non admin' do
let(:id) { promotion.id }
before do
allow(Spree::Config.promotions.promotion_finder_class).to receive(:by_code_or_id).and_return(found_promotion)
end

it 'should be unauthorized' do
subject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ Spree.formatMoney = function(amount, currency) {
var thousand = Spree.t('currency_delimiter');
var decimal = Spree.t('currency_separator');

return accounting.formatMoney(amount, currencyInfo[0], currencyInfo[1], thousand, decimal, currencyInfo[2]);
return accounting.formatMoney(amount, currencyInfo[0], currencyInfo[1], thousand, decimal, currencyInfo[2]).trim();
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@
JSON.dump(
Spree::Config.available_currencies.map { |c|
format =
if c.symbol == "" || c.symbol_first
if c.format.present?
c.format.gsub("%u", "%s").gsub("%n", "%v")
elsif c.symbol == "" || c.symbol_first
"%s%v"
else
"%v %s"
end

[c.id.to_s.upcase, [
c.symbol || "¤",
c.exponent,
Expand Down
13 changes: 7 additions & 6 deletions bin/build-ci
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ class Project

def self.all
[
new('admin', weight: 215),
new('api', weight: 50),
new('backend', weight: 215),
new('backend', test_type: :teaspoon, title: "backend JS", weight: 15),
new('core', weight: 220),
new('sample', weight: 22)
new('admin', weight: 102),
new('api', weight: 69),
new('backend', weight: 282),
new('backend', test_type: :teaspoon, title: "backend JS", weight: 18),
new('core', weight: 266),
new('sample', weight: 28),
new('legacy_promotions', weight: 63)
]
end

Expand Down
1 change: 1 addition & 0 deletions bin/rspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ LIBS = %w[
backend
core
sample
legacy_promotions
]

# Ignore line info, e.g. foo/bar.rb:123 would become foo/bar.rb
Expand Down
9 changes: 9 additions & 0 deletions core/app/models/spree/null_promotion_finder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

module Spree
module NullPromotionFinder
def self.by_code_or_id(*)
raise ActiveRecord::RecordNotFound, "No promotion system configured."
end
end
end
9 changes: 9 additions & 0 deletions core/app/models/spree/promotion_finder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

module Spree
class PromotionFinder
def self.by_code_or_id(coupon_code)
Spree::Promotion.with_coupon_code(coupon_code.to_s) || Spree::Promotion.find(coupon_code)
end
end
end
8 changes: 8 additions & 0 deletions core/lib/spree/core/null_promotion_configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ class NullPromotionConfiguration < Spree::Preferences::Configuration
# the standard coupon code handler class
# Spree::NullPromotionHandler.
class_name_attribute :coupon_code_handler_class, default: 'Spree::NullPromotionHandler'

# Allows providing a different promotion finder.
# @!attribute [rw] promotion_finder_class
# @see Spree::NullPromotionFinder
# @return [Class] an object that conforms to the API of
# the standard promotion finder class
# Spree::NullPromotionFinder.
class_name_attribute :promotion_finder_class, default: 'Spree::NullPromotionFinder'
end
end
end
3 changes: 3 additions & 0 deletions core/lib/spree/core/promotion_configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ class PromotionConfiguration < Spree::Preferences::Configuration
# promotion_adjuster_class allows extensions to provide their own Promotion Adjuster
class_name_attribute :promotion_adjuster_class, default: 'Spree::Promotion::OrderAdjustmentsRecalculator'

# promotion_finder_class allows extensions to provide their own Promotion Finder
class_name_attribute :promotion_finder_class, default: 'Spree::PromotionFinder'

# Allows providing a different shipping promotion handler.
# @!attribute [rw] shipping_promotion_handler_class
# @see Spree::PromotionHandler::Shipping
Expand Down
4 changes: 4 additions & 0 deletions core/spec/lib/spree/core/null_promotion_configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@
it "uses the null coupon code handler class by default" do
expect(config.coupon_code_handler_class).to eq Spree::NullPromotionHandler
end

it "uses the null promotion finder class by default" do
expect(config.promotion_finder_class).to eq Spree::NullPromotionFinder
end
end
4 changes: 4 additions & 0 deletions core/spec/lib/spree/core/promotion_configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
expect(config.shipping_promotion_handler_class).to eq Spree::PromotionHandler::Shipping
end

it "uses promotion finder class by default" do
expect(config.promotion_finder_class).to eq Spree::PromotionFinder
end

describe "#calculators" do
subject { config.calculators[promotion_action] }

Expand Down
11 changes: 11 additions & 0 deletions core/spec/models/spree/null_promotion_finder_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.describe Spree::NullPromotionFinder do
describe ".by_code_or_id" do
it "raises ActiveRecord::RecordNotFound" do
expect { described_class.by_code_or_id("promo") }.to raise_error(ActiveRecord::RecordNotFound)
end
end
end
23 changes: 23 additions & 0 deletions core/spec/models/spree/promotion_finder_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.describe Spree::PromotionFinder do
describe ".by_code_or_id" do
let!(:promotion) { create(:promotion, code: "promo") }

it "finds a promotion by its code" do
expect(described_class.by_code_or_id("promo")).to eq promotion
end

it "finds a promotion by its ID" do
expect(described_class.by_code_or_id(promotion.id)).to eq promotion
end

context "when the promotion does not exist" do
it "raises an error" do
expect { described_class.by_code_or_id("nonexistent") }.to raise_error(ActiveRecord::RecordNotFound)
end
end
end
end
42 changes: 42 additions & 0 deletions legacy_promotions/Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# frozen_string_literal: true

require 'rubygems'
require 'rake'
require 'rake/testtask'
require 'rspec/core/rake_task'
require 'spree/testing_support/dummy_app/rake_tasks'
require 'bundler/gem_tasks'

RSpec::Core::RakeTask.new
task default: :spec

DummyApp::RakeTasks.new(
gem_root: File.dirname(__FILE__),
lib_name: 'solidus_legacy_promotions'
)

require 'yard/rake/yardoc_task'
YARD::Rake::YardocTask.new(:yard)
# The following workaround can be removed
# once https://github.com/lsegal/yard/pull/1457 is merged.
task('yard:require') { require 'yard' }
task yard: 'yard:require'

namespace :spec do
task :isolated do
spec_files = Dir['spec/**/*_spec.rb']
failed_specs =
spec_files.reject do |file|
puts "rspec #{file}"
system('rspec', file)
end

if !failed_specs.empty?
puts "Failed specs:"
puts failed_specs
exit 1
end
end
end

task test_app: 'db:reset'
13 changes: 13 additions & 0 deletions legacy_promotions/bin/rails
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env ruby
# This command will automatically be run when you run "rails" with Rails gems
# installed from the root of your application.

ENGINE_ROOT = File.expand_path('..', __dir__)
ENGINE_PATH = File.expand_path('../lib/spree/core/engine', __dir__)

# Set up gems listed in the Gemfile.
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __dir__)
require "bundler/setup" if File.exist?(ENV["BUNDLE_GEMFILE"])

require "rails/all"
require "rails/engine/commands"
10 changes: 10 additions & 0 deletions legacy_promotions/lib/solidus_legacy_promotions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

require "solidus_core"
require "solidus_support"

module SolidusLegacyPromotions
VERSION = Spree.solidus_version
end

require "solidus_legacy_promotions/engine"
7 changes: 7 additions & 0 deletions legacy_promotions/lib/solidus_legacy_promotions/engine.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

module SolidusLegacyPromotions
class Engine < ::Rails::Engine
include SolidusSupport::EngineExtensions
end
end
28 changes: 28 additions & 0 deletions legacy_promotions/solidus_legacy_promotions.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

require_relative '../core/lib/spree/core/version'

Gem::Specification.new do |s|
s.platform = Gem::Platform::RUBY
s.name = 'solidus_legacy_promotions'
s.version = Spree.solidus_version
s.summary = 'Legacy Solidus promotion system'
s.description = s.summary

s.author = 'Solidus Team'
s.email = '[email protected]'
s.homepage = 'http://solidus.io'
s.license = 'BSD-3-Clause'

s.metadata['rubygems_mfa_required'] = 'true'

s.files = `git ls-files -z`.split("\x0").reject do |f|
f.match(%r{^(spec|script)/})
end

s.required_ruby_version = '>= 3.0.0'
s.required_rubygems_version = '>= 1.8.23'

s.add_dependency 'solidus_core', s.version
s.add_dependency 'solidus_support'
end
9 changes: 9 additions & 0 deletions legacy_promotions/spec/lib/solidus_legacy_promotions_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

require "spec_helper"

RSpec.describe SolidusLegacyPromotions do
it "has a version number" do
expect(SolidusLegacyPromotions::VERSION).not_to be nil
end
end
Loading
Loading