Class: ActiveForce::SObject

Inherits:
Object
  • Object
show all
Extended by:
Association, Bulk, ActiveModel::Callbacks, Forwardable
Includes:
ActiveModel::API, ActiveModel::AttributeMethods, ActiveModel::Attributes, ActiveModel::Dirty, ActiveModel::Model, ActiveModel::Serializers::JSON
Defined in:
lib/active_force/sobject.rb

Constant Summary

Constants included from Bulk

Bulk::TIMEOUT_MESSAGE

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Association

associations, belongs_to, find_association, has_many, has_one

Methods included from Bulk

bulk_delete_all, bulk_insert_all, bulk_update_all

Instance Attribute Details

#build_attributesObject

Returns the value of attribute build_attributes.



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

def build_attributes
  @build_attributes
end

#idObject

Returns the value of attribute id.



31
32
33
# File 'lib/active_force/sobject.rb', line 31

def id
  @id
end

#titleObject

Returns the value of attribute title.



31
32
33
# File 'lib/active_force/sobject.rb', line 31

def title
  @title
end

Class Method Details

.build(mash, association_mapping = {}) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/active_force/sobject.rb', line 81

def self.build mash, association_mapping={}
  return unless mash
  sobject = new

  attributes_not_selected = sobject.class.fields.reject{|key| mash.keys.include?(key)}
  sobject.uninitialize_attributes(attributes_not_selected)
  sobject.build_attributes = mash[:build_attributes] || mash
  sobject.run_callbacks(:build) do
    mash.each do |column, value|
      if association_mapping.has_key?(column.downcase)
        column = association_mapping[column.downcase]
      end
      sobject.write_value column, value, association_mapping
    end
  end
  sobject.clear_changes_information
  sobject
end

.create(args) ⇒ Object



157
158
159
# File 'lib/active_force/sobject.rb', line 157

def self.create args
  new(args).create
end

.create!(args) ⇒ Object



161
162
163
# File 'lib/active_force/sobject.rb', line 161

def self.create! args
  new(args).create!
end

.describeObject



76
77
78
# File 'lib/active_force/sobject.rb', line 76

def self.describe
  sfdc_client.describe(table_name)
end

.field(field_name, args = {}) ⇒ Object



189
190
191
192
193
194
195
# File 'lib/active_force/sobject.rb', line 189

def self.field field_name, args = {}
  options = args.except(:as, :from, :sfdc_name)
  mapping.field field_name, args
  cast_type = args.fetch(:as, :string)
  attribute field_name, cast_type, **options
  define_attribute_methods field_name
end

.fieldsObject



68
69
70
# File 'lib/active_force/sobject.rb', line 68

def self.fields
  mapping.sfdc_names
end

.mappingObject



64
65
66
# File 'lib/active_force/sobject.rb', line 64

def self.mapping
  @mapping ||= ActiveForce::Mapping.new name
end

.queryObject



72
73
74
# File 'lib/active_force/sobject.rb', line 72

def self.query
  ActiveForce::ActiveQuery.new self
end

.update(id, attributes) ⇒ Object



38
39
40
# File 'lib/active_force/sobject.rb', line 38

def update(id, attributes)
  prepare_for_update(id, attributes).update
end

.update!(id, attributes) ⇒ Object



42
43
44
# File 'lib/active_force/sobject.rb', line 42

def update!(id, attributes)
  prepare_for_update(id, attributes).update!
end

Instance Method Details

#[](name) ⇒ Object



217
218
219
# File 'lib/active_force/sobject.rb', line 217

def [](name)
  send(name.to_sym)
end

#[]=(name, value) ⇒ Object



221
222
223
# File 'lib/active_force/sobject.rb', line 221

def []=(name,value)
  send("#{name.to_sym}=", value)
end

#createObject



144
145
146
147
148
149
# File 'lib/active_force/sobject.rb', line 144

def create
  create!
rescue Faraday::ClientError, RecordInvalid => error
  handle_save_error error
  self
end

#create!Object



122
123
124
125
126
127
128
129
130
131
# File 'lib/active_force/sobject.rb', line 122

def create!
  validate!
  run_callbacks :save do
    run_callbacks :create do
      self.id = sfdc_client.create! table_name, attributes_for_sfdb
      clear_changes_information
    end
  end
  self
end

#destroyObject



151
152
153
154
155
# File 'lib/active_force/sobject.rb', line 151

def destroy
  run_callbacks(:destroy) do
    sfdc_client.destroy! self.class.table_name, id
  end
end

#modified_attributesObject



197
198
199
# File 'lib/active_force/sobject.rb', line 197

def modified_attributes
  attributes.select{ |attr, key| changed.include? attr.to_s }
end

#persisted?Boolean

Returns:

  • (Boolean)


185
186
187
# File 'lib/active_force/sobject.rb', line 185

def persisted?
  !!id
end

#reloadObject



201
202
203
204
205
206
207
# File 'lib/active_force/sobject.rb', line 201

def reload
  association_cache.clear
  reloaded = self.class.find(id)
  self.attributes = reloaded.attributes
  clear_changes_information
  self
end

#saveObject



175
176
177
178
179
# File 'lib/active_force/sobject.rb', line 175

def save
  save!
rescue Faraday::ClientError, RecordInvalid => error
  handle_save_error error
end

#save!Object



165
166
167
168
169
170
171
172
173
# File 'lib/active_force/sobject.rb', line 165

def save!
  run_callbacks :save do
    if persisted?
      !!update!
    else
      !!create!
    end
  end
end

#to_paramObject



181
182
183
# File 'lib/active_force/sobject.rb', line 181

def to_param
  id
end

#uninitialize_attributes(attrs) ⇒ Object



133
134
135
136
137
138
139
140
141
142
# File 'lib/active_force/sobject.rb', line 133

def uninitialize_attributes(attrs)
  return if attrs.blank?
  self.instance_variable_get(:@attributes).instance_variable_get(:@attributes).each do |key, value|
    if attrs.include?(self.mappings.dig(value.name.to_sym))
      self.instance_variable_get(:@attributes).instance_variable_get(:@attributes)[key] = ActiveModel::Attribute::UninitializedValue.new(value.name, value.type)
    else
      key
    end
  end
end

#update_attributes(attributes = {}) ⇒ Object Also known as: update



114
115
116
117
118
# File 'lib/active_force/sobject.rb', line 114

def update_attributes attributes = {}
  update_attributes! attributes
rescue Faraday::ClientError, RecordInvalid => error
  handle_save_error error
end

#update_attributes!(attributes = {}) ⇒ Object Also known as: update!



100
101
102
103
104
105
106
107
108
109
110
# File 'lib/active_force/sobject.rb', line 100

def update_attributes! attributes = {}
  assign_attributes attributes
  validate!
  run_callbacks :save do
    run_callbacks :update do
      sfdc_client.update! table_name, attributes_for_sfdb
      clear_changes_information
    end
  end
  true
end

#write_value(key, value, association_mapping = {}) ⇒ Object



209
210
211
212
213
214
215
# File 'lib/active_force/sobject.rb', line 209

def write_value(key, value, association_mapping = {})
  if (association = self.class.find_association(key.to_sym))
    write_association_value(association, value, association_mapping)
  else
    write_field_value(key, value)
  end
end