has_constant 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 +105 -2
- data/VERSION +1 -1
- data/has_constant.gemspec +1 -1
- metadata +3 -3
data/README.rdoc
CHANGED
@@ -1,6 +1,109 @@
|
|
1
|
-
=
|
1
|
+
= HasConstant
|
2
2
|
|
3
|
-
|
3
|
+
HasConstant is a gem to limit the values that a certain field may contain.
|
4
|
+
I find it very useful for multilingual sites to translate the values in forms and in the corresponding models.
|
5
|
+
It's also useful for pretty much any field in a model which is filled in from a select field or radio button.
|
6
|
+
|
7
|
+
|
8
|
+
Because HasConstant takes a list, and stores the select index in the model, it can be used to translate
|
9
|
+
the values. Two important points to acheive this though; First of all, the two lists must be the same length,
|
10
|
+
so, if you are storing salutations 'Mr' and 'Mrs', then you must have the corresponding 'Herr' and 'Frau'
|
11
|
+
translations in your de locale file. Also, you must always add any new values to the end of the list.
|
12
|
+
|
13
|
+
== Examples
|
14
|
+
|
15
|
+
class User < ActiveRecord::Base
|
16
|
+
include HasConstant
|
17
|
+
include HasConstant::Orm::ActiveRecord
|
18
|
+
|
19
|
+
has_constant :salutations, lambda { I18n.t(:salutations) }
|
20
|
+
end
|
21
|
+
|
22
|
+
In the view
|
23
|
+
|
24
|
+
<% form_for @user || User.new do |form| %>
|
25
|
+
<%= form.select :salutation, User.salutations -%>
|
26
|
+
<%= form.submit 'submit' -%>
|
27
|
+
<% end %>
|
28
|
+
|
29
|
+
u = User.new(:salutation => 'Mrs')
|
30
|
+
|
31
|
+
u.salutation #=> 'Mrs'
|
32
|
+
|
33
|
+
I18n.locale = :de
|
34
|
+
|
35
|
+
u.salutation #=> 'Frau'
|
36
|
+
|
37
|
+
=== with ActiveRecord
|
38
|
+
class User < ActiveRecord::Base
|
39
|
+
include HasConstant
|
40
|
+
include HasConstant::Orm::ActiveRecord
|
41
|
+
|
42
|
+
has_constant :salutations, ['Mr', 'Mrs']
|
43
|
+
end
|
44
|
+
|
45
|
+
User.salutations #=> ['Mr', 'Mrs']
|
46
|
+
|
47
|
+
u = User.new(:salutation => 'Dr')
|
48
|
+
|
49
|
+
u.salutation #=> nil
|
50
|
+
|
51
|
+
u.salutation = 'Mr'
|
52
|
+
|
53
|
+
u.salutation #=> 'Mr'
|
54
|
+
|
55
|
+
=== with Mongoid
|
56
|
+
|
57
|
+
class User
|
58
|
+
include Mongoid::Document
|
59
|
+
include HasConstant
|
60
|
+
include HasConstant::Orm::Mongoid
|
61
|
+
|
62
|
+
has_constant :industries, ['IT/Development', 'Marketing']
|
63
|
+
end
|
64
|
+
|
65
|
+
User.industries #=> ['IT/Development', 'Marketing']
|
66
|
+
|
67
|
+
=== without a database
|
68
|
+
|
69
|
+
class User
|
70
|
+
include HasConstant
|
71
|
+
|
72
|
+
has_constant :job_roles, ['Junior Developer', 'Developer', 'CTO', 'Other']
|
73
|
+
end
|
74
|
+
|
75
|
+
User.job_roles #=> ['Junior Developer', 'Developer', 'CTO', 'Other']
|
76
|
+
|
77
|
+
=== with a lambda or proc
|
78
|
+
|
79
|
+
class User
|
80
|
+
include HasConstant
|
81
|
+
|
82
|
+
has_constant :salutation, lambda { I18n.t(:salutations) }
|
83
|
+
has_constant :industries, Proc.new { I18n.t(:industries) }
|
84
|
+
end
|
85
|
+
|
86
|
+
(assuming the correct translations exist)
|
87
|
+
|
88
|
+
I18n.locale = :en
|
89
|
+
|
90
|
+
User.salutations #=> ['Mr', 'Mrs']
|
91
|
+
|
92
|
+
I18n.locale = :de
|
93
|
+
|
94
|
+
User.salutations #=> ['Herr', 'Frau']
|
95
|
+
|
96
|
+
u = User.new(:salutation => 'Herr')
|
97
|
+
|
98
|
+
u.salutation #=> 'Herr'
|
99
|
+
|
100
|
+
I18n.locale = :en
|
101
|
+
|
102
|
+
u.salutation #=> 'Mr'
|
103
|
+
|
104
|
+
== with simple_form (http://github.com/plataformatec/simple_form)
|
105
|
+
|
106
|
+
<%= form.input :salutation, :collection => User.salutations -%>
|
4
107
|
|
5
108
|
== Note on Patches/Pull Requests
|
6
109
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.1
|
data/has_constant.gemspec
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: has_constant
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 21
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 2
|
9
|
-
-
|
10
|
-
version: 0.2.
|
9
|
+
- 1
|
10
|
+
version: 0.2.1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- mattbeedle
|