acts-as-optionable 0.4.1 → 0.4.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -44,6 +44,11 @@ You can also store an optional "kind" attribute as a hint to what you are storin
44
44
  style.set_option("bgcolor", "white", :kind => "color")
45
45
  style.get_option("bgcolor").kind => "color"
46
46
 
47
+ Add a "category" if you want to group options:
48
+
49
+ style.set_option("bgcolor", "white", :category => "colors")
50
+ style.get_option("bgcolor").category => "colors"
51
+
47
52
  You can also store an optional display name for human consumption:
48
53
 
49
54
  style.set_option("bgcolor", "white", :display_name => "Background Color")
@@ -55,13 +60,14 @@ You can also store an optional display name for human consumption:
55
60
  class StyleWithDefaults < ActiveRecord::Base
56
61
  acts_as_optionable
57
62
 
58
- specify_option :background_color, :default => "white", :kind => "color"
63
+ specify_option :background_color, :default => "white", :kind => "color", :category => "background"
59
64
  specify_option :color, :default => "black"
60
65
  end
61
66
 
62
67
  style = StyleWithDefaults.create
63
68
  style.get_option("background_color").value => "white"
64
69
  style.get_option("background_color").kind => "color"
70
+ style.get_option("background_color").category => "background"
65
71
 
66
72
  === Specifying instance options at runtime
67
73
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.1
1
+ 0.4.2
@@ -5,6 +5,7 @@ class CreateOptions < ActiveRecord::Migration
5
5
  t.string :display_name
6
6
  t.string :value
7
7
  t.string :kind
8
+ t.string :category
8
9
  t.references :optionable, :polymorphic => true
9
10
  t.timestamps
10
11
  end
@@ -46,6 +46,7 @@ class Option < ActiveRecord::Base
46
46
  option_hash["display_name"] = display_name
47
47
  option_hash["default"] = default if default
48
48
  option_hash["kind"] = kind if kind
49
+ option_hash["category"] = category if category
49
50
  option_hash.dup
50
51
  end
51
52
 
@@ -17,7 +17,7 @@ module ActiveRecord
17
17
  module IntanceMethods
18
18
  # Store an option persistently and override default option
19
19
  def set_option(name, value, opts = {})
20
- option = get_stored_option(name) || options.build(:name => name.to_s, :value => value, :kind => opts[:kind], :display_name => opts[:display_name])
20
+ option = get_stored_option(name) || options.build(:name => name.to_s, :value => value, :kind => opts[:kind], :display_name => opts[:display_name], :category => opts[:category])
21
21
  return if !opts[:force_set] && new_option_matches_current?(option, value)
22
22
  option.value = value
23
23
  ret = option.save!
@@ -6,7 +6,7 @@ module ActiveRecord
6
6
  # Setup a default value at the class level.
7
7
  def specify_option(option_name, opts = {})
8
8
  name = option_name.to_s
9
- optionable_specified_options[name] = Option.new_readonly(:name => name, :default => opts[:default], :kind => opts[:kind], :display_name => opts[:display_name])
9
+ optionable_specified_options[name] = Option.new_readonly(:name => name, :default => opts[:default], :kind => opts[:kind], :display_name => opts[:display_name], :category => opts[:category])
10
10
  end
11
11
 
12
12
  # Returns a hash of options specified at the class level
@@ -40,7 +40,7 @@ module ActiveRecord
40
40
  @instance_specified_options = {}
41
41
  opts.each do |option_name, attributes|
42
42
  attributes.symbolize_keys!
43
- @instance_specified_options[option_name.to_s] = Option.new_readonly(:name => option_name.to_s, :default => attributes[:default], :kind => attributes[:kind], :display_name => attributes[:display_name])
43
+ @instance_specified_options[option_name.to_s] = Option.new_readonly(:name => option_name.to_s, :default => attributes[:default], :kind => attributes[:kind], :display_name => attributes[:display_name], :category => attributes[:category])
44
44
  end
45
45
  end
46
46
  end
@@ -9,7 +9,7 @@ class SpecifiedMixin < Mixin
9
9
  acts_as_optionable
10
10
 
11
11
  specify_option :foo, :default => "FOOFOO"
12
- specify_option :bar, :default => "BARBAR", :kind => "example", :display_name => "Bar Bar"
12
+ specify_option :bar, :default => "BARBAR", :kind => "example", :display_name => "Bar Bar", :category => "tabs"
13
13
  end
14
14
 
15
15
  class NoSpecifiedMixin < Mixin
@@ -31,6 +31,7 @@ def setup_db
31
31
  t.string :display_name
32
32
  t.string :value
33
33
  t.string :kind
34
+ t.string :category
34
35
  t.references :optionable, :polymorphic => true
35
36
  t.timestamps
36
37
  end
@@ -84,6 +85,10 @@ describe "ActsAsOptionable" do
84
85
  @optionable.get_option(:bar).display_name.should == "Bar Bar"
85
86
  end
86
87
 
88
+ it "should be able to specify a category" do
89
+ @optionable.get_option(:bar).category.should == "tabs"
90
+ end
91
+
87
92
  it "should not mix specifications across unrelated classes" do
88
93
  class Foobar < Mixin
89
94
  acts_as_optionable
@@ -123,6 +128,11 @@ describe "ActsAsOptionable" do
123
128
  @optionable.get_option(:kind_is_set).kind.should == "example_kind"
124
129
  end
125
130
 
131
+ it "should have the category if set" do
132
+ @optionable.instance_specified_options = @options_template.merge(:category_is_set => {:default => "category_is_set", :category => "tabs" })
133
+ @optionable.get_option(:category_is_set).category.should == "tabs"
134
+ end
135
+
126
136
  it "should have the display name if set" do
127
137
  @optionable.instance_specified_options = @options_template.merge(:display_name_is_set => {:default => "kind_is_set", :kind => "example_kind", :display_name => "Example Name" })
128
138
  @optionable.get_option(:display_name_is_set).display_name.should == "Example Name"
@@ -204,11 +214,16 @@ describe "ActsAsOptionable" do
204
214
  @optionable.get_option(@key).value.should be_false
205
215
  end
206
216
 
207
- it "should allowing storing the option kind" do
217
+ it "should allow storing the option kind" do
208
218
  @optionable.set_option(@key, "red", :kind => "color")
209
219
  @optionable.get_option(@key).kind.should == "color"
210
220
  end
211
221
 
222
+ it "should allow storing the option category" do
223
+ @optionable.set_option(@key, "foo", :category => "tabs")
224
+ @optionable.get_option(@key).category.should == "tabs"
225
+ end
226
+
212
227
  it "should not update the option if it already matches current" do
213
228
  @optionable.set_option(@key, "red", "color")
214
229
  timestamp = @optionable.get_option(@key).updated_at.dup
@@ -314,7 +329,7 @@ describe "ActsAsOptionable" do
314
329
 
315
330
  describe "getting options and defaults as a hash" do
316
331
  before(:each) do
317
- @optionable.set_option("example_option", "example_value", :kind => "example", :display_name => "Example Name")
332
+ @optionable.set_option("example_option", "example_value", :kind => "example", :display_name => "Example Name", :category => "tabs")
318
333
  @options_and_defaults_hash = @optionable.options_and_defaults_hash
319
334
  end
320
335
 
@@ -336,6 +351,10 @@ describe "ActsAsOptionable" do
336
351
  @options_and_defaults_hash["bar"]["kind"].should == "example"
337
352
  end
338
353
 
354
+ it "should include the category" do
355
+ @options_and_defaults_hash["example_option"]["category"].should == "tabs"
356
+ end
357
+
339
358
  it "should include the display name" do
340
359
  @options_and_defaults_hash["example_option"]["display_name"].should == "Example Name"
341
360
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 4
8
- - 1
9
- version: 0.4.1
8
+ - 2
9
+ version: 0.4.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Brendon Murphy
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-05-05 00:00:00 -07:00
17
+ date: 2010-05-06 00:00:00 -07:00
18
18
  default_executable:
19
19
  dependencies: []
20
20