Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 102 lines (61 sloc) 2.449 kb
a165c0f Christos Zisopoulos Update Readme
authored
1 ## AttributeChoices
8916ed5 Update README and MIT-LICENSE
Christos Zisopoulos authored
2
a165c0f Christos Zisopoulos Update Readme
authored
3 [![Build Status](https://secure.travis-ci.org/christos/attribute_choices.png)](http://travis-ci.org/christos/attribute_choices)
8916ed5 Update README and MIT-LICENSE
Christos Zisopoulos authored
4
a165c0f Christos Zisopoulos Update Readme
authored
5 Extends ActiveRecord attributes with a `:choices` pseudo-type that provides convenient methods for mapping each choice to its human readable form.
8916ed5 Update README and MIT-LICENSE
Christos Zisopoulos authored
6
a165c0f Christos Zisopoulos Update Readme
authored
7 ## Examples
8
9 By defining the available choices for the `gender` attribute in your `User` model,
10
11 class User < ActiveRecord::Base
12 attribute_choices :gender, [ ['m', "Male"], ['f', 'Female'] ],
13 :validate => true
14 end
15
16 ...for any given `User` instance,
17
18 @john = User.new(:gender => 'm')
19 @john.gender
20 => 'm'
21
22 ...you can access the human readable attribute value like this:
23
24 @john.gender_display
25 => 'Male'
26
27 If you need to provide the available choices in a `select` tag, you can simply use `User#gender_choices`,
28
29 <%= select("user", "gender", User.gender_choices) %>
30
31 ...which would give you the following HTML snippet
32
33 <select name="user[gender]">
34 <option value="m">Male</option>
35 <option value="f">Femail</option>
36 </select>
37
38 Validation is also taken care of, if you specify `:validate => true` in the options
39
40 @john.gender = 'x'
41 @john.valid?
42 => false
43
44 And if you work with multiple languages, given the following `es.yml`
45
46 es:
47 user:
48 gender_choices:
49 male: 'Hombre
50 female: 'Mujer'
51
52 ...you can specify the attribute choices like this:
53
54 class User < ActiveRecord::Base
55 attribute_choices :gender, [ ['m', 'user.gender_choices.male'], ['f', 'user.gender_choices.female'] ],
56 :validate => true, :localize => true
57 end
58
59 Then, provided that `I18n.locale == :es`, you are good to go:
60
61 @john.gender_display
62 => 'Hombre'
63
64 User.gender_choices
65 => [["Hombre", 'm'], ['Mujer', 'f']]
66
67
68 ## Installation
69
70 #### Rails 3.x
8916ed5 Update README and MIT-LICENSE
Christos Zisopoulos authored
71
74c3638 Sergio Gil Pérez de la Manga Gemify.me
porras authored
72 In your `Gemfile` add
73
74 gem 'attribute_choices'
75
a165c0f Christos Zisopoulos Update Readme
authored
76 #### Rails 2.x
74c3638 Sergio Gil Pérez de la Manga Gemify.me
porras authored
77
78 In `environment.rb` add:
79
80 config.gem 'attribute_choices'
81
a165c0f Christos Zisopoulos Update Readme
authored
82 ## Changelog
ed645a0 Documentation updates
Christos Zisopoulos authored
83
a165c0f Christos Zisopoulos Update Readme
authored
84 #### V1.0.2
cc8cc8b Update README again
Christos Zisopoulos authored
85
a165c0f Christos Zisopoulos Update Readme
authored
86 * Works correctly with overridden ActiveRecord attribute accessors
87 * Multi-ruby integration testing with Travis-CI
8916ed5 Update README and MIT-LICENSE
Christos Zisopoulos authored
88
a165c0f Christos Zisopoulos Update Readme
authored
89 #### v1.0.1
200d500 Documentation updates
Christos Zisopoulos authored
90
a165c0f Christos Zisopoulos Update Readme
authored
91 * Refactor to eliminate deprecation warnings in Rails 3.1 (@porras)
8916ed5 Update README and MIT-LICENSE
Christos Zisopoulos authored
92
a165c0f Christos Zisopoulos Update Readme
authored
93 #### v1.0.0
94
95 * Initial release
96
97 ## ToDo
98
99 * Validate absence of _display and _choices methods
ed645a0 Documentation updates
Christos Zisopoulos authored
100
74c3638 Sergio Gil Pérez de la Manga Gemify.me
porras authored
101 Copyright (c) 2009-2011 Christos Zisopoulos, released under the MIT license
Something went wrong with that request. Please try again.