symbolize 4.4.1 → 4.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +4 -1
- data/Rakefile +3 -30
- data/lib/symbolize/active_record.rb +34 -6
- data/lib/symbolize/mongoid.rb +6 -7
- data/lib/symbolize/version.rb +1 -1
- data/spec/symbolize/active_record_spec.rb +168 -107
- data/spec/symbolize/mongoid_spec.rb +103 -87
- metadata +91 -21
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5fa03980e8f95cd5a9f9517d246364128972c3fd
|
4
|
+
data.tar.gz: 934d6ff3ef97dd046ed3d3d7f5241ee0bebf3bff
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a1b4cc6c15e0bfb030401926817481f578f4e37d9292066996b3bc42346f6023dfe32ca3a6fb0ebfa7ec9ed8b3d8c41d7e44bf0f9d189b027cb14f3d12e923ec
|
7
|
+
data.tar.gz: ad06350b50ecd3bcd63abbda8baa36ab34a2fecea34a10c8af86ce50e8421fe135ceda22fb693ac096beca821adf7fffb8b257e4cd678e57281fda95694dd708
|
data/README.md
CHANGED
@@ -143,6 +143,9 @@ If you don`t provide a hash with values, it will try i18n:
|
|
143
143
|
female: Girl
|
144
144
|
male: Boy
|
145
145
|
|
146
|
+
In some cases, if automatic lookup failes, you may want to try to translate it manully
|
147
|
+
|
148
|
+
user.get_gender_text # here gender is symbolized field
|
146
149
|
|
147
150
|
You can skip i18n lookup with :i18n => false
|
148
151
|
|
@@ -154,7 +157,7 @@ Scopes
|
|
154
157
|
|
155
158
|
With the ':scopes => true' option, you may filter/read/write easily:
|
156
159
|
|
157
|
-
User.
|
160
|
+
User.gender(:female).each ... # => User.where({ :gender => :female })
|
158
161
|
|
159
162
|
|
160
163
|
Now, if you provide the ':scopes => :shallow' option, fancy named scopes
|
data/Rakefile
CHANGED
@@ -1,34 +1,7 @@
|
|
1
|
-
require 'bundler'
|
2
|
-
Bundler.setup
|
1
|
+
require 'bundler/gem_tasks'
|
3
2
|
|
4
|
-
require
|
5
|
-
require "rspec/core/rake_task"
|
3
|
+
require 'rspec/core/rake_task'
|
6
4
|
|
7
|
-
|
8
|
-
require "symbolize/version"
|
9
|
-
|
10
|
-
desc "Builds the gem"
|
11
|
-
task :gem => :build
|
12
|
-
task :build do
|
13
|
-
system "gem build symbolize.gemspec"
|
14
|
-
Dir.mkdir("pkg") unless Dir.exists?("pkg")
|
15
|
-
system "mv symbolize-#{Symbolize::VERSION}.gem pkg/"
|
16
|
-
end
|
17
|
-
|
18
|
-
task :install => :build do
|
19
|
-
system "sudo gem install pkg/symbolize-#{Symbolize::VERSION}.gem"
|
20
|
-
end
|
21
|
-
|
22
|
-
desc "Release the gem - Gemcutter"
|
23
|
-
task :release => :build do
|
24
|
-
system "git tag -a v#{Symbolize::VERSION} -m 'Tagging #{Symbolize::VERSION}'"
|
25
|
-
system "git push --tags"
|
26
|
-
system "gem push pkg/symbolize-#{Symbolize::VERSION}.gem"
|
27
|
-
end
|
28
|
-
|
29
|
-
|
30
|
-
RSpec::Core::RakeTask.new(:spec) do |spec|
|
31
|
-
spec.pattern = "spec/**/*_spec.rb"
|
32
|
-
end
|
5
|
+
RSpec::Core::RakeTask.new
|
33
6
|
|
34
7
|
task :default => [:spec]
|
@@ -6,6 +6,12 @@ end
|
|
6
6
|
module Symbolize::ActiveRecord
|
7
7
|
extend ActiveSupport::Concern
|
8
8
|
|
9
|
+
included do
|
10
|
+
# Returns an array of all the attributes that have been specified for symbolization
|
11
|
+
class_attribute :symbolized_attributes, :instance_reader => false
|
12
|
+
self.symbolized_attributes = []
|
13
|
+
end
|
14
|
+
|
9
15
|
# Symbolize ActiveRecord attributes. Add
|
10
16
|
# symbolize :attr_name
|
11
17
|
# to your model class, to make an attribute return symbols instead of
|
@@ -149,16 +155,38 @@ module Symbolize::ActiveRecord
|
|
149
155
|
class_eval("def #{attr_name}_text; #{attr_name}.to_s; end")
|
150
156
|
end
|
151
157
|
end
|
158
|
+
|
159
|
+
# merge new symbolized attribute and create a new array to ensure that each class in inheritance hierarchy
|
160
|
+
# has its own array of symbolized attributes
|
161
|
+
self.symbolized_attributes += attr_names.map(&:to_s)
|
162
|
+
end
|
163
|
+
|
164
|
+
# Hook used by Rails to do extra stuff to attributes when they are initialized.
|
165
|
+
def initialize_attributes *args
|
166
|
+
super.tap do |attributes|
|
167
|
+
# Make sure any default values read from the database are symbolized
|
168
|
+
symbolized_attributes.each do |attr_name|
|
169
|
+
attributes[attr_name] = symbolize_attribute(attributes[attr_name])
|
170
|
+
end
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
174
|
+
# String becomes symbol, booleans string and nil nil.
|
175
|
+
def symbolize_attribute value
|
176
|
+
case value
|
177
|
+
when String
|
178
|
+
value.presence.try(:to_sym)
|
179
|
+
when Symbol, TrueClass, FalseClass, Numeric
|
180
|
+
value
|
181
|
+
else
|
182
|
+
nil
|
183
|
+
end
|
152
184
|
end
|
153
185
|
end
|
154
186
|
|
155
187
|
# String becomes symbol, booleans string and nil nil.
|
156
|
-
def symbolize_attribute
|
157
|
-
|
158
|
-
when String then attr.empty? ? nil : attr.to_sym
|
159
|
-
when Symbol, TrueClass, FalseClass, Numeric then attr
|
160
|
-
else nil
|
161
|
-
end
|
188
|
+
def symbolize_attribute value
|
189
|
+
self.class.symbolize_attribute value
|
162
190
|
end
|
163
191
|
|
164
192
|
# Return an attribute's value as a symbol or nil
|
data/lib/symbolize/mongoid.rb
CHANGED
@@ -62,7 +62,7 @@ module Mongoid
|
|
62
62
|
validation = configuration.delete(:validate) != false
|
63
63
|
field_type = configuration.delete :type
|
64
64
|
default_opt = configuration.delete :default
|
65
|
-
enum = [true, false] if
|
65
|
+
enum = [true, false] if [Boolean, ::Boolean].include?(field_type)
|
66
66
|
|
67
67
|
unless enum.nil?
|
68
68
|
|
@@ -72,7 +72,7 @@ module Mongoid
|
|
72
72
|
#
|
73
73
|
# Builds Mongoid 'field :name, type: type, :default'
|
74
74
|
#
|
75
|
-
const =
|
75
|
+
const = "#{attr_name}_values"
|
76
76
|
mongo_opts = ", :type => #{field_type || 'Symbol'}"
|
77
77
|
mongo_opts += ", :default => :#{default_opt}" if default_opt
|
78
78
|
class_eval("field :#{attr_name} #{mongo_opts}")
|
@@ -110,9 +110,8 @@ module Mongoid
|
|
110
110
|
if scopes
|
111
111
|
if scopes == :shallow
|
112
112
|
values.each do |k, v|
|
113
|
-
|
114
|
-
|
115
|
-
end
|
113
|
+
next unless k.respond_to?(:to_sym)
|
114
|
+
scope k.to_sym, -> { where(attr_name => k) }
|
116
115
|
end
|
117
116
|
else # scoped scopes
|
118
117
|
scope attr_name, ->(enum) { where(attr_name => enum) }
|
@@ -122,8 +121,8 @@ module Mongoid
|
|
122
121
|
if validation
|
123
122
|
v = "validates :#{attr_names.join(', :')}" +
|
124
123
|
",:inclusion => { :in => #{values.keys.inspect} }"
|
125
|
-
v +=
|
126
|
-
v +=
|
124
|
+
v += ',:allow_nil => true' if configuration[:allow_nil]
|
125
|
+
v += ',:allow_blank => true' if configuration[:allow_blank]
|
127
126
|
class_eval v
|
128
127
|
end
|
129
128
|
|
data/lib/symbolize/version.rb
CHANGED
@@ -38,31 +38,35 @@ class Permission < ActiveRecord::Base
|
|
38
38
|
symbolize :lvl, :in => (1..9).to_a, :i18n => false#, :default => 1
|
39
39
|
end
|
40
40
|
|
41
|
-
|
42
|
-
|
43
|
-
class << ActiveRecord::Base
|
44
|
-
public :with_scope
|
41
|
+
class PermissionSubclass < Permission
|
42
|
+
symbolize :sub_lvl
|
45
43
|
end
|
46
|
-
#end
|
47
|
-
|
48
44
|
|
49
45
|
describe "Symbolize" do
|
50
46
|
|
51
47
|
it "should respond to symbolize" do
|
52
|
-
ActiveRecord::Base.
|
48
|
+
expect(ActiveRecord::Base).to respond_to :symbolize
|
53
49
|
end
|
54
50
|
|
55
51
|
it "should have a valid blueprint" do
|
56
52
|
# Test records
|
57
53
|
u = User.create(:name => 'Bob' , :other => :bar,:status => :inactive, :so => :mac, :gui => :gtk, :language => :en, :sex => false, :cool => false)
|
58
|
-
u.errors.messages.
|
54
|
+
expect(u.errors.messages).to be_blank
|
59
55
|
end
|
60
56
|
|
61
57
|
it "should work nice with default values from active model" do
|
62
58
|
u = User.create(:name => 'Niu' , :other => :bar, :so => :mac, :gui => :gtk, :language => :en, :sex => false, :cool => false)
|
63
|
-
u.errors.messages.
|
64
|
-
u.status.
|
65
|
-
u.
|
59
|
+
expect(u.errors.messages).to be_blank
|
60
|
+
expect(u.status).to eql(:active)
|
61
|
+
expect(u).to be_active
|
62
|
+
end
|
63
|
+
|
64
|
+
describe ".symbolized_attributes" do
|
65
|
+
it "returns the symbolized attribute for the class" do
|
66
|
+
expect(UserExtra.symbolized_attributes).to eq ['key']
|
67
|
+
expect(Permission.symbolized_attributes).to match_array ['kind', 'lvl']
|
68
|
+
expect(PermissionSubclass.symbolized_attributes).to match_array ['kind', 'lvl', 'sub_lvl']
|
69
|
+
end
|
66
70
|
end
|
67
71
|
|
68
72
|
describe "User Instantiated" do
|
@@ -74,160 +78,200 @@ describe "Symbolize" do
|
|
74
78
|
|
75
79
|
describe "test_symbolize_string" do
|
76
80
|
let(:status) { 'inactive' }
|
77
|
-
|
81
|
+
|
82
|
+
describe '#status' do
|
83
|
+
subject { super().status }
|
84
|
+
it { is_expected.to eq(:inactive) }
|
85
|
+
end
|
78
86
|
# @user.status_before_type_cast.should eql(:inactive)
|
79
87
|
# @user.read_attribute(:status).should eql('inactive')
|
80
88
|
end
|
81
89
|
|
82
90
|
describe "test_symbolize_symbol" do
|
83
|
-
|
84
|
-
|
91
|
+
describe '#status' do
|
92
|
+
subject { super().status }
|
93
|
+
it { is_expected.to eq(:active) }
|
94
|
+
end
|
95
|
+
|
96
|
+
describe '#status_before_type_cast' do
|
97
|
+
subject { super().status_before_type_cast }
|
98
|
+
it { is_expected.to eq(:active) }
|
99
|
+
end
|
85
100
|
# @user.read_attribute(:status).should eql('active')
|
86
101
|
end
|
87
102
|
|
88
103
|
describe "should work nice with numbers" do
|
89
104
|
let(:status) { 43 }
|
90
|
-
|
105
|
+
|
106
|
+
describe '#status' do
|
107
|
+
subject { super().status }
|
108
|
+
it { is_expected.to be_present }
|
109
|
+
end
|
91
110
|
# @user.status_before_type_cast.should be_nil
|
92
111
|
# @user.read_attribute(:status).should be_nil
|
93
112
|
end
|
94
113
|
|
95
114
|
describe "should acts nice with nil" do
|
96
115
|
let(:status) { nil }
|
97
|
-
|
98
|
-
|
99
|
-
|
116
|
+
|
117
|
+
describe '#status' do
|
118
|
+
subject { super().status }
|
119
|
+
it { is_expected.to be_nil }
|
120
|
+
end
|
121
|
+
|
122
|
+
describe '#status_before_type_cast' do
|
123
|
+
subject { super().status_before_type_cast }
|
124
|
+
it { is_expected.to be_nil }
|
125
|
+
end
|
126
|
+
it { expect(subject.read_attribute(:status)).to be_nil }
|
100
127
|
end
|
101
128
|
|
102
129
|
describe "should acts nice with blank" do
|
103
130
|
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 }
|
107
|
-
end
|
108
131
|
|
109
|
-
|
110
|
-
|
132
|
+
describe '#status' do
|
133
|
+
subject { super().status }
|
134
|
+
it { is_expected.to be_nil }
|
135
|
+
end
|
136
|
+
|
137
|
+
describe '#status_before_type_cast' do
|
138
|
+
subject { super().status_before_type_cast }
|
139
|
+
it { is_expected.to be_nil }
|
140
|
+
end
|
141
|
+
it { expect(subject.read_attribute(:status)).to be_nil }
|
111
142
|
end
|
112
143
|
|
113
144
|
it "should not validates other" do
|
114
145
|
subject.other = nil
|
115
|
-
subject.
|
146
|
+
expect(subject).to be_valid
|
116
147
|
subject.other = ""
|
117
|
-
subject.
|
148
|
+
expect(subject).to be_valid
|
118
149
|
end
|
119
150
|
|
120
151
|
it "should get the correct values" do
|
121
|
-
User.get_status_values.
|
122
|
-
User::STATUS_VALUES.
|
152
|
+
expect(User.get_status_values).to eql([["Active", :active],["Inactive", :inactive]])
|
153
|
+
expect(User::STATUS_VALUES).to eql({:inactive=>"Inactive", :active=>"Active"})
|
123
154
|
end
|
124
155
|
|
125
156
|
it "should get the values for RailsAdmin" do
|
126
|
-
User.status_enum.
|
157
|
+
expect(User.status_enum).to eql([["Active", :active],["Inactive", :inactive]])
|
127
158
|
end
|
128
159
|
|
129
160
|
describe "test_symbolize_humanize" do
|
130
|
-
|
161
|
+
describe '#status_text' do
|
162
|
+
subject { super().status_text }
|
163
|
+
it { is_expected.to eql("Active") }
|
164
|
+
end
|
131
165
|
end
|
132
166
|
|
133
167
|
it "should get the correct values" do
|
134
|
-
User.get_gui_values.
|
135
|
-
User::GUI_VALUES.
|
168
|
+
expect(User.get_gui_values).to match_array([["cocoa", :cocoa], ["qt", :qt], ["gtk", :gtk]])
|
169
|
+
expect(User::GUI_VALUES).to eql({:cocoa=>"cocoa", :qt=>"qt", :gtk=>"gtk"})
|
136
170
|
end
|
137
171
|
|
138
172
|
describe "test_symbolize_humanize" do
|
139
|
-
|
173
|
+
describe '#gui_text' do
|
174
|
+
subject { super().gui_text }
|
175
|
+
it { is_expected.to eql("qt") }
|
176
|
+
end
|
140
177
|
end
|
141
178
|
|
142
179
|
it "should get the correct values" do
|
143
|
-
User.get_so_values.
|
144
|
-
User::SO_VALUES.
|
180
|
+
expect(User.get_so_values).to match_array([["Linux", :linux], ["Mac OS X", :mac], ["Videogame", :win]])
|
181
|
+
expect(User::SO_VALUES).to eql({:linux => "Linux", :mac => "Mac OS X", :win => "Videogame"})
|
145
182
|
end
|
146
183
|
|
147
184
|
describe "test_symbolize_humanize" do
|
148
|
-
|
185
|
+
describe '#so_text' do
|
186
|
+
subject { super().so_text }
|
187
|
+
it { is_expected.to eql("Linux") }
|
188
|
+
end
|
149
189
|
end
|
150
190
|
|
151
191
|
describe "test_symbolize_humanize" do
|
152
192
|
let(:so) { :mac }
|
153
|
-
|
193
|
+
|
194
|
+
describe '#so_text' do
|
195
|
+
subject { super().so_text }
|
196
|
+
it { is_expected.to eql("Mac OS X") }
|
197
|
+
end
|
154
198
|
end
|
155
199
|
|
156
200
|
it "should stringify" do
|
157
|
-
subject.other_text.
|
201
|
+
expect(subject.other_text).to eql("fo")
|
158
202
|
subject.other = :foo
|
159
|
-
subject.other_text.
|
203
|
+
expect(subject.other_text).to eql("foo")
|
160
204
|
end
|
161
205
|
|
162
206
|
describe "should validate status" do
|
163
207
|
let(:status) { nil }
|
164
|
-
it {
|
165
|
-
it
|
208
|
+
it { is_expected.not_to be_valid }
|
209
|
+
it 'has 1 error' do
|
210
|
+
expect(subject.errors.size).to eq(1)
|
211
|
+
end
|
166
212
|
end
|
167
213
|
|
168
214
|
it "should not validate so" do
|
169
215
|
subject.so = nil
|
170
|
-
subject.
|
216
|
+
expect(subject).to be_valid
|
171
217
|
end
|
172
218
|
|
173
219
|
it "test_symbols_with_weird_chars_quoted_id" do
|
174
220
|
subject.status = :"weird'; chars"
|
175
|
-
subject.status_before_type_cast.
|
176
|
-
# assert_equal "weird'; chars", @user.read_attribute(:status)
|
177
|
-
# assert_equal "'weird''; chars'", @user.status.quoted_id
|
221
|
+
expect(subject.status_before_type_cast).to eql(:"weird'; chars")
|
178
222
|
end
|
179
223
|
|
180
224
|
it "should work fine through relations" do
|
181
225
|
subject.extras.create(:key => :one)
|
182
|
-
UserExtra.first.key.
|
226
|
+
expect(UserExtra.first.key).to eql(:one)
|
183
227
|
end
|
184
228
|
|
185
229
|
it "should play fine with null db columns" do
|
186
230
|
new_extra = subject.extras.build
|
187
|
-
new_extra.
|
231
|
+
expect(new_extra).not_to be_valid
|
188
232
|
end
|
189
233
|
|
190
234
|
it "should play fine with null db columns" do
|
191
235
|
new_extra = subject.extras.build
|
192
|
-
new_extra.
|
236
|
+
expect(new_extra).not_to be_valid
|
193
237
|
end
|
194
238
|
|
195
239
|
describe "i18n" do
|
196
240
|
|
197
241
|
it "should test i18n ones" do
|
198
|
-
subject.language_text.
|
242
|
+
expect(subject.language_text).to eql("Português")
|
199
243
|
end
|
200
244
|
|
201
245
|
it "should get the correct values" do
|
202
|
-
User.get_language_values.
|
246
|
+
expect(User.get_language_values).to match_array([["Português", :pt], ["Inglês", :en]])
|
203
247
|
end
|
204
248
|
|
205
249
|
it "should get the correct values" do
|
206
|
-
User::LANGUAGE_VALUES.
|
250
|
+
expect(User::LANGUAGE_VALUES).to eql({:pt=>"pt", :en=>"en"})
|
207
251
|
end
|
208
252
|
|
209
253
|
it "should test boolean" do
|
210
|
-
subject.sex_text.
|
254
|
+
expect(subject.sex_text).to eql("Feminino")
|
211
255
|
subject.sex = false
|
212
|
-
subject.sex_text.
|
256
|
+
expect(subject.sex_text).to eql('Masculino')
|
213
257
|
end
|
214
258
|
|
215
259
|
it "should get the correct values" do
|
216
|
-
User.get_sex_values.
|
260
|
+
expect(User.get_sex_values).to eql([["Feminino", true],["Masculino", false]])
|
217
261
|
end
|
218
262
|
|
219
263
|
it "should get the correct values" do
|
220
|
-
User::SEX_VALUES.
|
264
|
+
expect(User::SEX_VALUES).to eql({true=>"true", false=>"false"})
|
221
265
|
end
|
222
266
|
|
223
267
|
it "should translate a multiword class" do
|
224
268
|
@skill = UserSkill.create(:kind => :magic)
|
225
|
-
@skill.kind_text.
|
269
|
+
expect(@skill.kind_text).to eql("Mágica")
|
226
270
|
end
|
227
271
|
|
228
272
|
it "should return nil if there's no value" do
|
229
273
|
@skill = UserSkill.create(:kind => nil)
|
230
|
-
@skill.kind_text.
|
274
|
+
expect(@skill.kind_text).to be_nil
|
231
275
|
end
|
232
276
|
|
233
277
|
end
|
@@ -235,20 +279,20 @@ describe "Symbolize" do
|
|
235
279
|
describe "Methods" do
|
236
280
|
|
237
281
|
it "should play nice with other stuff" do
|
238
|
-
subject.karma.
|
239
|
-
User::KARMA_VALUES.
|
282
|
+
expect(subject.karma).to be_nil
|
283
|
+
expect(User::KARMA_VALUES).to eql({:bad => "bad", :ugly => "ugly", :good => "good"})
|
240
284
|
end
|
241
285
|
|
242
286
|
it "should provide a boolean method" do
|
243
|
-
subject.
|
287
|
+
expect(subject).not_to be_good
|
244
288
|
subject.karma = :ugly
|
245
|
-
subject.
|
289
|
+
expect(subject).to be_ugly
|
246
290
|
end
|
247
291
|
|
248
292
|
it "should work" do
|
249
293
|
subject.karma = "good"
|
250
|
-
subject.
|
251
|
-
subject.
|
294
|
+
expect(subject).to be_good
|
295
|
+
expect(subject).not_to be_bad
|
252
296
|
end
|
253
297
|
|
254
298
|
end
|
@@ -256,31 +300,48 @@ describe "Symbolize" do
|
|
256
300
|
describe "Changes" do
|
257
301
|
|
258
302
|
it "is dirty if you change the attribute value" do
|
259
|
-
subject.language.
|
260
|
-
subject.language_changed
|
303
|
+
expect(subject.language).to eq(:pt)
|
304
|
+
expect(subject.language_changed?).to be false
|
261
305
|
|
262
306
|
return_value = subject.language = :en
|
263
|
-
return_value.
|
264
|
-
subject.language_changed
|
307
|
+
expect(return_value).to eq(:en)
|
308
|
+
expect(subject.language_changed?).to be true
|
265
309
|
end
|
266
310
|
|
267
311
|
it "is not dirty if you set the attribute value to the same value" do
|
268
|
-
subject.language.
|
269
|
-
subject.language_changed
|
312
|
+
expect(subject.language).to eq(:pt)
|
313
|
+
expect(subject.language_changed?).to be false
|
270
314
|
|
271
315
|
return_value = subject.language = :pt
|
272
|
-
return_value.
|
273
|
-
subject.language_changed
|
316
|
+
expect(return_value).to eq(:pt)
|
317
|
+
expect(subject.language_changed?).to be false
|
274
318
|
end
|
275
319
|
|
276
320
|
it "is not dirty if you set the attribute value to the same value (string)" do
|
277
|
-
subject.language.
|
278
|
-
subject.language_changed
|
321
|
+
expect(subject.language).to eq(:pt)
|
322
|
+
expect(subject.language_changed?).to be false
|
279
323
|
|
280
324
|
return_value = subject.language = 'pt'
|
281
|
-
subject.language_changed
|
325
|
+
expect(subject.language_changed?).to be false
|
282
326
|
end
|
283
327
|
|
328
|
+
it "is not dirty if you set the default attribute value to the same value" do
|
329
|
+
user = User.create!(:language => :pt, :sex => true, :cool => true)
|
330
|
+
expect(user.status).to eq(:active)
|
331
|
+
expect(user).not_to be_changed
|
332
|
+
|
333
|
+
user.status = :active
|
334
|
+
expect(user).not_to be_changed
|
335
|
+
end
|
336
|
+
|
337
|
+
it "is not dirty if you set the default attribute value to the same value (string)" do
|
338
|
+
user = User.create!(:language => :pt, :sex => true, :cool => true)
|
339
|
+
expect(user.status).to eq(:active)
|
340
|
+
expect(user).not_to be_changed
|
341
|
+
|
342
|
+
user.status = 'active'
|
343
|
+
expect(user).not_to be_changed
|
344
|
+
end
|
284
345
|
end
|
285
346
|
|
286
347
|
end
|
@@ -288,23 +349,23 @@ describe "Symbolize" do
|
|
288
349
|
describe "more tests on Permission" do
|
289
350
|
|
290
351
|
it "should use default value on object build" do
|
291
|
-
Permission.new.kind.
|
352
|
+
expect(Permission.new.kind).to eql(:perm)
|
292
353
|
end
|
293
354
|
|
294
355
|
it "should not interfer on create" do
|
295
356
|
Permission.create!(:name => "p7", :kind =>:temp, :lvl => 7)
|
296
|
-
Permission.find_by_name("p7").kind.
|
357
|
+
expect(Permission.find_by_name("p7").kind).to eql(:temp)
|
297
358
|
end
|
298
359
|
|
299
360
|
it "should work on create" do
|
300
361
|
pm = Permission.new(:name => "p7", :lvl => 7)
|
301
|
-
pm.
|
302
|
-
pm.save.
|
362
|
+
expect(pm).to be_valid
|
363
|
+
expect(pm.save).to be true
|
303
364
|
end
|
304
365
|
|
305
366
|
it "should work on create" do
|
306
367
|
Permission.create!(:name => "p8", :lvl => 9)
|
307
|
-
Permission.find_by_name("p8").kind.
|
368
|
+
expect(Permission.find_by_name("p8").kind).to eql(:perm)
|
308
369
|
end
|
309
370
|
|
310
371
|
it "should work on edit" do
|
@@ -312,14 +373,14 @@ describe "Symbolize" do
|
|
312
373
|
pm = Permission.find_by_name("p8")
|
313
374
|
pm.kind = :temp
|
314
375
|
pm.save
|
315
|
-
Permission.find_by_name("p8").kind.
|
376
|
+
expect(Permission.find_by_name("p8").kind).to eql(:temp)
|
316
377
|
end
|
317
378
|
|
318
379
|
it "should work with default values" do
|
319
380
|
pm = Permission.new(:name => "p9")
|
320
381
|
pm.lvl = 9
|
321
382
|
pm.save
|
322
|
-
Permission.find_by_name("p9").lvl.to_i.
|
383
|
+
expect(Permission.find_by_name("p9").lvl.to_i).to eql(9)
|
323
384
|
end
|
324
385
|
|
325
386
|
end
|
@@ -332,22 +393,21 @@ describe "Symbolize" do
|
|
332
393
|
end
|
333
394
|
|
334
395
|
it "test_symbolized_finder" do
|
335
|
-
User.where({ :status => :inactive }).all.map(&:name).
|
336
|
-
User.find_all_by_status(:inactive).map(&:name).should eql(['Mary'])
|
396
|
+
expect(User.where({ :status => :inactive }).all.map(&:name)).to eql(['Mary'])
|
337
397
|
end
|
338
398
|
|
339
|
-
it "
|
340
|
-
User.
|
341
|
-
User.all.map(&:name).
|
399
|
+
it "test_symbolized_scoping" do
|
400
|
+
User.where({ :status => :inactive }).scoping do
|
401
|
+
expect(User.all.map(&:name)).to eql(['Mary'])
|
342
402
|
end
|
343
403
|
end
|
344
404
|
|
345
405
|
it "should have main named scope" do
|
346
|
-
User.inactive.
|
406
|
+
expect(User.inactive).to eq([@mary])
|
347
407
|
end
|
348
408
|
|
349
409
|
it "should have other to test better" do
|
350
|
-
User.so(:linux).
|
410
|
+
expect(User.so(:linux)).to eq([@anna])
|
351
411
|
end
|
352
412
|
|
353
413
|
# it "should have 'with' helper" do
|
@@ -376,46 +436,47 @@ describe "Symbolize" do
|
|
376
436
|
|
377
437
|
it "should be considered during validation" do
|
378
438
|
@user.valid?
|
379
|
-
@user.errors.full_messages.
|
439
|
+
expect(@user.errors.full_messages).to eq([])
|
380
440
|
end
|
381
441
|
|
382
442
|
it "should be taken from the DB schema definition" do
|
383
|
-
@user.country.
|
384
|
-
@user.country_text.
|
443
|
+
expect(@user.country).to eq(:pt)
|
444
|
+
expect(@user.country_text).to eq("Pt")
|
385
445
|
end
|
386
446
|
|
387
447
|
it "should be applied to new, just saved, and reloaded objects, and also play fine with :methods option" do
|
388
|
-
@user.role.
|
389
|
-
@user.role_text.
|
390
|
-
@user.
|
448
|
+
expect(@user.role).to eq(:reader)
|
449
|
+
expect(@user.role_text).to eq("reader")
|
450
|
+
expect(@user).to be_reader
|
391
451
|
@user.save!
|
392
|
-
@user.role.
|
393
|
-
@user.
|
452
|
+
expect(@user.role).to eq(:reader)
|
453
|
+
expect(@user).to be_reader
|
394
454
|
@user.reload
|
395
|
-
@user.role.
|
396
|
-
@user.
|
455
|
+
expect(@user.role).to eq(:reader)
|
456
|
+
expect(@user).to be_reader
|
397
457
|
end
|
398
458
|
|
399
459
|
it "should be overridable" do
|
400
460
|
@user.role = :writer
|
401
|
-
@user.role.
|
402
|
-
@user.
|
461
|
+
expect(@user.role).to eq(:writer)
|
462
|
+
expect(@user).to be_writer
|
403
463
|
@user.save!
|
404
|
-
@user.role.
|
405
|
-
@user.
|
464
|
+
expect(@user.role).to eq(:writer)
|
465
|
+
expect(@user).to be_writer
|
406
466
|
@user.reload
|
407
|
-
@user.role.
|
408
|
-
@user.
|
467
|
+
expect(@user.role).to eq(:writer)
|
468
|
+
expect(@user).to be_writer
|
409
469
|
end
|
410
470
|
|
411
471
|
# This feature is for the next major version (b/o the compatibility problem)
|
412
|
-
|
413
|
-
|
472
|
+
it "should detect name collision caused by ':methods => true' option" do
|
473
|
+
pending 'next major version'
|
474
|
+
expect {
|
414
475
|
User.class_eval do
|
415
476
|
# 'reader?' method is already defined, so the line below should raise an error
|
416
477
|
symbolize :some_attr, :in => [:reader, :guest], :methods => true
|
417
478
|
end
|
418
|
-
}.
|
479
|
+
}.to raise_error(ArgumentError)
|
419
480
|
end
|
420
481
|
|
421
482
|
end
|