From c129ae20161861ff70abe584da7abb8be7d0f0d2 Mon Sep 17 00:00:00 2001 From: Steve Klabnik Date: Tue, 19 Mar 2013 14:53:08 -0700 Subject: [PATCH] Generate id method on Ruby 1.8. Because object_id and id went through some flux, it's best to tell 1.8 specifically what we mean. Closes #127. --- CHANGELOG.md | 3 +++ .../serializer/serializer_generator.rb | 5 +++-- .../serializer/templates/serializer.rb | 11 +++++++++++ test/generators_test.rb | 16 +++++++++++++++- 4 files changed, 32 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3037c336..27295560 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # unreleased +* On Ruby 1.8, we now generate an `id` method that properly serializes `id` + columns. See issue #127 for more. + * Add an alias for `scope` method to be the name of the context. By default this is `current_user`. The name is automatically set when using `serialization_scope` in the controller. diff --git a/lib/generators/serializer/serializer_generator.rb b/lib/generators/serializer/serializer_generator.rb index 4dfc13ff..b7096f83 100644 --- a/lib/generators/serializer/serializer_generator.rb +++ b/lib/generators/serializer/serializer_generator.rb @@ -12,9 +12,10 @@ module Rails template 'serializer.rb', File.join('app/serializers', class_path, "#{file_name}_serializer.rb") end - # hook_for :test_framework - private + def generate_id_method + RUBY_VERSION =~ /1\.8/ + end def attributes_names [:id] + attributes.select { |attr| !attr.reference? }.map { |a| a.name.to_sym } diff --git a/lib/generators/serializer/templates/serializer.rb b/lib/generators/serializer/templates/serializer.rb index 4ebb004e..302354b4 100644 --- a/lib/generators/serializer/templates/serializer.rb +++ b/lib/generators/serializer/templates/serializer.rb @@ -4,5 +4,16 @@ class <%= class_name %>Serializer < <%= parent_class_name %> <% association_names.each do |attribute| -%> has_one :<%= attribute %> <% end -%> +<% if generate_id_method %> + + # due to the difference between 1.8 and 1.9 with respect to #id and + # #object_id, we reccomend that if you wish to serialize id columns, you + # do this. Feel free to remove this if you don't feel that it's appropriate. + # + # For more: https://github.com/rails-api/active_model_serializers/issues/127 + def id + object.read_attribute_for_serialization(:id) + end +<% end -%> end <% end -%> diff --git a/test/generators_test.rb b/test/generators_test.rb index e37ebe09..6988ea7e 100644 --- a/test/generators_test.rb +++ b/test/generators_test.rb @@ -36,6 +36,18 @@ class SerializerGeneratorTest < Rails::Generators::TestCase Object.send :remove_const, :ApplicationSerializer end + def test_serializer_gets_id + run_generator + + assert_file "app/serializers/account_serializer.rb" do |content| + if RUBY_VERSION =~ /1.8/ + assert_match /def id/, content + else + assert_no_match /def id/, content + end + end + end + # def test_uses_namespace_application_serializer_if_one_exists # Object.const_set(:SerializerNamespace, Module.new) # SerializerNamespace.const_set(:ApplicationSerializer, Class.new) @@ -66,6 +78,8 @@ class SerializerGeneratorTest < Rails::Generators::TestCase def test_with_no_attributes_does_not_add_extra_space run_generator ["account"] - assert_file "app/serializers/account_serializer.rb", /class AccountSerializer < ActiveModel::Serializer\n attributes :id\nend/ + assert_file "app/serializers/account_serializer.rb" do |content| + assert_no_match /\n\nend/, content + end end end