Skip to content
This repository has been archived by the owner on Jun 20, 2018. It is now read-only.

Commit

Permalink
changes syntax for helper methods from ? to ! (#171)
Browse files Browse the repository at this point in the history
I did this because the previous syntax was a major breaking change that
is not compatible with the Amber templates.  This accomplishes the same
but changes the default to be nilable and adds a bang version for
not_nil! equivalent.
  • Loading branch information
drujensen committed Mar 31, 2018
1 parent 481f7eb commit d0d580c
Show file tree
Hide file tree
Showing 15 changed files with 87 additions and 87 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,34 +138,34 @@ end
#### Find First

```crystal
post = Post.first?
post = Post.first
if post
puts post.name
end
post = Post.first # raises when no records exist
post = Post.first! # raises when no records exist
```

#### Find

```crystal
post = Post.find? 1
post = Post.find 1
if post
puts post.name
end
post = Post.find 1 # raises when no records found
post = Post.find! 1 # raises when no records found
```

#### Find By

```crystal
post = Post.find_by? :slug, "example_slug"
post = Post.find_by :slug, "example_slug"
if post
puts post.name
end
post = Post.find_by :slug, "foo" # raises when no records found
post = Post.find_by! :slug, "foo" # raises when no records found
```

#### Insert
Expand Down
4 changes: 2 additions & 2 deletions spec/granite_orm/callbacks/callbacks_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module {{adapter.capitalize.id}}
describe "#save" do
it "runs before_save, before_update, after_update, after_save" do
Callback.new(name: "foo").save
callback = Callback.first
callback = Callback.first!
callback.save

callback.history.to_s.strip.should eq <<-EOF
Expand All @@ -35,7 +35,7 @@ module {{adapter.capitalize.id}}
describe "#destroy" do
it "runs before_destroy, after_destroy" do
Callback.new(name: "foo").save
callback = Callback.first
callback = Callback.first!
callback.destroy

callback.history.to_s.strip.should eq <<-EOF
Expand Down
6 changes: 3 additions & 3 deletions spec/granite_orm/fields/primary_key_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ module {{adapter.capitalize.id}}

describe "{{ adapter.id }} .new(primary_key: value)" do
it "ignores the value in default" do
Parent.new(id: 1).id?.should eq(nil)
Parent.new(id: 1).id.should eq(nil)
end

it "sets the value when the primary is defined as `auto: false`" do
Kvs.new(k: "foo").k?.should eq("foo")
Kvs.new(k: "foo", v: "v").k?.should eq("foo")
Kvs.new(k: "foo").k.should eq("foo")
Kvs.new(k: "foo", v: "v").k.should eq("foo")
end
end
end
Expand Down
26 changes: 13 additions & 13 deletions spec/granite_orm/fields/timestamps_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ require "../../spec_helper"
module {{adapter.capitalize.id}}
{%
avoid_macro_bug = 1 # https://github.com/crystal-lang/crystal/issues/5724

# TODO mysql timestamp support should work better
if adapter == "pg"
time_kind_on_read = "Time::Kind::Utc".id
Expand All @@ -17,42 +17,42 @@ module {{adapter.capitalize.id}}
describe "{{ adapter.id }} timestamps" do
it "consistently uses UTC for created_at" do
parent = Parent.new(name: "parent").tap(&.save)
found_parent = Parent.find(parent.id)
found_parent = Parent.find!(parent.id)

original_timestamp = parent.created_at
read_timestamp = found_parent.created_at
original_timestamp = parent.created_at!
read_timestamp = found_parent.created_at!

original_timestamp.kind.should eq Time::Kind::Utc
read_timestamp.kind.should eq {{ time_kind_on_read }}
end

it "consistently uses UTC for updated_at" do
parent = Parent.new(name: "parent").tap(&.save)
found_parent = Parent.find(parent.id)
found_parent = Parent.find!(parent.id)

original_timestamp = parent.updated_at
read_timestamp = found_parent.updated_at
original_timestamp = parent.updated_at!
read_timestamp = found_parent.updated_at!

original_timestamp.kind.should eq Time::Kind::Utc
read_timestamp.kind.should eq {{ time_kind_on_read }}
end

it "truncates the subsecond parts of created_at" do
parent = Parent.new(name: "parent").tap(&.save)
found_parent = Parent.find(parent.id)
found_parent = Parent.find!(parent.id)

original_timestamp = parent.created_at
read_timestamp = found_parent.created_at
original_timestamp = parent.created_at!
read_timestamp = found_parent.created_at!

original_timestamp.epoch.should eq read_timestamp.epoch
end

it "truncates the subsecond parts of updated_at" do
parent = Parent.new(name: "parent").tap(&.save)
found_parent = Parent.find(parent.id)
found_parent = Parent.find!(parent.id)

original_timestamp = parent.updated_at
read_timestamp = found_parent.updated_at
original_timestamp = parent.updated_at!
read_timestamp = found_parent.updated_at!

original_timestamp.epoch.should eq read_timestamp.epoch
end
Expand Down
18 changes: 9 additions & 9 deletions spec/granite_orm/querying/find_by_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ require "../../spec_helper"

{% for adapter in GraniteExample::ADAPTERS %}
module {{adapter.capitalize.id}}
describe "{{ adapter.id }} #find_by?, #find_by" do
describe "{{ adapter.id }} #find_by, #find_by!" do
it "finds an object with a string field" do
Parent.clear
name = "robinson"
Expand All @@ -11,10 +11,10 @@ module {{adapter.capitalize.id}}
model.name = name
model.save

found = Parent.find_by?("name", name)
found = Parent.find_by("name", name)
found.not_nil!.id.should eq model.id

found = Parent.find_by("name", name)
found = Parent.find_by!("name", name)
found.should be_a(Parent)
end

Expand All @@ -26,10 +26,10 @@ module {{adapter.capitalize.id}}
model.name = name
model.save

found = Parent.find_by?(:name, name)
found = Parent.find_by(:name, name)
found.not_nil!.id.should eq model.id

found = Parent.find_by(:name, name)
found = Parent.find_by!(:name, name)
found.id.should eq model.id
end

Expand All @@ -41,20 +41,20 @@ module {{adapter.capitalize.id}}
model.all = value
model.save

found = ReservedWord.find_by?("all", value)
found = ReservedWord.find_by("all", value)
found.not_nil!.id.should eq model.id

found = ReservedWord.find_by(:all, value)
found = ReservedWord.find_by!(:all, value)
found.id.should eq model.id
end

it "returns nil or raises if no result" do
Parent.clear
found = Parent.find_by?("name", "xxx")
found = Parent.find_by("name", "xxx")
found.should be_nil

expect_raises(Granite::ORM::Querying::NotFound, /Couldn't find .*Parent.* with name=xxx/) do
Parent.find_by("name", "xxx")
Parent.find_by!("name", "xxx")
end
end
end
Expand Down
22 changes: 11 additions & 11 deletions spec/granite_orm/querying/find_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ require "../../spec_helper"

{% for adapter in GraniteExample::ADAPTERS %}
module {{adapter.capitalize.id}}
describe "{{ adapter.id }} #find?, #find" do
describe "{{ adapter.id }} #find, #find!" do
it "finds an object by id" do
model = Parent.new
model.name = "Test Comment"
model.save

found = Parent.find? model.id
found = Parent.find model.id
found.should_not be_nil
found.not_nil!.id.should eq model.id

found = Parent.find model.id
found = Parent.find! model.id
found.id.should eq model.id
end

Expand All @@ -22,7 +22,7 @@ module {{adapter.capitalize.id}}
model.save
model_id = model.id

model = Parent.find(model_id)
model = Parent.find!(model_id)
model.new_record?.should be_false
model.persisted?.should be_true
end
Expand All @@ -34,10 +34,10 @@ module {{adapter.capitalize.id}}
school.save
primary_key = school.custom_id

found_school = School.find? primary_key
found_school = School.find primary_key
found_school.should_not be_nil

found_school = School.find primary_key
found_school = School.find! primary_key
found_school.should be_a(School)
end
end
Expand All @@ -49,20 +49,20 @@ module {{adapter.capitalize.id}}
county.save
primary_key = county.id

found_county = Nation::County.find? primary_key
found_county = Nation::County.find primary_key
found_county.should_not be_nil

found_county = Nation::County.find primary_key
found_county = Nation::County.find! primary_key
found_county.should be_a(Nation::County)
end
end

it "returns nil or raises if no result" do
found = Parent.find? 0
found = Parent.find 0
found.should be_nil

expect_raises(Granite::ORM::Querying::NotFound, /Couldn't find .*Parent.* with id=0/) do
Parent.find 0
Parent.find! 0
end
end
end
Expand Down
14 changes: 7 additions & 7 deletions spec/granite_orm/querying/first_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ require "../../spec_helper"

{% for adapter in GraniteExample::ADAPTERS %}
module {{adapter.capitalize.id}}
describe "{{ adapter.id }} #first?, #first" do
describe "{{ adapter.id }} #first, #first!" do
it "finds the first object" do
Parent.clear
first = Parent.new.tap do |model|
Expand All @@ -15,10 +15,10 @@ module {{adapter.capitalize.id}}
model.save
end

found = Parent.first?
found = Parent.first
found.not_nil!.id.should eq first.id

found = Parent.first
found = Parent.first!
found.id.should eq first.id
end

Expand All @@ -34,10 +34,10 @@ module {{adapter.capitalize.id}}
model.save
end

found = Parent.first?("ORDER BY id DESC")
found = Parent.first("ORDER BY id DESC")
found.not_nil!.id.should eq second.id

found = Parent.first("ORDER BY id DESC")
found = Parent.first!("ORDER BY id DESC")
found.id.should eq second.id
end

Expand All @@ -48,11 +48,11 @@ module {{adapter.capitalize.id}}
model.save
end

found = Parent.first?("WHERE name = 'Test 2'")
found = Parent.first("WHERE name = 'Test 2'")
found.should be nil

expect_raises(Granite::ORM::Querying::NotFound, /Couldn't find .*Parent.* with first\(WHERE name = 'Test 2'\)/) do
Parent.first("WHERE name = 'Test 2'")
Parent.first!("WHERE name = 'Test 2'")
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/granite_orm/transactions/create_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module {{adapter.capitalize.id}}

it "does not create an invalid object" do
parent = Parent.create(name: "")
parent.id?.should be_nil
parent.id.should be_nil
end

describe "with a custom primary key" do
Expand Down
6 changes: 3 additions & 3 deletions spec/granite_orm/transactions/destroy_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module {{adapter.capitalize.id}}

id = parent.id
parent.destroy
found = Parent.find? id
found = Parent.find id
found.should be_nil
end

Expand All @@ -37,7 +37,7 @@ module {{adapter.capitalize.id}}
primary_key = school.custom_id
school.destroy

found_school = School.find? primary_key
found_school = School.find primary_key
found_school.should be_nil
end
end
Expand All @@ -50,7 +50,7 @@ module {{adapter.capitalize.id}}
primary_key = county.id
county.destroy

found_county = Nation::County.find? primary_key
found_county = Nation::County.find primary_key
found_county.should be_nil
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/granite_orm/transactions/save_natural_key_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module {{adapter.capitalize.id}}
kv.k = "foo"
kv.save.should be_true

kv = Kvs.find("foo").not_nil!
kv = Kvs.find!("foo")
kv.k.should eq("foo")
end

Expand Down Expand Up @@ -43,7 +43,7 @@ module {{adapter.capitalize.id}}
Kvs.count.should eq(1)

## Read
port = Kvs.find("mysql_port")
port = Kvs.find!("mysql_port")
port.v.should eq("3306")
port.new_record?.should be_false

Expand Down
Loading

0 comments on commit d0d580c

Please sign in to comment.