Class: ActiveForce::SObject

Inherits:
Object
  • Object
show all
Extended by:
Association, ActiveModel::Callbacks, Forwardable
Includes:
ActiveAttr::Dirty, ActiveAttr::Model
Defined in:
lib/active_force/sobject.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Association

associations, belongs_to, find_association, has_many

Methods included from ActiveAttr::Dirty

#attributes_and_changes, #forget_attribute_assignments, #mutations_from_database

Class Method Details

.build(mash) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'lib/active_force/sobject.rb', line 52

def self.build mash
  return unless mash
  sobject = new
  mash.each do |column, sf_value|
    sobject.write_value column, sf_value
  end
  sobject.clear_changes_information
  sobject
end

.create(args) ⇒ Object



106
107
108
# File 'lib/active_force/sobject.rb', line 106

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

.create!(args) ⇒ Object



110
111
112
# File 'lib/active_force/sobject.rb', line 110

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

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



138
139
140
141
# File 'lib/active_force/sobject.rb', line 138

def self.field field_name, args = {}
  mapping.field field_name, args
  attribute field_name
end

.fieldsObject



44
45
46
# File 'lib/active_force/sobject.rb', line 44

def self.fields
  mapping.sfdc_names
end

.mappingObject



40
41
42
# File 'lib/active_force/sobject.rb', line 40

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

.queryObject



48
49
50
# File 'lib/active_force/sobject.rb', line 48

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

Instance Method Details

#createObject



95
96
97
98
99
100
# File 'lib/active_force/sobject.rb', line 95

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

#create!Object



84
85
86
87
88
89
90
91
92
93
# File 'lib/active_force/sobject.rb', line 84

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

#destroyObject



102
103
104
# File 'lib/active_force/sobject.rb', line 102

def destroy
  sfdc_client.destroy! self.class.table_name, id
end

#persisted?Boolean

Returns:

  • (Boolean)


134
135
136
# File 'lib/active_force/sobject.rb', line 134

def persisted?
  !!id
end

#reloadObject



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

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

#saveObject



124
125
126
127
128
# File 'lib/active_force/sobject.rb', line 124

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

#save!Object



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

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

#to_paramObject



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

def to_param
  id
end

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



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

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!



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/active_force/sobject.rb', line 62

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

#write_value(column, value) ⇒ Object



151
152
153
154
155
156
157
158
159
160
# File 'lib/active_force/sobject.rb', line 151

def write_value column, value
  if association = self.class.find_association(column)
    field = association.relation_name
    value = Association::RelationModelBuilder.build(association, value)
  else
    field = mappings.invert[column]
    value = self.class.mapping.translate_value value, field unless value.nil?
  end
  send "#{field}=", value if field
end