-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add helper queries class for application urn
FindApplicationIndexQuery will be used to retrieve the index of an created application and to later match it with a random urn
- Loading branch information
1 parent
57901cb
commit 6e9281d
Showing
4 changed files
with
96 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
class ApplicationsIndexQuery | ||
delegate :to_sql, to: :query | ||
|
||
def execute | ||
ApplicationRecord.connection.execute(to_sql) | ||
end | ||
|
||
def query | ||
@query ||= applications | ||
.project(projection) | ||
.order(applications[:created_at].asc) | ||
end | ||
|
||
def projection | ||
[ | ||
applications[:id], | ||
row_number.as("application_index"), | ||
] | ||
end | ||
|
||
def row_number | ||
Arel::Nodes::Over.new( | ||
Arel::Nodes::NamedFunction.new("ROW_NUMBER", []), | ||
Arel.sql("(ORDER BY created_at ASC)"), | ||
) | ||
end | ||
|
||
private | ||
|
||
def applications | ||
Application.arel_table | ||
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,37 @@ | ||
class FindApplicationIndexQuery | ||
delegate :to_sql, to: :query | ||
attr_reader :application_id | ||
|
||
def initialize(application_id:) | ||
@application_id = application_id | ||
end | ||
|
||
def execute | ||
ApplicationRecord.connection.execute(to_sql) | ||
end | ||
|
||
def query | ||
@query ||= manager | ||
.project(projection) | ||
.from(from_clause) | ||
.where(where_clause) | ||
end | ||
|
||
def projection | ||
[Arel.star] | ||
end | ||
|
||
def from_clause | ||
ApplicationsIndexQuery.new.query.as("list") | ||
end | ||
|
||
def where_clause | ||
Arel.sql("list.id").eq(application_id) | ||
end | ||
|
||
private | ||
|
||
def manager | ||
@manager ||= Arel::SelectManager.new(Arel::Table.engine) | ||
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,12 @@ | ||
require "rails_helper" | ||
|
||
RSpec.describe ApplicationsIndexQuery, type: :model do | ||
subject(:sql) { described_class.new.to_sql } | ||
|
||
describe "#to_sql" do | ||
it { expect(sql).to include("SELECT") } | ||
it { expect(sql).to include('"applications"."id"') } | ||
it { expect(sql).to include("ROW_NUMBER() OVER (ORDER BY created_at ASC) AS application_index") } | ||
it { expect(sql).to include('FROM "applications"') } | ||
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,14 @@ | ||
require "rails_helper" | ||
|
||
RSpec.describe FindApplicationIndexQuery, type: :model do | ||
subject(:sql) { described_class.new(application_id: application.id).to_sql } | ||
|
||
let(:application) { create(:application) } | ||
let(:subquery) { ApplicationsIndexQuery.new.to_sql } | ||
|
||
describe "#to_sql" do | ||
it { expect(sql).to include("SELECT *") } | ||
it { expect(sql).to include("FROM (#{subquery}) list") } | ||
it { expect(sql).to include("WHERE list.id = #{application.id}") } | ||
end | ||
end |