Class: Reparty::Report::ActiveRecord
- Inherits:
-
Reparty::Report
- Object
- Reparty::Report
- Reparty::Report::ActiveRecord
- Defined in:
- lib/reparty/report/active_record.rb
Constant Summary collapse
- @@color_index =
0
Instance Attribute Summary collapse
-
#field ⇒ Object
readonly
Returns the value of attribute field.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#operation ⇒ Object
readonly
Returns the value of attribute operation.
Attributes inherited from Reparty::Report
Instance Method Summary collapse
- #attach(attachments) ⇒ Object
- #daily_dataset ⇒ Object
-
#initialize(*args, &block) ⇒ ActiveRecord
constructor
A new instance of ActiveRecord.
- #total ⇒ Object
- #yesterday ⇒ Object
Constructor Details
#initialize(*args, &block) ⇒ ActiveRecord
Returns a new instance of ActiveRecord.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/reparty/report/active_record.rb', line 9 def initialize(*args, &block) super(args.shift, 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 |
Instance Attribute Details
#field ⇒ Object (readonly)
Returns the value of attribute field.
7 8 9 |
# File 'lib/reparty/report/active_record.rb', line 7 def field @field end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
7 8 9 |
# File 'lib/reparty/report/active_record.rb', line 7 def model @model end |
#operation ⇒ Object (readonly)
Returns the value of attribute operation.
7 8 9 |
# File 'lib/reparty/report/active_record.rb', line 7 def operation @operation end |
Instance Method Details
#attach(attachments) ⇒ Object
30 31 32 33 34 35 36 37 |
# File 'lib/reparty/report/active_record.rb', line 30 def attach() @graph = @operation == :total ? build_daily_area_graph : @graph.data(@model.to_s.pluralize, daily_dataset, @graph.colors[@@color_index]) @@color_index = (@@color_index + 1) % @graph.colors.length .inline["#{self.hash}.png"] = @graph.to_blob end |
#daily_dataset ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/reparty/report/active_record.rb', line 39 def daily_dataset if @operation == :total 6.downto(0).map { |x| @model.where("#{@field.to_s} < ?", DateTime.now.utc.at_midnight-x).send(:count, @field) } else 7.downto(1).map { |x| @model.where("DATE(#{@field.to_s}) = ?", DateTime.now.utc.to_date-x).send(@operation, @field) } end end |
#total ⇒ Object
52 53 54 55 |
# File 'lib/reparty/report/active_record.rb', line 52 def total op = @operation == :total ? :count : @operation @model.send(op, @field) end |
#yesterday ⇒ Object
47 48 49 50 |
# File 'lib/reparty/report/active_record.rb', line 47 def yesterday op = @operation == :total ? :count : @operation @model.where("DATE(#{@field.to_s}) = ?", DateTime.now.utc.to_date-1).send(op, @field) end |