Sha256: 98954a95369f768204b4808a43cee5eda58c38e971853cfc83a539396e1152a8

Contents?: true

Size: 1.22 KB

Versions: 2

Compression:

Stored size: 1.22 KB

Contents

class Table < ActiveRecord::Base
  include SharedBehaviors
  
  belongs_to :user
  has_many :reports
  
  generate_guid :guid
  
  validates_presence_of :user
  validates_unique_presence_of :name
  validates_stripped_presence_of :data
  validates_stripped_presence_of :data_type

  validate :data_type_known
  
  serialize :column_names, Array
  
  def sql?
    data_type == "sql"
  end
  
  def result
    fetch
  end
  
  def fetch
    time = Time.now.to_i
    
    out = fetch_data
    out = apply_transform(out)

    atts = {}
    atts[:fetch_time_in_seconds] = Time.now.to_i - time
    atts[:column_names] = out.column_names
    self.attributes = atts
    save unless new_record?
    
    out
  end
  
  def test
    # ouputs html of the table
    begin
      fetch.to_html.html_safe
    rescue => e
      out = "#{e.message}"
      out += "\n\nTrace shown in development environment:\n#{e.backtrace.join("\n")}" if Rails.env.development?
      out.gsub("\n", "<br/>").html_safe
    end
  end
    
  protected
  def data_type_known
    return if data_type.blank?
    unless sql?
      errors.add(:data_type, "is not known")
    end
  end
  
  def fetch_data
    return Ruport::Query.new(data).result if sql?
    Ruport::Data::Table.new
  end
  
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
daily-0.0.3 app/models/table.rb
daily-0.0.2 app/models/table.rb