symbolize 3.1.3 → 3.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/VERSION +1 -1
- data/lib/symbolize.rb +14 -3
- data/spec/symbolize_spec.rb +52 -4
- data/symbolize.gemspec +2 -7
- metadata +6 -8
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
3.
|
1
|
+
3.2.0
|
data/lib/symbolize.rb
CHANGED
@@ -60,6 +60,7 @@ module Symbolize
|
|
60
60
|
default_option = configuration.delete :default
|
61
61
|
|
62
62
|
unless enum.nil?
|
63
|
+
# enum = enum.map { |e| e.respond_to?(:to_sym) ? e.to_sym : e}
|
63
64
|
# Little monkeypatching, <1.8 Hashes aren't ordered.
|
64
65
|
hsh = RUBY_VERSION > '1.9' || !defined?("ActiveSupport") ? Hash : ActiveSupport::OrderedHash
|
65
66
|
|
@@ -70,7 +71,10 @@ module Symbolize
|
|
70
71
|
values = enum
|
71
72
|
else
|
72
73
|
values = hsh.new
|
73
|
-
enum.map
|
74
|
+
enum.map do |val|
|
75
|
+
key = val.respond_to?(:to_sym) ? val.to_sym : val
|
76
|
+
values[key] = capitalize ? val.to_s.capitalize : val.to_s
|
77
|
+
end
|
74
78
|
end
|
75
79
|
|
76
80
|
# Get the values of :in
|
@@ -119,7 +123,7 @@ module Symbolize
|
|
119
123
|
|
120
124
|
if default_option
|
121
125
|
class_eval("def #{attr_name}; read_and_symbolize_attribute('#{attr_name}') || :#{default_option}; end")
|
122
|
-
class_eval("def #{attr_name}= (value); write_symbolized_attribute('#{attr_name}',
|
126
|
+
class_eval("def #{attr_name}= (value); write_symbolized_attribute('#{attr_name}', value); end")
|
123
127
|
class_eval("def set_default_for_attr_#{attr_name}; self[:#{attr_name}] ||= :#{default_option}; end")
|
124
128
|
class_eval("before_save :set_default_for_attr_#{attr_name}")
|
125
129
|
else
|
@@ -161,7 +165,14 @@ module Symbolize
|
|
161
165
|
def write_symbolized_attribute attr_name, value
|
162
166
|
val = { "true" => true, "false" => false }[value]
|
163
167
|
val = symbolize_attribute(value) if val.nil?
|
164
|
-
|
168
|
+
|
169
|
+
current_value = self.send(attr_name)
|
170
|
+
if current_value == val
|
171
|
+
current_value
|
172
|
+
else
|
173
|
+
self[attr_name] = val
|
174
|
+
val
|
175
|
+
end
|
165
176
|
end
|
166
177
|
end
|
167
178
|
|
data/spec/symbolize_spec.rb
CHANGED
@@ -14,7 +14,7 @@ class User < ActiveRecord::Base
|
|
14
14
|
:win => 'Videogame'
|
15
15
|
}, :scopes => true
|
16
16
|
symbolize :gui, :allow_blank => true, :in => [:cocoa, :qt, :gtk], :i18n => false
|
17
|
-
symbolize :karma, :in =>
|
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
20
|
has_many :extras, :dependent => :destroy, :class_name => "UserExtra"
|
@@ -44,7 +44,7 @@ end
|
|
44
44
|
|
45
45
|
# Test records
|
46
46
|
User.create(:name => 'Anna', :other => :fo, :status => :active , :so => :linux, :gui => :qt, :language => :pt, :sex => true, :cool => true)
|
47
|
-
User.create(:name => 'Bob' , :other => :bar,:status => :inactive, :so => :mac, :gui => :gtk, :language => :en, :sex => false, :cool => false)
|
47
|
+
User.create!(:name => 'Bob' , :other => :bar,:status => :inactive, :so => :mac, :gui => :gtk, :language => :en, :sex => false, :cool => false)
|
48
48
|
|
49
49
|
|
50
50
|
describe "Symbolize" do
|
@@ -68,7 +68,7 @@ describe "Symbolize" do
|
|
68
68
|
it "test_symbolize_symbol" do
|
69
69
|
@user.status = :active
|
70
70
|
@user.status.should eql(:active)
|
71
|
-
@user.status_before_type_cast.should eql(
|
71
|
+
@user.status_before_type_cast.should eql('active')
|
72
72
|
# @user.read_attribute(:status).should eql('active')
|
73
73
|
end
|
74
74
|
|
@@ -317,7 +317,7 @@ describe "Symbolize" do
|
|
317
317
|
#
|
318
318
|
# ActiveRecord <= 2
|
319
319
|
#
|
320
|
-
if ActiveRecord::VERSION::MAJOR
|
320
|
+
if ActiveRecord::VERSION::MAJOR <= 2
|
321
321
|
|
322
322
|
it "test_symbolized_finder" do
|
323
323
|
User.find(:all, :conditions => { :status => :inactive }).map(&:name).should eql(['Bob'])
|
@@ -330,6 +330,30 @@ describe "Symbolize" do
|
|
330
330
|
end
|
331
331
|
end
|
332
332
|
|
333
|
+
describe "dirty tracking / changed flag" do
|
334
|
+
before do
|
335
|
+
@anna = User.find_by_name!('Anna')
|
336
|
+
end
|
337
|
+
|
338
|
+
it "is dirty if you change the attribute value" do
|
339
|
+
@anna.language.should == :pt
|
340
|
+
@anna.language_changed?.should be_false
|
341
|
+
|
342
|
+
return_value = @anna.language = :en
|
343
|
+
return_value.should == :en
|
344
|
+
@anna.language_changed?.should be_true
|
345
|
+
end
|
346
|
+
|
347
|
+
it "is not dirty if you set the attribute value to the same value it was originally" do
|
348
|
+
@anna.language.should == :pt
|
349
|
+
@anna.language_changed?.should be_false
|
350
|
+
|
351
|
+
return_value = @anna.language = :pt
|
352
|
+
return_value.should == :pt
|
353
|
+
@anna.language_changed?.should be_false
|
354
|
+
end
|
355
|
+
end
|
356
|
+
|
333
357
|
#
|
334
358
|
# ActiveRecord >= 3
|
335
359
|
#
|
@@ -346,6 +370,30 @@ describe "Symbolize" do
|
|
346
370
|
end
|
347
371
|
end
|
348
372
|
|
373
|
+
describe "dirty tracking / changed flag" do
|
374
|
+
before do
|
375
|
+
@anna = User.find_by_name!('Anna')
|
376
|
+
end
|
377
|
+
|
378
|
+
it "is dirty if you change the attribute value" do
|
379
|
+
@anna.language.should == :pt
|
380
|
+
@anna.language_changed?.should be_false
|
381
|
+
|
382
|
+
return_value = @anna.language = :en
|
383
|
+
return_value.should == :en
|
384
|
+
@anna.language_changed?.should be_true
|
385
|
+
end
|
386
|
+
|
387
|
+
it "is not dirty if you set the attribute value to the same value it was originally" do
|
388
|
+
@anna.language.should == :pt
|
389
|
+
@anna.language_changed?.should be_false
|
390
|
+
|
391
|
+
return_value = @anna.language = :pt
|
392
|
+
return_value.should == :pt
|
393
|
+
@anna.language_changed?.should be_false
|
394
|
+
end
|
395
|
+
end
|
396
|
+
|
349
397
|
describe "Named Scopes" do
|
350
398
|
|
351
399
|
before 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 = "3.
|
8
|
+
s.version = "3.2.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-
|
12
|
+
s.date = %q{2011-05-26}
|
13
13
|
s.description = %q{ActiveRecord enums with i18n}
|
14
14
|
s.email = %q{x@nofxx.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -37,11 +37,6 @@ Gem::Specification.new do |s|
|
|
37
37
|
s.require_paths = ["lib"]
|
38
38
|
s.rubygems_version = %q{1.3.7}
|
39
39
|
s.summary = %q{ActiveRecord enums with i18n}
|
40
|
-
s.test_files = [
|
41
|
-
"spec/db/create_testing_structure.rb",
|
42
|
-
"spec/spec_helper.rb",
|
43
|
-
"spec/symbolize_spec.rb"
|
44
|
-
]
|
45
40
|
|
46
41
|
if s.respond_to? :specification_version then
|
47
42
|
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 3
|
7
|
-
-
|
8
|
-
-
|
9
|
-
version: 3.
|
7
|
+
- 2
|
8
|
+
- 0
|
9
|
+
version: 3.2.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Marcos Piccinini
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2011-
|
17
|
+
date: 2011-05-26 00:00:00 -03:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -100,7 +100,5 @@ rubygems_version: 1.3.7
|
|
100
100
|
signing_key:
|
101
101
|
specification_version: 3
|
102
102
|
summary: ActiveRecord enums with i18n
|
103
|
-
test_files:
|
104
|
-
|
105
|
-
- spec/spec_helper.rb
|
106
|
-
- spec/symbolize_spec.rb
|
103
|
+
test_files: []
|
104
|
+
|