attribute_choices 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/License.md +23 -0
  2. data/README.md +62 -0
  3. data/lib/attribute_choices.rb +90 -0
  4. metadata +125 -0
data/License.md ADDED
@@ -0,0 +1,23 @@
1
+ Copyright (c) 2009-2011 Christos Zisopoulos ( [@christos](http://twitter.com/christos) )
2
+ ========================================================================================
3
+
4
+ The "attribute_choices" plugin is released under the **MIT LICENSE**
5
+ --------------------------------------------------------------------
6
+
7
+ Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ of this software and associated documentation files (the "Software"), to deal
9
+ in the Software without restriction, including without limitation the rights
10
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ copies of the Software, and to permit persons to whom the Software is
12
+ furnished to do so, subject to the following conditions:
13
+
14
+ The above copyright notice and this permission notice shall be included in
15
+ all copies or substantial portions of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
20
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,62 @@
1
+ # AttributeChoices
2
+
3
+ AttributeChoices is a plugin that simplifies the common pattern of mapping a set of discreet values for an ActiveRecord model attribute, to a set of display values for human consumption.
4
+
5
+ # Installation
6
+
7
+ ## Rails 3
8
+
9
+ In your `Gemfile` add
10
+
11
+ gem 'attribute_choices'
12
+
13
+ ## Rails 2
14
+
15
+ In `environment.rb` add:
16
+
17
+ config.gem 'attribute_choices'
18
+
19
+ # Usage
20
+
21
+ Define your model:
22
+
23
+ class User < ActiveRecord::Base
24
+ attribute_choices :gender, { 'm' => "Male", 'f' => 'Female'}, :validate => true
25
+ attribute_choices :age_group, [
26
+ ['18-24', '18 to 24 years old'],
27
+ ['25-45', '25 to 45 years old']
28
+ ]
29
+ end
30
+
31
+ Then try this in the console:
32
+
33
+ >> @john # User.new :gender => 'm', :age_group => '18-24', :name => 'John'
34
+ >> @john.gender
35
+ => 'm'
36
+ >> @john.gender_display
37
+ => 'Male'
38
+ >> User.gender_choices
39
+ => [["Male", 'm'], ['Female', 'f']]
40
+
41
+ >> @john.valid?
42
+ => true
43
+ >> @john.gender # 'o'
44
+ >> @john.valid?
45
+ => false
46
+
47
+ => I18n.locale # :es
48
+ >> @john.gender_display
49
+ => "translation missing: es, Male"
50
+
51
+ >> @john.age_group_display
52
+ => '18 to 24 years old'
53
+ >> User.age_group_choices
54
+ => [['18-24', '18 to 24 years old], ['25-45', '25 to 45 years old']]
55
+
56
+ # ToDo
57
+
58
+ * Validate absence of _display and _choices methods
59
+ * Consider the usefulness of _choices# method
60
+
61
+
62
+ Copyright (c) 2009-2011 Christos Zisopoulos, released under the MIT license
@@ -0,0 +1,90 @@
1
+ module AttributeChoices
2
+ def self.included(base) #:nodoc:
3
+ base.extend(AttributeChoicesMacro)
4
+ end
5
+
6
+ module AttributeChoicesMacro
7
+
8
+ # Associate a list of display values for an attribute with a list of discreet values
9
+ #
10
+ # The arguments are:
11
+ #
12
+ # * +attribute+ - The attribute whose values you want to map to display values
13
+ # * +choices+ - Either an +Array+ of tupples where the first value of the tupple is the attribute \
14
+ # value and the second one is the display value mapping, or a +Hash+ where the key is the \
15
+ # attribute value and the value is the display value mapping.
16
+ # * <tt>options</tt> - An optional hash of options:
17
+ # * <tt>:localize</tt> - If set to +true+, then <tt>I18n.trasnlate</tt> is used to translate the value \
18
+ # returned by the +_display+ instance methods as well as translate the display \
19
+ # values returned by the +_choices+ class method
20
+ # * <tt>:validate</tt> - If set to +true+, +validates_inclusion_of+ is used to ensure that the attribute \
21
+ # only accepts the values passed in with the +choices+
22
+ #
23
+ # For example:
24
+ # class User < ActiveRecord::Base
25
+ # attribute_choices :gender, { 'm' => "Male", 'f' => 'Female'}
26
+ # attribute_choices :age_group, [
27
+ # ['18-24', '18 to 24 years old],
28
+ # ['25-45', '25 to 45 years old']
29
+ # ], :localize => true, :validate => false
30
+ # end
31
+ #
32
+ # The macro adds an instance method named after the attribute, suffixed with <tt>_display</tt>
33
+ # (e.g. <tt>gender_display</tt> for <tt>:gender</tt>) that returns the display value of the
34
+ # attribute for a given value, or <tt>nil</tt> if a mapping for a value is missing.
35
+ #
36
+ # It also adds a class method named after the attribute, suffixed with <tt>_choices</tt>
37
+ # (e.g. <tt>User.gender_choices</tt> for <tt>:gender</tt>) that returns an array of choices and values
38
+ # in a fomrat that is suitable for passing directly to the Rails <tt>select_*</tt> helpers.
39
+ #
40
+ # NOTE: You can use a Hash for the +choices+ argument which is converted to an Array. The order of the \
41
+ # tupples of the resulting Array is only guaranteed to be preserved if you are using Ruby 1.9
42
+ def attribute_choices(attribute, choices, *args)
43
+
44
+ assert_valid_attribute(attribute.to_s)
45
+
46
+ write_inheritable_hash :attribute_choices_storage, {}
47
+ class_inheritable_reader :attribute_choices_storage
48
+
49
+ write_inheritable_hash :attribute_choices_options, {}
50
+ class_inheritable_reader :attribute_choices_options
51
+
52
+ attribute = attribute.to_sym
53
+
54
+ options = args.extract_options!
55
+ options.reverse_merge!(:validate => false, :localize => false)
56
+ options.assert_valid_keys(:validate, :localize)
57
+ attribute_choices_options[attribute.to_sym] = options
58
+
59
+ if options[:localize]
60
+ attribute_choices_storage[attribute] = choices.to_a.collect {|t| [t.first, I18n.translate(t.last)]}
61
+ else
62
+ attribute_choices_storage[attribute] = choices.to_a
63
+ end
64
+
65
+ define_method("#{attribute.to_s}_display") do
66
+ tupple = attribute_choices_storage[attribute].assoc(read_attribute(attribute))
67
+ tupple && tupple.last
68
+ end
69
+
70
+ (class << self; self; end).send(:define_method, "#{attribute.to_s}_choices") do
71
+ attribute_choices_storage[attribute].collect(&:reverse)
72
+ end
73
+
74
+ if column_names.include?(attribute.to_s) && options[:validate]
75
+ validates_inclusion_of attribute.to_sym, :in => attribute_choices_storage[attribute].collect {|i| i.first}
76
+ end
77
+
78
+ end
79
+
80
+ private
81
+ def assert_valid_attribute(attr_name)
82
+ unless column_names.include?(attr_name.to_s)
83
+ raise ArgumentError, "Model attribute '#{attr_name.to_s}' doesn't exist"
84
+ end
85
+ end
86
+ end
87
+ end
88
+
89
+ ActiveRecord::Base.send(:include, AttributeChoices)
90
+
metadata ADDED
@@ -0,0 +1,125 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: attribute_choices
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease:
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 0
10
+ version: 1.0.0
11
+ platform: ruby
12
+ authors:
13
+ - Christos Zisopoulos
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-05-12 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rake
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :development
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: sqlite3
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
46
+ version: "0"
47
+ type: :development
48
+ version_requirements: *id002
49
+ - !ruby/object:Gem::Dependency
50
+ name: activesupport
51
+ prerelease: false
52
+ requirement: &id003 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ hash: 3
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ type: :runtime
62
+ version_requirements: *id003
63
+ - !ruby/object:Gem::Dependency
64
+ name: activerecord
65
+ prerelease: false
66
+ requirement: &id004 !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ hash: 3
72
+ segments:
73
+ - 0
74
+ version: "0"
75
+ type: :runtime
76
+ version_requirements: *id004
77
+ description: AttributeChoices is a plugin that simplifies the common pattern of mapping a set of discreet values for an ActiveRecord model attribute, to a set of display values for human consumption.
78
+ email: christos@me.com
79
+ executables: []
80
+
81
+ extensions: []
82
+
83
+ extra_rdoc_files:
84
+ - README.md
85
+ files:
86
+ - License.md
87
+ - README.md
88
+ - lib/attribute_choices.rb
89
+ has_rdoc: true
90
+ homepage: http://github.com/christos/attribute_choices
91
+ licenses: []
92
+
93
+ post_install_message:
94
+ rdoc_options:
95
+ - --main
96
+ - README.md
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ none: false
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ hash: 3
105
+ segments:
106
+ - 0
107
+ version: "0"
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ none: false
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ hash: 3
114
+ segments:
115
+ - 0
116
+ version: "0"
117
+ requirements: []
118
+
119
+ rubyforge_project:
120
+ rubygems_version: 1.6.2
121
+ signing_key:
122
+ specification_version: 3
123
+ summary: AttributeChoices is a plugin that simplifies the common pattern of mapping a set of discreet values for an ActiveRecord model attribute, to a set of display values for human consumption.
124
+ test_files: []
125
+