preferences 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.rdoc CHANGED
@@ -1,5 +1,9 @@
1
1
  == master
2
2
 
3
+ == 0.1.4 / 2008-10-26
4
+
5
+ * Change how the base module is included to prevent namespacing conflicts
6
+
3
7
  == 0.1.3 / 2008-06-29
4
8
 
5
9
  * Add +prefs+ as an alias for +preferences+
data/README.rdoc CHANGED
@@ -181,4 +181,4 @@ To run against a specific version of Rails:
181
181
  == Dependencies
182
182
 
183
183
  * Rails 2.1 or later
184
- * plugins_plus[http://github.com/pluginaweek/plugins_plus]
184
+ * plugins_plus[http://github.com/pluginaweek/plugins_plugins] (optional if app files are copied to your project tree)
data/Rakefile CHANGED
@@ -5,11 +5,11 @@ require 'rake/contrib/sshpublisher'
5
5
 
6
6
  spec = Gem::Specification.new do |s|
7
7
  s.name = 'preferences'
8
- s.version = '0.1.3'
8
+ s.version = '0.1.4'
9
9
  s.platform = Gem::Platform::RUBY
10
10
  s.summary = 'Adds support for easily creating custom preferences for models'
11
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)
12
+ s.files = FileList['{app,lib,test}/**/*'] + %w(CHANGELOG.rdoc init.rb LICENSE Rakefile README.rdoc) - FileList['test/app_root/{log,log/*,script,script/*}']
13
13
  s.require_path = 'lib'
14
14
  s.has_rdoc = true
15
15
  s.test_files = Dir['test/**/*_test.rb']
@@ -1,12 +1,12 @@
1
1
  # Represents a preferred value for a particular preference on a model.
2
2
  #
3
- # == Targeted preferences
3
+ # == Grouped preferences
4
4
  #
5
- # In addition to simple named preferences, preferences can also be targeted for
6
- # a particular record. For example, a User may have a preferred color for a
7
- # particular Car. In this case, the +owner+ is the User, the +preference+ is
8
- # the color, and the +target+ is the Car. This allows preferences to have a sort
9
- # of context around them.
5
+ # In addition to simple named preferences, preferences can also be grouped by
6
+ # a particular value, be it a string or ActiveRecord object. For example, a
7
+ # User may have a preferred color for a particular Car. In this case, the
8
+ # +owner+ is the User, the +attribute+ is the color, and the +group+ is the Car.
9
+ # This allows preferences to have a sort of context around them.
10
10
  class Preference < ActiveRecord::Base
11
11
  belongs_to :owner,
12
12
  :polymorphic => true
@@ -20,7 +20,16 @@ class Preference < ActiveRecord::Base
20
20
  :if => :group_id?
21
21
 
22
22
  class << self
23
- # Splits the given group into its corresponding id and type
23
+ # Splits the given group into its corresponding id and type. For simple
24
+ # primitives, the id will be nil. For complex types, specifically ActiveRecord
25
+ # objects, the id is the unique identifier stored in the databse for the
26
+ # record.
27
+ #
28
+ # For example,
29
+ #
30
+ # Preference.split_group('google') # => [nil, "google"]
31
+ # Preference.split_group(1) # => [nil, 1]
32
+ # Preference.split_group(User.find(1)) # => [1, "User"]
24
33
  def split_group(group = nil)
25
34
  if group.is_a?(ActiveRecord::Base)
26
35
  group_id, group_type = group.id, group.class.base_class.name.to_s
@@ -28,7 +37,7 @@ class Preference < ActiveRecord::Base
28
37
  group_id, group_type = nil, group
29
38
  end
30
39
 
31
- return group_id, group_type
40
+ [group_id, group_type]
32
41
  end
33
42
  end
34
43
 
data/lib/preferences.rb CHANGED
@@ -21,12 +21,6 @@ module PluginAWeek #:nodoc:
21
21
  # u.attributes = {:prefers_notifications => true}
22
22
  # u.save!
23
23
  module Preferences
24
- def self.included(base) #:nodoc:
25
- base.class_eval do
26
- extend PluginAWeek::Preferences::MacroMethods
27
- end
28
- end
29
-
30
24
  module MacroMethods
31
25
  # Defines a new preference for all records in the model. By default, preferences
32
26
  # are assumed to have a boolean data type, so all values will be typecasted
@@ -286,5 +280,5 @@ module PluginAWeek #:nodoc:
286
280
  end
287
281
 
288
282
  ActiveRecord::Base.class_eval do
289
- include PluginAWeek::Preferences
283
+ extend PluginAWeek::Preferences::MacroMethods
290
284
  end
@@ -1,5 +1,4 @@
1
1
  module PluginAWeek #:nodoc:
2
- # Adds support for defining preferences on ActiveRecord models.
3
2
  module Preferences
4
3
  # Represents the definition of a preference for a particular model
5
4
  class PreferenceDefinition
@@ -24,7 +23,8 @@ module PluginAWeek #:nodoc:
24
23
  @column.default
25
24
  end
26
25
 
27
- # Typecasts the value based on the type of preference that was defined
26
+ # Typecasts the value based on the type of preference that was defined.
27
+ # This uses ActiveRecord's typecast functionality.
28
28
  def type_cast(value)
29
29
  if @type == 'any'
30
30
  value
data/test/factory.rb CHANGED
@@ -13,12 +13,16 @@ module Factory
13
13
  def valid_attributes_for(model, attributes = {})
14
14
  name = model.to_s.underscore
15
15
  send("#{name}_attributes", attributes)
16
+ attributes.stringify_keys!
16
17
  attributes
17
18
  end
18
19
 
19
20
  # Build an unsaved record
20
21
  def new_record(model, *args)
21
- model.new(valid_attributes_for(model, *args))
22
+ attributes = valid_attributes_for(model, *args)
23
+ record = model.new(attributes)
24
+ attributes.each {|attr, value| record.send("#{attr}=", value) if model.accessible_attributes && !model.accessible_attributes.include?(attr) || model.protected_attributes && model.protected_attributes.include?(attr)}
25
+ record
22
26
  end
23
27
 
24
28
  # Build and save/reload a record
data/test/test_helper.rb CHANGED
@@ -8,6 +8,6 @@ ActiveRecord::Migrator.migrate("#{Rails.root}/db/migrate")
8
8
 
9
9
  # Mixin the factory helper
10
10
  require File.expand_path("#{File.dirname(__FILE__)}/factory")
11
- class Test::Unit::TestCase #:nodoc:
11
+ Test::Unit::TestCase.class_eval do
12
12
  include Factory
13
13
  end
@@ -80,6 +80,26 @@ class PreferenceTest < Test::Unit::TestCase
80
80
  assert !preference.valid?
81
81
  assert_equal 1, Array(preference.errors.on(:group_type)).size
82
82
  end
83
+
84
+ def test_should_protect_attributes_from_mass_assignment
85
+ preference = Preference.new(
86
+ :id => 1,
87
+ :attribute => 'notifications',
88
+ :value => '123',
89
+ :owner_id => 1,
90
+ :owner_type => 'User',
91
+ :group_id => 1,
92
+ :group_type => 'Car'
93
+ )
94
+
95
+ assert_nil preference.id
96
+ assert_equal 'notifications', preference.attribute
97
+ assert_equal '123', preference.value
98
+ assert_equal 1, preference.owner_id
99
+ assert_equal 'User', preference.owner_type
100
+ assert_equal 1, preference.group_id
101
+ assert_equal 'Car', preference.group_type
102
+ end
83
103
  end
84
104
 
85
105
  class PreferenceAsAClassTest < Test::Unit::TestCase
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: preferences
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aaron Pfeifer
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-06-29 00:00:00 -04:00
12
+ date: 2008-10-26 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -39,7 +39,6 @@ files:
39
39
  - test/app_root/db/migrate/001_create_users.rb
40
40
  - test/app_root/db/migrate/003_migrate_preferences_to_version_1.rb
41
41
  - test/app_root/db/migrate/002_create_cars.rb
42
- - test/app_root/log
43
42
  - test/functional
44
43
  - test/functional/preferences_test.rb
45
44
  - test/test_helper.rb
@@ -74,7 +73,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
74
73
  requirements: []
75
74
 
76
75
  rubyforge_project: pluginaweek
77
- rubygems_version: 1.1.1
76
+ rubygems_version: 1.2.0
78
77
  signing_key:
79
78
  specification_version: 2
80
79
  summary: Adds support for easily creating custom preferences for models