Skip to content

Commit

Permalink
Deprecate calling "preference" without including Preferable
Browse files Browse the repository at this point in the history
The error message here calls for "persistable", because all objects that
inherit from Spree::Base *actually* need the Persistable module.
  • Loading branch information
mamhoff committed Mar 30, 2021
1 parent 0b34da4 commit 9c472a9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
16 changes: 16 additions & 0 deletions core/app/models/spree/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,22 @@ class Spree::Base < ActiveRecord::Base
include Spree::Core::Permalinks
include Spree::RansackableAttributes

def self.preference(*args)
Spree::Deprecation.warn <<~WARN
#{name} has a `preferences` column, but does not explicitly (de)serialize this column.
In order to make #{name} work with future versions of Solidus (and Rails), please add the
following line to your class:
```
class #{name}
include Spree::Preferences::Persistable
...
end
```
WARN
include Spree::Preferences::Persistable
preference(*args)
end

def preferences
value = read_attribute(:preferences)
if !value.is_a?(Hash)
Expand Down
9 changes: 7 additions & 2 deletions core/spec/models/spree/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,18 @@ class PrefTestWithoutSerialization < Spree::Base

it "returns a Hash nevertheless" do
instance = PrefTestWithoutSerialization.new
expect(instance.preferences).to eq({})
expect(instance.preferences).to be_a(Hash)
end

it "returns a Hash when there's already values in the table" do
ActiveRecord::Base.connection.execute("INSERT INTO pref_tests (col, preferences) VALUES ('test', '---\n:percent: 20')")
instance = PrefTestWithoutSerialization.first
expect(instance.preferences).to eq(percent: 20)
expect(instance.preferences).to include(percent: 20)
end

it "includes the persistable module when calling #preference and sets the preference default" do
PrefTestWithoutSerialization.preference :percentage, :number, default: 5
expect(PrefTestWithoutSerialization.new.preferences).to eq(percentage: 5)
end
end
end

0 comments on commit 9c472a9

Please sign in to comment.