mschuerig-enumerate_by 0.4.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,18 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+
3
+ class JSONSerializerTest < ActiveRecord::TestCase
4
+ def setup
5
+ @red = create_color(:name => 'red')
6
+ @car = create_car(:name => 'Ford Mustang', :color => @red)
7
+ @json = @car.to_json
8
+ end
9
+
10
+ def test_should_include_enumeration_in_json
11
+ assert_match %r{"color": "red"}, @json
12
+ end
13
+
14
+ def test_should_render_other_attributes
15
+ assert_match %r{"id": #{@car.id}}, @json
16
+ assert_match %r{"name": "Ford Mustang"}, @json
17
+ end
18
+ end
@@ -0,0 +1,166 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+
3
+ class SerializerByDefaultTest < ActiveRecord::TestCase
4
+ def setup
5
+ @red = create_color(:name => 'red')
6
+ @car = create_car(:name => 'Ford Mustang', :color => @red)
7
+ @serializer = ActiveRecord::Serialization::Serializer.new(@car)
8
+ end
9
+
10
+ def test_should_include_enumerations_in_serializable_attribute_names
11
+ assert_equal %w(color feature_id feature_type id name), @serializer.serializable_attribute_names
12
+ end
13
+
14
+ def test_should_typecast_serializable_record
15
+ expected = {
16
+ 'color' => 'red',
17
+ 'feature_id' => nil,
18
+ 'feature_type' => nil,
19
+ 'id' => @car.id,
20
+ 'name' => 'Ford Mustang'
21
+ }
22
+
23
+ assert_equal expected, @serializer.serializable_record
24
+ end
25
+ end
26
+
27
+ class SerializerWithoutEnumerationsTest < ActiveRecord::TestCase
28
+ def setup
29
+ @red = create_color(:name => 'red')
30
+ @car = create_car(:name => 'Ford Mustang', :color => @red)
31
+ @serializer = ActiveRecord::Serialization::Serializer.new(@car, :enumerations => false)
32
+ end
33
+
34
+ def test_should_not_include_enumerations_in_serializable_attribute_names
35
+ assert_equal %w(color_id feature_id feature_type id name), @serializer.serializable_attribute_names
36
+ end
37
+
38
+ def test_should_not_typecast_serializable_record
39
+ expected = {
40
+ 'color_id' => @red.id,
41
+ 'feature_id' => nil,
42
+ 'feature_type' => nil,
43
+ 'id' => @car.id,
44
+ 'name' => 'Ford Mustang'
45
+ }
46
+
47
+ assert_equal expected, @serializer.serializable_record
48
+ end
49
+ end
50
+
51
+ class SerializerWithOnlyEnumerationAttributeTest < ActiveRecord::TestCase
52
+ def setup
53
+ @red = create_color(:name => 'red')
54
+ @car = create_car(:name => 'Ford Mustang', :color => @red)
55
+ @serializer = ActiveRecord::Serialization::Serializer.new(@car, :only => [:id, :color_id])
56
+ end
57
+
58
+ def test_should_not_include_enumeration_in_serializable_attribute_names
59
+ assert_equal %w(color_id id), @serializer.serializable_attribute_names
60
+ end
61
+
62
+ def test_should_not_typecast_serializable_record
63
+ expected = {
64
+ 'color_id' => @red.id,
65
+ 'id' => @car.id
66
+ }
67
+
68
+ assert_equal expected, @serializer.serializable_record
69
+ end
70
+ end
71
+
72
+ class SerializerWithOnlyEnumerationAssociationTest < ActiveRecord::TestCase
73
+ def setup
74
+ @red = create_color(:name => 'red')
75
+ @car = create_car(:name => 'Ford Mustang', :color => @red)
76
+ @serializer = ActiveRecord::Serialization::Serializer.new(@car, :only => [:color, :id])
77
+ end
78
+
79
+ def test_should_include_enumeration_in_serializable_attribute_names
80
+ assert_equal %w(color id), @serializer.serializable_attribute_names
81
+ end
82
+
83
+ def test_should_typecast_serializable_record
84
+ expected = {
85
+ 'color' => 'red',
86
+ 'id' => @car.id
87
+ }
88
+
89
+ assert_equal expected, @serializer.serializable_record
90
+ end
91
+ end
92
+
93
+ class SerializerWithExceptEnumerationAttributeTest < ActiveRecord::TestCase
94
+ def setup
95
+ @red = create_color(:name => 'red')
96
+ @car = create_car(:name => 'Ford Mustang', :color => @red)
97
+ @serializer = ActiveRecord::Serialization::Serializer.new(@car, :except => :color_id)
98
+ end
99
+
100
+ def test_should_not_include_enumeration_in_serializable_attribute_names
101
+ assert_equal %w(feature_id feature_type id name), @serializer.serializable_attribute_names
102
+ end
103
+
104
+ def test_should_not_include_enumeration_in_serializable_record
105
+ expected = {
106
+ 'feature_id' => nil,
107
+ 'feature_type' => nil,
108
+ 'id' => @car.id,
109
+ 'name' => 'Ford Mustang'
110
+ }
111
+
112
+ assert_equal expected, @serializer.serializable_record
113
+ end
114
+ end
115
+
116
+ class SerializerWithExceptEnumerationAssociationTest < ActiveRecord::TestCase
117
+ def setup
118
+ @red = create_color(:name => 'red')
119
+ @car = create_car(:name => 'Ford Mustang', :color => @red)
120
+ @serializer = ActiveRecord::Serialization::Serializer.new(@car, :except => :color)
121
+ end
122
+
123
+ def test_should_not_include_enumeration_in_serializable_attribute_names
124
+ assert_equal %w(feature_id feature_type id name), @serializer.serializable_attribute_names
125
+ end
126
+
127
+ def test_should_not_include_enumeration_in_serializable_record
128
+ expected = {
129
+ 'feature_id' => nil,
130
+ 'feature_type' => nil,
131
+ 'id' => @car.id,
132
+ 'name' => 'Ford Mustang'
133
+ }
134
+
135
+ assert_equal expected, @serializer.serializable_record
136
+ end
137
+ end
138
+
139
+ class SerializerWithIncludeEnumerationTest < ActiveRecord::TestCase
140
+ def setup
141
+ @red = create_color(:name => 'red')
142
+ @car = create_car(:name => 'Ford Mustang', :color => @red)
143
+ @serializer = ActiveRecord::Serialization::Serializer.new(@car, :include => :color)
144
+ end
145
+
146
+ def test_should_not_include_enumeration_in_serializable_attribute_names
147
+ assert_equal %w(color_id feature_id feature_type id name), @serializer.serializable_attribute_names
148
+ end
149
+
150
+ def test_should_include_entire_enumeration_in_serializable_record
151
+ expected = {
152
+ :color => {
153
+ 'html' => nil,
154
+ 'id' => @red.id,
155
+ 'name' => 'red'
156
+ },
157
+ 'color_id' => @red.id,
158
+ 'feature_id' => nil,
159
+ 'feature_type' => nil,
160
+ 'id' => @car.id,
161
+ 'name' => 'Ford Mustang'
162
+ }
163
+
164
+ assert_equal expected, @serializer.serializable_record
165
+ end
166
+ end
@@ -0,0 +1,71 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+
3
+ class XmlSerializerAttributeWithEnumerationTest < ActiveRecord::TestCase
4
+ def setup
5
+ @red = create_color(:name => 'red')
6
+ @car = create_car(:name => 'Ford Mustang', :color => @red)
7
+ @attribute = ActiveRecord::XmlSerializer::Attribute.new('color', @car)
8
+ end
9
+
10
+ def test_should_have_a_string_type
11
+ assert_equal :string, @attribute.type
12
+ end
13
+
14
+ def test_should_use_enumerator
15
+ assert_equal 'red', @attribute.value
16
+ end
17
+ end
18
+
19
+ class XmlSerializerAttributeWithNilEnumerationTest < ActiveRecord::TestCase
20
+ def setup
21
+ @red = create_color(:name => 'red')
22
+ @car = create_car(:name => 'Ford Mustang', :color => nil)
23
+ @attribute = ActiveRecord::XmlSerializer::Attribute.new('color', @car)
24
+ end
25
+
26
+ def test_should_have_a_string_type
27
+ assert_equal :string, @attribute.type
28
+ end
29
+
30
+ def test_should_use_nil
31
+ assert_nil @attribute.value
32
+ end
33
+ end
34
+
35
+ class XmlSerializerAttributeWithoutEnumerationTest < ActiveRecord::TestCase
36
+ def setup
37
+ @red = create_color(:name => 'red')
38
+ @car = create_car(:name => 'Ford Mustang', :color => @red)
39
+ @attribute = ActiveRecord::XmlSerializer::Attribute.new('id', @car)
40
+ end
41
+
42
+ def test_should_use_column_type
43
+ assert_equal :integer, @attribute.type
44
+ end
45
+
46
+ def test_should_use_attribute_value
47
+ assert_equal @car.id, @attribute.value
48
+ end
49
+ end
50
+
51
+ class XmlSerializerTest < ActiveRecord::TestCase
52
+ def setup
53
+ @red = create_color(:name => 'red')
54
+ @car = create_car(:name => 'Ford Mustang', :color => @red)
55
+ end
56
+
57
+ def test_should_be_able_to_convert_to_xml
58
+ expected = <<-eos
59
+ <?xml version="1.0" encoding="UTF-8"?>
60
+ <car>
61
+ <color>red</color>
62
+ <feature-id type="integer" nil="true"></feature-id>
63
+ <feature-type nil="true"></feature-type>
64
+ <id type="integer">#{@car.id}</id>
65
+ <name>Ford Mustang</name>
66
+ </car>
67
+ eos
68
+
69
+ assert_equal expected, @car.to_xml
70
+ end
71
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mschuerig-enumerate_by
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.2
5
+ platform: ruby
6
+ authors:
7
+ - Aaron Pfeifer
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-05-28 15:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Adds support for declaring an ActiveRecord class as an enumeration
17
+ email: aaron@pluginaweek.org
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - lib/enumerate_by
26
+ - lib/enumerate_by/extensions
27
+ - lib/enumerate_by/extensions/associations.rb
28
+ - lib/enumerate_by/extensions/xml_serializer.rb
29
+ - lib/enumerate_by/extensions/serializer.rb
30
+ - lib/enumerate_by/extensions/base_conditions.rb
31
+ - lib/enumerate_by.rb
32
+ - test/app_root
33
+ - test/app_root/app
34
+ - test/app_root/app/models
35
+ - test/app_root/app/models/color.rb
36
+ - test/app_root/app/models/car.rb
37
+ - test/app_root/db
38
+ - test/app_root/db/migrate
39
+ - test/app_root/db/migrate/002_create_cars.rb
40
+ - test/app_root/db/migrate/001_create_colors.rb
41
+ - test/test_helper.rb
42
+ - test/factory.rb
43
+ - test/unit
44
+ - test/unit/serializer_test.rb
45
+ - test/unit/assocations_test.rb
46
+ - test/unit/xml_serializer_test.rb
47
+ - test/unit/json_serializer_test.rb
48
+ - test/unit/enumerate_by_test.rb
49
+ - test/unit/base_conditions_test.rb
50
+ - CHANGELOG.rdoc
51
+ - rails/init.rb
52
+ - LICENSE
53
+ - Rakefile
54
+ - README.rdoc
55
+ has_rdoc: true
56
+ homepage: http://www.pluginaweek.org
57
+ post_install_message:
58
+ rdoc_options: []
59
+
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: "0"
67
+ version:
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: "0"
73
+ version:
74
+ requirements: []
75
+
76
+ rubyforge_project: pluginaweek
77
+ rubygems_version: 1.2.0
78
+ signing_key:
79
+ specification_version: 3
80
+ summary: Adds support for declaring an ActiveRecord class as an enumeration
81
+ test_files:
82
+ - test/unit/serializer_test.rb
83
+ - test/unit/assocations_test.rb
84
+ - test/unit/xml_serializer_test.rb
85
+ - test/unit/json_serializer_test.rb
86
+ - test/unit/enumerate_by_test.rb
87
+ - test/unit/base_conditions_test.rb