Method: Preference.split_group

Defined in:
app/models/preference.rb

.split_group(group = nil) ⇒ Object

Splits the given group into its corresponding id and type. For simple primitives, the id will be nil. For complex types, specifically ActiveRecord objects, the id is the unique identifier stored in the database for the record.

For example,

Preference.split_group('google')      # => [nil, "google"]
Preference.split_group(1)             # => [nil, 1]
Preference.split_group(User.find(1))  # => [1, "User"]


28
29
30
31
32
33
34
35
36
# File 'app/models/preference.rb', line 28

def split_group(group = nil)
  if group.is_a?(ActiveRecord::Base)
    group_id, group_type = group.id, group.class.base_class.name.to_s
  else
    group_id, group_type = nil, group.is_a?(Symbol) ? group.to_s : group
  end
  
  [group_id, group_type]
end