49 lines
		
	
	
		
			1.0 KiB
		
	
	
	
		
			Ruby
		
	
	
	
	
	
		
		
			
		
	
	
			49 lines
		
	
	
		
			1.0 KiB
		
	
	
	
		
			Ruby
		
	
	
	
	
	
|   | require "date" | ||
|  | 
 | ||
|  | module Connectors | ||
|  |   class Report < Base | ||
|  |     include Helpers::ShowCommand, | ||
|  |       Helpers::ListCommand | ||
|  | 
 | ||
|  |     def initialize(db) | ||
|  |       self.collection = db.collection('reports') | ||
|  |     end | ||
|  | 
 | ||
|  |     def save_report r | ||
|  |       r.created_at = Time.new | ||
|  |       collection.insert(r.to_mongo_hash) | ||
|  |     end | ||
|  | 
 | ||
|  |     def reports options={} | ||
|  |       date = {} | ||
|  |       if options.has_key?("date_from") or options.has_key?("date_to") | ||
|  |         if options.has_key?("date_from") | ||
|  |           begin | ||
|  |             d = Date.parse(options["date_from"]) | ||
|  |             date["$gte"] = d.to_time | ||
|  |           rescue ArgumentError | ||
|  |           end | ||
|  |         end | ||
|  |         if options.has_key?("date_to") | ||
|  |           begin | ||
|  |             d = Date.parse(options["date_to"]) | ||
|  |             date["$lt"] = d.to_time | ||
|  |           rescue ArgumentError | ||
|  |           end | ||
|  |         end | ||
|  |         options.delete("date_from") | ||
|  |         options.delete("date_to") | ||
|  |         options["created_at"] = date unless date.empty? | ||
|  |       end | ||
|  |       list(options) | ||
|  |     end | ||
|  | 
 | ||
|  |     private | ||
|  | 
 | ||
|  |     def model_from_bson(bson) | ||
|  |       ::Report.new(bson) | ||
|  |     end | ||
|  |   end | ||
|  | 
 | ||
|  | end |