module Reportly::ConsoleMethods

Public Instance Methods

is_valid_klass?(model) click to toggle source

accepts Array or a ActiveRecord_Relation Should respond_to? :each and be descend from active record

# File lib/reportly/console_methods.rb, line 50
def is_valid_klass?(model)
  model.first.class.descends_from_active_record? rescue false and  model.respond_to? :each
end
make_array_for(model) click to toggle source
# File lib/reportly/console_methods.rb, line 23
def make_array_for(model)
  # report User.first
  # 2.1.3 :003 >   User.first.class
  #  => User(id: uuid, partner_id: uuid, created_at: datetime, updated_at: datetime)
  model = [model] if model.class.superclass == ActiveRecord::Base
  # report User
  # 2.1.3 :004 > User.class
  #  => Class
  model = model.send(:all) if model.class.name == 'Class' and model.respond_to? :all
  
  # User.all.first(2) and User.where(name: 'yannis') are kind of arrays and respond to :each
  
  # 2.1.3 :006 > User.first(2).class
  #  => Array
   
  # 2.1.3 :013 > User.all.class
  #  => User::ActiveRecord_Relation
  model
  
end
report(model, *fields) click to toggle source
# File lib/reportly/console_methods.rb, line 9
def report(model, *fields)
  
  model = make_array_for(model)
  
  validate(model)
  report = Reportly::Engine.report(model, *fields)
  @results = report
  puts report.join("\n")
end
results() click to toggle source
# File lib/reportly/console_methods.rb, line 19
def results
  @results
end
validate(model) click to toggle source
# File lib/reportly/console_methods.rb, line 44
def validate(model)
  raise ReportlyNotValid, "Reportly accepts only ActiveRecord Objects" unless is_valid_klass?(model)
end