acts-as-optionable 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +7 -3
- data/Rakefile +1 -1
- data/VERSION +1 -1
- data/generators/option/option_generator.rb +13 -0
- data/generators/option/templates/create_options.rb +15 -0
- data/generators/option/templates/option.rb +65 -0
- metadata +6 -3
data/README.rdoc
CHANGED
@@ -19,7 +19,7 @@ Available as a gem:
|
|
19
19
|
This creates an Option model in you app.
|
20
20
|
|
21
21
|
1. script/generate option
|
22
|
-
2.
|
22
|
+
2. rake db:migrate
|
23
23
|
|
24
24
|
== Usage
|
25
25
|
|
@@ -38,19 +38,24 @@ Use #get_option, #set_option, and #delete_option for interacting with options.
|
|
38
38
|
style.get_option("color").value => "red"
|
39
39
|
style.delete_option("color")
|
40
40
|
style.get_option("color") => nil
|
41
|
+
|
42
|
+
You can also store an optional "kind" attribute as a hint to what you are storing:
|
41
43
|
|
44
|
+
style.set_option("bgcolor", "white", "color")
|
45
|
+
style.get_option("bgcolor").kind => "color"
|
42
46
|
|
43
47
|
=== Specifying default options at the class level
|
44
48
|
|
45
49
|
class StyleWithDefaults < ActiveRecord::Base
|
46
50
|
acts_as_optionable
|
47
51
|
|
48
|
-
specify_option :background_color, :default => "white"
|
52
|
+
specify_option :background_color, :default => "white", :kind => "color"
|
49
53
|
specify_option :color, :default => "black"
|
50
54
|
end
|
51
55
|
|
52
56
|
style = StyleWithDefaults.create
|
53
57
|
style.get_option("background_color").value => "white"
|
58
|
+
style.get_option("background_color").kind => "color"
|
54
59
|
|
55
60
|
=== Specifying instance options at runtime
|
56
61
|
|
@@ -110,6 +115,5 @@ This is useful, for instance, for passing a set of options into a liquid templat
|
|
110
115
|
* 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
116
|
* Add in a way to return an option value straight away.
|
112
117
|
* Assign multiple settings at a time.
|
113
|
-
* Add a type or hint storage on options (i.e. this option is for a color)
|
114
118
|
|
115
119
|
Copyright (c) 2010 Brendon Murphy, released under the MIT license
|
data/Rakefile
CHANGED
@@ -11,7 +11,7 @@ begin
|
|
11
11
|
gemspec.email = "xternal1+aao@gmail.com"
|
12
12
|
gemspec.homepage = "http://github.com/bemurphy/acts_as_optionable"
|
13
13
|
gemspec.authors = ["Brendon Murphy"]
|
14
|
-
gemspec.files = FileList["[A-Z]*", "{lib,spec,rails}/**/*"] - FileList["**/*.log"]
|
14
|
+
gemspec.files = FileList["[A-Z]*", "{lib,spec,rails,generators}/**/*"] - FileList["**/*.log"]
|
15
15
|
end
|
16
16
|
Jeweler::GemcutterTasks.new
|
17
17
|
rescue LoadError
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.1
|
@@ -0,0 +1,13 @@
|
|
1
|
+
class OptionGenerator < Rails::Generator::Base
|
2
|
+
def manifest
|
3
|
+
record do |m|
|
4
|
+
m.directory 'app/models'
|
5
|
+
m.file 'option.rb', 'app/models/option.rb'
|
6
|
+
m.migration_template "create_options.rb", "db/migrate"
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
def file_name
|
11
|
+
"create_options"
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
class CreateOptions < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
create_table :options do |t|
|
4
|
+
t.string :name
|
5
|
+
t.string :value
|
6
|
+
t.string :kind
|
7
|
+
t.references :optionable, :polymorphic => true
|
8
|
+
t.timestamps
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.down
|
13
|
+
drop_table :options
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
class Option < ActiveRecord::Base
|
2
|
+
include ActiveRecord::Acts::Optionable
|
3
|
+
extend ActiveSupport::Memoizable
|
4
|
+
|
5
|
+
belongs_to :optionable, :polymorphic => true
|
6
|
+
|
7
|
+
default_scope :order => 'created_at ASC'
|
8
|
+
|
9
|
+
validates_uniqueness_of :name, :scope => [:optionable_id, :optionable_type]
|
10
|
+
validates_presence_of [:optionable_type, :optionable_id]
|
11
|
+
|
12
|
+
attr_accessor :default
|
13
|
+
|
14
|
+
def self.new_readonly(attrs)
|
15
|
+
option = new(attrs)
|
16
|
+
option.readonly!
|
17
|
+
option
|
18
|
+
end
|
19
|
+
|
20
|
+
def value
|
21
|
+
val = read_attribute(:value)
|
22
|
+
val ? YAML.load(val) : default
|
23
|
+
end
|
24
|
+
memoize :value
|
25
|
+
|
26
|
+
def value_or_default
|
27
|
+
value || default
|
28
|
+
end
|
29
|
+
memoize :value_or_default
|
30
|
+
|
31
|
+
def value=(val)
|
32
|
+
unless value_class_ok?(val)
|
33
|
+
raise ArgumentError, "Only store booleans, numbers, and strings, please"
|
34
|
+
end
|
35
|
+
|
36
|
+
write_attribute(:value, val.to_yaml)
|
37
|
+
end
|
38
|
+
|
39
|
+
def display_name
|
40
|
+
name.humanize.titleize
|
41
|
+
end
|
42
|
+
|
43
|
+
# def default_value
|
44
|
+
# @config['default']
|
45
|
+
# end
|
46
|
+
#
|
47
|
+
# def evaluated_value
|
48
|
+
# value && !value.empty? ? value : default_value
|
49
|
+
# end
|
50
|
+
#
|
51
|
+
# def color?
|
52
|
+
# @config['type'] == :color
|
53
|
+
# end
|
54
|
+
#
|
55
|
+
# def config=(h)
|
56
|
+
# @config = h
|
57
|
+
# write_attribute(:name, h['name'])
|
58
|
+
# end
|
59
|
+
|
60
|
+
protected
|
61
|
+
|
62
|
+
def value_class_ok?(val)
|
63
|
+
val.is_a?(TrueClass) || val.is_a?(FalseClass) || val.kind_of?(Numeric) || val.is_a?(String)
|
64
|
+
end
|
65
|
+
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 2
|
8
|
-
-
|
9
|
-
version: 0.2.
|
8
|
+
- 1
|
9
|
+
version: 0.2.1
|
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-27 00:00:00 -07:00
|
18
18
|
default_executable:
|
19
19
|
dependencies: []
|
20
20
|
|
@@ -31,6 +31,9 @@ files:
|
|
31
31
|
- README.rdoc
|
32
32
|
- Rakefile
|
33
33
|
- VERSION
|
34
|
+
- generators/option/option_generator.rb
|
35
|
+
- generators/option/templates/create_options.rb
|
36
|
+
- generators/option/templates/option.rb
|
34
37
|
- lib/acts-as-optionable.rb
|
35
38
|
- lib/acts_as_optionable/acts_as_optionable.rb
|
36
39
|
- lib/acts_as_optionable/option_methods.rb
|