Skip to content

Commit 394b26b

Browse files
authored
Merge pull request #17 from crystal-garage/refactor4
add Collection#find with parameters
2 parents 26f1430 + 3d3b89c commit 394b26b

File tree

2 files changed

+62
-14
lines changed

2 files changed

+62
-14
lines changed

spec/model/collection_spec.cr

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -516,20 +516,56 @@ module CollectionSpec
516516
end
517517
end
518518

519-
it "find / find!" do
520-
temporary do
521-
reinit_example_models
519+
context "find / find!" do
520+
it "with block" do
521+
temporary do
522+
reinit_example_models
522523

523-
10.times do |x|
524-
User.create! first_name: "user #{x}"
524+
10.times do |x|
525+
User.create! first_name: "user #{x}"
526+
end
527+
528+
User.query.find! { first_name == "user 2" }.first_name.should eq("user 2")
529+
User.query.find { first_name == "not_exists" }.should be_nil
530+
531+
expect_raises(Clear::SQL::RecordNotFoundError) {
532+
User.query.find! { first_name == "not_exists" }
533+
}
525534
end
535+
end
526536

527-
User.query.find! { first_name == "user 2" }.first_name.should eq("user 2")
528-
User.query.find { first_name == "not_exists" }.should be_nil
537+
it "with NamedTuple" do
538+
temporary do
539+
reinit_example_models
529540

530-
expect_raises(Clear::SQL::RecordNotFoundError) {
531-
User.query.find! { first_name == "not_exists" }
532-
}
541+
10.times do |x|
542+
User.create! first_name: "user #{x}"
543+
end
544+
545+
User.query.find!({first_name: "user 2"}).first_name.should eq("user 2")
546+
User.query.find({first_name: "not_exists"}).should be_nil
547+
548+
expect_raises(Clear::SQL::RecordNotFoundError) {
549+
User.query.find!({first_name: "not_exists"})
550+
}
551+
end
552+
end
553+
554+
it "with arguments" do
555+
temporary do
556+
reinit_example_models
557+
558+
10.times do |x|
559+
User.create!(first_name: "first #{x}", last_name: "last #{x}")
560+
end
561+
562+
User.query.find!(first_name: "first 2", last_name: "last 2").first_name.should eq("first 2")
563+
User.query.find(first_name: "not_exists").should be_nil
564+
565+
expect_raises(Clear::SQL::RecordNotFoundError) {
566+
User.query.find!(first_name: "not_exists")
567+
}
568+
end
533569
end
534570
end
535571

src/clear/model/collection.cr

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -547,28 +547,40 @@ module Clear::Model
547547
self.offset(range.begin).limit(range.end - range.begin).to_a(fetch_columns)
548548
end
549549

550-
# A convenient way to write `where { condition }.first`
550+
# A convenient way to write `where { condition }.first(fetch_columns)`
551551
def find(fetch_columns = false, &block) : T?
552552
x = Clear::Expression.ensure_node!(with Clear::Expression.new yield)
553+
553554
where(x).first(fetch_columns)
554555
end
555556

556-
# A convenient way to write `where({any_column: "any_value"}).first`
557+
# A convenient way to write `where({any_column: "any_value"}).first(fetch_columns)`
557558
def find(tuple : NamedTuple, fetch_columns = false) : T?
558559
where(tuple).first(fetch_columns)
559560
end
560561

561-
# A convenient way to write `where({any_column: "any_value"}).first!`
562+
# A convenient way to write `where({any_column: "any_value"}).first`
563+
def find(**tuple) : T?
564+
where(tuple).first
565+
end
566+
567+
# A convenient way to write `where { condition }.first!(fetch_columns)`
562568
def find!(fetch_columns = false, &block) : T
563569
x = Clear::Expression.ensure_node!(with Clear::Expression.new yield)
570+
564571
where(x).first!(fetch_columns)
565572
end
566573

567-
# A convenient way to write `where { condition }.first!`
574+
# A convenient way to write `where({any_column: "any_value"}).first!(fetch_columns)`
568575
def find!(tuple : NamedTuple, fetch_columns = false) : T
569576
where(tuple).first!(fetch_columns)
570577
end
571578

579+
# A convenient way to write `where({any_column: "any_value"}).first!`
580+
def find!(**tuple) : T
581+
where(tuple).first!
582+
end
583+
572584
# Try to fetch a row. If not found, build a new object and setup
573585
# the fields like setup in the condition tuple.
574586
def find_or_build(**tuple, &block : T -> Nil) : T

0 commit comments

Comments
 (0)