symbolize 4.0.2 → 4.0.3
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/README.rdoc +40 -83
- data/Rakefile +42 -33
- data/lib/symbolize/active_record.rb +11 -27
- data/lib/symbolize/mongoid.rb +25 -51
- data/lib/symbolize/version.rb +3 -0
- data/spec/locales/pt.yml +3 -3
- data/spec/spec_helper.rb +1 -2
- data/spec/spec_helper_mongoid.rb +1 -1
- data/spec/symbolize/active_record_spec.rb +1 -39
- data/spec/symbolize/mongoid_spec.rb +114 -139
- metadata +53 -23
- data/.document +0 -5
- data/MIT-LICENSE +0 -20
- data/VERSION +0 -1
- data/lib/symbolize/symbolize_helper.rb +0 -60
- data/symbolize.gemspec +0 -61
data/README.rdoc
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
= Symbolize attribute values
|
2
2
|
|
3
|
-
This plugin introduces an easy way to use symbols for values of attributes.
|
3
|
+
This plugin introduces an easy way to use symbols for values of attributes.
|
4
|
+
Symbolized attributes return a ruby symbol (or nil) as their value
|
4
5
|
and can be set using :symbols or "strings".
|
5
6
|
|
6
7
|
== Install
|
@@ -23,28 +24,21 @@ and can be set using :symbols or "strings".
|
|
23
24
|
|
24
25
|
== About
|
25
26
|
|
26
|
-
|
27
|
-
attribute will return symbol values and can be set using
|
28
|
-
string values
|
29
|
-
|
30
|
-
On SQL-based DBs, an attribute to symbolize should be a string (varchar) column in the database.
|
27
|
+
Just use "symbolize :attribute" in your model, and the specified
|
28
|
+
attribute will return symbol values and can be set using symbols
|
29
|
+
(setting string values works, which is important when using forms).
|
31
30
|
|
31
|
+
On schema DBs, the attribute should be a string (varchar) column.
|
32
32
|
|
33
33
|
|
34
34
|
== Usage
|
35
35
|
|
36
|
-
Add "symbolize :attr_name" to your model class. You may also want to add
|
37
|
-
validates_inclusion_of to restrict the possible values (just like an enum).
|
38
36
|
|
39
|
-
# ActiveRecord
|
40
37
|
class User < ActiveRecord::Base
|
41
|
-
|
42
|
-
# Mongoid
|
43
|
-
class User
|
44
|
-
include Mongoid::Document
|
45
|
-
include Mongoid::Symbolize
|
38
|
+
or Mongoid::Document
|
46
39
|
|
47
40
|
symbolize :gender, :in => [:female, :male], :scopes => true
|
41
|
+
|
48
42
|
symbolize :so, :in => {
|
49
43
|
:linux => "Linux",
|
50
44
|
:mac => "Mac OS X"
|
@@ -60,17 +54,18 @@ validates_inclusion_of to restrict the possible values (just like an enum).
|
|
60
54
|
symbolize :angry, :in => [true, false], :scopes => true
|
61
55
|
|
62
56
|
# Don`t validate
|
63
|
-
symbolize :lang, :in => [:ruby, :c, :erlang], :validate => false
|
57
|
+
symbolize :lang, :in => [:ruby, :js, :c, :erlang], :validate => false
|
64
58
|
|
65
59
|
# Default
|
66
|
-
symbolize :kind, :in => [:admin, :manager, :user], :default => :
|
67
|
-
|
60
|
+
symbolize :kind, :in => [:admin, :manager, :user], :default => :user
|
61
|
+
|
62
|
+
end
|
68
63
|
|
69
64
|
|
70
65
|
=== in/within
|
71
66
|
|
72
|
-
The values allowed on the enum field, you can provide a hash
|
73
|
-
{:value => "Human text"} or an array of keys to
|
67
|
+
The values allowed on the enum field, you can provide a hash like
|
68
|
+
{:value => "Human text"} or an array of keys to run i18n on.
|
74
69
|
Booleans are also supported. See below.
|
75
70
|
|
76
71
|
allow_(blank|nil): What you expect.
|
@@ -108,6 +103,8 @@ Its possible to use boolean fields also.
|
|
108
103
|
If you don`t provide a hash with values, it will try i18n:
|
109
104
|
|
110
105
|
activerecord:
|
106
|
+
or
|
107
|
+
mongoid:
|
111
108
|
attributes:
|
112
109
|
user:
|
113
110
|
enums:
|
@@ -119,20 +116,27 @@ If you don`t provide a hash with values, it will try i18n:
|
|
119
116
|
female: Girl
|
120
117
|
male: Boy
|
121
118
|
|
119
|
+
|
122
120
|
You can skip i18n lookup with :i18n => false
|
123
|
-
symbolize :gender, :in => [:female, :male], :i18n => false
|
124
121
|
|
122
|
+
symbolize :style, :in => [:rock, :punk, :funk, :jazz], :i18n => false
|
125
123
|
|
126
|
-
|
124
|
+
|
125
|
+
=== scopes
|
127
126
|
|
128
127
|
If you provide the scopes option, some fancy named scopes will be added:
|
129
|
-
In our User example, gender has
|
128
|
+
In our User example, gender has male/female options, so you can do:
|
129
|
+
|
130
|
+
# AR
|
131
|
+
User.male # => User.all(:conditions => { :gender => :male })
|
132
|
+
|
133
|
+
# Mongoid
|
134
|
+
User.female # => User.where({ :gender => :female })
|
130
135
|
|
131
|
-
User.female => User.find(:all, :conditions => { :gender => :female })
|
132
136
|
|
133
137
|
You can chain named scopes as well:
|
134
138
|
|
135
|
-
User.female.mac => User.
|
139
|
+
User.female.mac => User.all :conditions => { :gender => :female, :so => :mac }
|
136
140
|
|
137
141
|
For boolean colums you can use
|
138
142
|
|
@@ -142,50 +146,36 @@ For boolean colums you can use
|
|
142
146
|
( or with_[attribute] and without_[attribute] )
|
143
147
|
|
144
148
|
|
145
|
-
=== default
|
146
|
-
|
147
|
-
As the name suggest, the symbol you choose as default will be set in new objects automatically.
|
148
|
-
|
149
|
-
User.new.kind # Will print :admin
|
150
|
-
|
149
|
+
=== default
|
151
150
|
|
152
|
-
|
151
|
+
As the name suggest, the symbol you choose as default will be set
|
152
|
+
in new objects automatically. Mongoid only for now.
|
153
153
|
|
154
|
-
|
155
|
-
u.gender # => :female
|
154
|
+
symbolize :mood, :in => [:happy, :sad, :euphoric], :default => (MarvinDay ? :sad : :happy)
|
156
155
|
|
157
|
-
|
158
|
-
u.gender # => :male
|
156
|
+
User.new.kind # It may print :happy
|
159
157
|
|
160
|
-
u = User.find(:all, :conditions => { :gender => :female })
|
161
|
-
u = User.female
|
162
|
-
|
163
|
-
u = User.new(:name => 'ET', :gender => :unknown)
|
164
|
-
u.save # => validation fails
|
165
158
|
|
166
159
|
== Rails Form Example
|
167
160
|
|
161
|
+
You may call `Class.get_<attribute>_values` anywhere to get a nice array.
|
162
|
+
Works nice with dropdowns. Examples:
|
163
|
+
|
168
164
|
class Coffee
|
169
165
|
symbolize :genetic, :in => [:arabica, :robusta, :blend]
|
170
166
|
end
|
171
167
|
|
172
|
-
form_for(@coffee) do |f|
|
168
|
+
- form_for(@coffee) do |f|
|
173
169
|
= f.label :genetic
|
174
170
|
= f.select :genetic, Coffee.get_genetic_values
|
175
171
|
|
176
|
-
== Model Helpers
|
177
|
-
|
178
|
-
|
179
|
-
You may call `Class.get_attr_values` anywhere to get a nice array.
|
180
|
-
Works nice with dropdowns. Examples:
|
181
|
-
|
182
|
-
|
183
172
|
Somewhere on a view:
|
184
173
|
|
185
174
|
= select_tag :kind, Coffee.get_genetic_values
|
186
175
|
|
187
176
|
|
188
|
-
== View Helpers
|
177
|
+
== View Helpers (DEPRECATED)
|
178
|
+
|
189
179
|
|
190
180
|
<% form_for @user do |f| %>
|
191
181
|
<%= f.radio_sym "gender" %>
|
@@ -193,40 +183,7 @@ Somewhere on a view:
|
|
193
183
|
<%= f.select_sym "so" %>
|
194
184
|
<!-- Fixed order -->
|
195
185
|
<%= f.select_sym "office" %>
|
196
|
-
<%
|
197
|
-
|
198
|
-
output:
|
199
|
-
|
200
|
-
<form action="users/1" method="post">
|
201
|
-
<div style="margin:0;padding:0">...</div>
|
202
|
-
<label>Female <input id="user_gender_female" name="user[gender]" type="radio" value="female"></label>
|
203
|
-
<label>Male <input checked="checked" id="user_gender_male" name="user[gender]" type="radio" value="male" ></label>
|
204
|
-
<!-- Alphabetic order -->
|
205
|
-
<select id="user_so" name="post[so]">
|
206
|
-
<option value="linux" selected="selected">Linux</option>
|
207
|
-
<option value="mac">Mac OS X</option>
|
208
|
-
<option value="windows">Windows XP</option>
|
209
|
-
</select>
|
210
|
-
<!-- Fixed order -->
|
211
|
-
<select id="user_office" name="post[office]">
|
212
|
-
<option value="kde" selected="selected">Koffice</option>
|
213
|
-
<option value="ms">Microsoft Office</option>
|
214
|
-
<option value="open">Open Office</option>
|
215
|
-
</select>
|
216
|
-
</form>
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
=== Plugin:
|
221
|
-
|
222
|
-
Try at your own risk.
|
223
|
-
|
224
|
-
rails plugin install git://github.com/nofxx/symbolize.git
|
225
|
-
|
226
|
-
|
227
|
-
== Rails 3.1 (beta)
|
228
|
-
|
229
|
-
Specs pass with rails 3, but a scope :public == fail.
|
186
|
+
<%end>
|
230
187
|
|
231
188
|
|
232
189
|
== Specs
|
data/Rakefile
CHANGED
@@ -1,24 +1,33 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
require 'jeweler'
|
7
|
-
Jeweler::Tasks.new do |gem|
|
8
|
-
gem.name = "symbolize"
|
9
|
-
gem.summary = "Object enums with i18n in AR or Mongoid"
|
10
|
-
gem.description = "ActiveRecord/Mongoid enums with i18n"
|
11
|
-
gem.email = "x@nofxx.com"
|
12
|
-
gem.homepage = "http://github.com/nofxx/symbolize"
|
13
|
-
gem.authors = ["Marcos Piccinini"]
|
14
|
-
gem.add_development_dependency "rspec"
|
15
|
-
gem.add_development_dependency "sqlite3"
|
16
|
-
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
17
|
-
end
|
18
|
-
rescue LoadError
|
19
|
-
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
1
|
+
require "rspec"
|
2
|
+
require "rspec/core/rake_task"
|
3
|
+
|
4
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
5
|
+
spec.pattern = "spec/**/*_spec.rb"
|
20
6
|
end
|
21
7
|
|
8
|
+
task :default => [:spec]
|
9
|
+
|
10
|
+
# require 'rubygems'
|
11
|
+
# require 'rake'
|
12
|
+
# #require 'spec/rake/spectask'
|
13
|
+
|
14
|
+
# begin
|
15
|
+
# require 'jeweler'
|
16
|
+
# Jeweler::Tasks.new do |gem|
|
17
|
+
# gem.name = "symbolize"
|
18
|
+
# gem.summary = "Object enums with i18n in AR or Mongoid"
|
19
|
+
# gem.description = "ActiveRecord/Mongoid enums with i18n"
|
20
|
+
# gem.email = "x@nofxx.com"
|
21
|
+
# gem.homepage = "http://github.com/nofxx/symbolize"
|
22
|
+
# gem.authors = ["Marcos Piccinini"]
|
23
|
+
# gem.add_development_dependency "rspec"
|
24
|
+
# gem.add_development_dependency "sqlite3"
|
25
|
+
# # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
26
|
+
# end
|
27
|
+
# rescue LoadError
|
28
|
+
# puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
29
|
+
# end
|
30
|
+
|
22
31
|
# Spec::Rake::SpecTask.new(:spec) do |spec|
|
23
32
|
# spec.libs << 'lib' << 'spec'
|
24
33
|
# spec.spec_files = FileList['spec/**/*_spec.rb']
|
@@ -39,17 +48,17 @@ end
|
|
39
48
|
# rdoc.rdoc_files.include('README')
|
40
49
|
# rdoc.rdoc_files.include('lib/**/*.rb')
|
41
50
|
# end
|
42
|
-
require 'rdoc/task'
|
43
|
-
Rake::RDocTask.new do |rdoc|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
end
|
51
|
+
# require 'rdoc/task'
|
52
|
+
# Rake::RDocTask.new do |rdoc|
|
53
|
+
# if File.exist?('VERSION.yml')
|
54
|
+
# config = YAML.load(File.read('VERSION.yml'))
|
55
|
+
# version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
|
56
|
+
# else
|
57
|
+
# version = ""
|
58
|
+
# end
|
59
|
+
|
60
|
+
# rdoc.rdoc_dir = 'rdoc'
|
61
|
+
# rdoc.title = "symbolize #{version}"
|
62
|
+
# rdoc.rdoc_files.include('README*')
|
63
|
+
# rdoc.rdoc_files.include('lib/**/*.rb')
|
64
|
+
# end
|
@@ -89,8 +89,9 @@ module Symbolize
|
|
89
89
|
|
90
90
|
if methods
|
91
91
|
values.each do |value|
|
92
|
-
|
93
|
-
|
92
|
+
key = value[0]
|
93
|
+
define_method("#{key}?") do
|
94
|
+
self[attr_name] == key
|
94
95
|
end
|
95
96
|
end
|
96
97
|
end
|
@@ -111,10 +112,14 @@ module Symbolize
|
|
111
112
|
end
|
112
113
|
end
|
113
114
|
end
|
114
|
-
end
|
115
115
|
|
116
|
-
|
117
|
-
|
116
|
+
if validation
|
117
|
+
validation = "validates_inclusion_of :#{attr_names.join(', :')}"
|
118
|
+
validation += ", :in => #{values.keys.inspect}"
|
119
|
+
validation += ", :allow_nil => true" if configuration[:allow_nil]
|
120
|
+
validation += ", :allow_blank => true" if configuration[:allow_blank]
|
121
|
+
class_eval validation
|
122
|
+
end
|
118
123
|
end
|
119
124
|
end
|
120
125
|
|
@@ -170,25 +175,4 @@ module Symbolize
|
|
170
175
|
end
|
171
176
|
end
|
172
177
|
|
173
|
-
|
174
|
-
# The idea behind this is, that symbols are converted to plain strings
|
175
|
-
# when being quoted by ActiveRecord::ConnectionAdapters::Quoting#quote.
|
176
|
-
# This makes it possible to work with symbolized attibutes in sql conditions.
|
177
|
-
# E.g. validates_uniqueness_of could not use :scope with a symbolized
|
178
|
-
# attribute, because AR quotes it to YAML:
|
179
|
-
# "... AND status = '--- :active\n'"
|
180
|
-
# Having support for quoted_id in Symbol, makes AR quoting symbols correctly:
|
181
|
-
# "... AND status = 'active'"
|
182
|
-
# NOTE: Normally quoted_id should be implemented as a singleton method
|
183
|
-
# only used on symbols returned by read_and_symbolize_attribute,
|
184
|
-
# but unfortunately this is not possible since Symbol is an immediate
|
185
|
-
# value and therefore does not support singleton methods.
|
186
|
-
# class Symbol
|
187
|
-
# def quoted_id
|
188
|
-
# # A symbol can contain almost every character (even a backslash or an
|
189
|
-
# # apostrophe), so make sure to properly quote the string value here.
|
190
|
-
# "'#{ActiveRecord::Base.connection.quote_string(self.to_s)}'"
|
191
|
-
# end
|
192
|
-
# end
|
193
|
-
|
194
|
-
ActiveRecord::Base.send(:include, Symbolize) if ActiveRecord::VERSION::MAJOR >= 3
|
178
|
+
ActiveRecord::Base.send(:include, Symbolize)
|
data/lib/symbolize/mongoid.rb
CHANGED
@@ -63,16 +63,16 @@ module Mongoid
|
|
63
63
|
default_opt = configuration.delete :default
|
64
64
|
|
65
65
|
unless enum.nil?
|
66
|
-
# Little monkeypatching, <1.8 Hashes aren't ordered.
|
67
|
-
# hsh = RUBY_VERSION > '1.9' || !defined?("ActiveSupport") ? Hash : ActiveSupport::OrderedHash
|
68
66
|
|
69
67
|
attr_names.each do |attr_name|
|
70
|
-
attr_name = attr_name.to_s
|
68
|
+
# attr_name = attr_name.to_s
|
71
69
|
|
72
|
-
#
|
73
|
-
|
74
|
-
|
75
|
-
|
70
|
+
#
|
71
|
+
# Builds Mongoid 'field :name, type: type, :default'
|
72
|
+
#
|
73
|
+
mongo_opts = ", :type => Symbol"
|
74
|
+
mongo_opts += ", :default => :#{default_opt}" if default_opt
|
75
|
+
class_eval("field :#{attr_name} #{mongo_opts}")
|
76
76
|
|
77
77
|
const = "#{attr_name}_values"
|
78
78
|
if enum.is_a?(Hash)
|
@@ -108,65 +108,39 @@ module Mongoid
|
|
108
108
|
scope_comm = lambda { |*args| scope(*args)}
|
109
109
|
values.each do |value|
|
110
110
|
if value[0].respond_to?(:to_sym)
|
111
|
-
scope_comm.call value[0].to_sym,
|
111
|
+
scope_comm.call value[0].to_sym, where({ attr_name => value[0].to_sym })
|
112
112
|
end
|
113
113
|
end
|
114
114
|
end
|
115
115
|
|
116
116
|
if validation
|
117
|
-
|
117
|
+
validation = "validates_inclusion_of :#{attr_names.join(', :')}"
|
118
|
+
validation += ", :in => #{values.keys.inspect}"
|
119
|
+
validation += ", :allow_nil => true" if configuration[:allow_nil]
|
120
|
+
validation += ", :allow_blank => true" if configuration[:allow_blank]
|
121
|
+
class_eval validation
|
118
122
|
end
|
119
123
|
end
|
120
124
|
end
|
121
125
|
|
126
|
+
#
|
127
|
+
# Creates <attribute>_text helper, human text for attribute.
|
128
|
+
#
|
122
129
|
attr_names.each do |attr_name|
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
# class_eval("before_save :set_default_for_attr_#{attr_name}")
|
129
|
-
# else
|
130
|
-
# class_eval("def #{attr_name}; read_and_symbolize_attribute('#{attr_name}'); end")
|
131
|
-
# class_eval("def #{attr_name}= (value); write_symbolized_attribute('#{attr_name}', value); end")
|
132
|
-
# end
|
133
|
-
if i18n
|
134
|
-
class_eval("def #{attr_name}_text; read_i18n_attribute('#{attr_name}'); end")
|
130
|
+
if i18n # memoize call to translate... good idea?
|
131
|
+
define_method "#{attr_name}_text" do
|
132
|
+
return nil unless attr = read_attribute(attr_name)
|
133
|
+
I18n.t("mongoid.attributes.#{ActiveSupport::Inflector.underscore(self.class)}.enums.#{attr_name}.#{attr}")
|
134
|
+
end
|
135
135
|
elsif enum
|
136
136
|
class_eval("def #{attr_name}_text; #{attr_name.to_s.upcase}_VALUES[#{attr_name}]; end")
|
137
137
|
else
|
138
138
|
class_eval("def #{attr_name}_text; #{attr_name}.to_s; end")
|
139
139
|
end
|
140
140
|
end
|
141
|
-
end
|
142
|
-
end
|
143
|
-
|
144
|
-
# String becomes symbol, booleans string and nil nil.
|
145
|
-
# def symbolize_attribute attr
|
146
|
-
# case attr
|
147
|
-
# when String then attr.empty? ? nil : attr.to_sym
|
148
|
-
# when Symbol, TrueClass, FalseClass, Numeric then attr
|
149
|
-
# else nil
|
150
|
-
# end
|
151
|
-
# end
|
152
|
-
|
153
|
-
# # Return an attribute's value as a symbol or nil
|
154
|
-
# def read_and_symbolize_attribute attr_name
|
155
|
-
# symbolize_attribute self[attr_name]
|
156
|
-
# end
|
157
141
|
|
158
|
-
|
159
|
-
def read_i18n_attribute attr_name
|
160
|
-
return nil unless attr = read_attribute(attr_name)
|
161
|
-
I18n.translate("mongoid.attributes.#{ActiveSupport::Inflector.underscore(self.class)}.enums.#{attr_name}.#{attr}") #.to_sym rescue nila
|
162
|
-
end
|
163
|
-
|
164
|
-
# # Write a symbolized value. Watch out for booleans.
|
165
|
-
# def write_symbolized_attribute attr_name, value
|
166
|
-
# val = { "true" => true, "false" => false }[value]
|
167
|
-
# val = symbolize_attribute(value) if val.nil?
|
142
|
+
end
|
168
143
|
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
end
|
144
|
+
end # ClassMethods
|
145
|
+
end # Symbolize
|
146
|
+
end # Mongoid
|
data/spec/locales/pt.yml
CHANGED
@@ -14,9 +14,9 @@ pt:
|
|
14
14
|
kind:
|
15
15
|
magic: Mágica
|
16
16
|
agility: Agilidade
|
17
|
-
|
17
|
+
mongoid:
|
18
18
|
attributes:
|
19
|
-
|
19
|
+
person:
|
20
20
|
enums:
|
21
21
|
language:
|
22
22
|
pt: Português
|
@@ -24,7 +24,7 @@ pt:
|
|
24
24
|
sex:
|
25
25
|
"true": Feminino
|
26
26
|
"false": Masculino
|
27
|
-
|
27
|
+
person_skill:
|
28
28
|
enums:
|
29
29
|
kind:
|
30
30
|
magic: Mágica
|
data/spec/spec_helper.rb
CHANGED
@@ -8,8 +8,7 @@ require 'pry'
|
|
8
8
|
|
9
9
|
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
10
10
|
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
11
|
-
require '
|
12
|
-
require 'action_view'
|
11
|
+
require 'i18n'
|
13
12
|
|
14
13
|
|
15
14
|
I18n.load_path += Dir[File.join(File.dirname(__FILE__), "locales", "*.{rb,yml}")]
|
data/spec/spec_helper_mongoid.rb
CHANGED
@@ -177,43 +177,6 @@ describe "Symbolize" do
|
|
177
177
|
new_extra.should_not be_valid
|
178
178
|
end
|
179
179
|
|
180
|
-
|
181
|
-
|
182
|
-
describe "View helpers" do
|
183
|
-
include ActionView::Helpers::FormHelper
|
184
|
-
include ActionView::Helpers::FormOptionsHelper
|
185
|
-
|
186
|
-
before(:each) do
|
187
|
-
@options_status = [['Active', :active], ['Inactive', :inactive]]
|
188
|
-
@options_gui = [["cocoa", :cocoa], ["qt", :qt], ["gtk", :gtk]]
|
189
|
-
@options_so = [["Linux", :linux] , ["Mac OS X", :mac], ["Videogame", :win]]
|
190
|
-
end
|
191
|
-
|
192
|
-
it "test_helper_select_sym" do
|
193
|
-
@user.status = :inactive
|
194
|
-
output = "<select id=\"user_status\" name=\"user[status]\">#{options_for_select(@options_status, @user.status)}</select>"
|
195
|
-
output.should eql(select_sym("user", "status", nil))
|
196
|
-
|
197
|
-
|
198
|
-
output = "<select id=\"user_status\" name=\"user[status]\">#{options_for_select(@options_status, @user.status)}</select>"
|
199
|
-
output.should eql(select_sym("user", "status", nil))
|
200
|
-
end
|
201
|
-
|
202
|
-
def test_helper_select_sym_order
|
203
|
-
output_so = "<select id=\"user_so\" name=\"user[so]\">#{options_for_select(@options_so, @user.so)}</select>"
|
204
|
-
output_office = "<select id=\"user_office\" name=\"user[office]\">#{options_for_select(@options_office, @user.office)}</select>"
|
205
|
-
|
206
|
-
assert_equal output_so, select_sym("user", "so", nil)
|
207
|
-
assert_equal output_office, select_sym("user", "office", nil)
|
208
|
-
end
|
209
|
-
|
210
|
-
def test_helper_radio_sym
|
211
|
-
output = radio_sym("user", "status", nil)
|
212
|
-
assert_equal("<label>Active: <input checked=\"checked\" id=\"user_status_active\" name=\"user[status]\" type=\"radio\" value=\"active\" /></label><label>Inactive: <input id=\"user_status_inactive\" name=\"user[status]\" type=\"radio\" value=\"inactive\" /></label>", output)
|
213
|
-
end
|
214
|
-
|
215
|
-
end
|
216
|
-
|
217
180
|
describe "i18n" do
|
218
181
|
|
219
182
|
it "should test i18n ones" do
|
@@ -387,13 +350,12 @@ describe "Symbolize" do
|
|
387
350
|
@anna.language_changed?.should be_true
|
388
351
|
end
|
389
352
|
|
390
|
-
it "is not dirty if you set the attribute value to the same value
|
353
|
+
it "is not dirty if you set the attribute value to the same value" do
|
391
354
|
@anna.language.should == :pt
|
392
355
|
@anna.language_changed?.should be_false
|
393
356
|
|
394
357
|
return_value = @anna.language = :pt
|
395
358
|
return_value.should == :pt
|
396
|
-
p @anna.changes
|
397
359
|
@anna.language_changed?.should be_false
|
398
360
|
end
|
399
361
|
end
|
@@ -1,9 +1,9 @@
|
|
1
1
|
# -*- coding: utf-8 -*-
|
2
|
-
require
|
2
|
+
require 'spec_helper_mongoid'
|
3
3
|
|
4
4
|
#
|
5
5
|
# Test model
|
6
|
-
class
|
6
|
+
class Person
|
7
7
|
include Mongoid::Document
|
8
8
|
include Mongoid::Symbolize
|
9
9
|
|
@@ -17,28 +17,32 @@ class User
|
|
17
17
|
:win => 'Videogame'
|
18
18
|
}, :scopes => true
|
19
19
|
symbolize :gui, :allow_blank => true, :in => [:cocoa, :qt, :gtk], :i18n => false
|
20
|
-
symbolize :karma, :in => %w{
|
20
|
+
symbolize :karma, :in => %w{good bad ugly}, :methods => true, :i18n => false, :allow_nil => true
|
21
|
+
symbolize :planet, :in => %w{earth centauri tatooine}, :default => :earth
|
21
22
|
# symbolize :cool, :in => [true, false], :scopes => true
|
22
23
|
|
23
|
-
has_many :
|
24
|
-
|
24
|
+
has_many :rights, :dependent => :destroy
|
25
|
+
has_many :extras, :dependent => :destroy, :class_name => "PersonExtra"
|
26
|
+
embeds_many :skills, :class_name => "PersonSkill"
|
25
27
|
end
|
26
28
|
|
27
|
-
class
|
29
|
+
class PersonSkill
|
28
30
|
include Mongoid::Document
|
29
31
|
include Mongoid::Symbolize
|
32
|
+
embedded_in :person, :inverse_of => :skills
|
30
33
|
|
31
34
|
symbolize :kind, :in => [:agility, :magic]
|
32
35
|
end
|
33
36
|
|
34
|
-
class
|
37
|
+
class PersonExtra
|
35
38
|
include Mongoid::Document
|
36
39
|
include Mongoid::Symbolize
|
40
|
+
belongs_to :person, :inverse_of => :extras
|
37
41
|
|
38
42
|
symbolize :key, :in => [:one, :another]
|
39
43
|
end
|
40
44
|
|
41
|
-
class
|
45
|
+
class Right
|
42
46
|
include Mongoid::Document
|
43
47
|
include Mongoid::Symbolize
|
44
48
|
|
@@ -46,208 +50,177 @@ class Permission
|
|
46
50
|
symbolize :kind, :in => [:temp, :perm], :default => :perm
|
47
51
|
end
|
48
52
|
|
49
|
-
[User, UserExtra, UserSkill, Permission].each { |k| k.destroy_all }
|
50
|
-
|
51
|
-
# Test records
|
52
|
-
User.create(:name => 'Anna', :other => :fo, :status => :active , :so => :linux, :gui => :qt, :language => :pt, :sex => true, :cool => true)
|
53
|
-
User.create!(:name => 'Bob' , :other => :bar,:status => :inactive, :so => :mac, :gui => :gtk, :language => :en, :sex => false, :cool => false)
|
54
|
-
|
55
|
-
|
56
53
|
describe "Symbolize" do
|
57
54
|
|
58
55
|
it "should be a module" do
|
59
56
|
Mongoid.const_defined?("Symbolize").should be_true
|
60
57
|
end
|
61
58
|
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
59
|
+
it "should instantiate" do
|
60
|
+
anna = Person.create(:name => 'Anna', :so => :mac, :gui => :cocoa, :language => :pt, :status => :active)
|
61
|
+
anna.should be_valid
|
62
|
+
# anna.errors.messages.should eql({})
|
63
|
+
end
|
64
|
+
|
65
|
+
describe "Person Instantiated" do
|
66
|
+
let(:person) { Person.create(:name => 'Anna', :other => :fo, :status => :active , :so => :linux, :gui => :qt, :language => :pt, :sex => true, :cool => true) }
|
66
67
|
|
67
68
|
it "test_symbolize_string" do
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
# @user.read_attribute(:status).should eql('inactive')
|
69
|
+
person.status = 'inactive'
|
70
|
+
person.status.should eql(:inactive)
|
71
|
+
person.read_attribute(:status).should eql(:inactive)
|
72
72
|
end
|
73
73
|
|
74
74
|
it "test_symbolize_symbol" do
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
75
|
+
person.status = :active
|
76
|
+
person.status.should eql(:active)
|
77
|
+
person.save
|
78
|
+
person.status.should eql(:active)
|
79
|
+
person[:status].should eql(:active)
|
80
80
|
end
|
81
81
|
|
82
82
|
# it "should work nice with numbers" do
|
83
83
|
# pending
|
84
|
-
#
|
85
|
-
#
|
86
|
-
# #
|
87
|
-
# #
|
84
|
+
# person.status = 43
|
85
|
+
# person.status.should_not be_nil
|
86
|
+
# # person.status_before_type_cast.should be_nil
|
87
|
+
# # person.read_attribute(:status).should be_nil
|
88
88
|
# end
|
89
89
|
|
90
90
|
# it "should acts nice with nil" do
|
91
|
-
#
|
92
|
-
#
|
93
|
-
#
|
94
|
-
#
|
91
|
+
# person.status = nil
|
92
|
+
# person.status.should be_nil
|
93
|
+
# person.status_before_type_cast.should be_nil
|
94
|
+
# person.read_attribute(:status).should be_nil
|
95
95
|
# end
|
96
96
|
|
97
97
|
# it "should acts nice with blank" do
|
98
|
-
#
|
99
|
-
#
|
100
|
-
#
|
101
|
-
#
|
98
|
+
# person.status = ""
|
99
|
+
# person.status.should be_nil
|
100
|
+
# person.status_before_type_cast.should be_nil
|
101
|
+
# person.read_attribute(:status).should be_nil
|
102
102
|
# end
|
103
103
|
|
104
104
|
it "should not validates other" do
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
105
|
+
person.other = nil
|
106
|
+
person.should be_valid
|
107
|
+
person.other = ""
|
108
|
+
person.should be_valid
|
109
109
|
end
|
110
110
|
|
111
111
|
it "should get the correct values" do
|
112
|
-
|
113
|
-
|
112
|
+
Person.get_status_values.should eql([["Active", :active],["Inactive", :inactive]])
|
113
|
+
Person::STATUS_VALUES.should eql({ inactive: "Inactive", active: "Active"})
|
114
114
|
end
|
115
115
|
|
116
116
|
it "test_symbolize_humanize" do
|
117
|
-
|
117
|
+
person.status_text.should eql("Active")
|
118
118
|
end
|
119
119
|
|
120
120
|
it "should get the correct values" do
|
121
|
-
|
122
|
-
|
121
|
+
Person.get_gui_values.should =~ [["cocoa", :cocoa], ["qt", :qt], ["gtk", :gtk]]
|
122
|
+
Person::GUI_VALUES.should eql({cocoa: "cocoa", qt: "qt", gtk: "gtk"})
|
123
123
|
end
|
124
124
|
|
125
125
|
it "test_symbolize_humanize" do
|
126
|
-
|
126
|
+
person.gui_text.should eql("qt")
|
127
127
|
end
|
128
128
|
|
129
129
|
it "should get the correct values" do
|
130
|
-
|
131
|
-
|
130
|
+
Person.get_so_values.should =~ [["Linux", :linux], ["Mac OS X", :mac], ["Videogame", :win]]
|
131
|
+
Person::SO_VALUES.should eql({linux: "Linux", mac: "Mac OS X", win: "Videogame"})
|
132
132
|
end
|
133
133
|
|
134
134
|
it "test_symbolize_humanize" do
|
135
|
-
|
135
|
+
person.so_text.should eql("Linux")
|
136
136
|
end
|
137
137
|
|
138
138
|
it "test_symbolize_humanize" do
|
139
|
-
|
140
|
-
|
139
|
+
person.so = :mac
|
140
|
+
person.so_text.should eql("Mac OS X")
|
141
141
|
end
|
142
142
|
|
143
143
|
it "should stringify" do
|
144
|
-
|
145
|
-
|
146
|
-
|
144
|
+
person.other_text.should eql("fo")
|
145
|
+
person.other = :foo
|
146
|
+
person.other_text.should eql("foo")
|
147
147
|
end
|
148
148
|
|
149
149
|
it "should validate status" do
|
150
|
-
|
151
|
-
|
152
|
-
|
150
|
+
person.status = nil
|
151
|
+
person.should_not be_valid
|
152
|
+
person.should have(1).errors
|
153
153
|
end
|
154
154
|
|
155
155
|
it "should not validate so" do
|
156
|
-
|
157
|
-
|
156
|
+
person.so = nil
|
157
|
+
person.should be_valid
|
158
158
|
end
|
159
159
|
|
160
|
-
it "
|
161
|
-
|
162
|
-
|
160
|
+
it "should work with weird chars" do
|
161
|
+
person.status = :"weird'; chars"
|
162
|
+
person.status.should eql(:"weird'; chars")
|
163
163
|
end
|
164
164
|
|
165
165
|
it "should work fine through relations" do
|
166
|
-
|
167
|
-
|
166
|
+
person.extras.create(:key => :one)
|
167
|
+
PersonExtra.first.key.should eql(:one)
|
168
|
+
end
|
169
|
+
|
170
|
+
it "should work fine through embeds" do
|
171
|
+
person.skills.create(:kind => :magic)
|
172
|
+
person.skills.first.kind.should eql(:magic)
|
168
173
|
end
|
169
174
|
|
170
175
|
# it "should play fine with null db columns" do
|
171
|
-
# new_extra =
|
176
|
+
# new_extra = person.extras.build
|
172
177
|
# new_extra.should_not be_valid
|
173
178
|
# end
|
174
179
|
|
175
180
|
# it "should play fine with null db columns" do
|
176
|
-
# new_extra =
|
181
|
+
# new_extra = person.extras.build
|
177
182
|
# new_extra.should_not be_valid
|
178
183
|
# end
|
179
184
|
|
185
|
+
it "should default planet to earth" do
|
186
|
+
Person.new.planet.should eql(:earth)
|
187
|
+
end
|
180
188
|
|
181
189
|
|
182
|
-
# describe "View helpers" do
|
183
|
-
# include ActionView::Helpers::FormHelper
|
184
|
-
# include ActionView::Helpers::FormOptionsHelper
|
185
|
-
|
186
|
-
# before(:each) do
|
187
|
-
# @options_status = [['Active', :active], ['Inactive', :inactive]]
|
188
|
-
# @options_gui = [["cocoa", :cocoa], ["qt", :qt], ["gtk", :gtk]]
|
189
|
-
# @options_so = [["Linux", :linux] , ["Mac OS X", :mac], ["Videogame", :win]]
|
190
|
-
# end
|
191
|
-
|
192
|
-
# it "test_helper_select_sym" do
|
193
|
-
# @user.status = :inactive
|
194
|
-
# output = "<select id=\"user_status\" name=\"user[status]\">#{options_for_select(@options_status, @user.status)}</select>"
|
195
|
-
# output.should eql(select_sym("user", "status", nil))
|
196
|
-
|
197
|
-
|
198
|
-
# output = "<select id=\"user_status\" name=\"user[status]\">#{options_for_select(@options_status, @user.status)}</select>"
|
199
|
-
# output.should eql(select_sym("user", "status", nil))
|
200
|
-
# end
|
201
|
-
|
202
|
-
# def test_helper_select_sym_order
|
203
|
-
# output_so = "<select id=\"user_so\" name=\"user[so]\">#{options_for_select(@options_so, @user.so)}</select>"
|
204
|
-
# output_office = "<select id=\"user_office\" name=\"user[office]\">#{options_for_select(@options_office, @user.office)}</select>"
|
205
|
-
|
206
|
-
# assert_equal output_so.should, select_sym("user", "so", nil)
|
207
|
-
# assert_equal output_office, select_sym("user", "office", nil)
|
208
|
-
# end
|
209
|
-
|
210
|
-
# def test_helper_radio_sym
|
211
|
-
# output = radio_sym("user", "status", nil)
|
212
|
-
# assert_equal("<label>Active: <input checked=\"checked\" id=\"user_status_active\" name=\"user[status]\" type=\"radio\" value=\"active\" /></label><label>Inactive: <input id=\"user_status_inactive\" name=\"user[status]\" type=\"radio\" value=\"inactive\" /></label>", output)
|
213
|
-
# end
|
214
|
-
|
215
|
-
# end
|
216
|
-
|
217
190
|
describe "i18n" do
|
218
191
|
|
219
192
|
it "should test i18n ones" do
|
220
|
-
|
193
|
+
person.language_text.should eql("Português")
|
221
194
|
end
|
222
195
|
|
223
196
|
it "should get the correct values" do
|
224
|
-
|
197
|
+
Person.get_language_values.should =~ [["Português", :pt], ["Inglês", :en]]
|
225
198
|
end
|
226
199
|
|
227
200
|
it "should get the correct values" do
|
228
|
-
|
201
|
+
Person::LANGUAGE_VALUES.should eql({:pt=>"pt", :en=>"en"})
|
229
202
|
end
|
230
203
|
|
231
204
|
# it "should test boolean" do
|
232
|
-
#
|
205
|
+
# person.sex_text.should eql("Feminino")
|
233
206
|
# end
|
234
207
|
|
235
208
|
# it "should get the correct values" do
|
236
|
-
#
|
209
|
+
# Person.get_sex_values.should eql([["Feminino", true],["Masculino", false]])
|
237
210
|
# end
|
238
211
|
|
239
212
|
# it "should get the correct values" do
|
240
|
-
#
|
213
|
+
# Person::SEX_VALUES.should eql({true=>"true", false=>"false"})
|
241
214
|
# end
|
242
215
|
|
243
|
-
it "should translate a multiword
|
244
|
-
|
245
|
-
|
216
|
+
it "should translate a multiword classname" do
|
217
|
+
skill = PersonSkill.new(:kind => :magic)
|
218
|
+
skill.kind_text.should eql("Mágica")
|
246
219
|
end
|
247
220
|
|
248
221
|
it "should return nil if there's no value" do
|
249
|
-
|
250
|
-
|
222
|
+
skill = PersonSkill.new(:kind => nil)
|
223
|
+
skill.kind_text.should be_nil
|
251
224
|
end
|
252
225
|
|
253
226
|
end
|
@@ -255,53 +228,53 @@ describe "Symbolize" do
|
|
255
228
|
describe "Methods" do
|
256
229
|
|
257
230
|
it "should play nice with other stuff" do
|
258
|
-
|
259
|
-
|
231
|
+
person.karma.should be_nil
|
232
|
+
Person::KARMA_VALUES.should eql({:bad => "bad", :ugly => "ugly", :good => "good"})
|
260
233
|
end
|
261
234
|
|
262
235
|
it "should provide a boolean method" do
|
263
|
-
|
264
|
-
|
265
|
-
|
236
|
+
person.should_not be_good
|
237
|
+
person.karma = :ugly
|
238
|
+
person.should be_ugly
|
266
239
|
end
|
267
240
|
|
268
241
|
it "should work" do
|
269
|
-
|
270
|
-
|
271
|
-
|
242
|
+
person.karma = "good"
|
243
|
+
person.should be_good
|
244
|
+
person.should_not be_bad
|
272
245
|
end
|
273
246
|
|
274
247
|
end
|
275
248
|
|
276
249
|
end
|
277
250
|
|
278
|
-
describe "more tests on
|
251
|
+
describe "more tests on Right" do
|
279
252
|
|
280
253
|
it "should use default value on object build" do
|
281
|
-
|
254
|
+
Right.new.kind.should eql(:perm)
|
282
255
|
end
|
283
256
|
|
284
257
|
it "should not interfer on create" do
|
285
|
-
|
286
|
-
|
258
|
+
Right.create!(:name => "p7", :kind => :temp)
|
259
|
+
Right.where(name: "p7").first.kind.should eql(:temp)
|
287
260
|
end
|
288
261
|
|
289
262
|
it "should work on create" do
|
290
|
-
pm =
|
263
|
+
pm = Right.new(:name => "p7")
|
291
264
|
pm.should be_valid
|
292
265
|
pm.save.should be_true
|
293
266
|
end
|
294
267
|
|
295
268
|
it "should work on create" do
|
296
|
-
|
297
|
-
|
269
|
+
Right.create(:name => "p8")
|
270
|
+
Right.first(conditions: { name: "p8" }).kind.should eql(:perm)
|
298
271
|
end
|
299
272
|
|
300
273
|
it "should work on edit" do
|
301
|
-
pm =
|
274
|
+
pm = Right.where(name: "p8").first
|
302
275
|
pm.kind = :temp
|
303
276
|
pm.save
|
304
|
-
|
277
|
+
Right.where(name: "p8").first.kind.should eql(:temp)
|
305
278
|
end
|
306
279
|
|
307
280
|
end
|
@@ -309,19 +282,21 @@ describe "Symbolize" do
|
|
309
282
|
describe "Mongoid stuff" do
|
310
283
|
|
311
284
|
it "test_symbolized_finder" do
|
312
|
-
|
313
|
-
|
285
|
+
Person.create(:name => 'Bob' , :other => :bar, :status => :inactive, :so => :mac, :gui => :gtk, :language => :en, :sex => false, :cool => false)
|
286
|
+
|
287
|
+
Person.where({ :status => :inactive }).all.map(&:name).should eql(['Bob'])
|
288
|
+
Person.where(status: :inactive).map(&:name).should eql(['Bob'])
|
314
289
|
end
|
315
290
|
|
316
|
-
# it "
|
317
|
-
#
|
318
|
-
#
|
291
|
+
# it "should work under scope" do
|
292
|
+
# Person.with_scope({ :status => :inactive }) do
|
293
|
+
# Person.all.map(&:name).should eql(['Bob'])
|
319
294
|
# end
|
320
295
|
# end
|
321
296
|
|
322
297
|
describe "dirty tracking / changed flag" do
|
323
298
|
before do
|
324
|
-
@anna =
|
299
|
+
@anna = Person.where(name: 'Anna').first
|
325
300
|
end
|
326
301
|
|
327
302
|
it "is dirty if you change the attribute value" do
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 4
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 4.0.
|
8
|
+
- 3
|
9
|
+
version: 4.0.3
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Marcos Piccinini
|
@@ -14,64 +14,94 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2011-
|
17
|
+
date: 2011-10-15 23:00:00 -03:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
|
-
name:
|
21
|
+
name: i18n
|
22
22
|
prerelease: false
|
23
23
|
requirement: &id001 !ruby/object:Gem::Requirement
|
24
24
|
none: false
|
25
25
|
requirements:
|
26
|
-
- -
|
26
|
+
- - ~>
|
27
27
|
- !ruby/object:Gem::Version
|
28
28
|
segments:
|
29
29
|
- 0
|
30
|
-
|
30
|
+
- 6
|
31
|
+
- 0
|
32
|
+
version: 0.6.0
|
31
33
|
type: :development
|
32
34
|
version_requirements: *id001
|
33
35
|
- !ruby/object:Gem::Dependency
|
34
|
-
name:
|
36
|
+
name: rspec
|
35
37
|
prerelease: false
|
36
38
|
requirement: &id002 !ruby/object:Gem::Requirement
|
37
39
|
none: false
|
38
40
|
requirements:
|
39
|
-
- -
|
41
|
+
- - ~>
|
40
42
|
- !ruby/object:Gem::Version
|
41
43
|
segments:
|
44
|
+
- 2
|
45
|
+
- 6
|
42
46
|
- 0
|
43
|
-
version:
|
47
|
+
version: 2.6.0
|
44
48
|
type: :development
|
45
49
|
version_requirements: *id002
|
50
|
+
- !ruby/object:Gem::Dependency
|
51
|
+
name: mongoid
|
52
|
+
prerelease: false
|
53
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ~>
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
segments:
|
59
|
+
- 2
|
60
|
+
- 3
|
61
|
+
- 1
|
62
|
+
version: 2.3.1
|
63
|
+
type: :development
|
64
|
+
version_requirements: *id003
|
65
|
+
- !ruby/object:Gem::Dependency
|
66
|
+
name: active_record
|
67
|
+
prerelease: false
|
68
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
69
|
+
none: false
|
70
|
+
requirements:
|
71
|
+
- - ~>
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
segments:
|
74
|
+
- 3
|
75
|
+
- 1
|
76
|
+
- 1
|
77
|
+
version: 3.1.1
|
78
|
+
type: :development
|
79
|
+
version_requirements: *id004
|
46
80
|
description: ActiveRecord/Mongoid enums with i18n
|
47
81
|
email: x@nofxx.com
|
48
82
|
executables: []
|
49
83
|
|
50
84
|
extensions: []
|
51
85
|
|
52
|
-
extra_rdoc_files:
|
53
|
-
|
86
|
+
extra_rdoc_files: []
|
87
|
+
|
54
88
|
files:
|
55
|
-
- .document
|
56
|
-
- MIT-LICENSE
|
57
|
-
- README.rdoc
|
58
|
-
- Rakefile
|
59
|
-
- VERSION
|
60
89
|
- lib/symbolize.rb
|
61
90
|
- lib/symbolize/active_record.rb
|
62
91
|
- lib/symbolize/mongoid.rb
|
92
|
+
- lib/symbolize/version.rb
|
63
93
|
- lib/symbolize/railtie.rb
|
64
|
-
-
|
65
|
-
- spec/db/001_create_testing_structure.rb
|
66
|
-
- spec/locales/en.yml
|
67
|
-
- spec/locales/pt.yml
|
94
|
+
- spec/spec_helper_mongoid.rb
|
68
95
|
- spec/spec_helper.rb
|
96
|
+
- spec/symbolize_spec.rb
|
69
97
|
- spec/spec_helper_ar.rb
|
70
|
-
- spec/spec_helper_mongoid.rb
|
71
98
|
- spec/symbolize/active_record_spec.rb
|
72
99
|
- spec/symbolize/mongoid_spec.rb
|
73
|
-
- spec/
|
74
|
-
-
|
100
|
+
- spec/db/001_create_testing_structure.rb
|
101
|
+
- spec/locales/pt.yml
|
102
|
+
- spec/locales/en.yml
|
103
|
+
- README.rdoc
|
104
|
+
- Rakefile
|
75
105
|
has_rdoc: true
|
76
106
|
homepage: http://github.com/nofxx/symbolize
|
77
107
|
licenses: []
|
data/.document
DELETED
data/MIT-LICENSE
DELETED
@@ -1,20 +0,0 @@
|
|
1
|
-
Copyright (c) 2007-2008 Andreas Neuhaus <zargony@zargony.com>
|
2
|
-
|
3
|
-
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
-
a copy of this software and associated documentation files (the
|
5
|
-
"Software"), to deal in the Software without restriction, including
|
6
|
-
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
-
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
-
permit persons to whom the Software is furnished to do so, subject to
|
9
|
-
the following conditions:
|
10
|
-
|
11
|
-
The above copyright notice and this permission notice shall be
|
12
|
-
included in all copies or substantial portions of the Software.
|
13
|
-
|
14
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
-
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
-
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
-
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/VERSION
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
4.0.2
|
@@ -1,60 +0,0 @@
|
|
1
|
-
# Goes in lib/meta_tag_helper.rb
|
2
|
-
module ActionView
|
3
|
-
module Helpers
|
4
|
-
module FormHelper
|
5
|
-
# helper to create a select drop down list for the symbolize values
|
6
|
-
def select_sym(object, method, choices = nil, options = {}, html_options = {})
|
7
|
-
|
8
|
-
InstanceTag.new(object, method, self, options.delete(:object)).
|
9
|
-
to_select_sym_tag(choices, options, html_options)
|
10
|
-
end
|
11
|
-
|
12
|
-
def radio_sym(object, method, choices = nil, options = {})
|
13
|
-
InstanceTag.new(object, method, self, options.delete(:object)).
|
14
|
-
to_radio_sym_tag(choices, options)
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
class FormBuilder
|
19
|
-
def select_sym(method, choices = nil, options = {}, html_options = {})
|
20
|
-
@template.select_sym(@object_name, method, choices, objectify_options(options), html_options)
|
21
|
-
end
|
22
|
-
|
23
|
-
def radio_sym(method, choices = nil, options = {})
|
24
|
-
@template.radio_sym(@object_name, method, choices, objectify_options(options))
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
class InstanceTag
|
29
|
-
# Create a select tag and one option for each of the
|
30
|
-
# symbolize values.
|
31
|
-
def to_select_sym_tag(choices, options, html_options)
|
32
|
-
choices = symbolize_values(choices)
|
33
|
-
to_select_tag(choices, options, html_options)
|
34
|
-
end
|
35
|
-
|
36
|
-
def to_radio_sym_tag(choices, options)
|
37
|
-
choices = symbolize_values(choices)
|
38
|
-
raise ArgumentError, "No values for radio tag" unless choices
|
39
|
-
add_default_name_and_id(options)
|
40
|
-
v = value(object)
|
41
|
-
tag_text = ''
|
42
|
-
template = options.dup
|
43
|
-
template.delete('checked')
|
44
|
-
choices.each do |choice|
|
45
|
-
opts = template.dup
|
46
|
-
opts['checked'] = 'checked' if v and v == choice[1]
|
47
|
-
opts['id'] = "#{opts['id']}_#{choice[1]}"
|
48
|
-
tag_text << "<label>#{choice[0]}: "
|
49
|
-
tag_text << to_radio_button_tag(choice[1], opts)
|
50
|
-
tag_text << "</label>"
|
51
|
-
end
|
52
|
-
tag_text
|
53
|
-
end
|
54
|
-
|
55
|
-
def symbolize_values(choices)
|
56
|
-
choices.nil? ? object.class.send("get_#{@method_name}_values") : choices
|
57
|
-
end
|
58
|
-
end
|
59
|
-
end
|
60
|
-
end
|
data/symbolize.gemspec
DELETED
@@ -1,61 +0,0 @@
|
|
1
|
-
# Generated by jeweler
|
2
|
-
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
-
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
-
# -*- encoding: utf-8 -*-
|
5
|
-
|
6
|
-
Gem::Specification.new do |s|
|
7
|
-
s.name = %q{symbolize}
|
8
|
-
s.version = "4.0.2"
|
9
|
-
|
10
|
-
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
-
s.authors = ["Marcos Piccinini"]
|
12
|
-
s.date = %q{2011-09-29}
|
13
|
-
s.description = %q{ActiveRecord/Mongoid enums with i18n}
|
14
|
-
s.email = %q{x@nofxx.com}
|
15
|
-
s.extra_rdoc_files = [
|
16
|
-
"README.rdoc"
|
17
|
-
]
|
18
|
-
s.files = [
|
19
|
-
".document",
|
20
|
-
"MIT-LICENSE",
|
21
|
-
"README.rdoc",
|
22
|
-
"Rakefile",
|
23
|
-
"VERSION",
|
24
|
-
"lib/symbolize.rb",
|
25
|
-
"lib/symbolize/active_record.rb",
|
26
|
-
"lib/symbolize/mongoid.rb",
|
27
|
-
"lib/symbolize/railtie.rb",
|
28
|
-
"lib/symbolize/symbolize_helper.rb",
|
29
|
-
"spec/db/001_create_testing_structure.rb",
|
30
|
-
"spec/locales/en.yml",
|
31
|
-
"spec/locales/pt.yml",
|
32
|
-
"spec/spec_helper.rb",
|
33
|
-
"spec/spec_helper_ar.rb",
|
34
|
-
"spec/spec_helper_mongoid.rb",
|
35
|
-
"spec/symbolize/active_record_spec.rb",
|
36
|
-
"spec/symbolize/mongoid_spec.rb",
|
37
|
-
"spec/symbolize_spec.rb",
|
38
|
-
"symbolize.gemspec"
|
39
|
-
]
|
40
|
-
s.homepage = %q{http://github.com/nofxx/symbolize}
|
41
|
-
s.require_paths = ["lib"]
|
42
|
-
s.rubygems_version = %q{1.3.7}
|
43
|
-
s.summary = %q{Object enums with i18n in AR or Mongoid}
|
44
|
-
|
45
|
-
if s.respond_to? :specification_version then
|
46
|
-
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
47
|
-
s.specification_version = 3
|
48
|
-
|
49
|
-
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
50
|
-
s.add_development_dependency(%q<rspec>, [">= 0"])
|
51
|
-
s.add_development_dependency(%q<sqlite3>, [">= 0"])
|
52
|
-
else
|
53
|
-
s.add_dependency(%q<rspec>, [">= 0"])
|
54
|
-
s.add_dependency(%q<sqlite3>, [">= 0"])
|
55
|
-
end
|
56
|
-
else
|
57
|
-
s.add_dependency(%q<rspec>, [">= 0"])
|
58
|
-
s.add_dependency(%q<sqlite3>, [">= 0"])
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|