preferences 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG +7 -1
- data/README +7 -7
- data/Rakefile +1 -1
- data/lib/preferences.rb +17 -16
- data/test/app_root/log/in_memory.log +2706 -0
- data/test/functional/preferences_test.rb +33 -33
- data/test/unit/preference_test.rb +2 -2
- metadata +2 -2
data/CHANGELOG
CHANGED
@@ -1,5 +1,11 @@
|
|
1
1
|
*SVN*
|
2
2
|
|
3
|
+
*0.1.1* (June 20th, 2008)
|
4
|
+
|
5
|
+
* Rename preference_values hash to preferences
|
6
|
+
|
7
|
+
* Rename preferences association to stored_preferences
|
8
|
+
|
3
9
|
*0.1.0* (June 19th, 2008)
|
4
10
|
|
5
11
|
* Avoid string evaluation for dynamic methods
|
@@ -7,7 +13,7 @@
|
|
7
13
|
* Return hashes for the preference_values, e.g.
|
8
14
|
|
9
15
|
user.preference_values # => {'color' => 'red', 'number' => 11, 'website' => {'background' => 'white', 'foreground' => 'black'}}
|
10
|
-
user.
|
16
|
+
user.preference_values('website') # => {'background' => 'white', 'foreground' => 'black'}
|
11
17
|
|
12
18
|
* Add more generic grouping of preferences than with just other records, e.g.
|
13
19
|
|
data/README
CHANGED
@@ -105,15 +105,15 @@ Write method:
|
|
105
105
|
=== Accessing all preferences
|
106
106
|
|
107
107
|
To get the collection of all custom, stored preferences for a particular record,
|
108
|
-
you can access the +
|
108
|
+
you can access the +stored_preferences+ has_many association which is automatically
|
109
109
|
generated:
|
110
110
|
|
111
|
-
user.
|
111
|
+
user.stored_preferences
|
112
112
|
|
113
113
|
In addition to this, you can get a hash of all stored preferences *and* default
|
114
|
-
preferences, by accessing the +
|
114
|
+
preferences, by accessing the +preferences+ helper:
|
115
115
|
|
116
|
-
user.
|
116
|
+
user.preferences # => {"language"=>"English", "color"=>nil}
|
117
117
|
|
118
118
|
This hash will contain the value for every preference that has been defined for
|
119
119
|
the model, whether that's the default value or one that has been previously
|
@@ -122,7 +122,7 @@ stored.
|
|
122
122
|
=== Grouping preferences
|
123
123
|
|
124
124
|
In addition to defining generic preferences for the owning record, you can also
|
125
|
-
group preferences by ActiveRecord objects or
|
125
|
+
group preferences by ActiveRecord objects or arbitrary names. This is best shown
|
126
126
|
through an example:
|
127
127
|
|
128
128
|
user = User.find(:first)
|
@@ -151,8 +151,8 @@ preferences by name. For example,
|
|
151
151
|
user.preferred_color('automobiles') # => "red"
|
152
152
|
user.preferred_color('clothing') # => "tan"
|
153
153
|
|
154
|
-
user.
|
155
|
-
user.
|
154
|
+
user.preferences # => {"color"=>nil, "automobiles"=>{"color"=>"red"}, "clothing=>{"color=>"tan"}}
|
155
|
+
user.preferences('automobiles') # => {"color"=>"red"}
|
156
156
|
|
157
157
|
=== Saving preferences
|
158
158
|
|
data/Rakefile
CHANGED
data/lib/preferences.rb
CHANGED
@@ -53,7 +53,7 @@ module PluginAWeek #:nodoc:
|
|
53
53
|
#
|
54
54
|
# After the first preference is defined, the following associations are
|
55
55
|
# created for the model:
|
56
|
-
# * +
|
56
|
+
# * +stored_preferences+ - A collection of all the custom preferences specified for a record
|
57
57
|
#
|
58
58
|
# == Generated shortcut methods
|
59
59
|
#
|
@@ -97,11 +97,12 @@ module PluginAWeek #:nodoc:
|
|
97
97
|
class_inheritable_hash :preference_definitions
|
98
98
|
self.preference_definitions = {}
|
99
99
|
|
100
|
-
class_inheritable_hash :
|
101
|
-
self.
|
100
|
+
class_inheritable_hash :default_preferences
|
101
|
+
self.default_preferences = {}
|
102
102
|
|
103
|
-
has_many :
|
104
|
-
:as => :owner
|
103
|
+
has_many :stored_preferences,
|
104
|
+
:as => :owner,
|
105
|
+
:class_name => 'Preference'
|
105
106
|
|
106
107
|
after_save :update_preferences
|
107
108
|
|
@@ -112,7 +113,7 @@ module PluginAWeek #:nodoc:
|
|
112
113
|
attribute = attribute.to_s
|
113
114
|
definition = PreferenceDefinition.new(attribute, *args)
|
114
115
|
self.preference_definitions[attribute] = definition
|
115
|
-
self.
|
116
|
+
self.default_preferences[attribute] = definition.default_value
|
116
117
|
|
117
118
|
# Create short-hand helper methods, making sure that the attribute
|
118
119
|
# is method-safe in terms of what characters are allowed
|
@@ -147,22 +148,22 @@ module PluginAWeek #:nodoc:
|
|
147
148
|
#
|
148
149
|
# A user with no stored values:
|
149
150
|
# user = User.find(:first)
|
150
|
-
# user.
|
151
|
+
# user.preferences
|
151
152
|
# => {"language"=>"English", "color"=>nil}
|
152
153
|
#
|
153
154
|
# A user with stored values for a particular group:
|
154
155
|
# user.preferred_color = 'red', 'cars'
|
155
|
-
# user.
|
156
|
+
# user.preferences
|
156
157
|
# => {"language"=>"English", "color"=>nil, "cars"=>{"language=>"English", "color"=>"red"}}
|
157
158
|
#
|
158
159
|
# Getting preference values for the owning record:
|
159
|
-
# user.
|
160
|
+
# user.preferences(nil)
|
160
161
|
# => {"language"=>"English", "color"=>nil}
|
161
162
|
#
|
162
163
|
# Getting preference values for a particular group:
|
163
|
-
# user.
|
164
|
+
# user.preferences('cars')
|
164
165
|
# => {"language"=>"English", "color"=>"red"}
|
165
|
-
def
|
166
|
+
def preferences(*args)
|
166
167
|
if args.any?
|
167
168
|
group = args.first
|
168
169
|
group_id, group_type = Preference.split_group(group)
|
@@ -172,12 +173,12 @@ module PluginAWeek #:nodoc:
|
|
172
173
|
end
|
173
174
|
|
174
175
|
# Find all of the stored preferences
|
175
|
-
stored_preferences =
|
176
|
+
stored_preferences = self.stored_preferences.find(:all, :conditions => conditions)
|
176
177
|
|
177
178
|
# Hashify attribute -> value or group -> attribute -> value
|
178
|
-
stored_preferences.inject(self.class.
|
179
|
+
stored_preferences.inject(self.class.default_preferences.dup) do |preferences, preference|
|
179
180
|
if group = preference.group
|
180
|
-
preference_group = preferences[group] ||= self.class.
|
181
|
+
preference_group = preferences[group] ||= self.class.default_preferences.dup
|
181
182
|
else
|
182
183
|
preference_group = preferences
|
183
184
|
end
|
@@ -224,7 +225,7 @@ module PluginAWeek #:nodoc:
|
|
224
225
|
value = @preference_values[attribute][group]
|
225
226
|
else
|
226
227
|
group_id, group_type = Preference.split_group(group)
|
227
|
-
preference =
|
228
|
+
preference = stored_preferences.find(:first, :conditions => {:attribute => attribute, :group_id => group_id, :group_type => group_type})
|
228
229
|
value = preference ? preference.value : preference_definitions[attribute].default_value
|
229
230
|
end
|
230
231
|
|
@@ -264,7 +265,7 @@ module PluginAWeek #:nodoc:
|
|
264
265
|
attributes = {:attribute => attribute, :group_id => group_id, :group_type => group_type}
|
265
266
|
|
266
267
|
# Find an existing preference or build a new one
|
267
|
-
preference =
|
268
|
+
preference = stored_preferences.find(:first, :conditions => attributes) || stored_preferences.build(attributes)
|
268
269
|
preference.value = value
|
269
270
|
preference.save!
|
270
271
|
end
|