class SalesforceModel
Abstract ActiveRecord subclass for easy access to Heroku Connect tables. To use, just inherit your model class from this class:
require 'salesforce_model' class Account << SalesforceModel end
You must set HEROKUCONNECT_URL
and HEROKUCONNNECT_SCHEMA
in your environment to point to your Heroku Connect database.
For simple interactive usage, you can call reflect_models
. This will introspect your database and automatically create AR models for the tables that it finds.
Public Class Methods
all_errors()
click to toggle source
# File lib/salesforce_model.rb, line 63 def self.all_errors TriggerLog.where(:state => 'FAILED').order("id DESC").all end
format_trigger_log_rows(rows)
click to toggle source
# File lib/salesforce_model.rb, line 88 def self.format_trigger_log_rows(rows) require 'text-table' tables = Hash.new {|hash,key| hash[key] = Text::Table.new} rows.each do |tl| table = tables[tl.table_name] data = eval("{#{tl.values.gsub(/NULL/,'nil')}}") header = ['log id', 'state','op','table','rec id'] row = [tl.id, tl.state, tl.action, tl.table_name, tl.record_id] data.keys.sort.each do |key| next if key == '_c5_source' header.append(key[0,8]) row.append(data[key]) end header.append('sf_msg') row.append(tl.sf_message) table.head ||= header table.rows.append(row) end tables.each do |table| puts table puts end nil end
last_trigger_id()
click to toggle source
# File lib/salesforce_model.rb, line 71 def self.last_trigger_id row = SalesforceModel.connection.select_all("select * From _trigger_last_id")[0] if row row.values[0].to_i else nil end end
pending_changes()
click to toggle source
# File lib/salesforce_model.rb, line 80 def self.pending_changes TriggerLog.where("state in ('PENDING','NEW')") end
pending_count()
click to toggle source
# File lib/salesforce_model.rb, line 67 def self.pending_count TriggerLog.where("state in ('PENDING','NEW')").count end
recent_changes()
click to toggle source
# File lib/salesforce_model.rb, line 84 def self.recent_changes TriggerLog.order("id DESC").limit(10).all end
recent_updates(table = nil, limit=10)
click to toggle source
# File lib/salesforce_model.rb, line 117 def self.recent_updates(table = nil, limit=10) self.format_trigger_log_rows(TriggerLog.order("id DESC").limit(limit)) end
reflect_models()
click to toggle source
Introspect tables from the active schema and generate SalesforceModel
subclasses for each table. This is meant for quick bootstrapping models at the Rails console.
# File lib/salesforce_model.rb, line 40 def self.reflect_models self.connection.tables.each do |table| next if table.starts_with?("_") || table.starts_with?("c5") next if !self.connection.table_exists?(self.schema_name + "." + table) klass = table.dup klass[0] = klass[0].capitalize if !Object.const_defined?(klass) Object.const_set(klass, Class.new(SalesforceModel)) puts klass end end nil end
Public Instance Methods
pending_updates()
click to toggle source
# File lib/salesforce_model.rb, line 121 def pending_updates rows = TriggerLog.pending.where(:record_id => self.id) SalesforceModel.format_trigger_log_rows(rows) end
recent_updates()
click to toggle source
# File lib/salesforce_model.rb, line 126 def recent_updates SalesforceModel.format_trigger_log_rows(TriggerLog.where(:record_id => self.id).order("id DESC").limit(10)) end
salesforce_error()
click to toggle source
# File lib/salesforce_model.rb, line 134 def salesforce_error log = TriggerLog.where(:record_id => self.id, :state => 'FAILED').order("id DESC").last if !log.nil? return log.sf_message end end
salesforce_errors()
click to toggle source
# File lib/salesforce_model.rb, line 130 def salesforce_errors TriggerLog.where(:record_id => self.id, :state => 'FAILED').order("id DESC").all end