Module: Offroad::ModelExtensions::GroupDataInstanceMethods

Defined in:
lib/model_extensions.rb

Instance Method Summary collapse

Instance Method Details

#after_mirrored_data_destroyObject

:nodoc#



312
313
314
315
316
# File 'lib/model_extensions.rb', line 312

def after_mirrored_data_destroy
  Offroad::SendableRecordState::note_record_destroyed(self) if Offroad::app_offline?
  Offroad::GroupState::note_group_destroyed(self) if group_offline? && offroad_mode == :group_base
  return true
end

#after_mirrored_data_saveObject

:nodoc#



341
342
343
344
# File 'lib/model_extensions.rb', line 341

def after_mirrored_data_save
  Offroad::SendableRecordState::note_record_created_or_updated(self) if Offroad::app_offline? && changed?
  return true
end

#before_mirrored_data_destroyObject

:nodoc#

Raises:

  • (ActiveRecord::ReadOnlyRecord)


293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/model_extensions.rb', line 293

def before_mirrored_data_destroy
  if group_offline? && offroad_mode == :group_base
    group_state.update_attribute(:group_being_destroyed, true)
  end
  
  return true if unlocked_group_single_record?
  
  if locked_by_offroad?
    # The only thing that can be deleted is the entire group (possibly with its dependent records), and only if we're online
    raise ActiveRecord::ReadOnlyRecord unless Offroad::app_online? and (offroad_mode == :group_base or group_being_destroyed)
  end
  
  # If the app is offline, the only thing that CAN'T be deleted even if unlocked is the group
  raise ActiveRecord::ReadOnlyRecord if Offroad::app_offline? and offroad_mode == :group_base
  
  return true
end

#before_mirrored_data_saveObject

:nodoc#

Raises:



319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/model_extensions.rb', line 319

def before_mirrored_data_save
  return true if unlocked_group_single_record?
  
  raise DataError.new("Invalid owning group") unless owning_group
  
  if Offroad::app_offline?
    case offroad_mode
    when :group_base
      raise DataError.new("Cannot create groups in offline mode") if new_record?
    when :group_owned
      raise DataError.new("Owning group must be the offline group") if owning_group != Offroad::offline_group
    end
  end
  
  validate_changed_id_columns
  
  raise ActiveRecord::ReadOnlyRecord if locked_by_offroad?
  
  return true
end

#group_being_destroyedObject

:nodoc:#



352
353
354
355
# File 'lib/model_extensions.rb', line 352

def group_being_destroyed
  return true unless owning_group # If the group doesn't exist anymore, then it's pretty well destroyed
  return group_state.group_being_destroyed
end

#group_offline=(b) ⇒ Object

Raises:



258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/model_extensions.rb', line 258

def group_offline=(b)
  raise DataError.new("Unable to change a group's offline status in offline app") if Offroad::app_offline?

  if b and Offroad::group_single_models.size > 0 and Offroad::GroupState.count > 0
    raise DataError.new("Unable to set more than one group offline if there are any group single models")
  end

  if b && !group_state
    Offroad::GroupState.for_group(owning_group).create!
  elsif group_state
    group_state.destroy
  end
end

#group_offline?Boolean

Returns:

  • (Boolean)


250
251
252
# File 'lib/model_extensions.rb', line 250

def group_offline?
  not group_online?
end

#group_online?Boolean

Returns:

  • (Boolean)


254
255
256
# File 'lib/model_extensions.rb', line 254

def group_online?
  return group_state.nil?
end

#group_stateObject

:nodoc#



347
348
349
# File 'lib/model_extensions.rb', line 347

def group_state
  Offroad::GroupState.for_group(owning_group).first
end

#last_known_statusObject

Returns a hash with the latest information about this group in the offline app

Raises:



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/model_extensions.rb', line 233

def last_known_status
  raise DataError.new("This method is only for offline groups") if group_online?
  s = group_state
  fields_of_interest = [
    :last_installer_downloaded_at,
    :last_installation_at,
    :last_down_mirror_created_at,
    :last_down_mirror_loaded_at,
    :last_up_mirror_created_at,
    :last_up_mirror_loaded_at,
    :launcher_version,
    :app_version,
    :operating_system
  ]
  return fields_of_interest.map {|field_name| s.send(field_name)}
end

#locked_by_offroad?Boolean

Returns:

  • (Boolean)


219
220
221
222
223
# File 'lib/model_extensions.rb', line 219

def locked_by_offroad?
  return true if Offroad::app_online? && group_offline?
  return true if Offroad::app_offline? && (!group_state || group_state.group_locked?)
  return false
end

#offroad_group_lock!Object

If called on a group_owned_model, methods below bubble up to the group_base_model

Raises:



227
228
229
230
# File 'lib/model_extensions.rb', line 227

def offroad_group_lock!
  raise DataError.new("Cannot lock groups from online app") if Offroad::app_online?
  group_state.update_attribute(:group_locked, true)
end

#owning_groupObject



272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/model_extensions.rb', line 272

def owning_group
  return nil if unlocked_group_single_record?
  return Offroad::GroupState.first.app_group if offroad_mode == :group_single

  # Recurse upwards until we get to the group base
  if self.class.offroad_group_base?
    return self
  else
    parent = send(offroad_parent_assoc.name)
    if parent
      return parent.owning_group
    else
      return nil
    end
  end
end

#unlocked_group_single_record?Boolean

:nodoc:#

Returns:

  • (Boolean)


358
359
360
# File 'lib/model_extensions.rb', line 358

def unlocked_group_single_record?
  offroad_mode == :group_single && Offroad::GroupState.count == 0
end