acts-as-optionable 0.1.1 → 0.2.0
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/README.rdoc +1 -2
- data/VERSION +1 -1
- data/lib/acts_as_optionable/acts_as_optionable.rb +7 -5
- data/lib/acts_as_optionable/specify_option.rb +2 -2
- data/spec/acts_as_optionable_spec.rb +48 -3
- metadata +4 -4
data/README.rdoc
CHANGED
@@ -110,7 +110,6 @@ This is useful, for instance, for passing a set of options into a liquid templat
|
|
110
110
|
* Thinking about finding a way to optionally associate the option with an asset table. This would be useful for allowing user defined stylesheet options referencing S3 assets.
|
111
111
|
* Add in a way to return an option value straight away.
|
112
112
|
* Assign multiple settings at a time.
|
113
|
-
|
114
|
-
|
113
|
+
* Add a type or hint storage on options (i.e. this option is for a color)
|
115
114
|
|
116
115
|
Copyright (c) 2010 Brendon Murphy, released under the MIT license
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
@@ -16,9 +16,9 @@ module ActiveRecord
|
|
16
16
|
|
17
17
|
module IntanceMethods
|
18
18
|
# Store an option persistently and override default option
|
19
|
-
def set_option(name, value)
|
20
|
-
option = get_stored_option(name) || options.build(:name => name.to_s)
|
21
|
-
return if new_option_matches_current?(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)
|
21
|
+
return if new_option_matches_current?(option, value)
|
22
22
|
option.value = value
|
23
23
|
ret = option.save!
|
24
24
|
options(:reload)
|
@@ -55,6 +55,7 @@ module ActiveRecord
|
|
55
55
|
options = {}
|
56
56
|
options_and_defaults.each do |name, option|
|
57
57
|
options[name.to_s] = option.value
|
58
|
+
options["#{name.to_s}_kind"] = option.kind
|
58
59
|
end
|
59
60
|
OpenStruct.new(options)
|
60
61
|
end
|
@@ -77,8 +78,9 @@ module ActiveRecord
|
|
77
78
|
end
|
78
79
|
|
79
80
|
# Check if a new value provided is the same as the default option.
|
80
|
-
def new_option_matches_current?(option)
|
81
|
-
|
81
|
+
def new_option_matches_current?(option, new_value)
|
82
|
+
default_value = ( get_default_option(option.name).value rescue nil )
|
83
|
+
( option.value == default_value ) || ( option.value == new_value && ! option.new_record? )
|
82
84
|
end
|
83
85
|
|
84
86
|
# Return the stored options as a hash, with the option names as keys.
|
@@ -5,7 +5,7 @@ module ActiveRecord
|
|
5
5
|
module ClassMethods
|
6
6
|
# Setup a default value at the class level.
|
7
7
|
def specify_option(option_name, opts = {})
|
8
|
-
optionable_specified_options[option_name.to_s] = Option.new_readonly(:name => option_name.to_s, :default => opts[:default])
|
8
|
+
optionable_specified_options[option_name.to_s] = Option.new_readonly(:name => option_name.to_s, :default => opts[:default], :kind => opts[:kind])
|
9
9
|
end
|
10
10
|
|
11
11
|
# Returns a hash of options specified at the class level
|
@@ -39,7 +39,7 @@ module ActiveRecord
|
|
39
39
|
@instance_specified_options = {}
|
40
40
|
opts.each do |option_name, attributes|
|
41
41
|
attributes.symbolize_keys!
|
42
|
-
@instance_specified_options[option_name.to_s] = Option.new_readonly(:name => option_name, :default => attributes[:default])
|
42
|
+
@instance_specified_options[option_name.to_s] = Option.new_readonly(:name => option_name, :default => attributes[:default], :kind => attributes[:kind])
|
43
43
|
end
|
44
44
|
end
|
45
45
|
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"
|
12
|
+
specify_option :bar, :default => "BARBAR", :kind => "example"
|
13
13
|
end
|
14
14
|
|
15
15
|
class NoSpecifiedMixin < Mixin
|
@@ -23,16 +23,16 @@ def setup_db
|
|
23
23
|
ActiveRecord::Base.logger
|
24
24
|
ActiveRecord::Schema.define do
|
25
25
|
create_table :mixins do |t|
|
26
|
-
t.string :
|
26
|
+
t.string :stuff
|
27
27
|
end
|
28
28
|
|
29
29
|
create_table :options do |t|
|
30
30
|
t.string :name
|
31
31
|
t.string :value
|
32
|
+
t.string :kind
|
32
33
|
t.references :optionable, :polymorphic => true
|
33
34
|
t.timestamps
|
34
35
|
end
|
35
|
-
|
36
36
|
end
|
37
37
|
end
|
38
38
|
|
@@ -70,6 +70,15 @@ describe "ActsAsOptionable" do
|
|
70
70
|
@optionable.get_option(:foo).value.should == "FOOFOO"
|
71
71
|
@optionable.get_option(:bar).value.should == "BARBAR"
|
72
72
|
end
|
73
|
+
|
74
|
+
it "should not have a kind if none was specified" do
|
75
|
+
@optionable.get_option(:foo).kind.should be_blank
|
76
|
+
end
|
77
|
+
|
78
|
+
it "should be able to specify the kind of option" do
|
79
|
+
@optionable.get_option(:bar).kind.should == "example"
|
80
|
+
end
|
81
|
+
|
73
82
|
|
74
83
|
it "should not mix specifications across unrelated classes" do
|
75
84
|
class Foobar < Mixin
|
@@ -105,6 +114,16 @@ describe "ActsAsOptionable" do
|
|
105
114
|
@optionable.get_option(:buzz).value.should == "BUZZBUZZ"
|
106
115
|
end
|
107
116
|
|
117
|
+
it "should have the kind if set" do
|
118
|
+
@optionable.instance_specified_options = @options_template.merge(:kind_is_set => {:default => "kind_is_set", :kind => "example_kind" })
|
119
|
+
@optionable.get_option(:kind_is_set).kind.should == "example_kind"
|
120
|
+
end
|
121
|
+
|
122
|
+
it "should not have the kind if none was provided" do
|
123
|
+
@optionable.instance_specified_options = @options_template
|
124
|
+
@optionable.get_option(:fizz).kind.should be_blank
|
125
|
+
end
|
126
|
+
|
108
127
|
it "should return readonly records for the default options" do
|
109
128
|
@optionable.instance_specified_options = @options_template
|
110
129
|
@optionable.get_option(:fizz).should be_readonly
|
@@ -175,6 +194,26 @@ describe "ActsAsOptionable" do
|
|
175
194
|
@optionable.set_option(@key, false)
|
176
195
|
@optionable.get_option(@key).value.should be_false
|
177
196
|
end
|
197
|
+
|
198
|
+
it "should allowing storing the option kind" do
|
199
|
+
@optionable.set_option(@key, "red", "color")
|
200
|
+
@optionable.get_option(@key).kind.should == "color"
|
201
|
+
end
|
202
|
+
|
203
|
+
it "should not update the option if it already matches current" do
|
204
|
+
@optionable.set_option(@key, "red", "color")
|
205
|
+
timestamp = @optionable.get_option(@key).updated_at.dup
|
206
|
+
sleep 1 # not worth a dependency for 1 time check
|
207
|
+
@optionable.set_option(@key, "red", "color")
|
208
|
+
@optionable.get_option(@key).updated_at.should == timestamp
|
209
|
+
end
|
210
|
+
|
211
|
+
it "should not set the option if it matches default" do
|
212
|
+
@optionable.get_option(:foo).value.should == "FOOFOO"
|
213
|
+
lambda {
|
214
|
+
@optionable.set_option(:foo, "FOOFOO")
|
215
|
+
}.should_not change { Option.count }
|
216
|
+
end
|
178
217
|
end
|
179
218
|
|
180
219
|
describe "getting options" do
|
@@ -241,6 +280,12 @@ describe "ActsAsOptionable" do
|
|
241
280
|
option_values.bar.should == "BARBAR"
|
242
281
|
end
|
243
282
|
|
283
|
+
it "should have option_name_kind methods set" do
|
284
|
+
option_values = @optionable.options_values_struct
|
285
|
+
option_values.foo_kind.should be_blank
|
286
|
+
option_values.bar_kind.should == "example"
|
287
|
+
end
|
288
|
+
|
244
289
|
it "should contain both set and default options" do
|
245
290
|
@optionable.instance_specified_options = @options_template
|
246
291
|
@optionable.set_option("example_option", 99)
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
-
-
|
8
|
-
-
|
9
|
-
version: 0.
|
7
|
+
- 2
|
8
|
+
- 0
|
9
|
+
version: 0.2.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-
|
17
|
+
date: 2010-04-26 00:00:00 -07:00
|
18
18
|
default_executable:
|
19
19
|
dependencies: []
|
20
20
|
|