Class: SalesforceModel
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- SalesforceModel
- 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
Defined Under Namespace
Classes: TriggerLog
Class Method Summary collapse
- .all_errors ⇒ Object
- .format_trigger_log_rows(rows) ⇒ Object
- .last_trigger_id ⇒ Object
- .pending_changes ⇒ Object
- .pending_count ⇒ Object
- .recent_changes ⇒ Object
- .recent_updates(table = nil, limit = 10) ⇒ Object
-
.reflect_models ⇒ Object
Introspect tables from the active schema and generate SalesforceModel subclasses for each table.
Instance Method Summary collapse
- #pending_updates ⇒ Object
- #recent_updates ⇒ Object
- #salesforce_error ⇒ Object
- #salesforce_errors ⇒ Object
Class Method Details
.all_errors ⇒ Object
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.) table.head ||= header table.rows.append(row) end tables.each do |table| puts table puts end nil end |
.last_trigger_id ⇒ Object
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_changes ⇒ Object
80 81 82 |
# File 'lib/salesforce_model.rb', line 80 def self.pending_changes TriggerLog.where("state in ('PENDING','NEW')") end |
.pending_count ⇒ Object
67 68 69 |
# File 'lib/salesforce_model.rb', line 67 def self.pending_count TriggerLog.where("state in ('PENDING','NEW')").count end |
.recent_changes ⇒ Object
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_models ⇒ Object
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_updates ⇒ Object
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_updates ⇒ Object
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_error ⇒ Object
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. end end |
#salesforce_errors ⇒ Object
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 |