enumerate_by 0.4.3 → 0.4.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,13 @@
1
1
  == master
2
2
 
3
+ == 0.4.4 / 2010-03-07
4
+
5
+ * Fix serialization failing when ActiveRecord models have been defined prior to this plugin being loaded
6
+ * Fix cached enumeration records not being isolated from external modifications that don't sweep the cache
7
+ * Respect custom primary_key configurations in ActiveRecord models [Martin Honermeyer]
8
+ * Fix tests failing on Rails 2.3.3+
9
+ * Release gems via rake-gemcutter instead of rubyforge
10
+
3
11
  == 0.4.3 / 2009-06-14
4
12
 
5
13
  * Add support for looking up enumerators by their symbol equivalent
data/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2006-2009 Aaron Pfeifer
1
+ Copyright (c) 2006-2010 Aaron Pfeifer
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
@@ -38,7 +38,7 @@ associations. The important thing to remember, however, is that while the
38
38
  associations exist, the enumerator is always used outside of the application,
39
39
  while either the enumerator or the enumerator's record would be referenced
40
40
  *within* the application. This means that you would reference a Color record
41
- via it's enumerator (such as "red") everywhere in the code (conditions,
41
+ via its enumerator (such as "red") everywhere in the code (conditions,
42
42
  assigning associations, forms, etc.), but it would always be stored in the
43
43
  database as a true association with the integer value of 1.
44
44
 
@@ -75,9 +75,9 @@ database as a true association with the integer value of 1.
75
75
 
76
76
  Each of the above models is backed by the database with its own table. Both
77
77
  the Color and ColorGroup enumerations automatically synchronize with the
78
- records in the database based on what's define in their bootstrap data.
78
+ records in the database based on what's defined in their bootstrap data.
79
79
 
80
- The enumerations and their associations can be then be used like so:
80
+ The enumerations and their associations can then be used like so:
81
81
 
82
82
  car = Car.create(:color => 'red') # => #<Car id: 1, color_id: 1>
83
83
  car.color # => #<Color id: 1, name: "red">
data/Rakefile CHANGED
@@ -1,11 +1,12 @@
1
+ require 'rubygems'
2
+ require 'rake'
1
3
  require 'rake/testtask'
2
4
  require 'rake/rdoctask'
3
5
  require 'rake/gempackagetask'
4
- require 'rake/contrib/sshpublisher'
5
6
 
6
7
  spec = Gem::Specification.new do |s|
7
8
  s.name = 'enumerate_by'
8
- s.version = '0.4.3'
9
+ s.version = '0.4.4'
9
10
  s.platform = Gem::Platform::RUBY
10
11
  s.summary = 'Adds support for declaring an ActiveRecord class as an enumeration'
11
12
  s.description = s.summary
@@ -63,17 +64,17 @@ end
63
64
 
64
65
  Rake::GemPackageTask.new(spec) do |p|
65
66
  p.gem_spec = spec
66
- p.need_tar = true
67
- p.need_zip = true
68
67
  end
69
68
 
70
69
  desc 'Publish the beta gem.'
71
70
  task :pgem => [:package] do
71
+ require 'rake/contrib/sshpublisher'
72
72
  Rake::SshFilePublisher.new('aaron@pluginaweek.org', '/home/aaron/gems.pluginaweek.org/public/gems', 'pkg', "#{spec.name}-#{spec.version}.gem").upload
73
73
  end
74
74
 
75
75
  desc 'Publish the API documentation.'
76
76
  task :pdoc => [:rdoc] do
77
+ require 'rake/contrib/sshpublisher'
77
78
  Rake::SshDirPublisher.new('aaron@pluginaweek.org', "/home/aaron/api.pluginaweek.org/public/#{spec.name}", 'rdoc').upload
78
79
  end
79
80
 
@@ -82,15 +83,8 @@ task :publish => [:pgem, :pdoc, :release]
82
83
 
83
84
  desc 'Publish the release files to RubyForge.'
84
85
  task :release => [:gem, :package] do
85
- require 'rubyforge'
86
+ require 'rake/gemcutter'
86
87
 
87
- ruby_forge = RubyForge.new.configure
88
- ruby_forge.login
89
-
90
- %w(gem tgz zip).each do |ext|
91
- file = "pkg/#{spec.name}-#{spec.version}.#{ext}"
92
- puts "Releasing #{File.basename(file)}..."
93
-
94
- ruby_forge.add_release(spec.rubyforge_project, spec.name, spec.version, file)
95
- end
88
+ Rake::Gemcutter::Tasks.new(spec)
89
+ Rake::Task['gem:push'].invoke
96
90
  end
@@ -17,7 +17,11 @@ module EnumerateBy
17
17
  # Tracks which associations are backed by an enumeration
18
18
  # {"foreign key" => "association name"}
19
19
  class_inheritable_accessor :enumeration_associations
20
- self.enumeration_associations = {}
20
+
21
+ # Fix existing models not getting the default value
22
+ ([self] + subclasses).each do |model|
23
+ model.enumeration_associations = {}
24
+ end
21
25
  end
22
26
  end
23
27
 
@@ -178,7 +182,7 @@ module EnumerateBy
178
182
  [:find_by_sql, :exists?, :calculate].each do |method|
179
183
  define_method(method) do |*args|
180
184
  if EnumerateBy.perform_caching && perform_enumerator_caching
181
- enumerator_cache_store.fetch([method] + args) { super(*args) }
185
+ shallow_clone(enumerator_cache_store.fetch([method] + args) { super(*args) })
182
186
  else
183
187
  super(*args)
184
188
  end
@@ -209,6 +213,16 @@ module EnumerateBy
209
213
  enumerator.is_a?(Symbol) ? enumerator.to_s : enumerator
210
214
  end
211
215
  end
216
+
217
+ # Generates a copy of the given record(s), keeping intact the original id
218
+ def shallow_clone(result)
219
+ case result
220
+ when Array
221
+ result.map {|item| shallow_clone(item)}
222
+ when ActiveRecord::Base
223
+ result.class.send(:instantiate, result.instance_variable_get(:@attributes))
224
+ end
225
+ end
212
226
  end
213
227
 
214
228
  module Bootstrapped
@@ -265,13 +279,15 @@ module EnumerateBy
265
279
  # Otherwise, any changes to that column remain there.
266
280
  def bootstrap(*records)
267
281
  uncached do
282
+ primary_key = self.primary_key.to_sym
283
+
268
284
  # Remove records that are no longer being used
269
285
  records.flatten!
270
- ids = records.map {|record| record[:id]}.compact
271
- delete_all(ids.any? ? ['id NOT IN (?)', ids] : nil)
286
+ ids = records.map {|record| record[primary_key]}.compact
287
+ delete_all(ids.any? ? ["#{primary_key} NOT IN (?)", ids] : nil)
272
288
 
273
289
  # Find remaining existing records (to be updated)
274
- existing = all.inject({}) {|existing, record| existing[record.id] = record; existing}
290
+ existing = all.inject({}) {|existing, record| existing[record.send(primary_key)] = record; existing}
275
291
 
276
292
  records.map! do |attributes|
277
293
  attributes.symbolize_keys!
@@ -279,7 +295,7 @@ module EnumerateBy
279
295
 
280
296
  # Update with new attributes
281
297
  record =
282
- if record = existing[attributes[:id]]
298
+ if record = existing[attributes[primary_key]]
283
299
  attributes.merge!(defaults.delete_if {|attribute, value| record.send("#{attribute}?")}) if defaults
284
300
  record.attributes = attributes
285
301
  record
@@ -287,7 +303,7 @@ module EnumerateBy
287
303
  attributes.merge!(defaults) if defaults
288
304
  new(attributes)
289
305
  end
290
- record.id = attributes[:id]
306
+ record.send("#{primary_key}=", attributes[primary_key])
291
307
 
292
308
  # Force failed saves to stop execution
293
309
  record.save!
@@ -317,14 +333,16 @@ module EnumerateBy
317
333
  #
318
334
  # See EnumerateBy::Bootstrapped#bootstrap for information about usage.
319
335
  def fast_bootstrap(*records)
336
+ primary_key = self.primary_key.to_sym
337
+
320
338
  # Remove records that are no longer being used
321
339
  records.flatten!
322
- ids = records.map {|record| record[:id]}.compact
323
- delete_all(ids.any? ? ['id NOT IN (?)', ids] : nil)
340
+ ids = records.map {|record| record[primary_key]}.compact
341
+ delete_all(ids.any? ? ["#{primary_key} NOT IN (?)", ids] : nil)
324
342
 
325
343
  # Find remaining existing records (to be updated)
326
344
  quoted_table_name = self.quoted_table_name
327
- existing = connection.select_all("SELECT * FROM #{quoted_table_name}").inject({}) {|existing, record| existing[record['id'].to_i] = record; existing}
345
+ existing = connection.select_all("SELECT * FROM #{quoted_table_name}").inject({}) {|existing, record| existing[record[primary_key.to_s].to_i] = record; existing}
328
346
 
329
347
  records.each do |attributes|
330
348
  attributes.stringify_keys!
@@ -332,12 +350,12 @@ module EnumerateBy
332
350
  defaults.stringify_keys!
333
351
  end
334
352
 
335
- id = attributes['id']
353
+ id = attributes[primary_key.to_s]
336
354
  if existing_attributes = existing[id]
337
355
  # Record exists: Update attributes
338
- attributes.delete('id')
356
+ attributes.delete(primary_key.to_s)
339
357
  attributes.merge!(defaults.delete_if {|attribute, value| !existing_attributes[attribute].nil?}) if defaults
340
- update_all(attributes, :id => id)
358
+ update_all(attributes, primary_key => id)
341
359
  else
342
360
  # Record doesn't exist: create new one
343
361
  attributes.merge!(defaults) if defaults
@@ -75,18 +75,19 @@ module EnumerateBy
75
75
  primary_key_name = reflection.primary_key_name
76
76
  class_name = reflection.class_name
77
77
  klass = reflection.klass
78
+ klass_primary_key = klass.primary_key.to_sym
78
79
 
79
80
  # Inclusion scopes
80
81
  %W(with_#{name} with_#{name.to_s.pluralize}).each do |scope_name|
81
82
  named_scope scope_name.to_sym, lambda {|*enumerators| {
82
- :conditions => {primary_key_name => klass.find_all_by_enumerator!(enumerators).map(&:id)}
83
+ :conditions => {primary_key_name => klass.find_all_by_enumerator!(enumerators).map(&klass_primary_key)}
83
84
  }}
84
85
  end
85
86
 
86
87
  # Exclusion scopes
87
88
  %W(without_#{name} without_#{name.to_s.pluralize}).each do |scope_name|
88
89
  named_scope scope_name.to_sym, lambda {|*enumerators| {
89
- :conditions => ["#{primary_key_name} NOT IN (?)", klass.find_all_by_enumerator!(enumerators).map(&:id)]
90
+ :conditions => ["#{primary_key_name} NOT IN (?)", klass.find_all_by_enumerator!(enumerators).map(&klass_primary_key)]
90
91
  }}
91
92
  end
92
93
 
@@ -104,11 +104,12 @@ module EnumerateBy
104
104
  def enumerator_options_for(name, enumerator, allow_multiple = true)
105
105
  if reflection = reflect_on_enumeration(name)
106
106
  klass = reflection.klass
107
+ klass_primary_key = klass.primary_key.to_sym
107
108
  attribute = reflection.primary_key_name
108
109
  id = if allow_multiple && enumerator.is_a?(Array)
109
- klass.find_all_by_enumerator!(enumerator).map(&:id)
110
+ klass.find_all_by_enumerator!(enumerator).map(&klass_primary_key)
110
111
  else
111
- klass.find_by_enumerator!(enumerator).id
112
+ klass.find_by_enumerator!(enumerator).send(klass_primary_key)
112
113
  end
113
114
 
114
115
  {attribute => id}
@@ -1,4 +1,5 @@
1
1
  class Car < ActiveRecord::Base
2
2
  belongs_to :color
3
+ belongs_to :legacy_color
3
4
  belongs_to :feature, :polymorphic => true
4
5
  end
@@ -1,3 +1,5 @@
1
1
  class Color < ActiveRecord::Base
2
2
  enumerate_by :name
3
+
4
+ has_many :cars
3
5
  end
@@ -0,0 +1,5 @@
1
+ class LegacyColor < ActiveRecord::Base
2
+ set_primary_key :uid
3
+
4
+ enumerate_by :name
5
+ end
@@ -0,0 +1,8 @@
1
+ require 'config/boot'
2
+
3
+ Rails::Initializer.run do |config|
4
+ config.plugins = %w(plugin_with_model enumerate_by)
5
+ config.cache_classes = false
6
+ config.whiny_nils = true
7
+ config.action_controller.session = {:key => 'rails_session', :secret => 'd229e4d22437432705ab3985d4d246'}
8
+ end
@@ -2,7 +2,7 @@ class CreateCars < ActiveRecord::Migration
2
2
  def self.up
3
3
  create_table :cars do |t|
4
4
  t.string :name
5
- t.references :color
5
+ t.references :color, :legacy_color
6
6
  t.references :feature, :class_name => 'Color', :polymorphic => true
7
7
  end
8
8
  end
@@ -0,0 +1,12 @@
1
+ class CreateLegacyColors < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :legacy_colors, :primary_key => 'uid' do |t|
4
+ t.string :name, :null => false
5
+ t.string :html
6
+ end
7
+ end
8
+
9
+ def self.down
10
+ drop_table :legacy_colors
11
+ end
12
+ end
@@ -0,0 +1,11 @@
1
+ class CreateTags < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :tags do |t|
4
+ t.string :name
5
+ end
6
+ end
7
+
8
+ def self.down
9
+ drop_table :tags
10
+ end
11
+ end
@@ -0,0 +1 @@
1
+ require 'plugin_with_model'
@@ -0,0 +1,2 @@
1
+ class Tag < ActiveRecord::Base
2
+ end
@@ -34,7 +34,6 @@ module Factory
34
34
  end
35
35
 
36
36
  build Car do |attributes|
37
- attributes[:color] = create_color unless attributes.include?(:color)
38
37
  attributes.reverse_merge!(
39
38
  :name => 'Ford Mustang'
40
39
  )
@@ -52,10 +51,22 @@ module Factory
52
51
  )
53
52
  end
54
53
 
54
+ build LegacyColor do |attributes|
55
+ attributes.reverse_merge!(
56
+ :name => 'red'
57
+ )
58
+ end
59
+
55
60
  build Order do |attributes|
56
61
  attributes[:car_part] = create_car_part unless attributes.include?(:car_part)
57
62
  attributes.reverse_merge!(
58
63
  :state => 'pending'
59
64
  )
60
65
  end
66
+
67
+ build Tag do |attributes|
68
+ attributes.reverse_merge!(
69
+ :name => 'rails'
70
+ )
71
+ end
61
72
  end
@@ -17,7 +17,7 @@ class ModelWithBelongsToAssociationTest < ActiveRecord::TestCase
17
17
  assert_equal @green, @car.color
18
18
  end
19
19
 
20
- def test_should_find_assocation_from_record
20
+ def test_should_find_association_from_record
21
21
  @car.color = @green
22
22
  assert_equal @green, @car.color
23
23
  end
@@ -33,7 +33,7 @@ class ModelWithBelongsToAssociationTest < ActiveRecord::TestCase
33
33
  end
34
34
 
35
35
  def test_should_track_associations
36
- expected = {'color_id' => 'color'}
36
+ expected = {'color_id' => 'color', 'legacy_color_id' => 'legacy_color'}
37
37
  assert_equal expected, Car.enumeration_associations
38
38
  end
39
39
  end
@@ -73,3 +73,30 @@ class ModelWithPolymorphicBelongsToAssociationTest < ActiveRecord::TestCase
73
73
  assert !Car.respond_to?(:without_features)
74
74
  end
75
75
  end
76
+
77
+ class ModelWithEnumerationScopesUsingCustomPrimaryKeyTest < ActiveRecord::TestCase
78
+ def setup
79
+ @red = create_legacy_color(:name => 'red')
80
+ @blue = create_legacy_color(:name => 'blue')
81
+ @red_car = create_car(:name => 'Ford Mustang', :legacy_color => @red)
82
+ @blue_car = create_car(:name => 'Ford Mustang', :legacy_color => @blue)
83
+ end
84
+
85
+ def test_should_have_inclusion_scope_for_single_enumerator
86
+ assert_equal [@red_car], Car.with_legacy_color('red')
87
+ assert_equal [@blue_car], Car.with_legacy_color('blue')
88
+ end
89
+
90
+ def test_should_have_inclusion_scope_for_multiple_enumerators
91
+ assert_equal [@red_car, @blue_car], Car.with_legacy_color('red', 'blue')
92
+ end
93
+
94
+ def test_should_have_exclusion_scope_for_single_enumerator
95
+ assert_equal [@blue_car], Car.without_legacy_color('red')
96
+ assert_equal [@red_car], Car.without_legacy_color('blue')
97
+ end
98
+
99
+ def test_should_have_exclusion_scope_for_multiple_enumerators
100
+ assert_equal [], Car.without_legacy_colors('red', 'blue')
101
+ end
102
+ end
@@ -57,6 +57,23 @@ class EnumerationWithFinderConditionsTest < ActiveRecord::TestCase
57
57
  end
58
58
  end
59
59
 
60
+ class EnumerationWithCustomPrimaryKeyAndFinderConditionsTest < ActiveRecord::TestCase
61
+ def setup
62
+ @red = create_legacy_color(:name => 'red')
63
+ @blue = create_legacy_color(:name => 'blue')
64
+ @red_car = create_car(:name => 'Ford Mustang', :legacy_color => @red)
65
+ @blue_car = create_car(:name => 'Ford Mustang', :legacy_color => @blue)
66
+ end
67
+
68
+ def test_should_replace_enumerations_in_dynamic_finders
69
+ assert_equal @red_car, Car.find_by_legacy_color('red')
70
+ end
71
+
72
+ def test_should_replace_enumerations_in_find_conditions
73
+ assert_equal @red_car, Car.first(:conditions => {:legacy_color => 'red'})
74
+ end
75
+ end
76
+
60
77
  class EnumerationWithFinderUpdatesTest < ActiveRecord::TestCase
61
78
  def setup
62
79
  @red = create_color(:name => 'red')
@@ -86,3 +103,17 @@ class EnumerationWithFinderUpdatesTest < ActiveRecord::TestCase
86
103
  assert_equal @red, @red_car.color
87
104
  end
88
105
  end
106
+
107
+ class EnumerationWithCustomPrimaryKeyAndFinderUpdatesTest < ActiveRecord::TestCase
108
+ def setup
109
+ @red = create_legacy_color(:name => 'red')
110
+ @blue = create_legacy_color(:name => 'blue')
111
+ @red_car = create_car(:name => 'Ford Mustang', :legacy_color => @red)
112
+ end
113
+
114
+ def test_should_replace_enumerations_in_update_conditions
115
+ Car.update_all({:legacy_color => 'blue'}, :name => 'Ford Mustang')
116
+ @red_car.reload
117
+ assert_equal @blue, @red_car.legacy_color
118
+ end
119
+ end
@@ -284,6 +284,14 @@ class EnumerationWithCachingTest < ActiveRecord::TestCase
284
284
  assert Color.perform_enumerator_caching
285
285
  end
286
286
 
287
+ def test_should_not_return_same_objects
288
+ assert_not_same Color.find(@red.id), Color.find(@red.id)
289
+ end
290
+
291
+ def test_should_not_return_same_collections
292
+ assert_not_same Color.find_all_by_id(@red.id), Color.find_all_by_id(@red.id)
293
+ end
294
+
287
295
  def test_should_cache_all_finder_queries
288
296
  assert_queries(1) { Color.find(@red.id) }
289
297
  assert_queries(0) { Color.find(@red.id) }
@@ -310,6 +318,7 @@ class EnumerationWithCachingTest < ActiveRecord::TestCase
310
318
  end
311
319
 
312
320
  def teardown
321
+ Color.enumerator_cache_store.clear
313
322
  EnumerateBy.perform_caching = false
314
323
  end
315
324
  end
@@ -347,6 +356,33 @@ class EnumerationWithoutCachingTest < ActiveRecord::TestCase
347
356
  end
348
357
  end
349
358
 
359
+ class EnumerationWithAssociationsTest < ActiveRecord::TestCase
360
+ def setup
361
+ EnumerateBy.perform_caching = true
362
+
363
+ @red = create_color(:name => 'red')
364
+ @blue = create_color(:name => 'blue')
365
+ @red_car = create_car(:name => 'Ford Mustang', :color => @red)
366
+ @blue_car = create_car(:name => 'Ford Mustang', :color => @blue)
367
+ end
368
+
369
+ def test_should_find_associated_records
370
+ assert_equal [@red_car], Color['red'].cars
371
+ end
372
+
373
+ def test_should_not_cache_association_on_subsequent_usage
374
+ assert_equal [@red_car], Color['red'].cars
375
+
376
+ @second_red_car = create_car(:name => 'Ford Mustang', :color => @red)
377
+ assert_equal [@red_car, @second_red_car], Color['red'].cars
378
+ end
379
+
380
+ def teardown
381
+ Color.enumerator_cache_store.clear
382
+ EnumerateBy.perform_caching = false
383
+ end
384
+ end
385
+
350
386
  class EnumerationBootstrappedTest < ActiveRecord::TestCase
351
387
  def setup
352
388
  @red, @green = Color.bootstrap(
@@ -448,6 +484,28 @@ class EnumerationBootstrappedWithDefaultsTest < ActiveRecord::TestCase
448
484
  end
449
485
  end
450
486
 
487
+ class EnumerationWithCustomPrimaryKeyBootstrappedTest < ActiveRecord::TestCase
488
+ def setup
489
+ @red, @green = LegacyColor.bootstrap(
490
+ {:uid => 1, :name => 'red'},
491
+ {:uid => 2, :name => 'green'}
492
+ )
493
+ end
494
+
495
+ def test_should_not_raise_exception_if_primary_key_not_specified
496
+ assert_nothing_raised { LegacyColor.bootstrap({:name => 'red'}, {:name => 'green'}) }
497
+ assert_equal 2, LegacyColor.count
498
+ end
499
+
500
+ def test_should_create_records
501
+ assert_equal @red, LegacyColor.find(1)
502
+ assert_equal 'red', @red.name
503
+
504
+ assert_equal @green, LegacyColor.find(2)
505
+ assert_equal 'green', @green.name
506
+ end
507
+ end
508
+
451
509
  class EnumerationFastBootstrappedTest < ActiveRecord::TestCase
452
510
  def setup
453
511
  @result = Color.fast_bootstrap(
@@ -546,3 +604,23 @@ class EnumerationFastBootstrappedWithDefaultsTest < ActiveRecord::TestCase
546
604
  assert_equal '#00ff00', @green.html
547
605
  end
548
606
  end
607
+
608
+ class EnumerationWithCustomPrimaryKeyFastBootstrappedTest < ActiveRecord::TestCase
609
+ def setup
610
+ @result = LegacyColor.fast_bootstrap(
611
+ {:uid => 1, :name => 'red'},
612
+ {:uid => 2, :name => 'green'}
613
+ )
614
+ end
615
+
616
+ def test_should_not_raise_exception_if_primary_key_not_specified
617
+ assert_nothing_raised { LegacyColor.fast_bootstrap({:name => 'red'}, {:name => 'green'}) }
618
+ assert_equal 2, LegacyColor.count
619
+ end
620
+
621
+ def test_should_create_records
622
+ assert @result
623
+ assert_not_nil LegacyColor.find_by_name('red')
624
+ assert_not_nil LegacyColor.find_by_name('green')
625
+ end
626
+ end
@@ -8,11 +8,21 @@ class JSONSerializerTest < ActiveRecord::TestCase
8
8
  end
9
9
 
10
10
  def test_should_include_enumeration_in_json
11
- assert_match %r{"color": "red"}, @json
11
+ assert_match %r{"color":\s*"red"}, @json
12
12
  end
13
13
 
14
14
  def test_should_render_other_attributes
15
- assert_match %r{"id": #{@car.id}}, @json
16
- assert_match %r{"name": "Ford Mustang"}, @json
15
+ assert_match %r{"id":\s*#{@car.id}}, @json
16
+ assert_match %r{"name":\s*"Ford Mustang"}, @json
17
+ end
18
+ end
19
+
20
+ class JSONSerializerForPluginModelTest < ActiveRecord::TestCase
21
+ def setup
22
+ @tag = create_tag(:name => 'rails')
23
+ end
24
+
25
+ def test_should_serialize
26
+ assert_match %r{"name":\s*"rails"}, @tag.to_json
17
27
  end
18
28
  end
@@ -8,7 +8,7 @@ class SerializerByDefaultTest < ActiveRecord::TestCase
8
8
  end
9
9
 
10
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
11
+ assert_equal %w(color feature_id feature_type id legacy_color name), @serializer.serializable_attribute_names
12
12
  end
13
13
 
14
14
  def test_should_typecast_serializable_record
@@ -16,6 +16,7 @@ class SerializerByDefaultTest < ActiveRecord::TestCase
16
16
  'color' => 'red',
17
17
  'feature_id' => nil,
18
18
  'feature_type' => nil,
19
+ 'legacy_color' => nil,
19
20
  'id' => @car.id,
20
21
  'name' => 'Ford Mustang'
21
22
  }
@@ -32,7 +33,7 @@ class SerializerWithoutEnumerationsTest < ActiveRecord::TestCase
32
33
  end
33
34
 
34
35
  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
+ assert_equal %w(color_id feature_id feature_type id legacy_color_id name), @serializer.serializable_attribute_names
36
37
  end
37
38
 
38
39
  def test_should_not_typecast_serializable_record
@@ -40,6 +41,7 @@ class SerializerWithoutEnumerationsTest < ActiveRecord::TestCase
40
41
  'color_id' => @red.id,
41
42
  'feature_id' => nil,
42
43
  'feature_type' => nil,
44
+ 'legacy_color_id' => nil,
43
45
  'id' => @car.id,
44
46
  'name' => 'Ford Mustang'
45
47
  }
@@ -98,13 +100,14 @@ class SerializerWithExceptEnumerationAttributeTest < ActiveRecord::TestCase
98
100
  end
99
101
 
100
102
  def test_should_not_include_enumeration_in_serializable_attribute_names
101
- assert_equal %w(feature_id feature_type id name), @serializer.serializable_attribute_names
103
+ assert_equal %w(feature_id feature_type id legacy_color name), @serializer.serializable_attribute_names
102
104
  end
103
105
 
104
106
  def test_should_not_include_enumeration_in_serializable_record
105
107
  expected = {
106
108
  'feature_id' => nil,
107
109
  'feature_type' => nil,
110
+ 'legacy_color' => nil,
108
111
  'id' => @car.id,
109
112
  'name' => 'Ford Mustang'
110
113
  }
@@ -121,13 +124,14 @@ class SerializerWithExceptEnumerationAssociationTest < ActiveRecord::TestCase
121
124
  end
122
125
 
123
126
  def test_should_not_include_enumeration_in_serializable_attribute_names
124
- assert_equal %w(feature_id feature_type id name), @serializer.serializable_attribute_names
127
+ assert_equal %w(feature_id feature_type id legacy_color name), @serializer.serializable_attribute_names
125
128
  end
126
129
 
127
130
  def test_should_not_include_enumeration_in_serializable_record
128
131
  expected = {
129
132
  'feature_id' => nil,
130
133
  'feature_type' => nil,
134
+ 'legacy_color' => nil,
131
135
  'id' => @car.id,
132
136
  'name' => 'Ford Mustang'
133
137
  }
@@ -144,7 +148,7 @@ class SerializerWithIncludeEnumerationTest < ActiveRecord::TestCase
144
148
  end
145
149
 
146
150
  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
151
+ assert_equal %w(color_id feature_id feature_type id legacy_color name), @serializer.serializable_attribute_names
148
152
  end
149
153
 
150
154
  def test_should_include_entire_enumeration_in_serializable_record
@@ -157,6 +161,7 @@ class SerializerWithIncludeEnumerationTest < ActiveRecord::TestCase
157
161
  'color_id' => @red.id,
158
162
  'feature_id' => nil,
159
163
  'feature_type' => nil,
164
+ 'legacy_color' => nil,
160
165
  'id' => @car.id,
161
166
  'name' => 'Ford Mustang'
162
167
  }
@@ -62,6 +62,7 @@ class XmlSerializerTest < ActiveRecord::TestCase
62
62
  <feature-id type="integer" nil="true"></feature-id>
63
63
  <feature-type nil="true"></feature-type>
64
64
  <id type="integer">#{@car.id}</id>
65
+ <legacy-color nil="true"></legacy-color>
65
66
  <name>Ford Mustang</name>
66
67
  </car>
67
68
  eos
@@ -89,3 +90,21 @@ class XmlSerializerWithNumericEnumeratorAttributeTest < ActiveRecord::TestCase
89
90
  assert_equal expected, @order.to_xml
90
91
  end
91
92
  end
93
+
94
+ class XMLSerializerForPluginModelTest < ActiveRecord::TestCase
95
+ def setup
96
+ @tag = create_tag(:name => 'rails')
97
+ end
98
+
99
+ def test_should_serialize
100
+ expected = <<-eos
101
+ <?xml version="1.0" encoding="UTF-8"?>
102
+ <tag>
103
+ <id type="integer">#{@tag.id}</id>
104
+ <name>rails</name>
105
+ </tag>
106
+ eos
107
+
108
+ assert_equal expected, @tag.to_xml
109
+ end
110
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: enumerate_by
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.3
4
+ version: 0.4.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aaron Pfeifer
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-06-14 00:00:00 -04:00
12
+ date: 2010-03-07 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -22,35 +22,33 @@ extensions: []
22
22
  extra_rdoc_files: []
23
23
 
24
24
  files:
25
- - lib/enumerate_by.rb
26
- - lib/enumerate_by
27
- - lib/enumerate_by/extensions
28
- - lib/enumerate_by/extensions/base_conditions.rb
29
25
  - lib/enumerate_by/extensions/associations.rb
30
26
  - lib/enumerate_by/extensions/serializer.rb
31
27
  - lib/enumerate_by/extensions/xml_serializer.rb
32
- - test/unit
28
+ - lib/enumerate_by/extensions/base_conditions.rb
29
+ - lib/enumerate_by.rb
30
+ - test/unit/serializer_test.rb
31
+ - test/unit/base_conditions_test.rb
33
32
  - test/unit/xml_serializer_test.rb
34
33
  - test/unit/enumerate_by_test.rb
35
- - test/unit/serializer_test.rb
36
- - test/unit/assocations_test.rb
37
34
  - test/unit/json_serializer_test.rb
38
- - test/unit/base_conditions_test.rb
39
- - test/factory.rb
40
- - test/app_root
41
- - test/app_root/app
42
- - test/app_root/app/models
43
- - test/app_root/app/models/order.rb
44
- - test/app_root/app/models/color.rb
45
- - test/app_root/app/models/car.rb
46
- - test/app_root/app/models/car_part.rb
47
- - test/app_root/db
48
- - test/app_root/db/migrate
49
- - test/app_root/db/migrate/003_create_car_parts.rb
35
+ - test/unit/associations_test.rb
36
+ - test/app_root/vendor/plugins/plugin_with_model/init.rb
37
+ - test/app_root/vendor/plugins/plugin_with_model/lib/plugin_with_model.rb
38
+ - test/app_root/db/migrate/005_create_legacy_colors.rb
50
39
  - test/app_root/db/migrate/001_create_colors.rb
51
- - test/app_root/db/migrate/002_create_cars.rb
40
+ - test/app_root/db/migrate/006_create_tags.rb
41
+ - test/app_root/db/migrate/003_create_car_parts.rb
52
42
  - test/app_root/db/migrate/004_create_orders.rb
43
+ - test/app_root/db/migrate/002_create_cars.rb
44
+ - test/app_root/app/models/car.rb
45
+ - test/app_root/app/models/color.rb
46
+ - test/app_root/app/models/legacy_color.rb
47
+ - test/app_root/app/models/car_part.rb
48
+ - test/app_root/app/models/order.rb
49
+ - test/app_root/config/environment.rb
53
50
  - test/test_helper.rb
51
+ - test/factory.rb
54
52
  - CHANGELOG.rdoc
55
53
  - init.rb
56
54
  - LICENSE
@@ -58,6 +56,8 @@ files:
58
56
  - README.rdoc
59
57
  has_rdoc: true
60
58
  homepage: http://www.pluginaweek.org
59
+ licenses: []
60
+
61
61
  post_install_message:
62
62
  rdoc_options: []
63
63
 
@@ -78,14 +78,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
78
78
  requirements: []
79
79
 
80
80
  rubyforge_project: pluginaweek
81
- rubygems_version: 1.3.1
81
+ rubygems_version: 1.3.5
82
82
  signing_key:
83
- specification_version: 2
83
+ specification_version: 3
84
84
  summary: Adds support for declaring an ActiveRecord class as an enumeration
85
85
  test_files:
86
+ - test/unit/serializer_test.rb
87
+ - test/unit/base_conditions_test.rb
86
88
  - test/unit/xml_serializer_test.rb
87
89
  - test/unit/enumerate_by_test.rb
88
- - test/unit/serializer_test.rb
89
- - test/unit/assocations_test.rb
90
90
  - test/unit/json_serializer_test.rb
91
- - test/unit/base_conditions_test.rb
91
+ - test/unit/associations_test.rb