Extract model to handle chart data
We're adding it inside the Ahoy module because the Ahoy::DataSource class is also in that module. We might move it to a different place in the future.
This commit is contained in:
@@ -10,7 +10,7 @@ class Admin::Api::StatsController < Admin::Api::BaseController
|
|||||||
ds = Ahoy::DataSource.new
|
ds = Ahoy::DataSource.new
|
||||||
|
|
||||||
if params[:event].present?
|
if params[:event].present?
|
||||||
ds.add params[:event].titleize, Ahoy::Event.where(name: params[:event]).group_by_day(:time).count
|
ds.add params[:event].titleize, Ahoy::Chart.new(params[:event]).group_by_day(:time).count
|
||||||
end
|
end
|
||||||
|
|
||||||
if params[:visits].present?
|
if params[:visits].present?
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
class Admin::StatsController < Admin::BaseController
|
class Admin::StatsController < Admin::BaseController
|
||||||
def show
|
def show
|
||||||
@event_names = Ahoy::Event.distinct.order(:name).pluck(:name)
|
@event_names = Ahoy::Chart.active_event_names
|
||||||
|
|
||||||
@visits = Visit.count
|
@visits = Visit.count
|
||||||
@debates = Debate.with_hidden.count
|
@debates = Debate.with_hidden.count
|
||||||
@@ -34,7 +34,7 @@ class Admin::StatsController < Admin::BaseController
|
|||||||
@event = params[:event]
|
@event = params[:event]
|
||||||
|
|
||||||
if params[:event]
|
if params[:event]
|
||||||
@count = Ahoy::Event.where(name: params[:event]).count
|
@count = Ahoy::Chart.new(params[:event]).count
|
||||||
else
|
else
|
||||||
@count = params[:count]
|
@count = params[:count]
|
||||||
end
|
end
|
||||||
|
|||||||
20
app/models/ahoy/chart.rb
Normal file
20
app/models/ahoy/chart.rb
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
module Ahoy
|
||||||
|
class Chart
|
||||||
|
attr_reader :event_name
|
||||||
|
delegate :count, :group_by_day, to: :events
|
||||||
|
|
||||||
|
def initialize(event_name)
|
||||||
|
@event_name = event_name
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.active_event_names
|
||||||
|
Ahoy::Event.distinct.order(:name).pluck(:name)
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def events
|
||||||
|
Ahoy::Event.where(name: event_name)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
Reference in New Issue
Block a user