activerecord-string-enum 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 119c5e3985161d99305ceaffd9626b89e3f53e9b
4
- data.tar.gz: c1ea2773a67525dd178650aba25b3253886f139a
3
+ metadata.gz: 265b860ca05e81e1cb56da1852509800557900a4
4
+ data.tar.gz: 7a67a8ca407f451eb76332e95a02653bdc6e7a90
5
5
  SHA512:
6
- metadata.gz: cfe8875234b49015f76bd59272f2dc94400ebdeddd9c34f828017463ec842a8a781e5dbb367dba15a179decc10fd10f25d0b60e69c64071a8954ccd00df34dc1
7
- data.tar.gz: 1d30978531fb961b3b6fd0fc6ee38dd7cefc1f99d21f57c49af114f7b567d4a0f4e929c2fd2fd840c1a92c34f112a5b185b5d31c9de8178457e74b232e5cbdbe
6
+ metadata.gz: f405376b1533f4269f80a3f57dd6eefb41a7d7ae63389501f767621bacaf3f106b5f4853d6d1f2962548426fbcfdb884c2a0fffe46f60b2be96b3ed8213f43c1
7
+ data.tar.gz: 8a4e0be3cb6f0bbbeec390ff840fe9885196d05f16c73fcfb41d4800d053642534d5192a3a5f8bee9e6ad01fe00923c0e56a987ae9e09eccc3f262d0deaff156
@@ -75,7 +75,7 @@ module ActiveRecord
75
75
  return if value.blank?
76
76
 
77
77
  if mapping.include?(value.to_s)
78
- value
78
+ value.to_sym
79
79
  else
80
80
  raise ArgumentError, "'#{value}' is not a valid #{name}"
81
81
  end
@@ -83,7 +83,7 @@ module ActiveRecord
83
83
 
84
84
  def deserialize(value)
85
85
  return if value.nil?
86
- cast(value)
86
+ value.to_sym
87
87
  end
88
88
 
89
89
  def serialize(value)
@@ -111,9 +111,13 @@ module ActiveRecord
111
111
 
112
112
  # TODO: in Rails 4.2.1 this will be legal:
113
113
  # attribute name, EnumType.new(name, enum_values)
114
- # instead of the next two lines:
114
+ # instead of the next lines:
115
115
  type = EnumType.new(name, enum_values)
116
- define_method("#{name}=") { |value| self[name] = type.cast(value) }
116
+ define_method("#{name}=") do |value|
117
+ write_attribute(name, type.cast(value))
118
+ end
119
+
120
+ define_method(name) { type.deserialize(self[name]) }
117
121
 
118
122
  _enum_methods_module.module_eval do
119
123
  enum_values.each do |value|
@@ -1,5 +1,5 @@
1
1
  module ActiveRecord
2
2
  module StringEnum
3
- VERSION = "0.0.1"
3
+ VERSION = "0.0.2"
4
4
  end
5
5
  end
@@ -18,8 +18,8 @@ class EnumTest < ActiveRecord::TestCase
18
18
  end
19
19
 
20
20
  test "query state with strings" do
21
- assert_equal "proposed", @book.status
22
- assert_equal "unread", @book.read_status
21
+ assert_equal :proposed, @book.status
22
+ assert_equal :unread, @book.read_status
23
23
  end
24
24
 
25
25
  test "find via scope" do
@@ -61,7 +61,7 @@ class EnumTest < ActiveRecord::TestCase
61
61
  test "enum changes" do
62
62
  old_status = @book.status
63
63
  @book.status = :published
64
- assert_equal [old_status, 'published'], @book.changes[:status]
64
+ assert_equal [old_status, :published], @book.changes[:status]
65
65
  end
66
66
 
67
67
  test "enum attribute was" do
@@ -77,7 +77,7 @@ class EnumTest < ActiveRecord::TestCase
77
77
 
78
78
  test "enum attribute changed to" do
79
79
  @book.status = :published
80
- assert @book.attribute_changed?(:status, to: 'published')
80
+ assert @book.attribute_changed?(:status, to: :published)
81
81
  end
82
82
 
83
83
  test "enum attribute changed from" do
@@ -89,7 +89,7 @@ class EnumTest < ActiveRecord::TestCase
89
89
  test "enum attribute changed from old status to new status" do
90
90
  old_status = @book.status
91
91
  @book.status = :published
92
- assert @book.attribute_changed?(:status, from: old_status, to: 'published')
92
+ assert @book.attribute_changed?(:status, from: old_status, to: :published)
93
93
  end
94
94
 
95
95
  test "enum didn't change" do
@@ -246,7 +246,7 @@ class EnumTest < ActiveRecord::TestCase
246
246
  extend ActiveRecord::StringEnum
247
247
  def self.name; 'Book'; end
248
248
  str_enum status: [:proposed, :written]
249
- validates_inclusion_of :status, in: ["written"]
249
+ validates_inclusion_of :status, in: [:written]
250
250
  end
251
251
  klass.delete_all
252
252
  invalid_book = klass.new(status: "proposed")
@@ -270,11 +270,11 @@ class EnumTest < ActiveRecord::TestCase
270
270
 
271
271
  book1 = klass1.proposed.create!
272
272
  book1.status = :written
273
- assert_equal ['proposed', 'written'], book1.status_change
273
+ assert_equal [:proposed, :written], book1.status_change
274
274
 
275
275
  book2 = klass2.drafted.create!
276
276
  book2.status = :uploaded
277
- assert_equal ['drafted', 'uploaded'], book2.status_change
277
+ assert_equal [:drafted, :uploaded], book2.status_change
278
278
  end
279
279
 
280
280
  test "enums are inheritable" do
@@ -286,10 +286,10 @@ class EnumTest < ActiveRecord::TestCase
286
286
 
287
287
  book1 = subklass1.proposed.create!
288
288
  book1.status = :written
289
- assert_equal ['proposed', 'written'], book1.status_change
289
+ assert_equal [:proposed, :written], book1.status_change
290
290
 
291
291
  book2 = subklass2.drafted.create!
292
292
  book2.status = :uploaded
293
- assert_equal ['drafted', 'uploaded'], book2.status_change
293
+ assert_equal [:drafted, :uploaded], book2.status_change
294
294
  end
295
295
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-string-enum
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tinco Andringa
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-16 00:00:00.000000000 Z
11
+ date: 2015-03-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -190,4 +190,3 @@ test_files:
190
190
  - test/support/connection_helper.rb
191
191
  - test/support/ddl_helper.rb
192
192
  - test/support/schema_dumping_helper.rb
193
- has_rdoc: