Skip to content

Commit

Permalink
Properly compare enum attributes in Model#==
Browse files Browse the repository at this point in the history
  • Loading branch information
DmitryTsepelev committed Aug 23, 2019
1 parent c81a442 commit de31908
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
28 changes: 27 additions & 1 deletion spec/store_model/model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit de31908

Please sign in to comment.