diff --git a/CHANGELOG.md b/CHANGELOG.md index 8f45338..5e72335 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## master +- [PR #29](https://github.com/DmitryTsepelev/store_model/pull/29) Properly compare enum attributes in `Model#==` ([@DmitryTsepelev][]) - [PR #26](https://github.com/DmitryTsepelev/store_model/pull/26) Add YARD docs ([@DmitryTsepelev][]) ## 0.5.1 (2019-09-06) diff --git a/lib/store_model/model.rb b/lib/store_model/model.rb index 2658076..7bd4c12 100644 --- a/lib/store_model/model.rb +++ b/lib/store_model/model.rb @@ -35,7 +35,7 @@ def as_json(options = {}) def ==(other) return super unless other.is_a?(self.class) - attributes.all? { |name, value| value == other.send(name) } + attributes.all? { |name, value| value == other.attributes[name] } end # Allows to call :presence validation on the association itself. diff --git a/spec/store_model/model_spec.rb b/spec/store_model/model_spec.rb index eb9b90c..c643af9 100644 --- a/spec/store_model/model_spec.rb +++ b/spec/store_model/model_spec.rb @@ -63,10 +63,36 @@ end context "when two instances have different attributes" do - let(:second_setting) { Configuration.new(color: "user") } + let(:second_setting) { Configuration.new(color: "black") } it { is_expected.to be_falsey } end + + context "when StoreModel has enum attribute" do + let(:config_class) do + Class.new do + include StoreModel::Model + + enum :status, in: { active: 1, archived: 0 } + end + end + + let(:first_setting) { config_class.new(status: :active) } + + subject { first_setting == second_setting } + + context "when two instances have same attributes" do + let(:second_setting) { config_class.new(status: :active) } + + it { is_expected.to be_truthy } + end + + context "when two instances have different attributes" do + let(:second_setting) { config_class.new(status: :archived) } + + it { is_expected.to be_falsey } + end + end end describe ".to_type" do