-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #51 from Goltergaul/fix-model-inheritance
Fix model inheritance
- Loading branch information
Showing
5 changed files
with
67 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
PATH | ||
remote: . | ||
specs: | ||
definition (1.1.0) | ||
definition (1.1.1) | ||
activesupport | ||
i18n | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
# frozen_string_literal: true | ||
|
||
module Definition | ||
VERSION = "1.1.0" | ||
VERSION = "1.1.1" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -131,6 +131,61 @@ | |
end.to raise_error(Definition::InvalidModelError, /bar/) | ||
end | ||
end | ||
|
||
context "with inheritance" do | ||
subject(:new) { child_test_model_class.new(**kwargs) } | ||
|
||
let(:child_test_model_class) do | ||
ParentModel = test_model_class | ||
Class.new(ParentModel) do | ||
required :age, Definition.Type(Integer) | ||
optional :phone, Definition.Type(String) | ||
end | ||
end | ||
|
||
context "with required keywords only" do | ||
let(:kwargs) { { name: "John", age: 24 } } | ||
|
||
it "instantiates the model" do | ||
expect(new.name).to eq("John") | ||
expect(new.email).to be_nil | ||
expect(new.age).to eq(24) | ||
expect(new.phone).to be_nil | ||
end | ||
end | ||
|
||
context "with required and optional keywords" do | ||
let(:kwargs) { { name: "John", email: "[email protected]", age: 24, phone: "+4312345" } } | ||
|
||
it "instantiates the model" do | ||
expect(new.name).to eq("John") | ||
expect(new.email).to eq("[email protected]") | ||
expect(new.age).to eq(24) | ||
expect(new.phone).to eq("+4312345") | ||
end | ||
end | ||
|
||
it "throws error when parent's required attributes are missing" do | ||
expect do | ||
child_test_model_class.new(age: 24) | ||
end.to raise_error(Definition::InvalidModelError, /name/) | ||
end | ||
|
||
it "throws error when child's required attributes are missing" do | ||
expect do | ||
child_test_model_class.new(name: "John") | ||
end.to raise_error(Definition::InvalidModelError, /age/) | ||
end | ||
|
||
it "doesn't change parent's defintion" do | ||
expect do | ||
Class.new(test_model_class) do | ||
required :required_child_attribute, Definition.Type(Integer) | ||
optional :optional_child_attribute, Definition.Type(String) | ||
end | ||
end.not_to(change { test_model_class._definition.keys }) | ||
end | ||
end | ||
end | ||
|
||
describe ".to_h" do | ||
|