symbolize 3.0.3 → 3.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -60,10 +60,21 @@ validates_inclusion_of to restrict the possible values (just like an enum).
60
60
  :linux => "Linux",
61
61
  :mac => "Mac OS X"
62
62
  }, :scopes => true
63
+
64
+ # Allow blank
63
65
  symbolize :gui, :in => [:gnome, :kde, :xfce], :allow_blank => true
66
+
67
+ # Don`t i18n
64
68
  symbolize :browser, :in => [:firefox, :opera], :i18n => false, :methods => true
69
+
70
+ # Scopes
65
71
  symbolize :angry, :in => [true, false], :scopes => true
72
+
73
+ # Don`t validate
66
74
  symbolize :lang, :in => [:ruby, :c, :erlang], :validate => false
75
+
76
+ # Default
77
+ symbolize :kind, :in => [:admin, :manager, :user], :default => :admin
67
78
  end
68
79
 
69
80
 
@@ -78,7 +89,7 @@ allow_(blank|nil): What you expect.
78
89
 
79
90
  === validate
80
91
 
81
- Set to false to avoid the validation of the input.
92
+ Set to false to avoid the validation of the input.
82
93
  Useful for a dropdown with an "other" option textfield.
83
94
 
84
95
 
@@ -142,6 +153,13 @@ For boolean colums you can use
142
153
  ( or with_[attribute] and without_[attribute] )
143
154
 
144
155
 
156
+ === default (BETA)
157
+
158
+ As the name suggest, the symbol you choose as default will be set in new objects automatically.
159
+
160
+ User.new.kind # Will print :admin
161
+
162
+
145
163
  == Examples
146
164
 
147
165
  u = User.find_by_name('Anna') # => #<User Anna>
data/VERSION CHANGED
@@ -1 +1 @@
1
- 3.0.3
1
+ 3.1.0
data/lib/symbolize.rb CHANGED
@@ -52,10 +52,11 @@ module Symbolize
52
52
  configuration.update(attr_names.extract_options!)
53
53
 
54
54
  enum = configuration[:in] || configuration[:within]
55
- i18n = configuration[:i18n].nil? && !enum.instance_of?(Hash) && enum ? true : configuration[:i18n]
56
- methods = configuration[:methods]
57
- scopes = configuration[:scopes]
58
- validation = configuration[:validation] != false
55
+ i18n = configuration.delete(:i18n).nil? && !enum.instance_of?(Hash) && enum ? true : configuration[:i18n]
56
+ scopes = configuration.delete :scopes
57
+ methods = configuration.delete :methods
58
+ validation = configuration.delete(:validation) != false
59
+ default_option = configuration.delete :default
59
60
 
60
61
  unless enum.nil?
61
62
  # Little monkeypatching, <1.8 Hashes aren't ordered.
@@ -90,38 +91,44 @@ module Symbolize
90
91
  end
91
92
  end
92
93
 
93
-
94
94
  if scopes
95
95
  scope_comm = lambda { |*args| ActiveRecord::VERSION::MAJOR >= 3 ? scope(*args) : named_scope(*args)}
96
96
  values.each do |value|
97
- if value[0].respond_to?(:to_sym)
98
- scope_comm.call( value[0].to_sym, :conditions => { attr_name => value[0].to_sym })
99
- else
100
- if value[0] == true || value[0] == false
101
- scope_comm.call( "with_#{attr_name}", :conditions => { attr_name => true })
102
- scope_comm.call( "without_#{attr_name}", :conditions => { attr_name => false })
103
-
104
- scope_comm.call( attr_name.to_sym, :conditions => { attr_name => true })
105
- scope_comm.call( "not_#{attr_name}", :conditions => { attr_name => false })
106
- end
97
+ if value[0].respond_to?(:to_sym)
98
+ scope_comm.call value[0].to_sym, :conditions => { attr_name => value[0].to_sym }
99
+ else
100
+ if value[0] == true || value[0] == false
101
+ scope_comm.call "with_#{attr_name}", :conditions => { attr_name => true }
102
+ scope_comm.call "without_#{attr_name}", :conditions => { attr_name => false }
103
+
104
+ scope_comm.call attr_name.to_sym, :conditions => { attr_name => true }
105
+ scope_comm.call "not_#{attr_name}", :conditions => { attr_name => false }
107
106
  end
107
+ end
108
108
  end
109
109
  end
110
110
  end
111
111
 
112
112
  if validation
113
- class_eval "validates_inclusion_of :#{attr_names.join(', :')}, configuration"
113
+ class_eval "validates_inclusion_of :#{attr_names.join(', :')}, #{configuration}"
114
114
  end
115
115
  end
116
116
 
117
117
  attr_names.each do |attr_name|
118
- attr_name = attr_name.to_s
119
- class_eval("def #{attr_name}; read_and_symbolize_attribute('#{attr_name}'); end")
120
- class_eval("def #{attr_name}= (value); write_symbolized_attribute('#{attr_name}', value); end")
118
+
119
+ if default_option
120
+ class_eval("def #{attr_name}; read_and_symbolize_attribute('#{attr_name}') || :#{default_option}; end")
121
+ class_eval("def #{attr_name}= (value); write_symbolized_attribute('#{attr_name}', (value && !value.empty? ? value : #{default_option})); end")
122
+ class_eval("def set_default_for_attr_#{attr_name}; self[:#{attr_name}] ||= :#{default_option}; end")
123
+ class_eval("before_save :set_default_for_attr_#{attr_name}")
124
+ else
125
+ class_eval("def #{attr_name}; read_and_symbolize_attribute('#{attr_name}'); end")
126
+ class_eval("def #{attr_name}= (value); write_symbolized_attribute('#{attr_name}', value); end")
127
+ end
121
128
  if i18n
122
129
  class_eval("def #{attr_name}_text; read_i18n_attribute('#{attr_name}'); end")
123
130
  elsif enum
124
- class_eval("def #{attr_name}_text; #{attr_name.upcase}_VALUES[#{attr_name}]; end")
131
+ class_eval("def #{attr_name}_text; #{attr_name.to_s.upcase}_VALUES[#{attr_name}]; end")
125
132
  else
126
133
  class_eval("def #{attr_name}_text; #{attr_name}.to_s; end")
127
134
  end
@@ -133,14 +140,14 @@ module Symbolize
133
140
  def symbolize_attribute attr
134
141
  case attr
135
142
  when String then attr.empty? ? nil : attr.to_sym
136
- when Symbol, TrueClass, FalseClass then attr
143
+ when Symbol, TrueClass, FalseClass, Numeric then attr
137
144
  else nil
138
145
  end
139
146
  end
140
147
 
141
148
  # Return an attribute's value as a symbol or nil
142
149
  def read_and_symbolize_attribute attr_name
143
- symbolize_attribute read_attribute(attr_name)
150
+ symbolize_attribute self[attr_name]
144
151
  end
145
152
 
146
153
  # Return an attribute's i18n
@@ -152,7 +159,7 @@ module Symbolize
152
159
  def write_symbolized_attribute attr_name, value
153
160
  val = { "true" => true, "false" => false }[value]
154
161
  val = symbolize_attribute(value) if val.nil?
155
- write_attribute(attr_name, val)
162
+ self[attr_name] = val
156
163
  end
157
164
  end
158
165
 
@@ -0,0 +1,14 @@
1
+ # Rails 3 initialization
2
+ module Symbolize
3
+ if defined? Rails::Railtie
4
+ require 'rails'
5
+ class Railtie < Rails::Railtie
6
+ initializer 'symbolize.insert_into_active_record' do
7
+ ActiveSupport.on_load :active_record do
8
+ ActiveRecord::Base.extend(Symbolize::ClassMethods)
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
14
+
data/rails/init.rb CHANGED
@@ -1,2 +1,2 @@
1
- require File.join(File.dirname(__FILE__), '..', "lib", "symbolize_helper")
1
+ require File.join(File.dirname(__FILE__), '..', "lib", "symbolize", "symbolize_helper")
2
2
  ActiveRecord::Base.send(:include, Symbolize)
@@ -1,7 +1,7 @@
1
1
  class CreateTestingStructure < ActiveRecord::Migration
2
2
  def self.up
3
3
  create_table :users do |t|
4
- t.string :name, :so, :gui, :other, :status, :language
4
+ t.string :name, :so, :gui, :other, :status, :language, :kind
5
5
  t.string :limited, :limit => 10
6
6
  t.string :karma, :limit => 5
7
7
  t.boolean :sex
@@ -9,8 +9,18 @@ class CreateTestingStructure < ActiveRecord::Migration
9
9
  t.boolean :cool
10
10
  end
11
11
  create_table :user_skills do |t|
12
+ t.references :user
12
13
  t.string :kind
13
14
  end
15
+ create_table :user_extras do |t|
16
+ t.references :user
17
+ t.string :key, :null => false
18
+ end
19
+ create_table :permissions do |t|
20
+ t.string :name, :null => false
21
+ t.string :kind, :null => false
22
+ t.integer :lvl, :null => false
23
+ end
14
24
  end
15
25
 
16
26
  def self.down
@@ -16,12 +16,25 @@ class User < ActiveRecord::Base
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
+
20
+ has_many :extras, :dependent => :destroy, :class_name => "UserExtra"
21
+ has_many :access, :dependent => :destroy, :class_name => "UserAccess"
19
22
  end
20
23
 
21
24
  class UserSkill < ActiveRecord::Base
22
25
  symbolize :kind, :in => [:agility, :magic]
23
26
  end
24
27
 
28
+ class UserExtra < ActiveRecord::Base
29
+ symbolize :key, :in => [:one, :another]
30
+ end
31
+
32
+ class Permission < ActiveRecord::Base
33
+ validates_presence_of :name
34
+ symbolize :kind, :in => [:temp, :perm], :default => :perm
35
+ symbolize :lvl, :in => (1..9).to_a, :i18n => false#, :default => 1
36
+ end
37
+
25
38
  # Make with_scope public-usable for testing
26
39
  #if ActiveRecord::VERSION::MAJOR < 3
27
40
  class << ActiveRecord::Base
@@ -40,7 +53,7 @@ describe "Symbolize" do
40
53
  ActiveRecord::Base.should respond_to :symbolize
41
54
  end
42
55
 
43
- describe "Instantiated" do
56
+ describe "User Instantiated" do
44
57
  before(:each) do
45
58
  @user = User.first
46
59
  end
@@ -59,11 +72,11 @@ describe "Symbolize" do
59
72
  # @user.read_attribute(:status).should eql('active')
60
73
  end
61
74
 
62
- it "should acts nice with numbers" do
75
+ it "should work nice with numbers" do
63
76
  @user.status = 43
64
- @user.status.should be_nil
65
- @user.status_before_type_cast.should be_nil
66
- @user.read_attribute(:status).should be_nil
77
+ @user.status.should_not be_nil
78
+ # @user.status_before_type_cast.should be_nil
79
+ # @user.read_attribute(:status).should be_nil
67
80
  end
68
81
 
69
82
  it "should acts nice with nil" do
@@ -148,76 +161,22 @@ describe "Symbolize" do
148
161
  # assert_equal "'weird''; chars'", @user.status.quoted_id
149
162
  end
150
163
 
151
- describe "ActiveRecord stuff" do
152
-
153
- #
154
- # ActiveRecord < 3
155
- #
156
- if ActiveRecord::VERSION::MAJOR < 3
157
-
158
- it "test_symbolized_finder" do
159
- User.find(:all, :conditions => { :status => :inactive }).map(&:name).should eql(['Bob'])
160
- User.find_all_by_status(:inactive).map(&:name).should eql(['Bob'])
161
- end
162
-
163
- it "test_symbolized_with_scope" do
164
- User.with_scope(:find => { :conditions => { :status => :inactive }}) do
165
- User.find(:all).map(&:name).should eql(['Bob'])
166
- end
167
- end
168
-
169
- #
170
- # ActiveRecord >= 3
171
- #
172
- else
173
-
174
- it "test_symbolized_finder" do
175
- User.where({ :status => :inactive }).all.map(&:name).should eql(['Bob'])
176
- User.find_all_by_status(:inactive).map(&:name).should eql(['Bob'])
177
- end
178
-
179
- it "test_symbolized_with_scope" do
180
- User.with_scope(:find => { :conditions => { :status => :inactive }}) do
181
- User.find(:all).map(&:name).should eql(['Bob'])
182
- end
183
- end
184
-
185
- describe "Named Scopes" do
186
-
187
- before do
188
- @anna = User.find_by_name!('Anna')
189
- @bob = User.find_by_name!('Bob')
190
- end
191
-
192
- it "should have main named scope" do
193
- User.inactive.should == [@bob]
194
- end
195
-
196
- it "should have other to test better" do
197
- User.linux.should == [@anna]
198
- end
199
-
200
- it "should have 'with' helper" do
201
- User.with_sex.should == [@anna]
202
- end
203
-
204
- it "should have 'without' helper" do
205
- User.without_sex.should == [@bob]
206
- end
207
-
208
- it "should have 'attr_name' helper" do
209
- User.cool.should == [@anna]
210
- end
164
+ it "should work fine through relations" do
165
+ @user.extras.create(:key => :one)
166
+ UserExtra.first.key.should eql(:one)
167
+ end
211
168
 
212
- it "should have 'not_attr_name' helper" do
213
- User.not_cool.should == [@bob]
214
- end
169
+ it "should play fine with null db columns" do
170
+ new_extra = @user.extras.build
171
+ new_extra.should_not be_valid
172
+ end
215
173
 
216
- end
174
+ it "should play fine with null db columns" do
175
+ new_extra = @user.extras.build
176
+ new_extra.should_not be_valid
177
+ end
217
178
 
218
- end
219
179
 
220
- end
221
180
 
222
181
  describe "View helpers" do
223
182
  include ActionView::Helpers::FormHelper
@@ -310,4 +269,113 @@ describe "Symbolize" do
310
269
 
311
270
  end
312
271
 
272
+ describe "more tests on Permission" do
273
+
274
+ it "should use default value on object build" do
275
+ Permission.new.kind.should eql(:perm)
276
+ end
277
+
278
+ it "should not interfer on create" do
279
+ Permission.create!(:name => "p7", :kind =>:temp, :lvl => 7)
280
+ Permission.find_by_name("p7").kind.should eql(:temp)
281
+ end
282
+
283
+ it "should work on create" do
284
+ pm = Permission.new(:name => "p7", :lvl => 7)
285
+ pm.should be_valid
286
+ pm.save.should be_true
287
+ end
288
+
289
+ it "should work on create" do
290
+ Permission.create!(:name => "p8", :lvl => 9)
291
+ Permission.find_by_name("p8").kind.should eql(:perm)
292
+ end
293
+
294
+ it "should work on edit" do
295
+ pm = Permission.find_by_name("p8")
296
+ pm.kind = :temp
297
+ pm.save
298
+ Permission.find_by_name("p8").kind.should eql(:temp)
299
+ end
300
+
301
+ it "should work with default values" do
302
+ pm = Permission.new(:name => "p9")
303
+ pm.lvl = 9
304
+ pm.save
305
+ Permission.find_by_name("p9").lvl.to_i.should eql(9)
306
+ end
307
+
308
+ end
309
+
310
+ describe "ActiveRecord stuff" do
311
+
312
+ #
313
+ # ActiveRecord <= 2
314
+ #
315
+ if ActiveRecord::VERSION::MAJOR < 3
316
+
317
+ it "test_symbolized_finder" do
318
+ User.find(:all, :conditions => { :status => :inactive }).map(&:name).should eql(['Bob'])
319
+ User.find_all_by_status(:inactive).map(&:name).should eql(['Bob'])
320
+ end
321
+
322
+ it "test_symbolized_with_scope" do
323
+ User.with_scope(:find => { :conditions => { :status => :inactive }}) do
324
+ User.find(:all).map(&:name).should eql(['Bob'])
325
+ end
326
+ end
327
+
328
+ #
329
+ # ActiveRecord >= 3
330
+ #
331
+ else
332
+
333
+ it "test_symbolized_finder" do
334
+ User.where({ :status => :inactive }).all.map(&:name).should eql(['Bob'])
335
+ User.find_all_by_status(:inactive).map(&:name).should eql(['Bob'])
336
+ end
337
+
338
+ it "test_symbolized_with_scope" do
339
+ User.with_scope(:find => { :conditions => { :status => :inactive }}) do
340
+ User.find(:all).map(&:name).should eql(['Bob'])
341
+ end
342
+ end
343
+
344
+ describe "Named Scopes" do
345
+
346
+ before do
347
+ @anna = User.find_by_name!('Anna')
348
+ @bob = User.find_by_name!('Bob')
349
+ end
350
+
351
+ it "should have main named scope" do
352
+ User.inactive.should == [@bob]
353
+ end
354
+
355
+ it "should have other to test better" do
356
+ User.linux.should == [@anna]
357
+ end
358
+
359
+ it "should have 'with' helper" do
360
+ User.with_sex.should == [@anna]
361
+ end
362
+
363
+ it "should have 'without' helper" do
364
+ User.without_sex.should == [@bob]
365
+ end
366
+
367
+ it "should have 'attr_name' helper" do
368
+ User.cool.should == [@anna]
369
+ end
370
+
371
+ it "should have 'not_attr_name' helper" do
372
+ User.not_cool.should == [@bob]
373
+ end
374
+
375
+ end
376
+
377
+ end
378
+
379
+ end
380
+
313
381
  end
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 = "3.0.3"
8
+ s.version = "3.1.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{2011-03-18}
12
+ s.date = %q{2011-03-23}
13
13
  s.description = %q{ActiveRecord enums with i18n}
14
14
  s.email = %q{x@nofxx.com}
15
15
  s.extra_rdoc_files = [
@@ -23,7 +23,8 @@ Gem::Specification.new do |s|
23
23
  "VERSION",
24
24
  "init.rb",
25
25
  "lib/symbolize.rb",
26
- "lib/symbolize_helper.rb",
26
+ "lib/symbolize/railtie.rb",
27
+ "lib/symbolize/symbolize_helper.rb",
27
28
  "rails/init.rb",
28
29
  "spec/db/create_testing_structure.rb",
29
30
  "spec/locales/en.yml",
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: symbolize
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 3.0.3
5
+ version: 3.1.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Marcos Piccinini
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-03-18 00:00:00 -03:00
13
+ date: 2011-03-23 00:00:00 -03:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -51,7 +51,8 @@ files:
51
51
  - VERSION
52
52
  - init.rb
53
53
  - lib/symbolize.rb
54
- - lib/symbolize_helper.rb
54
+ - lib/symbolize/railtie.rb
55
+ - lib/symbolize/symbolize_helper.rb
55
56
  - rails/init.rb
56
57
  - spec/db/create_testing_structure.rb
57
58
  - spec/locales/en.yml