Class: Preference

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/preference.rb

Overview

Represents a preferred value for a particular preference on a model.

Targeted preferences

In addition to simple named preferences, preferences can also be targeted for a particular record. For example, a User may have a preferred color for a particular Car. In this case, the owner is the User, the preference is the color, and the target is the Car. This allows preferences to have a sort of context around them.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.split_group(group = nil) ⇒ Object

Splits the given group into its corresponding id and type



19
20
21
22
23
24
25
26
# File 'app/models/preference.rb', line 19

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
  end
  return group_id, group_type
end

Instance Method Details

#definitionObject

The definition for the attribute



30
31
32
# File 'app/models/preference.rb', line 30

def definition
  owner.preference_definitions[attribute] unless owner_type.blank?
end

#group_with_optional_lookupObject

Only searches for the group record if the group id is specified



42
43
44
# File 'app/models/preference.rb', line 42

def group_with_optional_lookup
  group_id ? group_without_optional_lookup : group_type
end

#valueObject

Typecasts the value depending on the preference definition’s declared type



35
36
37
38
39
# File 'app/models/preference.rb', line 35

def value
  value = read_attribute(:value)
  value = definition.type_cast(value) if definition
  value
end