selection_options_for 0.0.5 → 0.0.6
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/CHANGELOG.md +6 -0
- data/README.md +84 -78
- data/lib/selection_options_for/model_additions.rb +60 -60
- data/lib/selection_options_for/version.rb +1 -1
- data/pkg/selection_options_for-0.0.5.gem +0 -0
- data/selection_options_for.gemspec +4 -0
- data/test/selection_options_for_ex_test.rb +2 -7
- data/test/selection_options_for_test.rb +6 -25
- data/test/test_helper.rb +1 -1
- metadata +34 -6
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -16,115 +16,117 @@ And then execute:
|
|
16
16
|
|
17
17
|
Or install it yourself as:
|
18
18
|
|
19
|
-
$ gem install selection_options_for
|
20
|
-
|
21
|
-
## Usage
|
22
19
|
|
20
|
+
gem install selection_options_for
|
23
21
|
|
24
|
-
|
25
|
-
===================
|
22
|
+
## Usage
|
26
23
|
|
27
24
|
This code allows you to keep the display labels in the model
|
28
25
|
when the DB holds only a 1 character flag.
|
29
26
|
and when the code requires symbolic references to the value to use in algorithms
|
30
27
|
|
31
|
-
element 0 of the array passed in is always the logical symbol
|
32
|
-
If a 2-element Array is passed in, [key, label] and the first letter of label is the DB value
|
33
|
-
If a 3-element Array is passed in, [key, DB value, label]
|
34
|
-
Any other type passed in throws an error
|
28
|
+
* element 0 of the array passed in is always the logical symbol
|
29
|
+
* If a 2-element Array is passed in, [key, label] and the first letter of label is the DB value
|
30
|
+
* If a 3-element Array is passed in, [key, DB value, label]
|
31
|
+
* Any other type passed in throws an error
|
35
32
|
|
36
|
-
Limitations:
|
33
|
+
## Limitations:
|
34
|
+
Don't use this if you will run reports directly against the DB
|
37
35
|
In that case, the reports will not have access to the display labels
|
38
36
|
|
37
|
+
|
38
|
+
class Article < ActiveRecord::Base
|
39
|
+
selection_options_for :file_type_option,
|
40
|
+
[:pdf, 'PDF'],
|
41
|
+
[:html, 'HTML'],
|
42
|
+
[:msword, 'MS-Word']
|
43
|
+
[:text, 'X', 'Textfile']
|
44
|
+
end
|
45
|
+
|
39
46
|
|
40
|
-
|
41
|
-
selection_options_for :file_type_option,
|
42
|
-
[:pdf, 'PDF'],
|
43
|
-
[:html, 'HTML'],
|
44
|
-
[:msword, 'MS-Word']
|
45
|
-
[:text, 'X', 'Textfile']
|
46
|
-
end
|
47
|
-
|
48
|
-
adds the following CLASS METHODS to Article
|
47
|
+
### adds the following CLASS METHODS to Article
|
49
48
|
|
50
49
|
* file_type_options
|
51
50
|
returns a array of 2-value arrays suitable to fill a select tag
|
52
51
|
The second example shows how to start the selection on a blank
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
returns hash of symbols
|
52
|
+
|
53
|
+
<%= select :article, :file_type_option, Article.file_type_options %>
|
54
|
+
<%= select :article, :file_type_option, [['','']] + Article.file_type_options %>
|
55
|
+
|
56
|
+
assert_equal ["MS-Word", "PDF", "HTML"], Article.file_type_option_hash.values
|
57
|
+
assert_equal "['MS-Word', 'PDF', 'HTML']", Article.file_type_option_js_list
|
58
|
+
|
59
|
+
file_type_option_symbols # returns hash of symbols
|
61
60
|
|
62
61
|
adds the following INSTANCE METHODS to Article
|
63
62
|
|
64
|
-
|
65
|
-
|
66
|
-
returns the
|
67
|
-
|
68
|
-
$ file_type_option_label
|
69
|
-
returns the current values label
|
63
|
+
file_type_option_hash
|
64
|
+
file_type_option # returns the single character value as in the db
|
65
|
+
file_type_option_label # returns the current values label
|
66
|
+
file_type_option_symbol # returns the current values symbol
|
70
67
|
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
methods ending in '?' return boolean if the value is set
|
75
|
-
methods ending in '!' set the value
|
76
|
-
|
77
|
-
$ file_type_option_pdf?
|
78
|
-
$ file_type_option_pdf!
|
79
|
-
|
80
|
-
$ file_type_option_html?
|
81
|
-
$ file_type_option_html!
|
82
|
-
|
83
|
-
$ file_type_option_msword?
|
84
|
-
$ file_type_option_msword!
|
85
|
-
|
86
|
-
example #1: Selection list
|
87
|
-
|
88
|
-
article = Article.new
|
89
|
-
article.file_type_option_pdf!
|
90
|
-
assert_equal 'P', article.file_type_option
|
91
|
-
assert_equal :pdf, article.file_type_symbol
|
92
|
-
assert_equal true, article.file_type_option_pdf?
|
93
|
-
assert_equal 'PDF', article.file_type_option_label
|
94
|
-
assert_equal [["MS-Word", "M"], ["PDF", "P"], ["HTML", "H"]],
|
95
|
-
Article.file_type_options
|
96
|
-
assert_equal({"M"=>"MS-Word", "P"=>"PDF", "H"=>"HTML"},
|
97
|
-
Article.file_type_option_hash)
|
68
|
+
* methods ending in '?' return boolean if the value is set
|
69
|
+
* methods ending in '!' set the value
|
98
70
|
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
71
|
+
|
72
|
+
file_type_option_pdf?
|
73
|
+
file_type_option_pdf!
|
74
|
+
|
75
|
+
file_type_option_html?
|
76
|
+
file_type_option_html!
|
77
|
+
|
78
|
+
file_type_option_msword?
|
79
|
+
file_type_option_msword!
|
80
|
+
|
81
|
+
### example #1: Selection list
|
82
|
+
|
83
|
+
article = Article.new
|
84
|
+
article.file_type_option_pdf!
|
85
|
+
assert_equal 'P', article.file_type_option
|
86
|
+
assert_equal :pdf, article.file_type_symbol
|
87
|
+
assert_equal true, article.file_type_option_pdf?
|
88
|
+
assert_equal 'PDF', article.file_type_option_label
|
89
|
+
assert_equal [["MS-Word", "M"], ["PDF", "P"], ["HTML", "H"]],
|
90
|
+
Article.file_type_options
|
91
|
+
assert_equal({"M"=>"MS-Word", "P"=>"PDF", "H"=>"HTML"},
|
92
|
+
Article.file_type_option_hash)
|
93
|
+
|
94
|
+
assert_equals({'P'=>:pdf, 'H'=>:html, 'W'=>:msword, 'T'=>:text},
|
95
|
+
Article.file_type_option_symbols)
|
96
|
+
|
97
|
+
|
98
|
+
By default the value first letter of the label is used as the one character value
|
103
99
|
in the database field. When there are duplicate first letters
|
104
100
|
you can specify a different letter to be stored in the database
|
105
101
|
In the example below 'R' is stored in the database when
|
106
102
|
'Credit Card Account' is selected.
|
107
103
|
|
108
|
-
Example #
|
104
|
+
### Example #2: Selection list
|
109
105
|
|
110
|
-
class Article < ActiveRecord::Base
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
end
|
106
|
+
class Article < ActiveRecord::Base
|
107
|
+
selection_options_for :payment_method_option,
|
108
|
+
[:basic, 'Basic'],
|
109
|
+
[:cash, 'Cash Account'],
|
110
|
+
[:cc, 'R','Credit Card Account']
|
111
|
+
end
|
112
|
+
|
116
113
|
|
117
|
-
$ <%= select :article, :payment_method_option, Article.payment_method_options %>
|
118
114
|
|
119
|
-
|
115
|
+
<%= select :article, :payment_method_option, Article.payment_method_options %>
|
116
|
+
|
120
117
|
|
121
|
-
|
122
|
-
$ <%= radio_button :article, :payment_method_option, key %> <%= value %><br />
|
123
|
-
$ <% end %>
|
118
|
+
### Example #3: Radio button labels
|
124
119
|
|
125
|
-
|
126
|
-
|
120
|
+
<% Article.payment_method_option_hash.each do | key, value | %>
|
121
|
+
<%= radio_button :article, :payment_method_option, key %> <%= value %><br />
|
122
|
+
<% end %>
|
123
|
+
|
127
124
|
|
125
|
+
### Example #4 in a java_script list
|
126
|
+
|
127
|
+
|
128
|
+
payment_method_option_js_list
|
129
|
+
|
128
130
|
|
129
131
|
## Contributing
|
130
132
|
|
@@ -133,3 +135,7 @@ Example #3 in a java_script list
|
|
133
135
|
3. Commit your changes (`git commit -am 'Added some feature'`)
|
134
136
|
4. Push to the branch (`git push origin my-new-feature`)
|
135
137
|
5. Create new Pull Request
|
138
|
+
|
139
|
+
|
140
|
+
## Thanks To
|
141
|
+
* Scott Baron - for helping with the unit tests.
|
@@ -1,62 +1,62 @@
|
|
1
|
-
module
|
2
|
-
|
3
|
-
|
4
|
-
id
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
#{id_hash}.map { |key, value| [value,key] }.sort
|
18
|
-
end
|
19
|
-
def #{id}_label
|
20
|
-
#{self}.#{id_hash}[#{id}]
|
21
|
-
end
|
22
|
-
def self.#{id}_js_list
|
23
|
-
result = '['
|
24
|
-
#{id_hash}.values.sort.each{ |e| result = result + "'"+ e + "', "}
|
25
|
-
result.chop!.chop! if result.size > 2
|
26
|
-
result + ']'
|
27
|
-
end
|
28
|
-
def #{id}_symbol
|
29
|
-
#{self}.#{id_symbols}[self.#{id}]
|
30
|
-
end
|
31
|
-
EOF
|
32
|
-
# load the labels into the class id_hash variable
|
33
|
-
opts.each do |label|
|
34
|
-
unless label.class == Array
|
35
|
-
raise "Invalid item ["+ label.to_s + "] in :" + id + " of type [" + label.class.to_s + "]. Expected Array"
|
36
|
-
end
|
37
|
-
case label.size
|
38
|
-
when 2
|
39
|
-
letter, display_text = label[1].first, label[1]
|
40
|
-
when 3
|
41
|
-
letter, display_text = label[1], label[2]
|
42
|
-
else
|
43
|
-
raise "Invalid number of items in selection_options_for :" + id + " [" +
|
44
|
-
label.class.to_s + "]."
|
45
|
-
end
|
46
|
-
if(send(id_hash).has_key?(letter))
|
47
|
-
raise "Duplicate key during selection_options_for :" + id + ". key: " +
|
48
|
-
letter + ' for text: ' + display_text
|
49
|
-
end
|
50
|
-
send(id_hash)[letter] = display_text
|
51
|
-
send(id_symbols)[letter] = label[0]
|
52
|
-
module_eval <<-EOF2
|
53
|
-
def #{id}_#{label[0].to_s}?
|
54
|
-
self.#{id} == '#{letter}'
|
1
|
+
module SelectionOptionsFor
|
2
|
+
module ModelAdditions
|
3
|
+
|
4
|
+
def selection_options_for(id, *opts) # :nodoc:
|
5
|
+
id = id.to_s
|
6
|
+
id_hash = id + '_hash'
|
7
|
+
id_symbols = id + '_symbols'
|
8
|
+
class_eval <<-EOF
|
9
|
+
class_attribute "#{id_hash}".to_sym
|
10
|
+
send("#{id_hash}=".to_sym, {})
|
11
|
+
|
12
|
+
class_attribute "#{id_symbols}".to_sym
|
13
|
+
send("#{id_symbols}=".to_sym, {})
|
14
|
+
|
15
|
+
def self.#{id}s
|
16
|
+
#{id_hash}.map { |key, value| [value,key] }.sort
|
55
17
|
end
|
56
|
-
def #{id}
|
57
|
-
self.#{
|
18
|
+
def #{id}_label
|
19
|
+
#{self}.#{id_hash}[#{id}]
|
58
20
|
end
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
21
|
+
def self.#{id}_js_list
|
22
|
+
result = '['
|
23
|
+
#{id_hash}.values.sort.each{ |e| result = result + "'"+ e + "', "}
|
24
|
+
result.chop!.chop! if result.size > 2
|
25
|
+
result + ']'
|
26
|
+
end
|
27
|
+
def #{id}_symbol
|
28
|
+
#{self}.#{id_symbols}[self.#{id}]
|
29
|
+
end
|
30
|
+
EOF
|
31
|
+
# load the labels into the class id_hash variable
|
32
|
+
opts.each do |label|
|
33
|
+
unless label.class == Array
|
34
|
+
raise "Invalid item ["+ label.to_s + "] in :" + id + " of type [" + label.class.to_s + "]. Expected Array"
|
35
|
+
end
|
36
|
+
case label.size
|
37
|
+
when 2
|
38
|
+
letter, display_text = label[1].first, label[1]
|
39
|
+
when 3
|
40
|
+
letter, display_text = label[1], label[2]
|
41
|
+
else
|
42
|
+
raise "Invalid number of items in selection_options_for :" + id + " [" +
|
43
|
+
label.class.to_s + "]."
|
44
|
+
end
|
45
|
+
if(send(id_hash).has_key?(letter))
|
46
|
+
raise "Duplicate key during selection_options_for :" + id + ". key: " +
|
47
|
+
letter + ' for text: ' + display_text
|
48
|
+
end
|
49
|
+
send(id_hash)[letter] = display_text
|
50
|
+
send(id_symbols)[letter] = label[0]
|
51
|
+
module_eval <<-EOF2
|
52
|
+
def #{id}_#{label[0].to_s}?
|
53
|
+
self.#{id} == '#{letter}'
|
54
|
+
end
|
55
|
+
def #{id}_#{label[0].to_s}!
|
56
|
+
self.#{id} = '#{letter}'
|
57
|
+
end
|
58
|
+
EOF2
|
59
|
+
end # opts.each
|
60
|
+
end # selection_options_for
|
61
|
+
end # ModelAdditions
|
62
|
+
end # SelectionOptionsFor
|
Binary file
|
@@ -5,13 +5,8 @@ require 'test_helper'
|
|
5
5
|
# test bad data and exceptions
|
6
6
|
#
|
7
7
|
|
8
|
-
class ModelUnderTest <
|
9
|
-
|
10
|
-
def self.column(name, sql_type = nil, default = nil, null = true)
|
11
|
-
columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null)
|
12
|
-
end
|
13
|
-
|
14
|
-
column :price_option, :string
|
8
|
+
class ModelUnderTest < SuperModel::Base
|
9
|
+
extend ModelAdditions
|
15
10
|
|
16
11
|
begin
|
17
12
|
selection_options_for :price_option,
|
@@ -9,22 +9,13 @@ require 'test_helper'
|
|
9
9
|
# 3. SubClassOfModel -- should have it's own copy of class variables
|
10
10
|
#
|
11
11
|
|
12
|
-
class SiblingOfModelUnderTest <
|
13
|
-
|
14
|
-
def self.column(name, sql_type = nil, default = nil, null = true)
|
15
|
-
columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null)
|
16
|
-
end
|
12
|
+
class SiblingOfModelUnderTest < SuperModel::Base
|
13
|
+
extend ModelAdditions
|
17
14
|
end
|
18
15
|
|
19
|
-
class ModelUnderTest <
|
20
|
-
|
21
|
-
|
22
|
-
columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null)
|
23
|
-
end
|
24
|
-
|
25
|
-
column :status_option, :string
|
26
|
-
column :payment_method_option, :string
|
27
|
-
|
16
|
+
class ModelUnderTest < SuperModel::Base
|
17
|
+
extend ModelAdditions
|
18
|
+
|
28
19
|
selection_options_for :status_option,
|
29
20
|
[:partial, 'Partial Registration'],
|
30
21
|
[:active, 'Active'],
|
@@ -94,7 +85,7 @@ class SelectionOptionsForTest < Test::Unit::TestCase
|
|
94
85
|
assert !target.status_option_partial?
|
95
86
|
end
|
96
87
|
|
97
|
-
def
|
88
|
+
def test_sibling_of_target_not_effected_by_class_methods
|
98
89
|
SiblingOfModelUnderTest.payment_method_options
|
99
90
|
flunk "Should throw Exception"
|
100
91
|
rescue NoMethodError => ex
|
@@ -103,16 +94,6 @@ class SelectionOptionsForTest < Test::Unit::TestCase
|
|
103
94
|
flunk "wrong exception thrown"
|
104
95
|
end
|
105
96
|
|
106
|
-
def test_sibling_of_target_not_effected_by_instance_methods
|
107
|
-
sib = SiblingOfModelUnderTest.new
|
108
|
-
sib.status_option_partial?
|
109
|
-
flunk "Should throw Exception"
|
110
|
-
rescue NoMethodError => ex
|
111
|
-
assert true
|
112
|
-
rescue
|
113
|
-
flunk "wrong exception thrown"
|
114
|
-
end
|
115
|
-
|
116
97
|
def test_each_subclass_has_own_symbols
|
117
98
|
|
118
99
|
assert_equal [["Advanced", "Advanced"], ["ECash Account", "ECash Account"]].sort ,
|
data/test/test_helper.rb
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: selection_options_for
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 19
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 6
|
10
|
+
version: 0.0.6
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Mark Windholtz
|
@@ -15,9 +15,36 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2012-03-
|
19
|
-
dependencies:
|
20
|
-
|
18
|
+
date: 2012-03-22 00:00:00 Z
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: supermodel
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: 3
|
29
|
+
segments:
|
30
|
+
- 0
|
31
|
+
version: "0"
|
32
|
+
type: :development
|
33
|
+
version_requirements: *id001
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
name: activerecord
|
36
|
+
prerelease: false
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
hash: 3
|
43
|
+
segments:
|
44
|
+
- 0
|
45
|
+
version: "0"
|
46
|
+
type: :runtime
|
47
|
+
version_requirements: *id002
|
21
48
|
description: "\n This code allows you to keep the display labels in the model \n when the DB holds only a 1 character flag.\n and when the code requires symbolic references to the value to use in algorithms\n \n element 0 of the array passed in is always the logical symbol\n If a 2-element Array is passed in, [key, label] and the first letter of label is the DB value\n If a 3-element Array is passed in, [key, DB value, label] \n Any other type passed in throws an error\n \n Limitations: Don't use this if you will run reports directly against the DB \n In that case, the reports will not have access to the display labels \n "
|
22
49
|
email:
|
23
50
|
- mark@agiledna.com
|
@@ -38,6 +65,7 @@ files:
|
|
38
65
|
- lib/selection_options_for/model_additions.rb
|
39
66
|
- lib/selection_options_for/railtie.rb
|
40
67
|
- lib/selection_options_for/version.rb
|
68
|
+
- pkg/selection_options_for-0.0.5.gem
|
41
69
|
- selection_options_for.gemspec
|
42
70
|
- test/selection_options_for_ex_test.rb
|
43
71
|
- test/selection_options_for_test.rb
|