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

Do not initialize promotions object on startup #5728

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 11 additions & 1 deletion api/lib/spree/api_configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,17 @@ class ApiConfiguration < Preferences::Configuration
:variant_id
]

preference :promotion_attributes, :array, default: Spree::Config.promotions.promotion_api_attributes
def promotion_attributes
Spree::Config.promotions.promotion_api_attributes
end
alias_method :preferred_promotion_attributes, :promotion_attributes

def promotion_attributes=(value)
Spree::Config.promotions.promotion_api_attributes = value
end
alias_method :preferred_promotion_attributes=, :promotion_attributes=
promotion_attributes_deprecation_message = "Spree::ApiConfiguration#promotion_attributes= is deprecated. Please use Spree::Config.promotions.promotion_api_attributes= instead."
deprecate "promotion_attributes=" => promotion_attributes_deprecation_message, deprecator: Spree.deprecator

preference :store_attributes, :array, default: [
:id, :name, :url, :meta_description, :meta_keywords, :seo_title,
Expand Down
41 changes: 41 additions & 0 deletions api/spec/lib/spree/api_configuration_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# frozen_string_literal: true

require "spec_helper"

RSpec.describe Spree::ApiConfiguration do
subject(:config) { Spree::ApiConfiguration.new }

describe "#promotion_attributes" do
subject(:promotion_attributes) { config.promotion_attributes }

it { is_expected.to eq(Spree::Config.promotions.promotion_api_attributes) }

it "can be changed" do
config.promotion_attributes << :foo
expect(promotion_attributes).to include(:foo)
end

it "can delete attributes" do
expect(promotion_attributes).to include(:name)
config.promotion_attributes.delete(:name)
expect(promotion_attributes).not_to include(:name)
end
end

describe "#promotion_attributes=" do
Copy link
Member

Choose a reason for hiding this comment

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

I think this also supports pushing new attributes to the array.

config.promotion_attributes << [:name]

# or

config.promotion_attributes.push(:name)

And also removing items from the array. Shouldn't we also support those kind of customizations?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

All of these are still possible, as the list of attributes is just an array of symbols at the end. I've added some specs to prove it.

subject(:promotion_attributes_setter) { config.promotion_attributes = [:name] }

around do |example|
original_attributes = Spree::Config.promotions.promotion_api_attributes
Spree.deprecator.silence do
example.run
end
Spree::Config.promotions.promotion_api_attributes = original_attributes
end

it "sets the promotion_attributes" do
promotion_attributes_setter
expect(config.promotion_attributes).to eq([:name])
end
end
end