Sha256: c52719da2372cdc7e58a019909f4ca0bcbd648bfc7b7437d285d8b6cb030a7c1

Contents?: true

Size: 691 Bytes

Versions: 1

Compression:

Stored size: 691 Bytes

Contents

require "reportly/engine"

module Reportly
  module ConsoleMethods
    
    class ReportlyNotValid < StandardError; end
    
    def self.report(model, *fields)

      raise ReportlyNotValid, "Reportly accepts only ActiveRecord Objects" unless is_valid_klass?(model)
    
      # call :all for ActiveRecord::Base model
      model = model.send(:all) unless model.is_a?(Array)
  
      # create a new array if its a single record
      model = [model] unless model.respond_to? :each
    
      Reportly::Engine.report(model, *fields)
    end
    

    def self.is_valid_klass?(klass)
      klass.descends_from_active_record? rescue false or klass.is_a?(Array)
    end        
    
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
reportly-0.0.1 lib/reportly/console_methods.rb