Skip to content
Closed
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
7 changes: 4 additions & 3 deletions core/app/models/concerns/spree/preferences/persistable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ module Persistable
private

def initialize_preference_defaults
if has_attribute?(:preferences)
self.preferences = default_preferences.merge(preferences)
end
return unless has_attribute?(:preferences)

merged = default_preferences.merge(preferences)
self.preferences = merged unless merged == preferences
end
end
end
Expand Down
11 changes: 11 additions & 0 deletions core/spec/models/spree/preferences/preferable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,17 @@ class ComplexPreferableClass
end
end

it "does not re-serialize unchanged preferences when read after load" do

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.

loaded = PrefTest.find(@pt.id)

# NOTE: assigning preferences in after_initialize (even an identical value) flips

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Metrics/LineLength: Line is too long. [87/80]

# the attribute to "from user" state, so every later read re-dumps YAML. Skipping

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Metrics/LineLength: Line is too long. [87/80]

# the redundant assignment keeps reads on the cheaper "from database" path.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Metrics/LineLength: Line is too long. [81/80]

expect(Psych).not_to receive(:safe_dump)

loaded.preferences
end

it "clear preferences when record is deleted" do
@pt.save!
@pt.preferred_pref_test_pref = "lmn"
Expand Down
Loading