-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding transaction save!, create!, destroy! methods (#232)
* Added save! method * Added destroy! method * Added create! method * Added nil? check on create! * Changed nil check to errors check * Review changes * Update transactions.cr * Added custom exceptions and added tests for them * Removed Nil type because i fixed an bug than produced Nil values * Added update and update! methods * Changed other spec files with persisted? where possible * Using callback model in the spec_models instead of introducing an new one * Docker does not cleanup callback model properly. abort_at = 'temp' fixes * Forgot to change to save! again * RecordInvalid to RecordNotSaved and model is now attached to the errors * Added new methods to the README.md
- Loading branch information
Showing
10 changed files
with
262 additions
and
10 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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
require "../../spec_helper" | ||
|
||
{% for adapter in GraniteExample::ADAPTERS %} | ||
module {{adapter.capitalize.id}} | ||
describe Granite::RecordNotSaved do | ||
it "should have a message" do | ||
parent = Parent.new | ||
parent.save | ||
|
||
Granite::RecordNotSaved | ||
.new(Parent.name, parent) | ||
.message | ||
.should eq("Could not process {{adapter.capitalize.id}}::Parent") | ||
end | ||
|
||
it "should have a model" do | ||
parent = Parent.new | ||
parent.save | ||
|
||
Granite::RecordNotSaved | ||
.new(Parent.name, parent) | ||
.model | ||
.should eq(parent) | ||
end | ||
end | ||
end | ||
{% 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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
require "../../spec_helper" | ||
|
||
{% for adapter in GraniteExample::ADAPTERS %} | ||
module {{adapter.capitalize.id}} | ||
describe Granite::RecordNotDestroyed do | ||
it "should have a message" do | ||
parent = Parent.new | ||
parent.save | ||
|
||
Granite::RecordNotDestroyed | ||
.new(Parent.name, parent) | ||
.message | ||
.should eq("Could not destroy {{adapter.capitalize.id}}::Parent") | ||
end | ||
|
||
it "should have a model" do | ||
parent = Parent.new | ||
parent.save | ||
|
||
Granite::RecordNotDestroyed | ||
.new(Parent.name, parent) | ||
.model | ||
.should eq(parent) | ||
end | ||
end | ||
end | ||
{% 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
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
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
require "../../spec_helper" | ||
|
||
{% for adapter in GraniteExample::ADAPTERS %} | ||
module {{adapter.capitalize.id}} | ||
describe "{{ adapter.id }} #update" do | ||
it "updates an object" do | ||
parent = Parent.new(name: "New Parent") | ||
parent.save! | ||
|
||
parent.update(name: "Other parent").should be_true | ||
parent.name.should eq "Other parent" | ||
|
||
Parent.find!(parent.id).name.should eq "Other parent" | ||
end | ||
|
||
it "does not update an invalid object" do | ||
parent = Parent.new(name: "New Parent") | ||
parent.save! | ||
|
||
parent.update(name: "").should be_false | ||
parent.name.should eq "" | ||
|
||
Parent.find!(parent.id).name.should eq "New Parent" | ||
end | ||
end | ||
|
||
describe "{{ adapter.id }} #update!" do | ||
it "updates an object" do | ||
parent = Parent.new(name: "New Parent") | ||
parent.save! | ||
|
||
parent.update!(name: "Other parent") | ||
parent.name.should eq "Other parent" | ||
|
||
Parent.find!(parent.id).name.should eq "Other parent" | ||
end | ||
|
||
it "does not update but raises an exception" do | ||
parent = Parent.new(name: "New Parent") | ||
parent.save! | ||
|
||
expect_raises(Granite::RecordNotSaved, "{{adapter.capitalize.id}}::Parent") do | ||
parent.update!(name: "") | ||
end | ||
|
||
Parent.find!(parent.id).name.should eq "New Parent" | ||
end | ||
end | ||
end | ||
{% 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
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
module Granite | ||
class RecordNotSaved < ::Exception | ||
getter model : Granite::Base | ||
|
||
def initialize(class_name : String, model : Granite::Base) | ||
super("Could not process #{class_name}") | ||
|
||
@model = model | ||
end | ||
end | ||
|
||
class RecordNotDestroyed < ::Exception | ||
getter model : Granite::Base | ||
|
||
def initialize(class_name : String, model : Granite::Base) | ||
super("Could not destroy #{class_name}") | ||
|
||
@model = model | ||
end | ||
end | ||
end |
Oops, something went wrong.