acts-as-optionable 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -41,8 +41,14 @@ Use #get_option, #set_option, and #delete_option for interacting with options.
41
41
 
42
42
  You can also store an optional "kind" attribute as a hint to what you are storing:
43
43
 
44
- style.set_option("bgcolor", "white", "color")
44
+ style.set_option("bgcolor", "white", :kind => "color")
45
45
  style.get_option("bgcolor").kind => "color"
46
+
47
+ You can also store an optional display name for human consumption:
48
+
49
+ style.set_option("bgcolor", "white", :display_name => "Background Color")
50
+ style.get_option("bgcolor").display_name => "Background Color"
51
+
46
52
 
47
53
  === Specifying default options at the class level
48
54
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.1
1
+ 0.4.0
@@ -2,6 +2,7 @@ class CreateOptions < ActiveRecord::Migration
2
2
  def self.up
3
3
  create_table :options do |t|
4
4
  t.string :name
5
+ t.string :display_name
5
6
  t.string :value
6
7
  t.string :kind
7
8
  t.references :optionable, :polymorphic => true
@@ -37,34 +37,18 @@ class Option < ActiveRecord::Base
37
37
  end
38
38
 
39
39
  def display_name
40
- name.humanize.titleize
40
+ read_attribute(:display_name) || name.humanize.titleize
41
41
  end
42
42
 
43
43
  def to_h
44
44
  option_hash = {}
45
45
  option_hash["value"] = value
46
+ option_hash["display_name"] = display_name
46
47
  option_hash["default"] = default if default
47
48
  option_hash["kind"] = kind if kind
48
49
  option_hash.dup
49
50
  end
50
-
51
- # def default_value
52
- # @config['default']
53
- # end
54
- #
55
- # def evaluated_value
56
- # value && !value.empty? ? value : default_value
57
- # end
58
- #
59
- # def color?
60
- # @config['type'] == :color
61
- # end
62
- #
63
- # def config=(h)
64
- # @config = h
65
- # write_attribute(:name, h['name'])
66
- # end
67
-
51
+
68
52
  protected
69
53
 
70
54
  def value_class_ok?(val)
@@ -16,8 +16,8 @@ module ActiveRecord
16
16
 
17
17
  module IntanceMethods
18
18
  # Store an option persistently and override default option
19
- def set_option(name, value, kind = nil)
20
- option = get_stored_option(name) || options.build(:name => name.to_s, :value => value, :kind => kind)
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])
21
21
  return if new_option_matches_current?(option, value)
22
22
  option.value = value
23
23
  ret = option.save!
@@ -67,6 +67,7 @@ module ActiveRecord
67
67
  opt_key = name.to_s
68
68
  options[opt_key] = option.value
69
69
  options["#{opt_key}_kind"] = option.kind
70
+ options["#{opt_key}_display_name"] = option.display_name
70
71
  end
71
72
  OpenStruct.new(options)
72
73
  end
@@ -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])
9
+ optionable_specified_options[name] = Option.new_readonly(:name => name, :default => opts[:default], :kind => opts[:kind], :display_name => opts[:display_name])
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, :default => attributes[:default], :kind => attributes[:kind])
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])
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"
12
+ specify_option :bar, :default => "BARBAR", :kind => "example", :display_name => "Bar Bar"
13
13
  end
14
14
 
15
15
  class NoSpecifiedMixin < Mixin
@@ -28,6 +28,7 @@ def setup_db
28
28
 
29
29
  create_table :options do |t|
30
30
  t.string :name
31
+ t.string :display_name
31
32
  t.string :value
32
33
  t.string :kind
33
34
  t.references :optionable, :polymorphic => true
@@ -79,6 +80,9 @@ describe "ActsAsOptionable" do
79
80
  @optionable.get_option(:bar).kind.should == "example"
80
81
  end
81
82
 
83
+ it "should be able to specify a display name" do
84
+ @optionable.get_option(:bar).display_name.should == "Bar Bar"
85
+ end
82
86
 
83
87
  it "should not mix specifications across unrelated classes" do
84
88
  class Foobar < Mixin
@@ -119,6 +123,11 @@ describe "ActsAsOptionable" do
119
123
  @optionable.get_option(:kind_is_set).kind.should == "example_kind"
120
124
  end
121
125
 
126
+ it "should have the display name if set" do
127
+ @optionable.instance_specified_options = @options_template.merge(:display_name_is_set => {:default => "kind_is_set", :kind => "example_kind", :display_name => "Example Name" })
128
+ @optionable.get_option(:display_name_is_set).display_name.should == "Example Name"
129
+ end
130
+
122
131
  it "should not have the kind if none was provided" do
123
132
  @optionable.instance_specified_options = @options_template
124
133
  @optionable.get_option(:fizz).kind.should be_blank
@@ -196,7 +205,7 @@ describe "ActsAsOptionable" do
196
205
  end
197
206
 
198
207
  it "should allowing storing the option kind" do
199
- @optionable.set_option(@key, "red", "color")
208
+ @optionable.set_option(@key, "red", :kind => "color")
200
209
  @optionable.get_option(@key).kind.should == "color"
201
210
  end
202
211
 
@@ -289,7 +298,7 @@ describe "ActsAsOptionable" do
289
298
  it "should contain both set and default options" do
290
299
  @optionable.instance_specified_options = @options_template
291
300
  @optionable.set_option("example_option", 99)
292
- option_values = @optionable.options_values_struct
301
+ option_values = @optionable.options_values_struct
293
302
  option_values.foo.should == "FOOFOO"
294
303
  option_values.fizz.should == "FIZZFIZZ"
295
304
  option_values.example_option.should == 99
@@ -298,7 +307,7 @@ describe "ActsAsOptionable" do
298
307
 
299
308
  describe "getting options and defaults as a hash" do
300
309
  before(:each) do
301
- @optionable.set_option("example_option", "example_value")
310
+ @optionable.set_option("example_option", "example_value", :kind => "example", :display_name => "Example Name")
302
311
  @options_and_defaults_hash = @optionable.options_and_defaults_hash
303
312
  end
304
313
 
@@ -319,5 +328,9 @@ describe "ActsAsOptionable" do
319
328
  @options_and_defaults_hash["bar"]["default"].should == "BARBAR"
320
329
  @options_and_defaults_hash["bar"]["kind"].should == "example"
321
330
  end
331
+
332
+ it "should include the display name" do
333
+ @options_and_defaults_hash["example_option"]["display_name"].should == "Example Name"
334
+ end
322
335
  end
323
336
  end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 3
8
- - 1
9
- version: 0.3.1
7
+ - 4
8
+ - 0
9
+ version: 0.4.0
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-04-29 00:00:00 -07:00
17
+ date: 2010-04-30 00:00:00 -07:00
18
18
  default_executable:
19
19
  dependencies: []
20
20