symbolize 4.2.0 → 4.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -1,4 +1,4 @@
1
- = Symbolize attribute values
1
+ = Symbolize (attribute values)
2
2
 
3
3
  This plugin introduces an easy way to use symbols for values of attributes.
4
4
  Symbolized attributes return a ruby symbol (or nil) as their value
@@ -11,15 +11,9 @@ and can be set using :symbols or "strings".
11
11
  gem install symbolize
12
12
 
13
13
 
14
- === Mongoid
15
-
16
- gem "symbolize", :require => "symbolize/mongoid"
17
-
18
-
19
- === ActiveRecord
20
-
21
- gem "symbolize", :require => "symbolize/active_record"
14
+ === Gemfile
22
15
 
16
+ gem "symbolize"
23
17
 
24
18
 
25
19
  == About
@@ -35,22 +29,27 @@ On schema DBs, the attribute should be a string (varchar) column.
35
29
 
36
30
  ActiveRecord:
37
31
 
38
- class User < ActiveRecord::Base
32
+ class Contact < ActiveRecord::Base
39
33
 
40
- symbolize :gender, :in => [:female, :male], :scopes => true
34
+ symbolize :kind, :in => [:im, :mobile, :email], :scopes => true
41
35
 
42
- end
43
36
 
44
37
  Mongoid:
45
38
 
46
- class User
39
+ require 'symbolize/mongoid'
47
40
 
48
- include Mongoid::Document
41
+ class Contact
49
42
  include Mongoid::Symbolize
50
43
 
51
- symbolize :gender, :in => [:female, :male], :scopes => true
44
+ symbolize :kind, :in => [:im, :mobile, :email], :scopes => true
45
+
46
+ or just
47
+
48
+ class Contact
49
+ include Symbolize::Mongoid
50
+
51
+ symbolize :kind, :in => [:im, :mobile, :email], :scopes => true
52
52
 
53
- end
54
53
 
55
54
  Other examples:
56
55
 
@@ -171,7 +170,7 @@ in new objects automatically. Mongoid only for now.
171
170
 
172
171
  symbolize :mood, :in => [:happy, :sad, :euphoric], :default => (MarvinDay ? :sad : :happy)
173
172
 
174
- User.new.kind # It may print :happy
173
+ User.new.mood # It may print :happy
175
174
 
176
175
 
177
176
  == Rails Form Example
@@ -217,15 +216,3 @@ Run the adapter independently:
217
216
  This fork:
218
217
  http://github.com/nofxx/symbolize
219
218
 
220
-
221
- Forked from:
222
- http://github.com/nuxlli/activerecord_symbolize
223
-
224
-
225
- Initial work:
226
- I've been using this for quite some time and made it a rails plugin now. More
227
- background iinformation can be found at
228
- http://zargony.com/2007/09/07/symbolize-attribute-values-in-activerecord
229
-
230
-
231
- Copyright (c) 2007-2008 Andreas Neuhaus, released under the MIT license
data/lib/symbolize.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  module Symbolize
2
- # Require a specific adapter
3
- # symbolize/mongoid
4
- # symbolize/active_record
5
-
2
+ autoload :ActiveRecord, 'symbolize/active_record'
3
+ autoload :Mongoid, 'symbolize/mongoid'
6
4
  end
5
+
6
+ require 'symbolize/railtie' if defined? Rails
7
+
@@ -1,7 +1,10 @@
1
+ require 'active_support/concern'
2
+
1
3
  module Symbolize
2
- def self.included base
3
- base.extend(ClassMethods)
4
- end
4
+ end
5
+
6
+ module Symbolize::ActiveRecord
7
+ extend ActiveSupport::Concern
5
8
 
6
9
  # Symbolize ActiveRecord attributes. Add
7
10
  # symbolize :attr_name
@@ -51,7 +54,7 @@ module Symbolize
51
54
  configuration.update(attr_names.extract_options!)
52
55
 
53
56
  enum = configuration[:in] || configuration[:within]
54
- i18n = configuration.delete(:i18n).nil? && !enum.instance_of?(Hash) && enum ? true : configuration[:i18n]
57
+ i18n = configuration.delete(:i18n) || (!enum.instance_of?(Hash) && enum)
55
58
  scopes = configuration.delete :scopes
56
59
  methods = configuration.delete :methods
57
60
  capitalize = configuration.delete :capitalize
@@ -59,8 +62,6 @@ module Symbolize
59
62
  default_option = configuration.delete :default
60
63
 
61
64
  unless enum.nil?
62
- # Little monkeypatching, <1.8 Hashes aren't ordered.
63
- hsh = RUBY_VERSION > '1.9' || !defined?("ActiveSupport") ? Hash : ActiveSupport::OrderedHash
64
65
 
65
66
  attr_names.each do |attr_name|
66
67
  attr_name = attr_name.to_s
@@ -68,7 +69,7 @@ module Symbolize
68
69
  if enum.is_a?(Hash)
69
70
  values = enum
70
71
  else
71
- values = hsh.new
72
+ values = ActiveSupport::OrderedHash.new
72
73
  enum.map do |val|
73
74
  key = val.respond_to?(:to_sym) ? val.to_sym : val
74
75
  values[key] = capitalize ? val.to_s.capitalize : val.to_s
@@ -85,22 +86,30 @@ module Symbolize
85
86
  "def self.get_#{const}; #{const.upcase}.map(&:reverse); end"
86
87
  end
87
88
  class_eval(ev)
89
+ class_eval "def self.#{attr_name}_enum; self.get_#{const}; end"
88
90
 
89
91
  if methods
90
92
  values.each do |value|
91
93
  key = value[0]
94
+
95
+ # It's a good idea to test for name collisions here and raise exceptions.
96
+ # However, the existing software with this kind of errors will start crashing,
97
+ # so I'd postpone this improvement until the next major version
98
+ # this way it will not affect those people who use ~> in their Gemfiles
99
+
100
+ # raise ArgumentError, "re-defined #{key}? method of #{self.name} class due to 'symbolize'" if method_defined?("#{key}?")
101
+
92
102
  define_method("#{key}?") do
93
- self[attr_name].to_s == key.to_s
103
+ self.send(attr_name) == key.to_sym
94
104
  end
95
105
  end
96
106
  end
97
107
 
98
108
  if scopes
99
- scope_comm = lambda { |*args| ActiveRecord::VERSION::MAJOR >= 3 ? scope(*args) : named_scope(*args)}
100
109
  values.each do |value|
101
110
  name = value[0]
102
111
  if name.respond_to?(:to_sym)
103
- scope_comm.call name.to_sym, :conditions => { attr_name => name.to_s }
112
+ scope name.to_sym, :conditions => { attr_name => name.to_s }
104
113
  # Figure out if this as another option, or default...
105
114
  # scope_comm.call "not_#{attr_name}".to_sym, :conditions => { attr_name != name }
106
115
  end
@@ -156,8 +165,8 @@ module Symbolize
156
165
  # Return an attribute's i18n
157
166
  def read_i18n_attribute attr_name
158
167
  attr = read_attribute(attr_name)
159
- return nil if attr.nil?
160
- I18n.translate("activerecord.symbolizes.#{ActiveSupport::Inflector.underscore(self.class.model_name)}.#{attr_name}.#{attr}") #.to_sym rescue nila
168
+ t = I18n.translate("activerecord.symbolizes.#{self.class.model_name.underscore}.#{attr_name}.#{attr}") #.to_sym rescue nila
169
+ t.is_a?(Hash) ? nil : t
161
170
  end
162
171
 
163
172
  # Write a symbolized value. Watch out for booleans.
@@ -168,5 +177,3 @@ module Symbolize
168
177
  self[attr_name] = val #.to_s
169
178
  end
170
179
  end
171
-
172
- ActiveRecord::Base.send(:include, Symbolize)
@@ -1,8 +1,8 @@
1
+ require 'active_support/concern'
2
+
1
3
  module Mongoid
2
4
  module Symbolize
3
- def self.included base
4
- base.extend(ClassMethods)
5
- end
5
+ extend ActiveSupport::Concern
6
6
 
7
7
  # Symbolize Mongoid attributes. Add:
8
8
  # symbolize :attr_name
@@ -54,9 +54,10 @@ module Mongoid
54
54
  configuration.update(attr_names.extract_options!)
55
55
 
56
56
  enum = configuration[:in] || configuration[:within]
57
- i18n = configuration.delete(:i18n).nil? && !enum.instance_of?(Hash) ? true : configuration[:i18n]
58
- scopes = configuration.delete :scopes
59
- methods = configuration.delete :methods
57
+ i18n = configuration.delete(:i18n)
58
+ i18n = (!enum.instance_of?(Hash) && enum) if i18n.nil?
59
+ scopes = configuration.delete :scopes
60
+ methods = configuration.delete :methods
60
61
  capitalize = configuration.delete :capitalize
61
62
  validation = configuration.delete(:validate) != false
62
63
  field_type = configuration.delete :type
@@ -71,11 +72,11 @@ module Mongoid
71
72
  #
72
73
  # Builds Mongoid 'field :name, type: type, :default'
73
74
  #
75
+ const = "#{attr_name}_values"
74
76
  mongo_opts = ", :type => #{field_type || 'Symbol'}"
75
77
  mongo_opts += ", :default => :#{default_opt}" if default_opt
76
78
  class_eval("field :#{attr_name} #{mongo_opts}")
77
79
 
78
- const = "#{attr_name}_values"
79
80
  if enum.is_a?(Hash)
80
81
  values = enum
81
82
  else
@@ -96,31 +97,32 @@ module Mongoid
96
97
  "def self.get_#{const}; #{const.upcase}.map(&:reverse); end"
97
98
  end
98
99
  class_eval(ev)
100
+ class_eval "def self.#{attr_name}_enum; self.get_#{const}; end"
99
101
 
100
102
  if methods
101
- values.each do |value|
102
- define_method("#{value[0]}?") do
103
- self.send(attr_name) == value[0]
103
+ values.each do |k, v|
104
+ define_method("#{k}?") do
105
+ self.send(attr_name) == k
104
106
  end
105
107
  end
106
108
  end
107
109
 
108
110
  if scopes
109
- scope_comm = lambda { |*args| scope(*args)}
110
- values.each do |value|
111
- if value[0].respond_to?(:to_sym)
112
- scope_comm.call value[0].to_sym, where({ attr_name => value[0].to_sym })
111
+ values.each do |k, v|
112
+ if k.respond_to?(:to_sym)
113
+ scope k.to_sym, where({ attr_name => k.to_sym })
113
114
  end
114
115
  end
115
116
  end
116
117
 
117
118
  if validation
118
- validation = "validates :#{attr_names.join(', :')}"
119
- validation += ", :inclusion => { :in => #{values.keys.inspect} }"
120
- validation += ", :allow_nil => true" if configuration[:allow_nil]
121
- validation += ", :allow_blank => true" if configuration[:allow_blank]
122
- class_eval validation
119
+ v = "validates :#{attr_names.join(', :')}"
120
+ v += ",:inclusion => { :in => #{values.keys.inspect} }"
121
+ v += ",:allow_nil => true" if configuration[:allow_nil]
122
+ v += ",:allow_blank => true" if configuration[:allow_blank]
123
+ class_eval v
123
124
  end
125
+
124
126
  end
125
127
  end
126
128
 
@@ -130,8 +132,9 @@ module Mongoid
130
132
  attr_names.each do |attr_name|
131
133
  if i18n # memoize call to translate... good idea?
132
134
  define_method "#{attr_name}_text" do
133
- return nil unless attr = read_attribute(attr_name)
134
- I18n.t("mongoid.symbolizes.#{ActiveSupport::Inflector.underscore(self.class.model_name)}.#{attr_name}.#{attr}")
135
+ attr = read_attribute(attr_name)
136
+ return nil if attr.nil?
137
+ I18n.t("mongoid.symbolizes.#{self.class.model_name.underscore}.#{attr_name}.#{attr}")
135
138
  end
136
139
  elsif enum
137
140
  class_eval("def #{attr_name}_text; #{attr_name.to_s.upcase}_VALUES[#{attr_name}]; end")
@@ -145,3 +148,5 @@ module Mongoid
145
148
  end # ClassMethods
146
149
  end # Symbolize
147
150
  end # Mongoid
151
+
152
+ # Symbolize::Mongoid = Mongoid::Symbolize
@@ -1,15 +1,11 @@
1
1
  # Rails 3 initialization
2
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
3
+ require 'rails'
4
+ class Railtie < Rails::Railtie
5
+ initializer 'symbolize.insert_into_active_record' do
6
+ ActiveSupport.on_load :active_record do
7
+ ::ActiveRecord::Base.send :include, Symbolize::ActiveRecord
8
+ end
9
+ end
10
+ end
13
11
  end
14
-
15
-
@@ -1,3 +1,3 @@
1
1
  module Symbolize
2
- VERSION = '4.2.0'
2
+ VERSION = '4.3.0'
3
3
  end
@@ -8,6 +8,9 @@ class CreateTestingStructure < ActiveRecord::Migration
8
8
  t.boolean :sex
9
9
  t.boolean :public
10
10
  t.boolean :cool
11
+ t.string :role
12
+ t.string :country, :default => "pt"
13
+ t.string :some_attr # used in name collision tests
11
14
  end
12
15
  create_table :user_skills do |t|
13
16
  t.references :user
data/spec/locales/pt.yml CHANGED
@@ -2,6 +2,16 @@ pt:
2
2
  activerecord:
3
3
  symbolizes:
4
4
  user:
5
+ role: reader
6
+ status:
7
+ active: Active
8
+ inactive: Inactive
9
+ gui:
10
+ cocoa: cocoa
11
+ gtk: gtk
12
+ qt: qt
13
+ country:
14
+ pt: Pt
5
15
  language:
6
16
  pt: Português
7
17
  en: Inglês
@@ -2,8 +2,11 @@ require File.dirname(__FILE__) + '/spec_helper'
2
2
 
3
3
  require 'active_record'
4
4
  require 'symbolize/active_record'
5
+ ActiveRecord::Base.send :include, Symbolize::ActiveRecord # this is normally done by the railtie
6
+
5
7
  ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:") #'postgresql', :database => 'symbolize_test', :username => 'postgres')
6
8
 
9
+
7
10
  if ActiveRecord::VERSION::STRING >= "3.1"
8
11
  ActiveRecord::Migrator.migrate("spec/db")
9
12
  else
@@ -3,15 +3,16 @@ require 'mongoid'
3
3
  require 'mongoid/version'
4
4
 
5
5
  Mongoid.configure do |config|
6
- config.master = Mongo::Connection.new.db("symbolize_test")
7
- end
8
-
9
- Mongoid.database.collections.each do |collection|
10
- unless collection.name =~ /^system\./
11
- collection.remove
12
- end
6
+ #config.master = Mongo::Connection.new.db("symbolize_test")
7
+ config.connect_to('symbolize_test')
13
8
  end
14
9
 
15
10
  puts "Running Mongoid #{Mongoid::VERSION}"
16
11
 
17
12
  require 'symbolize/mongoid'
13
+
14
+ RSpec.configure do |config|
15
+ config.before(:each) do
16
+ Mongoid.purge!
17
+ end
18
+ end
@@ -17,6 +17,9 @@ class User < ActiveRecord::Base
17
17
  symbolize :karma, :in => %w{ good bad ugly}, :methods => true, :i18n => false, :allow_nil => true
18
18
  symbolize :cool, :in => [true, false], :scopes => true
19
19
 
20
+ symbolize :role, :in => [:reader, :writer, :some_existing_attr], :i18n => false, :methods => true, :default => :reader
21
+ symbolize :country, :in => [:us, :gb, :pt, :ru], :capitalize => true, :i18n => false # note: the default value is provided in db migration
22
+
20
23
  has_many :extras, :dependent => :destroy, :class_name => "UserExtra"
21
24
  has_many :access, :dependent => :destroy, :class_name => "UserAccess"
22
25
  end
@@ -52,67 +55,66 @@ describe "Symbolize" do
52
55
  it "should have a valid blueprint" do
53
56
  # Test records
54
57
  u = User.create(:name => 'Bob' , :other => :bar,:status => :inactive, :so => :mac, :gui => :gtk, :language => :en, :sex => false, :cool => false)
55
- u.errors.messages.should eql({})
58
+ u.errors.messages.should be_blank
56
59
  end
57
60
 
58
61
  it "should work nice with default values from active model" do
59
62
  u = User.create(:name => 'Niu' , :other => :bar, :so => :mac, :gui => :gtk, :language => :en, :sex => false, :cool => false)
60
- u.errors.messages.should eql({})
63
+ u.errors.messages.should be_blank
61
64
  u.status.should eql(:active)
62
65
  u.should be_active
63
66
  end
64
67
 
65
68
  describe "User Instantiated" do
66
- before(:each) do
67
- @user = User.create(:name => 'Anna', :other => :fo, :status => :active , :so => :linux, :gui => :qt, :language => :pt, :sex => true, :cool => true)
68
- end
69
-
70
- it "test_symbolize_string" do
71
- @user.status = 'inactive'
72
- @user.status.should eql(:inactive)
69
+ subject {
70
+ User.create(:name => 'Anna', :other => :fo, :status => status, :so => so, :gui => :qt, :language => :pt, :sex => true, :cool => true)
71
+ }
72
+ let(:status) { :active }
73
+ let(:so) { :linux }
74
+
75
+ describe "test_symbolize_string" do
76
+ let(:status) { 'inactive' }
77
+ its(:status) { should == :inactive }
73
78
  # @user.status_before_type_cast.should eql(:inactive)
74
79
  # @user.read_attribute(:status).should eql('inactive')
75
80
  end
76
81
 
77
- it "test_symbolize_symbol" do
78
- @user.status = :active
79
- @user.status.should eql(:active)
80
- @user.status_before_type_cast.should eql(:active)
82
+ describe "test_symbolize_symbol" do
83
+ its(:status) { should == :active }
84
+ its(:status_before_type_cast) { should == :active }
81
85
  # @user.read_attribute(:status).should eql('active')
82
86
  end
83
87
 
84
- it "should work nice with numbers" do
85
- @user.status = 43
86
- @user.status.should_not be_nil
88
+ describe "should work nice with numbers" do
89
+ let(:status) { 43 }
90
+ its(:status) { should be_present }
87
91
  # @user.status_before_type_cast.should be_nil
88
92
  # @user.read_attribute(:status).should be_nil
89
93
  end
90
94
 
91
- it "should acts nice with nil" do
92
- @user.status = nil
93
- @user.status.should be_nil
94
- @user.status_before_type_cast.should be_nil
95
- @user.read_attribute(:status).should be_nil
95
+ describe "should acts nice with nil" do
96
+ let(:status) { nil }
97
+ its(:status) { should be_nil }
98
+ its(:status_before_type_cast) { should be_nil }
99
+ it { subject.read_attribute(:status).should be_nil }
96
100
  end
97
101
 
98
- it "should acts nice with blank" do
99
- @user.status = ""
100
- @user.status.should be_nil
101
- @user.status_before_type_cast.should be_nil
102
- @user.read_attribute(:status).should be_nil
102
+ describe "should acts nice with blank" do
103
+ let(:status) { "" }
104
+ its(:status) { should be_nil }
105
+ its(:status_before_type_cast) { should be_nil }
106
+ it { subject.read_attribute(:status).should be_nil }
103
107
  end
104
108
 
105
- it "test_symbols_quoted_id" do
106
- pending
107
- @user.status = :active
108
- @user.status.quoted_id.should eql("'active'")
109
+ describe "test_symbols_quoted_id" do
110
+ pending { subject.status.quoted_id.should eql("'active'") }
109
111
  end
110
112
 
111
113
  it "should not validates other" do
112
- @user.other = nil
113
- @user.should be_valid
114
- @user.other = ""
115
- @user.should be_valid
114
+ subject.other = nil
115
+ subject.should be_valid
116
+ subject.other = ""
117
+ subject.should be_valid
116
118
  end
117
119
 
118
120
  it "should get the correct values" do
@@ -120,8 +122,12 @@ describe "Symbolize" do
120
122
  User::STATUS_VALUES.should eql({:inactive=>"Inactive", :active=>"Active"})
121
123
  end
122
124
 
123
- it "test_symbolize_humanize" do
124
- @user.status_text.should eql("Active")
125
+ it "should get the values for RailsAdmin" do
126
+ User.status_enum.should eql([["Active", :active],["Inactive", :inactive]])
127
+ end
128
+
129
+ describe "test_symbolize_humanize" do
130
+ its(:status_text) { should eql("Active") }
125
131
  end
126
132
 
127
133
  it "should get the correct values" do
@@ -129,8 +135,8 @@ describe "Symbolize" do
129
135
  User::GUI_VALUES.should eql({:cocoa=>"cocoa", :qt=>"qt", :gtk=>"gtk"})
130
136
  end
131
137
 
132
- it "test_symbolize_humanize" do
133
- @user.gui_text.should eql("qt")
138
+ describe "test_symbolize_humanize" do
139
+ its(:gui_text) { should eql("qt") }
134
140
  end
135
141
 
136
142
  it "should get the correct values" do
@@ -138,58 +144,58 @@ describe "Symbolize" do
138
144
  User::SO_VALUES.should eql({:linux => "Linux", :mac => "Mac OS X", :win => "Videogame"})
139
145
  end
140
146
 
141
- it "test_symbolize_humanize" do
142
- @user.so_text.should eql("Linux")
147
+ describe "test_symbolize_humanize" do
148
+ its(:so_text) { should eql("Linux") }
143
149
  end
144
150
 
145
- it "test_symbolize_humanize" do
146
- @user.so = :mac
147
- @user.so_text.should eql("Mac OS X")
151
+ describe "test_symbolize_humanize" do
152
+ let(:so) { :mac }
153
+ its(:so_text) { should eql("Mac OS X") }
148
154
  end
149
155
 
150
156
  it "should stringify" do
151
- @user.other_text.should eql("fo")
152
- @user.other = :foo
153
- @user.other_text.should eql("foo")
157
+ subject.other_text.should eql("fo")
158
+ subject.other = :foo
159
+ subject.other_text.should eql("foo")
154
160
  end
155
161
 
156
- it "should validate status" do
157
- @user.status = nil
158
- @user.should_not be_valid
159
- @user.should have(1).errors
162
+ describe "should validate status" do
163
+ let(:status) { nil }
164
+ it { should_not be_valid }
165
+ it { should have(1).errors }
160
166
  end
161
167
 
162
168
  it "should not validate so" do
163
- @user.so = nil
164
- @user.should be_valid
169
+ subject.so = nil
170
+ subject.should be_valid
165
171
  end
166
172
 
167
173
  it "test_symbols_with_weird_chars_quoted_id" do
168
- @user.status = :"weird'; chars"
169
- @user.status_before_type_cast.should eql(:"weird'; chars")
174
+ subject.status = :"weird'; chars"
175
+ subject.status_before_type_cast.should eql(:"weird'; chars")
170
176
  # assert_equal "weird'; chars", @user.read_attribute(:status)
171
177
  # assert_equal "'weird''; chars'", @user.status.quoted_id
172
178
  end
173
179
 
174
180
  it "should work fine through relations" do
175
- @user.extras.create(:key => :one)
181
+ subject.extras.create(:key => :one)
176
182
  UserExtra.first.key.should eql(:one)
177
183
  end
178
184
 
179
185
  it "should play fine with null db columns" do
180
- new_extra = @user.extras.build
186
+ new_extra = subject.extras.build
181
187
  new_extra.should_not be_valid
182
188
  end
183
189
 
184
190
  it "should play fine with null db columns" do
185
- new_extra = @user.extras.build
191
+ new_extra = subject.extras.build
186
192
  new_extra.should_not be_valid
187
193
  end
188
194
 
189
195
  describe "i18n" do
190
196
 
191
197
  it "should test i18n ones" do
192
- @user.language_text.should eql("Português")
198
+ subject.language_text.should eql("Português")
193
199
  end
194
200
 
195
201
  it "should get the correct values" do
@@ -201,9 +207,9 @@ describe "Symbolize" do
201
207
  end
202
208
 
203
209
  it "should test boolean" do
204
- @user.sex_text.should eql("Feminino")
205
- @user.sex = false
206
- @user.sex_text.should eql('Masculino')
210
+ subject.sex_text.should eql("Feminino")
211
+ subject.sex = false
212
+ subject.sex_text.should eql('Masculino')
207
213
  end
208
214
 
209
215
  it "should get the correct values" do
@@ -229,20 +235,20 @@ describe "Symbolize" do
229
235
  describe "Methods" do
230
236
 
231
237
  it "should play nice with other stuff" do
232
- @user.karma.should be_nil
238
+ subject.karma.should be_nil
233
239
  User::KARMA_VALUES.should eql({:bad => "bad", :ugly => "ugly", :good => "good"})
234
240
  end
235
241
 
236
242
  it "should provide a boolean method" do
237
- @user.should_not be_good
238
- @user.karma = :ugly
239
- @user.should be_ugly
243
+ subject.should_not be_good
244
+ subject.karma = :ugly
245
+ subject.should be_ugly
240
246
  end
241
247
 
242
248
  it "should work" do
243
- @user.karma = "good"
244
- @user.should be_good
245
- @user.should_not be_bad
249
+ subject.karma = "good"
250
+ subject.should be_good
251
+ subject.should_not be_bad
246
252
  end
247
253
 
248
254
  end
@@ -250,21 +256,21 @@ describe "Symbolize" do
250
256
  describe "Methods" do
251
257
 
252
258
  it "is dirty if you change the attribute value" do
253
- @user.language.should == :pt
254
- @user.language_changed?.should be_false
259
+ subject.language.should == :pt
260
+ subject.language_changed?.should be_false
255
261
 
256
- return_value = @user.language = :en
262
+ return_value = subject.language = :en
257
263
  return_value.should == :en
258
- @user.language_changed?.should be_true
264
+ subject.language_changed?.should be_true
259
265
  end
260
266
 
261
267
  it "is not dirty if you set the attribute value to the same value" do
262
- @user.language.should == :pt
263
- @user.language_changed?.should be_false
268
+ subject.language.should == :pt
269
+ subject.language_changed?.should be_false
264
270
 
265
- return_value = @user.language = :pt
271
+ return_value = subject.language = :pt
266
272
  return_value.should == :pt
267
- @user.language_changed?.should be_false
273
+ subject.language_changed?.should be_false
268
274
  end
269
275
 
270
276
  end
@@ -353,4 +359,56 @@ describe "Symbolize" do
353
359
  # end
354
360
 
355
361
  end
362
+
363
+
364
+ describe ": Default Value" do
365
+ before(:each) do
366
+ @user = User.new(:name => 'Anna', :other => :fo, :status => :active , :so => :linux, :gui => :qt, :language => :pt, :sex => true, :cool => true)
367
+ end
368
+
369
+ it "should be considered during validation" do
370
+ @user.valid?
371
+ @user.errors.full_messages.should == []
372
+ end
373
+
374
+ it "should be taken from the DB schema definition" do
375
+ @user.country.should == :pt
376
+ @user.country_text.should == "Pt"
377
+ end
378
+
379
+ it "should be applied to new, just saved, and reloaded objects, and also play fine with :methods option" do
380
+ @user.role.should == :reader
381
+ @user.role_text.should == "reader"
382
+ @user.should be_reader
383
+ @user.save!
384
+ @user.role.should == :reader
385
+ @user.should be_reader
386
+ @user.reload
387
+ @user.role.should == :reader
388
+ @user.should be_reader
389
+ end
390
+
391
+ it "should be overridable" do
392
+ @user.role = :writer
393
+ @user.role.should == :writer
394
+ @user.should be_writer
395
+ @user.save!
396
+ @user.role.should == :writer
397
+ @user.should be_writer
398
+ @user.reload
399
+ @user.role.should == :writer
400
+ @user.should be_writer
401
+ end
402
+
403
+ # This feature is for the next major version (b/o the compatibility problem)
404
+ pending "should detect name collision caused by ':methods => true' option" do
405
+ lambda {
406
+ User.class_eval do
407
+ # 'reader?' method is already defined, so the line below should raise an error
408
+ symbolize :some_attr, :in => [:reader, :guest], :methods => true
409
+ end
410
+ }.should raise_error(ArgumentError)
411
+ end
412
+
413
+ end
356
414
  end
@@ -9,8 +9,9 @@ class Person
9
9
  include Mongoid::Timestamps
10
10
 
11
11
  symbolize :other, :i18n => false
12
+
12
13
  symbolize :language, :in => [:pt, :en]
13
- symbolize :sex, :type => Boolean, :scopes => true
14
+ symbolize :sex, :type => Boolean, :scopes => true, :i18n => true
14
15
  symbolize :status , :in => [:active, :inactive], :i18n => false, :capitalize => true, :scopes => true
15
16
  symbolize :so, :allow_blank => true, :in => {
16
17
  :linux => 'Linux',
@@ -131,6 +132,10 @@ describe "Symbolize" do
131
132
  Person::STATUS_VALUES.should eql({ inactive: "Inactive", active: "Active"})
132
133
  end
133
134
 
135
+ it "should get the values for RailsAdmin" do
136
+ Person.status_enum.should eql([["Active", :active],["Inactive", :inactive]])
137
+ end
138
+
134
139
  it "should have a human _text method" do
135
140
  person.status_text.should eql("Active")
136
141
  end
@@ -249,6 +254,15 @@ describe "Symbolize" do
249
254
  skill.kind_text.should be_nil
250
255
  end
251
256
 
257
+ it "should return the proper 'false' i18n if the attr value is false" do
258
+ person = Person.new(:sex => false)
259
+ person.sex_text.should == "Masculino"
260
+ end
261
+
262
+ it "should use i18n if i18n => true" do
263
+ person.sex_text.should eql("Feminino")
264
+ end
265
+
252
266
  end
253
267
 
254
268
  describe "Methods" do
@@ -276,10 +290,6 @@ describe "Symbolize" do
276
290
 
277
291
  describe "more tests on Right" do
278
292
 
279
- it "should use default value on object build" do
280
- Right.new.kind.should eql(:perm)
281
- end
282
-
283
293
  it "should not interfer on create" do
284
294
  Right.create!(:name => "p7", :kind => :temp)
285
295
  Right.where(name: "p7").first.kind.should eql(:temp)
@@ -293,10 +303,11 @@ describe "Symbolize" do
293
303
 
294
304
  it "should work on create" do
295
305
  Right.create(:name => "p8")
296
- Right.first(conditions: { name: "p8" }).kind.should eql(:perm)
306
+ Right.find_by(name: "p8").kind.should eql(:perm)
297
307
  end
298
308
 
299
309
  it "should work on edit" do
310
+ Right.create(:name => "p8")
300
311
  pm = Right.where(name: "p8").first
301
312
  pm.kind = :temp
302
313
  pm.save
@@ -305,6 +316,19 @@ describe "Symbolize" do
305
316
 
306
317
  end
307
318
 
319
+ describe "Default Values" do
320
+
321
+ it "should use default value on object build" do
322
+ Right.new.kind.should eql(:perm)
323
+ end
324
+
325
+ it "should use default value in string" do
326
+ Project.new.state.should eql('active')
327
+ end
328
+
329
+ end
330
+
331
+
308
332
  describe "Scopes" do
309
333
  it "should work under scope" do
310
334
  # Person.with_scope({ :status => :inactive }) do
@@ -330,7 +354,9 @@ describe "Symbolize" do
330
354
  end
331
355
 
332
356
  describe "dirty tracking / changed flag" do
357
+
333
358
  before do
359
+ Person.create!(:name => 'Anna', :other => :fo, :status => :active , :so => :linux, :gui => :qt, :language => :pt, :sex => true, :cool => true)
334
360
  @anna = Person.where(name: 'Anna').first
335
361
  end
336
362
 
@@ -351,10 +377,9 @@ describe "Symbolize" do
351
377
  return_value.should == :pt
352
378
  @anna.language_changed?.should be_false
353
379
  end
354
- end
355
380
 
381
+ end
356
382
 
357
383
  end
358
384
 
359
385
  end
360
-
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.2.0
4
+ version: 4.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-01-31 00:00:00.000000000 Z
12
+ date: 2013-02-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: i18n
16
- requirement: &16099800 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,15 @@ dependencies:
21
21
  version: 0.6.0
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *16099800
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 0.6.0
25
30
  - !ruby/object:Gem::Dependency
26
31
  name: rspec
27
- requirement: &16097600 !ruby/object:Gem::Requirement
32
+ requirement: !ruby/object:Gem::Requirement
28
33
  none: false
29
34
  requirements:
30
35
  - - ~>
@@ -32,21 +37,31 @@ dependencies:
32
37
  version: 2.8.0
33
38
  type: :development
34
39
  prerelease: false
35
- version_requirements: *16097600
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 2.8.0
36
46
  - !ruby/object:Gem::Dependency
37
47
  name: mongoid
38
- requirement: &16110420 !ruby/object:Gem::Requirement
48
+ requirement: !ruby/object:Gem::Requirement
39
49
  none: false
40
50
  requirements:
41
51
  - - ~>
42
52
  - !ruby/object:Gem::Version
43
- version: 2.3.0
53
+ version: 3.1.0
44
54
  type: :development
45
55
  prerelease: false
46
- version_requirements: *16110420
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 3.1.0
47
62
  - !ruby/object:Gem::Dependency
48
63
  name: bson_ext
49
- requirement: &16107460 !ruby/object:Gem::Requirement
64
+ requirement: !ruby/object:Gem::Requirement
50
65
  none: false
51
66
  requirements:
52
67
  - - ~>
@@ -54,10 +69,15 @@ dependencies:
54
69
  version: 1.5.0
55
70
  type: :development
56
71
  prerelease: false
57
- version_requirements: *16107460
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 1.5.0
58
78
  - !ruby/object:Gem::Dependency
59
79
  name: sqlite3
60
- requirement: &16106020 !ruby/object:Gem::Requirement
80
+ requirement: !ruby/object:Gem::Requirement
61
81
  none: false
62
82
  requirements:
63
83
  - - ~>
@@ -65,10 +85,15 @@ dependencies:
65
85
  version: 1.3.0
66
86
  type: :development
67
87
  prerelease: false
68
- version_requirements: *16106020
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ version: 1.3.0
69
94
  - !ruby/object:Gem::Dependency
70
95
  name: pg
71
- requirement: &16121220 !ruby/object:Gem::Requirement
96
+ requirement: !ruby/object:Gem::Requirement
72
97
  none: false
73
98
  requirements:
74
99
  - - ~>
@@ -76,18 +101,28 @@ dependencies:
76
101
  version: 0.12.2
77
102
  type: :development
78
103
  prerelease: false
79
- version_requirements: *16121220
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ~>
108
+ - !ruby/object:Gem::Version
109
+ version: 0.12.2
80
110
  - !ruby/object:Gem::Dependency
81
111
  name: activerecord
82
- requirement: &16119380 !ruby/object:Gem::Requirement
112
+ requirement: !ruby/object:Gem::Requirement
83
113
  none: false
84
114
  requirements:
85
- - - ~>
115
+ - - ! '>='
86
116
  - !ruby/object:Gem::Version
87
- version: 3.1.0
117
+ version: 3.2.1
88
118
  type: :development
89
119
  prerelease: false
90
- version_requirements: *16119380
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: 3.2.1
91
126
  description: ActiveRecord/Mongoid enums with i18n
92
127
  email: x@nofxx.com
93
128
  executables: []
@@ -102,7 +137,6 @@ files:
102
137
  - spec/spec_helper_ar.rb
103
138
  - spec/locales/en.yml
104
139
  - spec/locales/pt.yml
105
- - spec/symbolize_spec.rb
106
140
  - spec/spec_helper_mongoid.rb
107
141
  - spec/spec_helper.rb
108
142
  - spec/symbolize/mongoid_spec.rb
@@ -124,7 +158,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
124
158
  version: '0'
125
159
  segments:
126
160
  - 0
127
- hash: 4056837590363536346
161
+ hash: -669350637973584292
128
162
  required_rubygems_version: !ruby/object:Gem::Requirement
129
163
  none: false
130
164
  requirements:
@@ -133,7 +167,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
133
167
  version: '0'
134
168
  requirements: []
135
169
  rubyforge_project:
136
- rubygems_version: 1.8.11
170
+ rubygems_version: 1.8.23
137
171
  signing_key:
138
172
  specification_version: 3
139
173
  summary: Object enums with i18n in AR or Mongoid
@@ -1,8 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
- require File.dirname(__FILE__) + '/spec_helper'
3
-
4
-
5
- describe "Symbolize" do
6
-
7
-
8
- end