Module: QuickbaseRecord::Queries

Extended by:
ActiveSupport::Concern
Includes:
Client
Included in:
Model
Defined in:
lib/quickbase_record/queries.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Methods included from Client

#qb_client

Instance Method Details

#deleteObject



269
270
271
272
273
274
275
276
# File 'lib/quickbase_record/queries.rb', line 269

def delete
  # we have to use [record id] because of the advantage_quickbase gem
  rid = public_send(record_id_field_name)
  return false unless rid

  successful = qb_client.delete_record(self.class.dbid, rid)
  return successful ? self : false
end

#primary_key_field_nameObject



242
243
244
# File 'lib/quickbase_record/queries.rb', line 242

def primary_key_field_name
  @primary_key_field ||= self.class.fields.select { |field_name, field| field.options.include?(:primary_key) }.keys.first
end

#record_id_field_nameObject



246
247
248
# File 'lib/quickbase_record/queries.rb', line 246

def record_id_field_name
  @record_id_field_name ||= self.class.fields.select { |field_name, field| field.fid == 3 }.keys.first
end

#remove_unwritable_fields(hash) ⇒ Object



299
300
301
302
303
304
305
# File 'lib/quickbase_record/queries.rb', line 299

def remove_unwritable_fields(hash)
  writable_fids = writable_fields.values.collect { |field| field.fid }

  hash.delete_if do |fid, value|
    value.nil? || !writable_fids.include?(fid)
  end
end

#saveObject



250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/quickbase_record/queries.rb', line 250

def save
  primary_key = public_send(primary_key_field_name)
  current_object = {}
  self.class.fields.each do |field_name, field|
    current_object[field.fid] = public_send(field_name)
  end

  if current_object[3] #object has a record_id, so we'll edit it
    remove_unwritable_fields(current_object)
    qb_client.edit_record(self.class.dbid, primary_key, current_object)
  else
    remove_unwritable_fields(current_object)
    new_rid = qb_client.add_record(self.class.dbid, current_object)
    public_send("#{record_id_field_name}=", new_rid)
  end

  return self
end

#update_attributes(attributes = {}) ⇒ Object



278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/quickbase_record/queries.rb', line 278

def update_attributes(attributes={})
  return false if attributes.blank?

  self.assign_attributes(attributes)
  updated_attributes = {}
  attributes.each { |field_name, value| updated_attributes[self.class.convert_field_name_to_fid(field_name)] = value }
  primary_key = public_send(primary_key_field_name)
  record_id = public_send(record_id_field_name)

  if record_id
    remove_unwritable_fields(updated_attributes)
    qb_client.edit_record(self.class.dbid, primary_key, updated_attributes)
  else
    remove_unwritable_fields(updated_attributes)
    new_id = qb_client.add_record(self.class.dbid, updated_attributes)
    public_send("#{record_id_field_name}=", new_id)
  end

  return self
end

#writable_fieldsObject

INSTANCE METHODS



238
239
240
# File 'lib/quickbase_record/queries.rb', line 238

def writable_fields
  @writable_fields ||= self.class.fields.reject{ |field_name, field| field.options.include?(:read_only) }
end