Les statistiques fonctionnent à la fois pour mysql et sqlite, en utilisant des requêtes spécifiques pour extraction des mois et année

This commit is contained in:
echarp 2014-10-24 01:33:50 +02:00
parent 2001e2ae1c
commit 37c0829b6f
3 changed files with 24 additions and 18 deletions

View File

@ -7,17 +7,26 @@ class RegionsController < InheritedResources::Base
@city_events = Event.group(:city).having('count(city) > 3')
.order('count(city) desc').count :city
# Used in sqlite
# .group('strftime("%Y", start_time)')
@year_events = Event
.group('extract(year from start_time)').count
@year_events = Event.group(year_grouping).count
# Used in sqlite
# .group('strftime("%Y", start_time)')
# .group('strftime("%m", start_time)')
@month_events = Event
.group('extract(year from start_time)')
.group('extract(month from start_time)')
.count
@month_events = Event.group(year_grouping, month_grouping).count
end
private
def year_grouping
if %w(Mysql2 MySQL PostgreSQL).include? Event.connection.adapter_name
'extract(year from start_time)'
elsif Event.connection.adapter_name == 'SQLite'
'strftime("%Y", start_time)'
end
end
def month_grouping
if %w(Mysql2 MySQL PostgreSQL).include? Event.connection.adapter_name
'extract(month from start_time)'
elsif Event.connection.adapter_name == 'SQLite'
'strftime("%m", start_time)'
end
end
end

View File

@ -25,7 +25,8 @@
%th.month= I18n.t('date.month_names')[m]
- @year_events.each do |year|
%td.quantity
- line = @month_events.find { |line| line[0][0] == year[0] && line[0][1] == m }
- line = @month_events.find { |line| line[0][0] == year[0] && line[0][1].try(:to_i) == m }
= link_to root_url(start_date: "#{year[0]}-#{m}-01") do
= number_with_delimiter line[1] if line
%td.sparkline/

View File

@ -1,12 +1,8 @@
development_old:
development:
adapter: sqlite3
pool: 5
timeout: 5000
database: db/development.sqlite3
development:
adapter: mysql2
database: adl_fr
username: manu
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".