Sha256: 928e729d200bb5fcf45d739d9d11230df166277cd985f94bcddf1358be414adf
Contents?: true
Size: 1.47 KB
Versions: 1
Compression:
Stored size: 1.47 KB
Contents
require 'active_record' module Reparty class Report class ActiveRecord < Report attr_reader :model, :operation, :field def initialize(*args, &block) super(args.shift) if args.first.is_a?(Symbol) @model = Kernel.const_get(args.first.to_s.capitalize) elsif args.first.is_a?(::ActiveRecord::Relation) @model = args.first else raise "Report::ActiveRecord: model undefined" end @operation = args.fetch(1, :count) @field = :created_at if args.last.is_a?(Hash) @field = args.last.fetch(:field, @field) end @color = "#85bdad" end def attach(attachments) @graph = build_daily_graph @graph.data(@model.to_s.pluralize, daily_dataset) attachments.inline["#{self.hash}.png"] = @graph.to_blob end def daily_dataset if @operation == :total 7.downto(1).map { |x| @model.where("#{@field.to_s} < ?", DateTime.now.at_midnight-x).send(:count, @field) } else 7.downto(1).map { |x| @model.where("DATE(#{@field.to_s}) = ?", DateTime.now.to_date-x).send(@operation, @field) } end end def yesterday op = @operation == :total ? :count : @operation @model.where("DATE(#{@field.to_s}) = ?", DateTime.now-1).send(op, @field) end def total op = @operation == :total ? :count : @operation @model.send(op, @field) end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
reparty-0.2.0 | lib/reparty/report/active_record.rb |