module HowAreWeDoing module Controllers module ReportsHelpers def self.included(base) base.respond_to :html, :json, :xml, :js base.before_filter :set_dates, :only => :index base.before_filter :set_graph_type, :only => :index base.before_filter :set_analyticals, :only => :index end # extend ActiveSupport::Concern # # included do # respond_to :html, :json, :xml, :js # before_filter :set_dates, :only => :index # before_filter :set_graph_type, :only => :index # before_filter :set_analyticals, :only => :index # end def index respond_to do |format| format.html format.json do render :json => { :metrics => { :totals => params[:exclude_totals] ? {} : { :data => Total.send("json_data_for_#{params[:graph_type]}",params), :label => "Totals", :color => 0, :ticks => Total.send("json_labels_for_#{params[:graph_type]}",params) }, :prints => params[:exclude_prints] ? {} : { :data => Print.send("json_data_for_#{params[:graph_type]}",params), :label => "Prints", :color => 1, :ticks => Print.send("json_labels_for_#{params[:graph_type]}",params) }, :views => params[:exclude_views] ? {} : { :data => View.send("json_data_for_#{params[:graph_type]}",params), :label => "Views", :color => 2, :ticks => View.send("json_labels_for_#{params[:graph_type]}",params) }, :shares => params[:exclude_shares] ? {} : { :data => Share.send("json_data_for_#{params[:graph_type]}",params), :label => "Shares", :color => 3, :ticks => Share.send("json_labels_for_#{params[:graph_type]}",params) } }, :format_options => format_options } end end end protected def set_analyticals @analyticals = params[:analytical_type] ? params[:analytical_type].classify.constantize.order('id DESC') : [] @analytical_ids = params[:analytical_ids] ? params[:analytical_ids] : @analyticals.first.nil? ? [] : [@analyticals.first.id] if @analytical_ids.empty? && params[:default_analytical_type] @analyticals = params[:default_analytical_type].classify.constantize.order('id DESC') @analytical_ids = @analyticals.limit(1).map(&:id) end end def format_options params[:graph_type] == 'line_graph' ? { :xaxis => { :max => "#{params[:end_date].to_time.to_i}000", :min => "#{params[:start_date].to_time.to_i}000"} } : {} end def set_graph_type params[:graph_type] = params[:analytical_type].blank? ? 'line_graph' : 'bar_graph' end def set_dates params[:start_date] ||= (Time.now - 2.weeks) params[:end_date] ||= Time.now if params[:start_date].to_s.match(/\//) params[:start_date] = Date.strptime(params[:start_date], '%m/%d/%Y') params[:end_date] = Date.strptime(params[:end_date], '%m/%d/%Y') else params[:start_date] = Date.parse(params[:start_date].to_s) params[:end_date] = Date.parse(params[:end_date].to_s) end params[:start_date] = params[:start_date].to_time.utc.beginning_of_day params[:end_date] = params[:end_date].to_time.utc.beginning_of_day end end end end