preferences 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +34 -0
- data/{MIT-LICENSE → LICENSE} +0 -0
- data/{README → README.rdoc} +12 -8
- data/Rakefile +44 -36
- data/lib/preferences.rb +16 -9
- data/test/factory.rb +27 -21
- data/test/functional/preferences_test.rb +37 -1
- metadata +7 -7
- data/CHANGELOG +0 -30
data/CHANGELOG.rdoc
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
== master
|
2
|
+
|
3
|
+
== 0.1.3 / 2008-06-29
|
4
|
+
|
5
|
+
* Add +prefs+ as an alias for +preferences+
|
6
|
+
* Fix +preferences+ not properly selecting preferences when a group is specified
|
7
|
+
* Improve test coverage
|
8
|
+
|
9
|
+
== 0.1.2 / 2008-06-22
|
10
|
+
|
11
|
+
* Remove log files from gems
|
12
|
+
|
13
|
+
== 0.1.1 / 2008-06-20
|
14
|
+
|
15
|
+
* Rename preference_values hash to preferences
|
16
|
+
* Rename preferences association to stored_preferences
|
17
|
+
|
18
|
+
== 0.1.0 / 2008-06-19
|
19
|
+
|
20
|
+
* Avoid string evaluation for dynamic methods
|
21
|
+
* Return hashes for the preference_values, e.g.
|
22
|
+
|
23
|
+
user.preference_values # => {'color' => 'red', 'number' => 11, 'website' => {'background' => 'white', 'foreground' => 'black'}}
|
24
|
+
user.preference_values('website') # => {'background' => 'white', 'foreground' => 'black'}
|
25
|
+
|
26
|
+
* Add more generic grouping of preferences than with just other records, e.g.
|
27
|
+
|
28
|
+
user.preferred_color('cars')
|
29
|
+
|
30
|
+
* Remove support for an options hash when specifying :for associations for preference
|
31
|
+
|
32
|
+
== 0.0.1 / 2008-05-10
|
33
|
+
|
34
|
+
* Initial public release
|
data/{MIT-LICENSE → LICENSE}
RENAMED
File without changes
|
data/{README → README.rdoc}
RENAMED
@@ -4,21 +4,21 @@
|
|
4
4
|
|
5
5
|
== Resources
|
6
6
|
|
7
|
-
Wiki
|
8
|
-
|
9
|
-
* http://wiki.pluginaweek.org/Preferences
|
10
|
-
|
11
7
|
API
|
12
8
|
|
13
9
|
* http://api.pluginaweek.org/preferences
|
14
10
|
|
11
|
+
Bugs
|
12
|
+
|
13
|
+
* http://pluginaweek.lighthouseapp.com/projects/13286-preferences
|
14
|
+
|
15
15
|
Development
|
16
16
|
|
17
|
-
* http://
|
17
|
+
* http://github.com/pluginaweek/preferences
|
18
18
|
|
19
19
|
Source
|
20
20
|
|
21
|
-
*
|
21
|
+
* git://github.com/pluginaweek/preferences.git
|
22
22
|
|
23
23
|
== Description
|
24
24
|
|
@@ -119,6 +119,10 @@ 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
|
120
120
|
stored.
|
121
121
|
|
122
|
+
A short-hand alternative for preferences is also available:
|
123
|
+
|
124
|
+
user.prefs # => {"language"=>"English", "color"=>nil}
|
125
|
+
|
122
126
|
=== Grouping preferences
|
123
127
|
|
124
128
|
In addition to defining generic preferences for the owning record, you can also
|
@@ -168,7 +172,7 @@ Preferences are stored in a separate table called "preferences".
|
|
168
172
|
== Testing
|
169
173
|
|
170
174
|
Before you can run any tests, the following gem must be installed:
|
171
|
-
* plugin_test_helper[http://
|
175
|
+
* plugin_test_helper[http://github.com/pluginaweek/plugin_test_helper]
|
172
176
|
|
173
177
|
To run against a specific version of Rails:
|
174
178
|
|
@@ -177,4 +181,4 @@ To run against a specific version of Rails:
|
|
177
181
|
== Dependencies
|
178
182
|
|
179
183
|
* Rails 2.1 or later
|
180
|
-
* plugins_plus[http://
|
184
|
+
* plugins_plus[http://github.com/pluginaweek/plugins_plus]
|
data/Rakefile
CHANGED
@@ -3,46 +3,54 @@ require 'rake/rdoctask'
|
|
3
3
|
require 'rake/gempackagetask'
|
4
4
|
require 'rake/contrib/sshpublisher'
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
6
|
+
spec = Gem::Specification.new do |s|
|
7
|
+
s.name = 'preferences'
|
8
|
+
s.version = '0.1.3'
|
9
|
+
s.platform = Gem::Platform::RUBY
|
10
|
+
s.summary = 'Adds support for easily creating custom preferences for models'
|
11
|
+
|
12
|
+
s.files = FileList['{app,lib,test}/**/*'].to_a - FileList['test/app_root/log/*'].to_a + %w(CHANGELOG.rdoc init.rb LICENSE Rakefile README.rdoc)
|
13
|
+
s.require_path = 'lib'
|
14
|
+
s.has_rdoc = true
|
15
|
+
s.test_files = Dir['test/**/*_test.rb']
|
16
|
+
|
17
|
+
s.author = 'Aaron Pfeifer'
|
18
|
+
s.email = 'aaron@pluginaweek.org'
|
19
|
+
s.homepage = 'http://www.pluginaweek.org'
|
20
|
+
s.rubyforge_project = 'pluginaweek'
|
21
|
+
end
|
22
|
+
|
23
|
+
desc 'Default: run all tests.'
|
12
24
|
task :default => :test
|
13
25
|
|
14
|
-
desc
|
26
|
+
desc "Test the #{spec.name} plugin."
|
15
27
|
Rake::TestTask.new(:test) do |t|
|
16
28
|
t.libs << 'lib'
|
17
|
-
t.
|
29
|
+
t.test_files = spec.test_files
|
18
30
|
t.verbose = true
|
19
31
|
end
|
20
32
|
|
21
|
-
|
33
|
+
begin
|
34
|
+
require 'rcov/rcovtask'
|
35
|
+
namespace :test do
|
36
|
+
desc "Test the #{spec.name} plugin with Rcov."
|
37
|
+
Rcov::RcovTask.new(:rcov) do |t|
|
38
|
+
t.libs << 'lib'
|
39
|
+
t.test_files = spec.test_files
|
40
|
+
t.rcov_opts << '--exclude="^(?!lib/|app/)"'
|
41
|
+
t.verbose = true
|
42
|
+
end
|
43
|
+
end
|
44
|
+
rescue LoadError
|
45
|
+
end
|
46
|
+
|
47
|
+
desc "Generate documentation for the #{spec.name} plugin."
|
22
48
|
Rake::RDocTask.new(:rdoc) do |rdoc|
|
23
49
|
rdoc.rdoc_dir = 'rdoc'
|
24
|
-
rdoc.title =
|
50
|
+
rdoc.title = spec.name
|
25
51
|
rdoc.template = '../rdoc_template.rb'
|
26
52
|
rdoc.options << '--line-numbers' << '--inline-source'
|
27
|
-
rdoc.rdoc_files.include('README')
|
28
|
-
rdoc.rdoc_files.include('lib/**/*.rb')
|
29
|
-
end
|
30
|
-
|
31
|
-
spec = Gem::Specification.new do |s|
|
32
|
-
s.name = PKG_NAME
|
33
|
-
s.version = PKG_VERSION
|
34
|
-
s.platform = Gem::Platform::RUBY
|
35
|
-
s.summary = 'Adds support for easily creating custom preferences for models'
|
36
|
-
|
37
|
-
s.files = FileList['{app,lib,test}/**/*'].to_a - FileList['test/app_root/log/*'].to_a + %w(CHANGELOG init.rb MIT-LICENSE Rakefile README)
|
38
|
-
s.require_path = 'lib'
|
39
|
-
s.autorequire = 'preferences'
|
40
|
-
s.has_rdoc = true
|
41
|
-
s.test_files = Dir['test/**/*_test.rb']
|
42
|
-
|
43
|
-
s.author = 'Aaron Pfeifer'
|
44
|
-
s.email = 'aaron@pluginaweek.org'
|
45
|
-
s.homepage = 'http://www.pluginaweek.org'
|
53
|
+
rdoc.rdoc_files.include('README.rdoc', 'CHANGELOG.rdoc', 'LICENSE', 'lib/**/*.rb', 'app/**/*.rb')
|
46
54
|
end
|
47
55
|
|
48
56
|
Rake::GemPackageTask.new(spec) do |p|
|
@@ -51,14 +59,14 @@ Rake::GemPackageTask.new(spec) do |p|
|
|
51
59
|
p.need_zip = true
|
52
60
|
end
|
53
61
|
|
54
|
-
desc 'Publish the beta gem'
|
62
|
+
desc 'Publish the beta gem.'
|
55
63
|
task :pgem => [:package] do
|
56
|
-
Rake::SshFilePublisher.new('aaron@pluginaweek.org', '/home/aaron/gems.pluginaweek.org/public/gems', 'pkg', "#{
|
64
|
+
Rake::SshFilePublisher.new('aaron@pluginaweek.org', '/home/aaron/gems.pluginaweek.org/public/gems', 'pkg', "#{spec.name}-#{spec.version}.gem").upload
|
57
65
|
end
|
58
66
|
|
59
|
-
desc 'Publish the API documentation'
|
67
|
+
desc 'Publish the API documentation.'
|
60
68
|
task :pdoc => [:rdoc] do
|
61
|
-
Rake::SshDirPublisher.new('aaron@pluginaweek.org', "/home/aaron/api.pluginaweek.org/public/#{
|
69
|
+
Rake::SshDirPublisher.new('aaron@pluginaweek.org', "/home/aaron/api.pluginaweek.org/public/#{spec.name}", 'rdoc').upload
|
62
70
|
end
|
63
71
|
|
64
72
|
desc 'Publish the API docs and gem'
|
@@ -71,10 +79,10 @@ task :release => [:gem, :package] do
|
|
71
79
|
ruby_forge = RubyForge.new.configure
|
72
80
|
ruby_forge.login
|
73
81
|
|
74
|
-
%w(
|
75
|
-
file = "pkg/#{
|
82
|
+
%w(gem tgz zip).each do |ext|
|
83
|
+
file = "pkg/#{spec.name}-#{spec.version}.#{ext}"
|
76
84
|
puts "Releasing #{File.basename(file)}..."
|
77
85
|
|
78
|
-
ruby_forge.add_release(
|
86
|
+
ruby_forge.add_release(spec.rubyforge_project, spec.name, spec.version, file)
|
79
87
|
end
|
80
88
|
end
|
data/lib/preferences.rb
CHANGED
@@ -140,6 +140,12 @@ module PluginAWeek #:nodoc:
|
|
140
140
|
end
|
141
141
|
|
142
142
|
module InstanceMethods
|
143
|
+
def self.included(base) #:nodoc:
|
144
|
+
base.class_eval do
|
145
|
+
alias_method :prefs, :preferences
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
143
149
|
# Finds all preferences, including defaults, for the current record. If
|
144
150
|
# any custom group preferences have been stored, then this will include
|
145
151
|
# all default preferences within that particular group.
|
@@ -164,27 +170,28 @@ module PluginAWeek #:nodoc:
|
|
164
170
|
# user.preferences('cars')
|
165
171
|
# => {"language"=>"English", "color"=>"red"}
|
166
172
|
def preferences(*args)
|
167
|
-
if args.
|
173
|
+
if args.empty?
|
174
|
+
group = nil
|
175
|
+
conditions = {}
|
176
|
+
else
|
168
177
|
group = args.first
|
169
178
|
group_id, group_type = Preference.split_group(group)
|
170
179
|
conditions = {:group_id => group_id, :group_type => group_type}
|
171
|
-
else
|
172
|
-
conditions = {}
|
173
180
|
end
|
174
181
|
|
175
182
|
# Find all of the stored preferences
|
176
183
|
stored_preferences = self.stored_preferences.find(:all, :conditions => conditions)
|
177
184
|
|
178
185
|
# Hashify attribute -> value or group -> attribute -> value
|
179
|
-
stored_preferences.inject(self.class.default_preferences.dup) do |
|
180
|
-
if group = preference.group
|
181
|
-
|
186
|
+
stored_preferences.inject(self.class.default_preferences.dup) do |all_preferences, preference|
|
187
|
+
if !group && (preference_group = preference.group)
|
188
|
+
preferences = all_preferences[preference_group] ||= self.class.default_preferences.dup
|
182
189
|
else
|
183
|
-
|
190
|
+
preferences = all_preferences
|
184
191
|
end
|
185
192
|
|
186
|
-
|
187
|
-
|
193
|
+
preferences[preference.attribute] = preference.value
|
194
|
+
all_preferences
|
188
195
|
end
|
189
196
|
end
|
190
197
|
|
data/test/factory.rb
CHANGED
@@ -1,26 +1,32 @@
|
|
1
1
|
module Factory
|
2
|
-
# Build actions for the
|
3
|
-
def self.build(
|
4
|
-
name =
|
5
|
-
define_method("#{name}_attributes", block)
|
2
|
+
# Build actions for the model
|
3
|
+
def self.build(model, &block)
|
4
|
+
name = model.to_s.underscore
|
6
5
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
6
|
+
define_method("#{name}_attributes", block)
|
7
|
+
define_method("valid_#{name}_attributes") {|*args| valid_attributes_for(model, *args)}
|
8
|
+
define_method("new_#{name}") {|*args| new_record(model, *args)}
|
9
|
+
define_method("create_#{name}") {|*args| create_record(model, *args)}
|
10
|
+
end
|
11
|
+
|
12
|
+
# Get valid attributes for the model
|
13
|
+
def valid_attributes_for(model, attributes = {})
|
14
|
+
name = model.to_s.underscore
|
15
|
+
send("#{name}_attributes", attributes)
|
16
|
+
attributes
|
17
|
+
end
|
18
|
+
|
19
|
+
# Build an unsaved record
|
20
|
+
def new_record(model, *args)
|
21
|
+
model.new(valid_attributes_for(model, *args))
|
22
|
+
end
|
23
|
+
|
24
|
+
# Build and save/reload a record
|
25
|
+
def create_record(model, *args)
|
26
|
+
record = new_record(model, *args)
|
27
|
+
record.save!
|
28
|
+
record.reload
|
29
|
+
record
|
24
30
|
end
|
25
31
|
|
26
32
|
build Car do |attributes|
|
@@ -209,6 +209,18 @@ class UserWithStoredPreferencesTest < Test::Unit::TestCase
|
|
209
209
|
assert_equal expected, @user.preferences
|
210
210
|
end
|
211
211
|
|
212
|
+
def test_should_use_preferences_for_prefs
|
213
|
+
expected = {
|
214
|
+
'hot_salsa' => nil,
|
215
|
+
'dark_chocolate' => true,
|
216
|
+
'color' => nil,
|
217
|
+
'car' => nil,
|
218
|
+
'language' => 'Latin'
|
219
|
+
}
|
220
|
+
|
221
|
+
assert_equal expected, @user.prefs
|
222
|
+
end
|
223
|
+
|
212
224
|
def test_should_not_remove_preference_if_set_to_default
|
213
225
|
@user.preferred_language = 'English'
|
214
226
|
@user.save!
|
@@ -260,7 +272,7 @@ class UserWithStoredPreferencesForBasicGroupsTest < Test::Unit::TestCase
|
|
260
272
|
assert_equal 1, @user.stored_preferences.size
|
261
273
|
end
|
262
274
|
|
263
|
-
def
|
275
|
+
def test_should_include_group_in_preferences
|
264
276
|
expected = {
|
265
277
|
'hot_salsa' => nil,
|
266
278
|
'dark_chocolate' => true,
|
@@ -279,6 +291,30 @@ class UserWithStoredPreferencesForBasicGroupsTest < Test::Unit::TestCase
|
|
279
291
|
assert_equal expected, @user.preferences
|
280
292
|
end
|
281
293
|
|
294
|
+
def test_should_be_able_to_show_all_preferences_just_for_the_owner
|
295
|
+
expected = {
|
296
|
+
'hot_salsa' => nil,
|
297
|
+
'dark_chocolate' => true,
|
298
|
+
'color' => nil,
|
299
|
+
'car' => nil,
|
300
|
+
'language' => 'English'
|
301
|
+
}
|
302
|
+
|
303
|
+
assert_equal expected, @user.preferences(nil)
|
304
|
+
end
|
305
|
+
|
306
|
+
def test_should_be_able_to_show_all_preferences_for_a_single_group
|
307
|
+
expected = {
|
308
|
+
'hot_salsa' => nil,
|
309
|
+
'dark_chocolate' => true,
|
310
|
+
'color' => 'red',
|
311
|
+
'car' => nil,
|
312
|
+
'language' => 'English'
|
313
|
+
}
|
314
|
+
|
315
|
+
assert_equal expected, @user.preferences('cars')
|
316
|
+
end
|
317
|
+
|
282
318
|
def test_should_not_have_preference_without_group
|
283
319
|
assert_nil @user.preferred_color
|
284
320
|
end
|
metadata
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: preferences
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Aaron Pfeifer
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-06-
|
12
|
+
date: 2008-06-29 00:00:00 -04:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -47,11 +47,11 @@ files:
|
|
47
47
|
- test/unit
|
48
48
|
- test/unit/preference_test.rb
|
49
49
|
- test/unit/preference_definition_test.rb
|
50
|
-
- CHANGELOG
|
50
|
+
- CHANGELOG.rdoc
|
51
51
|
- init.rb
|
52
|
-
-
|
52
|
+
- LICENSE
|
53
53
|
- Rakefile
|
54
|
-
- README
|
54
|
+
- README.rdoc
|
55
55
|
has_rdoc: true
|
56
56
|
homepage: http://www.pluginaweek.org
|
57
57
|
post_install_message:
|
@@ -73,7 +73,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
73
73
|
version:
|
74
74
|
requirements: []
|
75
75
|
|
76
|
-
rubyforge_project:
|
76
|
+
rubyforge_project: pluginaweek
|
77
77
|
rubygems_version: 1.1.1
|
78
78
|
signing_key:
|
79
79
|
specification_version: 2
|
data/CHANGELOG
DELETED
@@ -1,30 +0,0 @@
|
|
1
|
-
*SVN*
|
2
|
-
|
3
|
-
*0.1.2* (June 22nd, 2008)
|
4
|
-
|
5
|
-
* Remove log files from gems
|
6
|
-
|
7
|
-
*0.1.1* (June 20th, 2008)
|
8
|
-
|
9
|
-
* Rename preference_values hash to preferences
|
10
|
-
|
11
|
-
* Rename preferences association to stored_preferences
|
12
|
-
|
13
|
-
*0.1.0* (June 19th, 2008)
|
14
|
-
|
15
|
-
* Avoid string evaluation for dynamic methods
|
16
|
-
|
17
|
-
* Return hashes for the preference_values, e.g.
|
18
|
-
|
19
|
-
user.preference_values # => {'color' => 'red', 'number' => 11, 'website' => {'background' => 'white', 'foreground' => 'black'}}
|
20
|
-
user.preference_values('website') # => {'background' => 'white', 'foreground' => 'black'}
|
21
|
-
|
22
|
-
* Add more generic grouping of preferences than with just other records, e.g.
|
23
|
-
|
24
|
-
user.preferred_color('cars')
|
25
|
-
|
26
|
-
* Remove support for an options hash when specifying :for associations for preference
|
27
|
-
|
28
|
-
*0.0.1* (May 10th, 2008)
|
29
|
-
|
30
|
-
* Initial public release
|