symbolize 4.3.0 → 4.3.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +10 -30
- data/lib/symbolize.rb +3 -3
- data/lib/symbolize/mongoid.rb +4 -7
- data/lib/symbolize/version.rb +1 -1
- data/spec/symbolize/active_record_spec.rb +10 -2
- data/spec/symbolize/mongoid_spec.rb +44 -21
- metadata +7 -7
data/README.rdoc
CHANGED
@@ -13,7 +13,7 @@ and can be set using :symbols or "strings".
|
|
13
13
|
|
14
14
|
=== Gemfile
|
15
15
|
|
16
|
-
gem
|
16
|
+
gem 'symbolize'
|
17
17
|
|
18
18
|
|
19
19
|
== About
|
@@ -27,6 +27,7 @@ On schema DBs, the attribute should be a string (varchar) column.
|
|
27
27
|
|
28
28
|
== Usage
|
29
29
|
|
30
|
+
|
30
31
|
ActiveRecord:
|
31
32
|
|
32
33
|
class Contact < ActiveRecord::Base
|
@@ -36,20 +37,11 @@ ActiveRecord:
|
|
36
37
|
|
37
38
|
Mongoid:
|
38
39
|
|
39
|
-
require 'symbolize/mongoid'
|
40
|
-
|
41
40
|
class Contact
|
42
41
|
include Mongoid::Symbolize
|
43
42
|
|
44
43
|
symbolize :kind, :in => [:im, :mobile, :email], :scopes => true
|
45
44
|
|
46
|
-
or just
|
47
|
-
|
48
|
-
class Contact
|
49
|
-
include Symbolize::Mongoid
|
50
|
-
|
51
|
-
symbolize :kind, :in => [:im, :mobile, :email], :scopes => true
|
52
|
-
|
53
45
|
|
54
46
|
Other examples:
|
55
47
|
|
@@ -77,18 +69,20 @@ Other examples:
|
|
77
69
|
|
78
70
|
=== in/within
|
79
71
|
|
80
|
-
|
72
|
+
You can provide a hash like for values allowed on the field, e.g.:
|
81
73
|
{:value => "Human text"} or an array of keys to run i18n on.
|
82
74
|
Booleans are also supported. See below.
|
83
75
|
|
84
|
-
allow_(blank|nil): What you expect.
|
85
|
-
|
86
76
|
|
87
|
-
===
|
77
|
+
=== Validate
|
88
78
|
|
89
79
|
Set to false to avoid the validation of the input.
|
90
80
|
Useful for a dropdown with an "other" option textfield.
|
91
81
|
|
82
|
+
symbolize :color, :in => [:green, :red, :blue], :validate => false
|
83
|
+
|
84
|
+
There's is also allow_(blank|nil): As you expect.
|
85
|
+
|
92
86
|
|
93
87
|
=== method
|
94
88
|
|
@@ -139,7 +133,7 @@ You can skip i18n lookup with :i18n => false
|
|
139
133
|
symbolize :style, :in => [:rock, :punk, :funk, :jazz], :i18n => false
|
140
134
|
|
141
135
|
|
142
|
-
===
|
136
|
+
=== Scopes
|
143
137
|
|
144
138
|
If you provide the scopes option, some fancy named scopes will be added:
|
145
139
|
In our User example, gender has male/female options, so you can do:
|
@@ -163,7 +157,7 @@ For boolean colums you can use
|
|
163
157
|
( or with_[attribute] and without_[attribute] )
|
164
158
|
|
165
159
|
|
166
|
-
===
|
160
|
+
=== Default
|
167
161
|
|
168
162
|
As the name suggest, the symbol you choose as default will be set
|
169
163
|
in new objects automatically. Mongoid only for now.
|
@@ -190,19 +184,6 @@ Somewhere on a view:
|
|
190
184
|
|
191
185
|
= select_tag :kind, Coffee.get_genetic_values
|
192
186
|
|
193
|
-
|
194
|
-
== View Helpers (DEPRECATED)
|
195
|
-
|
196
|
-
|
197
|
-
<% form_for @user do |f| %>
|
198
|
-
<%= f.radio_sym "gender" %>
|
199
|
-
<!-- Alphabetic order -->
|
200
|
-
<%= f.select_sym "so" %>
|
201
|
-
<!-- Fixed order -->
|
202
|
-
<%= f.select_sym "office" %>
|
203
|
-
<%end>
|
204
|
-
|
205
|
-
|
206
187
|
== Specs
|
207
188
|
|
208
189
|
Run the adapter independently:
|
@@ -215,4 +196,3 @@ Run the adapter independently:
|
|
215
196
|
|
216
197
|
This fork:
|
217
198
|
http://github.com/nofxx/symbolize
|
218
|
-
|
data/lib/symbolize.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
+
# Todo: is this the best way for AR?
|
1
2
|
module Symbolize
|
2
3
|
autoload :ActiveRecord, 'symbolize/active_record'
|
3
|
-
autoload :Mongoid, 'symbolize/mongoid'
|
4
4
|
end
|
5
5
|
|
6
|
-
require 'symbolize/
|
7
|
-
|
6
|
+
require 'symbolize/mongoid' if defined? 'Mongoid'
|
7
|
+
require 'symbolize/railtie' if defined? 'Rails'
|
data/lib/symbolize/mongoid.rb
CHANGED
@@ -108,16 +108,13 @@ module Mongoid
|
|
108
108
|
end
|
109
109
|
|
110
110
|
if scopes
|
111
|
-
|
112
|
-
|
113
|
-
scope k.to_sym, where({ attr_name => k.to_sym })
|
114
|
-
end
|
115
|
-
end
|
111
|
+
# scoped scopes
|
112
|
+
scope attr_name, ->(enum) { where(attr_name => enum) }
|
116
113
|
end
|
117
114
|
|
118
115
|
if validation
|
119
|
-
v = "validates :#{attr_names.join(', :')}"
|
120
|
-
|
116
|
+
v = "validates :#{attr_names.join(', :')}" +
|
117
|
+
",:inclusion => { :in => #{values.keys.inspect} }"
|
121
118
|
v += ",:allow_nil => true" if configuration[:allow_nil]
|
122
119
|
v += ",:allow_blank => true" if configuration[:allow_blank]
|
123
120
|
class_eval v
|
data/lib/symbolize/version.rb
CHANGED
@@ -253,7 +253,7 @@ describe "Symbolize" do
|
|
253
253
|
|
254
254
|
end
|
255
255
|
|
256
|
-
describe "
|
256
|
+
describe "Changes" do
|
257
257
|
|
258
258
|
it "is dirty if you change the attribute value" do
|
259
259
|
subject.language.should == :pt
|
@@ -269,7 +269,15 @@ describe "Symbolize" do
|
|
269
269
|
subject.language_changed?.should be_false
|
270
270
|
|
271
271
|
return_value = subject.language = :pt
|
272
|
-
|
272
|
+
return_value.should == :pt
|
273
|
+
subject.language_changed?.should be_false
|
274
|
+
end
|
275
|
+
|
276
|
+
it "is not dirty if you set the attribute value to the same value (string)" do
|
277
|
+
subject.language.should == :pt
|
278
|
+
subject.language_changed?.should be_false
|
279
|
+
|
280
|
+
return_value = subject.language = 'pt'
|
273
281
|
subject.language_changed?.should be_false
|
274
282
|
end
|
275
283
|
|
@@ -98,6 +98,12 @@ describe "Symbolize" do
|
|
98
98
|
person[:status].should eql(:active)
|
99
99
|
end
|
100
100
|
|
101
|
+
it "should make strings symbols from initializer" do
|
102
|
+
other = Person.new(language: 'en', :so => :mac, :gui => :cocoa, :language => :pt, :sex => true, :status => 'active')
|
103
|
+
other[:status].should eq(:active)
|
104
|
+
other.status.should eq(:active)
|
105
|
+
end
|
106
|
+
|
101
107
|
# it "should work nice with numbers" do
|
102
108
|
# pending
|
103
109
|
# person.status = 43
|
@@ -177,17 +183,6 @@ describe "Symbolize" do
|
|
177
183
|
person.other_text.should eql("foo")
|
178
184
|
end
|
179
185
|
|
180
|
-
it "should validate status" do
|
181
|
-
person.status = nil
|
182
|
-
person.should_not be_valid
|
183
|
-
person.should have(1).errors
|
184
|
-
end
|
185
|
-
|
186
|
-
it "should not validate so" do
|
187
|
-
person.so = nil
|
188
|
-
person.should be_valid
|
189
|
-
end
|
190
|
-
|
191
186
|
it "should work with weird chars" do
|
192
187
|
person.status = :"weird'; chars"
|
193
188
|
person.status.should eql(:"weird'; chars")
|
@@ -203,20 +198,42 @@ describe "Symbolize" do
|
|
203
198
|
person.skills.first.kind.should eql(:magic)
|
204
199
|
end
|
205
200
|
|
206
|
-
# it "should play fine with null db columns" do
|
207
|
-
# new_extra = person.extras.build
|
208
|
-
# new_extra.should_not be_valid
|
209
|
-
# end
|
210
|
-
|
211
|
-
# it "should play fine with null db columns" do
|
212
|
-
# new_extra = person.extras.build
|
213
|
-
# new_extra.should_not be_valid
|
214
|
-
# end
|
215
|
-
|
216
201
|
it "should default planet to earth" do
|
217
202
|
Person.new.planet.should eql(:earth)
|
218
203
|
end
|
219
204
|
|
205
|
+
describe "validation" do
|
206
|
+
|
207
|
+
it "should validate from initializer" do
|
208
|
+
other = Person.new(language: 'en', :so => :mac, :gui => :cocoa, :language => :pt, :sex => true, :status => 'active')
|
209
|
+
other.should be_valid
|
210
|
+
other.errors.messages.should eq({})
|
211
|
+
end
|
212
|
+
|
213
|
+
it "should validate nil" do
|
214
|
+
person.status = nil
|
215
|
+
person.should_not be_valid
|
216
|
+
person.errors.messages.should have_key(:status)
|
217
|
+
end
|
218
|
+
|
219
|
+
it "should validate not included" do
|
220
|
+
person.language = 'xx'
|
221
|
+
person.should_not be_valid
|
222
|
+
person.errors.messages.should have_key(:language)
|
223
|
+
end
|
224
|
+
|
225
|
+
it "should not validate so" do
|
226
|
+
person.so = nil
|
227
|
+
person.should be_valid
|
228
|
+
end
|
229
|
+
|
230
|
+
it "should validate ok" do
|
231
|
+
person.language = 'pt'
|
232
|
+
person.should be_valid
|
233
|
+
person.errors.messages.should eq({})
|
234
|
+
end
|
235
|
+
|
236
|
+
end
|
220
237
|
|
221
238
|
describe "i18n" do
|
222
239
|
|
@@ -342,6 +359,12 @@ describe "Symbolize" do
|
|
342
359
|
Project.count.should eql(1)
|
343
360
|
end
|
344
361
|
|
362
|
+
it "should set some scopes" do
|
363
|
+
Person.create(:name => 'Bob' , :other => :bar, :status => :active, :so => :mac, :gui => :gtk, :language => :en, :sex => false, :cool => false)
|
364
|
+
Person.status(:active).should be_a(Mongoid::Criteria)
|
365
|
+
Person.status(:active).count.should eq(1)
|
366
|
+
end
|
367
|
+
|
345
368
|
end
|
346
369
|
|
347
370
|
describe "Mongoid stuff" do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: symbolize
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 4.3.
|
4
|
+
version: 4.3.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-02-
|
12
|
+
date: 2013-02-19 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: i18n
|
@@ -48,17 +48,17 @@ dependencies:
|
|
48
48
|
requirement: !ruby/object:Gem::Requirement
|
49
49
|
none: false
|
50
50
|
requirements:
|
51
|
-
- -
|
51
|
+
- - ! '>='
|
52
52
|
- !ruby/object:Gem::Version
|
53
|
-
version: 3.1.
|
53
|
+
version: 3.1.1
|
54
54
|
type: :development
|
55
55
|
prerelease: false
|
56
56
|
version_requirements: !ruby/object:Gem::Requirement
|
57
57
|
none: false
|
58
58
|
requirements:
|
59
|
-
- -
|
59
|
+
- - ! '>='
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: 3.1.
|
61
|
+
version: 3.1.1
|
62
62
|
- !ruby/object:Gem::Dependency
|
63
63
|
name: bson_ext
|
64
64
|
requirement: !ruby/object:Gem::Requirement
|
@@ -158,7 +158,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
158
158
|
version: '0'
|
159
159
|
segments:
|
160
160
|
- 0
|
161
|
-
hash: -
|
161
|
+
hash: -387453274023359957
|
162
162
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
163
163
|
none: false
|
164
164
|
requirements:
|