Created
November 8, 2015 09:01
-
-
Save kirs/5a098654f1c1205ddbaa to your computer and use it in GitHub Desktop.
This file contains hidden or 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
class MyRelation | |
def initialize(page) | |
@page = page.to_i | |
@limit = 10 | |
end | |
def current_page | |
@page | |
end | |
def total_pages | |
res = execute(to_count_sql) | |
total = res.getvalue(0, 0) | |
(total.to_f / @limit).ceil | |
end | |
def limit_value | |
@limit | |
end | |
def offset | |
(@page - 1) * @limit | |
end | |
def all | |
execute(to_sql) | |
end | |
def execute(sql) | |
PG.connect(...).exec(sql) | |
end | |
def to_sql | |
<<-SQL.strip_heredoc | |
select distinct on (applicant_number) applicant_number, created_at, account_id from sms_messages | |
where account_id = 147 | |
order by applicant_number, created_at DESC | |
limit #{@limit} offset #{offset} | |
SQL | |
end | |
def to_count_sql | |
"select count(distinct applicant_number) from sms_messages where account_id = 147" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment