Sha256: db62bb7d4bc970cbd8c2c5be19301ce82df0ad92cf07741a0ad8ef732ab8637b

Contents?: true

Size: 1.57 KB

Versions: 9

Compression:

Stored size: 1.57 KB

Contents

class Report < ActiveRecord::Base
  include Stateflow

  #
  # STATEFLOW
  #
  stateflow do
    initial :new

    state :new, :queue, :done, :error

    event :enqueue do
      transitions :from => [:error, :new, :done], :to => :queue
    end

    event :build do
      transitions :from => :queue, :to => [:done, :error], :decide => :build?
    end
  end

  def enqueue!
    enqueue
    save!
    ReportWorker.perform_async(id)
  end

  #
  # RELATIONS
  #
  belongs_to :report_template
  belongs_to :user
  has_many :report_results, :order => 'id DESC'

  #
  # VALIDATIONS
  #
  validates :report_template, :presence => true
  validates :user, :presence => true
  validates :start, :presence => true, :if => lambda{|x| x.report_builder.try(:requires_dates?)}
  validates :finish, :presence => true, :if => lambda{|x| x.report_builder.try(:requires_dates?)}

  #
  # MODIFICATIONS
  #
  after_create :enqueue!

  #
  # METHODS
  #
  def title
    "##{id} (#{report_template.title})"
  end

  def report_builder
    return nil if report_template.blank?
    @report_builder ||= report_template.report_builder(self)
  end

  def build?
    query  = report_builder.query

    begin
      data = ActiveRecord::Base.connection.exec_query(query).to_hash
      ReportResult.create! :report_id => id, :rows => data.length, :data => data
      return :done
    rescue Exception => e
      update_attribute :error, e.to_s
      return :error
    end
  end

  def decode_field(field)
    raise "Incorrect field '#{field}' given" unless field.starts_with?('_')
    report_template.fields[field[1..-1].to_i]
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
smartkiosk-server-0.13.1 app/models/report.rb
smartkiosk-server-0.13 app/models/report.rb
smartkiosk-server-0.12.1 app/models/report.rb
smartkiosk-server-0.12 app/models/report.rb
smartkiosk-server-0.11.11 app/models/report.rb
smartkiosk-server-0.11.10 app/models/report.rb
smartkiosk-server-0.11.9 app/models/report.rb
smartkiosk-server-0.11.8 app/models/report.rb
smartkiosk-server-0.11.7 app/models/report.rb