Skip to content
Browse files

Add generators for serializers.

  • Loading branch information...
1 parent 28bcda4 commit ebb8ea22f30dc029f4a868a550828443b160f639 @josevalim josevalim committed Nov 25, 2011
View
9 railties/lib/rails/generators/rails/serializer/USAGE
@@ -0,0 +1,9 @@
+Description:
+ Generates a serializer for the given resource with tests.
+
+Example:
+ `rails generate serializer Account name created_at`
+
+ For TestUnit it creates:
+ Serializer: app/serializers/account_serializer.rb
+ TestUnit: test/unit/account_serializer_test.rb
View
39 railties/lib/rails/generators/rails/serializer/serializer_generator.rb
@@ -0,0 +1,39 @@
+module Rails
+ module Generators
+ class SerializerGenerator < NamedBase
+ check_class_collision :suffix => "Serializer"
+
+ argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
+
+ class_option :parent, :type => :string, :desc => "The parent class for the generated serializer"
+
+ def create_serializer_file
+ template 'serializer.rb', File.join('app/serializers', class_path, "#{file_name}_serializer.rb")
+ end
+
+ hook_for :test_framework
+
+ private
+
+ def attributes_names
+ attributes.select { |attr| !attr.reference? }.map { |a| a.name.to_sym }
+ end
+
+ def association_names
+ attributes.select { |attr| attr.reference? }.map { |a| a.name.to_sym }
+ end
+
+ def parent_class_name
+ if options[:parent]
+ options[:parent]
+ elsif (n = Rails::Generators.namespace) && n.const_defined?(:ApplicationSerializer)
+ "ApplicationSerializer"
+ elsif Object.const_defined?(:ApplicationSerializer)
+ "ApplicationSerializer"
+ else
+ "ActiveModel::Serializer"
+ end
+ end
+ end
+ end
+end
View
9 railties/lib/rails/generators/rails/serializer/templates/serializer.rb
@@ -0,0 +1,9 @@
+<% module_namespacing do -%>
+class <%= class_name %>Serializer < <%= parent_class_name %>
+<% if attributes.any? -%> attributes <%= attributes_names.map(&:inspect).join(", ") %>
+<% end -%>
+<% association_names.each do |attribute| -%>
+ has_one :<%= attribute %>
+<% end -%>
+end
+<% end -%>
View
13 railties/lib/rails/generators/test_unit/serializer/serializer_generator.rb
@@ -0,0 +1,13 @@
+require 'rails/generators/test_unit'
+
+module TestUnit
+ module Generators
+ class SerializerGenerator < Base
+ check_class_collision :suffix => "SerializerTest"
+
+ def create_test_files
+ template 'unit_test.rb', File.join('test/unit', class_path, "#{file_name}_serializer_test.rb")
+ end
+ end
+ end
+end
View
9 railties/lib/rails/generators/test_unit/serializer/templates/unit_test.rb
@@ -0,0 +1,9 @@
+require 'test_helper'
+
+<% module_namespacing do -%>
+class <%= class_name %>SerializerTest < ActiveSupport::TestCase
+ # test "the truth" do
+ # assert true
+ # end
+end
+<% end -%>
View
63 railties/test/generators/serializer_generator_test.rb
@@ -0,0 +1,63 @@
+require 'generators/generators_test_helper'
+require 'rails/generators/rails/serializer/serializer_generator'
+
+class SerializerGeneratorTest < Rails::Generators::TestCase
+ include GeneratorsTestHelper
+ arguments %w(account name:string description:text business:references)
+
+ def test_generates_a_serializer
+ run_generator
+ assert_file "app/serializers/account_serializer.rb", /class AccountSerializer < ActiveModel::Serializer/
+ end
+
+ def test_generates_a_namespaced_serializer
+ run_generator ["admin/account"]
+ assert_file "app/serializers/admin/account_serializer.rb", /class Admin::AccountSerializer < ActiveModel::Serializer/
+ end
+
+ def test_uses_application_serializer_if_one_exists
+ Object.const_set(:ApplicationSerializer, Class.new)
+ run_generator
+ assert_file "app/serializers/account_serializer.rb", /class AccountSerializer < ApplicationSerializer/
+ ensure
+ Object.send :remove_const, :ApplicationSerializer
+ end
+
+ def test_uses_namespace_application_serializer_if_one_exists
+ Object.const_set(:SerializerNamespace, Module.new)
+ SerializerNamespace.const_set(:ApplicationSerializer, Class.new)
+ Rails::Generators.namespace = SerializerNamespace
+ run_generator
+ assert_file "app/serializers/serializer_namespace/account_serializer.rb",
+ /module SerializerNamespace\n class AccountSerializer < ApplicationSerializer/
+ ensure
+ Object.send :remove_const, :SerializerNamespace
+ Rails::Generators.namespace = nil
+ end
+
+ def test_uses_given_parent
+ Object.const_set(:ApplicationSerializer, Class.new)
+ run_generator ["Account", "--parent=MySerializer"]
+ assert_file "app/serializers/account_serializer.rb", /class AccountSerializer < MySerializer/
+ ensure
+ Object.send :remove_const, :ApplicationSerializer
+ end
+
+ def test_generates_attributes_and_associations
+ run_generator
+ assert_file "app/serializers/account_serializer.rb" do |serializer|
+ assert_match(/^ attributes :name, :description$/, serializer)
+ assert_match(/^ has_one :business$/, serializer)
+ end
+ end
+
+ def test_with_no_attributes_does_not_add_extra_space
+ run_generator ["account"]
+ assert_file "app/serializers/account_serializer.rb", /class AccountSerializer < ActiveModel::Serializer\nend/
+ end
+
+ def test_invokes_default_test_framework
+ run_generator
+ assert_file "test/unit/account_serializer_test.rb", /class AccountSerializerTest < ActiveSupport::TestCase/
+ end
+end

0 comments on commit ebb8ea2

Please sign in to comment.
Something went wrong with that request. Please try again.