Last active
December 1, 2015 15:19
-
-
Save artkirienko/44ebf4dd8a8cd3a5f753 to your computer and use it in GitHub Desktop.
Модель, например
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
if params[:date] | |
@date = Date._strptime(params[:date], '%m-%Y') | |
unless Date.today.month == @date[:mon] && Date.today.year == @date[:year] | |
@showings = Showing.by_date(@date) | |
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
class Showing < ActiveRecord::Base | |
def self.last | |
find_by_sql("SELECT * FROM showings WHERE date_trunc('hour', created_at) = | |
(SELECT date_trunc('hour', MAX(created_at)) FROM showings);") | |
end | |
def self.dashboard | |
find_by_sql("(SELECT title, value, format FROM showings WHERE date_trunc('hour', created_at) = | |
(SELECT date_trunc('hour', MAX(created_at)) FROM showings) | |
AND title = 'ФОТ сотрудников' | |
LIMIT 1) | |
UNION ALL | |
(SELECT title, value, format FROM showings WHERE date_trunc('hour', created_at) = | |
(SELECT date_trunc('hour', MAX(created_at)) FROM showings) | |
AND title = 'Количество сотрудников' | |
LIMIT 1) | |
UNION ALL | |
(SELECT title, value, format FROM showings WHERE date_trunc('hour', created_at) = | |
(SELECT date_trunc('hour', MAX(created_at)) FROM showings) | |
AND title = 'Остаток на счетах' | |
LIMIT 1) | |
;") | |
end | |
def self.history | |
find_by_sql( | |
"(SELECT title, value, format FROM showings WHERE date_trunc('hour', created_at) = | |
(SELECT date_trunc('hour', MAX(created_at)) FROM showings) | |
AND title != 'ФОТ сотрудников' | |
AND title != 'Количество сотрудников' | |
AND title != 'Остаток на счетах' | |
AND title != 'Итого') | |
UNION ALL | |
(SELECT title, value, format FROM showings WHERE date_trunc('hour', created_at) = | |
(SELECT date_trunc('hour', MAX(created_at)) FROM showings) | |
AND title = 'Итого' | |
LIMIT 1) | |
;" | |
) | |
end | |
def self.by_date(date) | |
find_by_sql( | |
"(SELECT title, value, format FROM showings WHERE date_trunc('hour', created_at) = | |
(SELECT date_trunc('hour', MAX(created_at)) FROM showings | |
WHERE extract(month from date) = #{date[:mon]} AND extract(year from date) = #{date[:year]}) | |
AND title != 'ФОТ сотрудников' | |
AND title != 'Количество сотрудников' | |
AND title != 'Остаток на счетах' | |
AND title != 'Итого') | |
UNION ALL | |
(SELECT title, value, format FROM showings WHERE date_trunc('hour', created_at) = | |
(SELECT date_trunc('hour', MAX(created_at)) FROM showings | |
WHERE extract(month from date) = #{date[:mon]} AND extract(year from date) = #{date[:year]}) | |
AND title = 'Итого' | |
LIMIT 1) | |
;" | |
) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment