|
1 | 1 | # Represents a preferred value for a particular preference on a model. |
2 | | -# |
| 2 | +# |
3 | 3 | # == Grouped preferences |
4 | | -# |
| 4 | +# |
5 | 5 | # In addition to simple named preferences, preferences can also be grouped by |
6 | 6 | # a particular value, be it a string or ActiveRecord object. For example, a |
7 | 7 | # User may have a preferred color for a particular Car. In this case, the |
8 | 8 | # +owner+ is the User record, the +name+ is "color", and the +group+ is the |
9 | 9 | # Car record. This allows preferences to have a sort of context around them. |
10 | 10 | class Preference < ActiveRecord::Base |
11 | | - belongs_to :owner, :polymorphic => true |
12 | | - belongs_to :group, :polymorphic => true |
13 | | - |
| 11 | + belongs_to :owner, polymorphic: true |
| 12 | + belongs_to :group, polymorphic: true |
| 13 | + |
14 | 14 | validates_presence_of :name, :owner_id, :owner_type |
15 | | - validates_presence_of :group_type, :if => :group_id? |
16 | | - |
| 15 | + validates_presence_of :group_type, if: :group_id? |
| 16 | + |
17 | 17 | class << self |
18 | 18 | # Splits the given group into its corresponding id and type. For simple |
19 | 19 | # primitives, the id will be nil. For complex types, specifically |
20 | 20 | # ActiveRecord objects, the id is the unique identifier stored in the |
21 | 21 | # database for the record. |
22 | | - # |
| 22 | + # |
23 | 23 | # For example, |
24 | | - # |
| 24 | + # |
25 | 25 | # Preference.split_group('google') # => [nil, "google"] |
26 | 26 | # Preference.split_group(1) # => [nil, 1] |
27 | 27 | # Preference.split_group(User.find(1)) # => [1, "User"] |
28 | 28 | def split_group(group = nil) |
29 | 29 | if group.is_a?(ActiveRecord::Base) |
30 | | - group_id, group_type = group.id, group.class.base_class.name.to_s |
| 30 | + group_id = group.id |
| 31 | + group_type = group.class.base_class.name.to_s |
31 | 32 | else |
32 | | - group_id, group_type = nil, group.is_a?(Symbol) ? group.to_s : group |
| 33 | + group_id = nil |
| 34 | + group_type = group.is_a?(Symbol) ? group.to_s : group |
33 | 35 | end |
34 | | - |
| 36 | + |
35 | 37 | [group_id, group_type] |
36 | 38 | end |
37 | 39 | end |
38 | | - |
| 40 | + |
39 | 41 | # The definition of the preference as defined in the owner's model |
40 | 42 | def definition |
41 | 43 | # Optimize number of queries to the database by only looking up the actual |
42 | 44 | # owner record for STI cases when the definition can't be found in the |
43 | 45 | # stored owner type class |
44 | 46 | owner_type && (find_definition(owner_type.constantize) || find_definition(owner.class)) |
45 | 47 | end |
46 | | - |
| 48 | + |
47 | 49 | # Typecasts the value depending on the preference definition's declared type |
48 | 50 | def value |
49 | 51 | value = read_attribute(:value) |
50 | 52 | value = definition.type_cast(value) if definition |
51 | 53 | value |
52 | 54 | end |
53 | | - |
| 55 | + |
54 | 56 | # Only searches for the group record if the group id is specified |
55 | 57 | def group_with_optional_lookup |
56 | 58 | group_id ? group_without_optional_lookup : group_type |
57 | 59 | end |
58 | 60 | alias_method_chain :group, :optional_lookup |
59 | | - |
| 61 | + |
60 | 62 | private |
61 | | - # Finds the definition for this preference in the given owner class. |
62 | | - def find_definition(owner_class) |
63 | | - owner_class.respond_to?(:preference_definitions) && owner_class.preference_definitions[name] |
64 | | - end |
| 63 | + |
| 64 | + # Finds the definition for this preference in the given owner class. |
| 65 | + def find_definition(owner_class) |
| 66 | + owner_class.respond_to?(:preference_definitions) && owner_class.preference_definitions[name] |
| 67 | + end |
65 | 68 | end |
0 commit comments