Class: SalesforceModel

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/salesforce_model.rb

Overview

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.

Direct Known Subclasses

TriggerLog

Defined Under Namespace

Classes: TriggerLog

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.all_errorsObject



63
64
65
# 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) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# 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_idObject



71
72
73
74
75
76
77
78
# 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_changesObject



80
81
82
# File 'lib/salesforce_model.rb', line 80

def self.pending_changes
  TriggerLog.where("state in ('PENDING','NEW')")
end

.pending_countObject



67
68
69
# File 'lib/salesforce_model.rb', line 67

def self.pending_count
  TriggerLog.where("state in ('PENDING','NEW')").count
end

.recent_changesObject



84
85
86
# 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) ⇒ Object



117
118
119
# 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_modelsObject

Introspect tables from the active schema and generate SalesforceModel subclasses for each table. This is meant for quick bootstrapping models at the Rails console.



40
41
42
43
44
45
46
47
48
49
50
51
52
# 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

Instance Method Details

#pending_updatesObject



121
122
123
124
# 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_updatesObject



126
127
128
# 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_errorObject



134
135
136
137
138
139
# 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_errorsObject



130
131
132
# File 'lib/salesforce_model.rb', line 130

def salesforce_errors
  TriggerLog.where(:record_id => self.id, :state => 'FAILED').order("id DESC").all 
end