Skip to content

Commit

Permalink
Add helper queries class for application urn
Browse files Browse the repository at this point in the history
FindApplicationIndexQuery will be used to retrieve the index of an
created application and to later match it with a random urn
  • Loading branch information
fumimowdan committed Oct 31, 2023
1 parent 57901cb commit 6e9281d
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 0 deletions.
33 changes: 33 additions & 0 deletions app/queries/applications_index_query.rb
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
37 changes: 37 additions & 0 deletions app/queries/find_application_index_query.rb
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
12 changes: 12 additions & 0 deletions spec/queries/applications_index_query_spec.rb
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
14 changes: 14 additions & 0 deletions spec/queries/find_application_index_query_spec.rb
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

0 comments on commit 6e9281d

Please sign in to comment.