Skip to content

Commit

Permalink
Properly compare enum attributes in Model#== (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
DmitryTsepelev committed Aug 23, 2019
1 parent c81a442 commit 71c147e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/store_model/model.rb
Expand Up @@ -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.
Expand Down
32 changes: 29 additions & 3 deletions spec/store_model/model_spec.rb
Expand Up @@ -59,13 +59,39 @@
context "when two instances have same attributes" do
let(:second_setting) { Configuration.new(color: "red") }

it { is_expected.to be_truthy }
it { is_expected.to be true }
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 }
it { is_expected.to be false }
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 true }
end

context "when two instances have different attributes" do
let(:second_setting) { config_class.new(status: :archived) }

it { is_expected.to be false }
end
end
end

Expand Down

0 comments on commit 71c147e

Please sign in to comment.