symbolize 1.0.1 → 3.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +40 -5
- data/Rakefile +1 -0
- data/VERSION +1 -1
- data/lib/symbolize.rb +28 -3
- data/spec/db/create_testing_structure.rb +2 -0
- data/spec/symbolize_spec.rb +70 -13
- data/symbolize.gemspec +6 -3
- metadata +29 -8
data/README.rdoc
CHANGED
@@ -26,13 +26,26 @@ Github: http://github.com/zargony/activerecord_symbolize
|
|
26
26
|
|
27
27
|
Gem:
|
28
28
|
|
29
|
-
gem install
|
30
|
-
config.gem "
|
29
|
+
gem install symbolize
|
30
|
+
config.gem "symbolize", :source => 'http://gemcutter.org'
|
31
|
+
|
32
|
+
|
33
|
+
Rails 3+ Gemfile
|
34
|
+
|
35
|
+
gem "symbolize"
|
31
36
|
|
32
37
|
|
33
38
|
Plugin:
|
34
39
|
|
35
|
-
./script/plugin install git://github.com/nofxx/
|
40
|
+
./script/plugin install git://github.com/nofxx/symbolize.git
|
41
|
+
|
42
|
+
or
|
43
|
+
|
44
|
+
rails plugin install in Rails3+
|
45
|
+
|
46
|
+
== Rails 3 (beta)
|
47
|
+
|
48
|
+
Specs pass with rails 3, but a scope :public == fail.
|
36
49
|
|
37
50
|
|
38
51
|
== Usage
|
@@ -41,15 +54,17 @@ Add "symbolize :attr_name" to your model class. You may also want to add
|
|
41
54
|
validates_inclusion_of to restrict the possible values (just like an enum).
|
42
55
|
|
43
56
|
class User < ActiveRecord::Base
|
44
|
-
symbolize :gender, :in => [:female, :male]
|
57
|
+
symbolize :gender, :in => [:female, :male], :scopes => true
|
45
58
|
symbolize :so, :in => {
|
46
59
|
:linux => "Linux",
|
47
60
|
:mac => "Mac OS X"
|
48
|
-
}
|
61
|
+
}, :scopes => true
|
49
62
|
symbolize :gui, , :in => [:gnome, :kde, :xfce], :allow_blank => true
|
50
63
|
symbolize :browser, :in => [:firefox, :opera], :i18n => false, :methods => true
|
64
|
+
symbolize :angry, :in => [true, false], :scopes => true
|
51
65
|
end
|
52
66
|
|
67
|
+
|
53
68
|
=== in/within
|
54
69
|
|
55
70
|
The values allowed on the enum field, you can provide a hash with
|
@@ -100,6 +115,25 @@ Its possible to use boolean fields also.
|
|
100
115
|
"nil": Unknown
|
101
116
|
|
102
117
|
|
118
|
+
=== scopes (BETA)
|
119
|
+
|
120
|
+
If you provide the scopes option, some fancy named scopes will be added:
|
121
|
+
In our User example, gender has this option, so you can do:
|
122
|
+
|
123
|
+
User.female => User.find(:all, :conditions => { :gender => :female })
|
124
|
+
|
125
|
+
You can chain named scopes as well:
|
126
|
+
|
127
|
+
User.female.mac => User.find(:all, :conditions => { :gender => :female, :so => :mac })
|
128
|
+
|
129
|
+
For boolean colums you can use
|
130
|
+
|
131
|
+
User.angry => User.find(:all, :conditions => { :angry => true })
|
132
|
+
User.not_angry => User.find(:all, :conditions => { :angry => false })
|
133
|
+
|
134
|
+
( or with_[attribute] and without_[attribute] )
|
135
|
+
|
136
|
+
|
103
137
|
== Examples
|
104
138
|
|
105
139
|
u = User.find_by_name('Anna') # => #<User Anna>
|
@@ -109,6 +143,7 @@ Its possible to use boolean fields also.
|
|
109
143
|
u.gender # => :male
|
110
144
|
|
111
145
|
u = User.find(:all, :conditions => { :gender => :female })
|
146
|
+
u = User.female
|
112
147
|
|
113
148
|
u = User.new(:name => 'ET', :gender => :unknown)
|
114
149
|
u.save # => validation fails
|
data/Rakefile
CHANGED
@@ -12,6 +12,7 @@ begin
|
|
12
12
|
gem.homepage = "http://github.com/nofxx/symbolize"
|
13
13
|
gem.authors = ["Marcos Piccinini"]
|
14
14
|
gem.add_development_dependency "rspec"
|
15
|
+
gem.add_development_dependency "sqlite3"
|
15
16
|
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
16
17
|
end
|
17
18
|
rescue LoadError
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
3.0.0
|
data/lib/symbolize.rb
CHANGED
@@ -46,6 +46,7 @@ module Symbolize
|
|
46
46
|
module ClassMethods
|
47
47
|
# Specifies that values of the given attributes should be returned
|
48
48
|
# as symbols. The table column should be created of type string.
|
49
|
+
|
49
50
|
def symbolize *attr_names
|
50
51
|
configuration = {}
|
51
52
|
configuration.update(attr_names.extract_options!)
|
@@ -53,12 +54,14 @@ module Symbolize
|
|
53
54
|
enum = configuration[:in] || configuration[:within]
|
54
55
|
i18n = configuration[:i18n].nil? && !enum.instance_of?(Hash) && enum ? true : configuration[:i18n]
|
55
56
|
methods = configuration[:methods]
|
57
|
+
scopes = configuration[:scopes]
|
58
|
+
validation = configuration[:validation] != false
|
56
59
|
|
57
60
|
unless enum.nil?
|
58
61
|
# Little monkeypatching, <1.8 Hashes aren't ordered.
|
59
|
-
hsh = if RUBY_VERSION > '1.9'
|
62
|
+
hsh = if RUBY_VERSION > '1.9'
|
60
63
|
Hash
|
61
|
-
|
64
|
+
elsif defined?('ActiveSupport')
|
62
65
|
ActiveSupport::OrderedHash
|
63
66
|
end
|
64
67
|
|
@@ -87,9 +90,29 @@ module Symbolize
|
|
87
90
|
end
|
88
91
|
end
|
89
92
|
end
|
93
|
+
|
94
|
+
|
95
|
+
if scopes
|
96
|
+
scope_comm = lambda { |*args| ActiveRecord::VERSION::MAJOR >= 3 ? scope(*args) : named_scope(*args)}
|
97
|
+
values.each do |value|
|
98
|
+
if value[0].respond_to?(:to_sym)
|
99
|
+
scope_comm.call( value[0].to_sym, :conditions => { attr_name => value[0].to_sym })
|
100
|
+
else
|
101
|
+
if value[0] == true || value[0] == false
|
102
|
+
scope_comm.call( "with_#{attr_name}", :conditions => { attr_name => true })
|
103
|
+
scope_comm.call( "without_#{attr_name}", :conditions => { attr_name => false })
|
104
|
+
|
105
|
+
scope_comm.call( attr_name.to_sym, :conditions => { attr_name => true })
|
106
|
+
scope_comm.call( "not_#{attr_name}", :conditions => { attr_name => false })
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
90
111
|
end
|
91
112
|
|
92
|
-
|
113
|
+
if validation
|
114
|
+
class_eval "validates_inclusion_of :#{attr_names.join(', :')}, configuration"
|
115
|
+
end
|
93
116
|
end
|
94
117
|
|
95
118
|
attr_names.each do |attr_name|
|
@@ -154,3 +177,5 @@ class Symbol
|
|
154
177
|
"'#{ActiveRecord::Base.connection.quote_string(self.to_s)}'"
|
155
178
|
end
|
156
179
|
end
|
180
|
+
|
181
|
+
ActiveRecord::Base.send(:include, Symbolize) if ActiveRecord::VERSION::MAJOR >= 3
|
data/spec/symbolize_spec.rb
CHANGED
@@ -6,16 +6,16 @@ require File.dirname(__FILE__) + '/spec_helper'
|
|
6
6
|
class User < ActiveRecord::Base
|
7
7
|
symbolize :other
|
8
8
|
symbolize :language, :in => [:pt, :en]
|
9
|
-
symbolize :sex, :in => [true, false]
|
10
|
-
symbolize :status , :in => [:active, :inactive], :i18n => false, :capitalize => true
|
9
|
+
symbolize :sex, :in => [true, false], :scopes => true
|
10
|
+
symbolize :status , :in => [:active, :inactive], :i18n => false, :capitalize => true, :scopes => true
|
11
11
|
symbolize :so, :allow_blank => true, :in => {
|
12
12
|
:linux => 'Linux',
|
13
13
|
:mac => 'Mac OS X',
|
14
14
|
:win => 'Videogame'
|
15
|
-
}
|
15
|
+
}, :scopes => true
|
16
16
|
symbolize :gui, :allow_blank => true, :in => [:cocoa, :qt, :gtk], :i18n => false
|
17
17
|
symbolize :karma, :in => [:good, :bad, :ugly], :methods => true, :i18n => false, :allow_nil => true
|
18
|
-
|
18
|
+
symbolize :cool, :in => [true, false], :scopes => true
|
19
19
|
end
|
20
20
|
|
21
21
|
class UserSkill < ActiveRecord::Base
|
@@ -23,19 +23,22 @@ class UserSkill < ActiveRecord::Base
|
|
23
23
|
end
|
24
24
|
|
25
25
|
# Make with_scope public-usable for testing
|
26
|
+
#if ActiveRecord::VERSION::MAJOR < 3
|
26
27
|
class << ActiveRecord::Base
|
27
28
|
public :with_scope
|
28
29
|
end
|
30
|
+
#end
|
29
31
|
|
30
32
|
# Test records
|
31
|
-
User.create(:name => 'Anna', :other => :fo, :status => :active , :so => :linux, :gui => :qt, :language => :pt, :sex => true)
|
32
|
-
User.create(:name => 'Bob' , :other => :bar,:status => :inactive, :so => :mac, :gui => :gtk, :language => :en, :sex => false)
|
33
|
+
User.create(:name => 'Anna', :other => :fo, :status => :active , :so => :linux, :gui => :qt, :language => :pt, :sex => true, :cool => true)
|
34
|
+
User.create(:name => 'Bob' , :other => :bar,:status => :inactive, :so => :mac, :gui => :gtk, :language => :en, :sex => false, :cool => false)
|
33
35
|
|
34
36
|
|
35
37
|
describe "Symbolize" do
|
36
38
|
|
37
39
|
|
38
40
|
it "should respond to symbolize" do
|
41
|
+
p ActiveRecord::VERSION::MAJOR
|
39
42
|
ActiveRecord::Base.should respond_to :symbolize
|
40
43
|
end
|
41
44
|
|
@@ -147,15 +150,69 @@ describe "Symbolize" do
|
|
147
150
|
# assert_equal "'weird''; chars'", @user.status.quoted_id
|
148
151
|
end
|
149
152
|
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
153
|
+
describe "ActiveRecord stuff" do
|
154
|
+
|
155
|
+
if ActiveRecord::VERSION::MAJOR < 3
|
156
|
+
|
157
|
+
it "test_symbolized_finder" do
|
158
|
+
User.find(:all, :conditions => { :status => :inactive }).map(&:name).should eql(['Bob'])
|
159
|
+
User.find_all_by_status(:inactive).map(&:name).should eql(['Bob'])
|
160
|
+
end
|
161
|
+
|
162
|
+
it "test_symbolized_with_scope" do
|
163
|
+
User.with_scope(:find => { :conditions => { :status => :inactive }}) do
|
164
|
+
User.find(:all).map(&:name).should eql(['Bob'])
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
168
|
+
else
|
169
|
+
|
170
|
+
it "test_symbolized_finder" do
|
171
|
+
User.where({ :status => :inactive }).all.map(&:name).should eql(['Bob'])
|
172
|
+
User.find_all_by_status(:inactive).map(&:name).should eql(['Bob'])
|
173
|
+
end
|
174
|
+
|
175
|
+
it "test_symbolized_with_scope" do
|
176
|
+
User.with_scope(:find => { :conditions => { :status => :inactive }}) do
|
177
|
+
User.find(:all).map(&:name).should eql(['Bob'])
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
181
|
+
describe "Named Scopes" do
|
182
|
+
|
183
|
+
before do
|
184
|
+
@anna = User.find_by_name!('Anna')
|
185
|
+
@bob = User.find_by_name!('Bob')
|
186
|
+
end
|
187
|
+
|
188
|
+
it "should have main named scope" do
|
189
|
+
User.inactive.should == [@bob]
|
190
|
+
end
|
191
|
+
|
192
|
+
it "should have other to test better" do
|
193
|
+
User.linux.should == [@anna]
|
194
|
+
end
|
195
|
+
|
196
|
+
it "should have 'with' helper" do
|
197
|
+
User.with_sex.should == [@anna]
|
198
|
+
end
|
199
|
+
|
200
|
+
it "should have 'without' helper" do
|
201
|
+
User.without_sex.should == [@bob]
|
202
|
+
end
|
203
|
+
|
204
|
+
it "should have 'attr_name' helper" do
|
205
|
+
User.cool.should == [@anna]
|
206
|
+
end
|
207
|
+
|
208
|
+
it "should have 'not_attr_name' helper" do
|
209
|
+
User.not_cool.should == [@bob]
|
210
|
+
end
|
211
|
+
|
212
|
+
end
|
154
213
|
|
155
|
-
it "test_symbolized_with_scope" do
|
156
|
-
User.with_scope(:find => { :conditions => { :status => :inactive }}) do
|
157
|
-
User.find(:all).map(&:name).should eql(['Bob'])
|
158
214
|
end
|
215
|
+
|
159
216
|
end
|
160
217
|
|
161
218
|
describe "View helpers" do
|
data/symbolize.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{symbolize}
|
8
|
-
s.version = "
|
8
|
+
s.version = "3.0.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Marcos Piccinini"]
|
12
|
-
s.date = %q{2010-
|
12
|
+
s.date = %q{2010-04-12}
|
13
13
|
s.description = %q{ActiveRecord enums with i18n}
|
14
14
|
s.email = %q{x@nofxx.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -35,7 +35,7 @@ Gem::Specification.new do |s|
|
|
35
35
|
s.homepage = %q{http://github.com/nofxx/symbolize}
|
36
36
|
s.rdoc_options = ["--charset=UTF-8"]
|
37
37
|
s.require_paths = ["lib"]
|
38
|
-
s.rubygems_version = %q{1.3.
|
38
|
+
s.rubygems_version = %q{1.3.6}
|
39
39
|
s.summary = %q{ActiveRecord enums with i18n}
|
40
40
|
s.test_files = [
|
41
41
|
"spec/db/create_testing_structure.rb",
|
@@ -49,11 +49,14 @@ Gem::Specification.new do |s|
|
|
49
49
|
|
50
50
|
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
51
51
|
s.add_development_dependency(%q<rspec>, [">= 0"])
|
52
|
+
s.add_development_dependency(%q<sqlite3>, [">= 0"])
|
52
53
|
else
|
53
54
|
s.add_dependency(%q<rspec>, [">= 0"])
|
55
|
+
s.add_dependency(%q<sqlite3>, [">= 0"])
|
54
56
|
end
|
55
57
|
else
|
56
58
|
s.add_dependency(%q<rspec>, [">= 0"])
|
59
|
+
s.add_dependency(%q<sqlite3>, [">= 0"])
|
57
60
|
end
|
58
61
|
end
|
59
62
|
|
metadata
CHANGED
@@ -1,7 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: symbolize
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 3
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
version: 3.0.0
|
5
10
|
platform: ruby
|
6
11
|
authors:
|
7
12
|
- Marcos Piccinini
|
@@ -9,19 +14,33 @@ autorequire:
|
|
9
14
|
bindir: bin
|
10
15
|
cert_chain: []
|
11
16
|
|
12
|
-
date: 2010-
|
17
|
+
date: 2010-04-12 00:00:00 -03:00
|
13
18
|
default_executable:
|
14
19
|
dependencies:
|
15
20
|
- !ruby/object:Gem::Dependency
|
16
21
|
name: rspec
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 0
|
29
|
+
version: "0"
|
17
30
|
type: :development
|
18
|
-
|
19
|
-
|
31
|
+
version_requirements: *id001
|
32
|
+
- !ruby/object:Gem::Dependency
|
33
|
+
name: sqlite3
|
34
|
+
prerelease: false
|
35
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
20
36
|
requirements:
|
21
37
|
- - ">="
|
22
38
|
- !ruby/object:Gem::Version
|
39
|
+
segments:
|
40
|
+
- 0
|
23
41
|
version: "0"
|
24
|
-
|
42
|
+
type: :development
|
43
|
+
version_requirements: *id002
|
25
44
|
description: ActiveRecord enums with i18n
|
26
45
|
email: x@nofxx.com
|
27
46
|
executables: []
|
@@ -59,18 +78,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
59
78
|
requirements:
|
60
79
|
- - ">="
|
61
80
|
- !ruby/object:Gem::Version
|
81
|
+
segments:
|
82
|
+
- 0
|
62
83
|
version: "0"
|
63
|
-
version:
|
64
84
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
65
85
|
requirements:
|
66
86
|
- - ">="
|
67
87
|
- !ruby/object:Gem::Version
|
88
|
+
segments:
|
89
|
+
- 0
|
68
90
|
version: "0"
|
69
|
-
version:
|
70
91
|
requirements: []
|
71
92
|
|
72
93
|
rubyforge_project:
|
73
|
-
rubygems_version: 1.3.
|
94
|
+
rubygems_version: 1.3.6
|
74
95
|
signing_key:
|
75
96
|
specification_version: 3
|
76
97
|
summary: ActiveRecord enums with i18n
|