diff options
6 files changed, 142 insertions, 0 deletions
diff --git a/railties/lib/rails/generators/rails/serializer/USAGE b/railties/lib/rails/generators/rails/serializer/USAGE new file mode 100644 index 0000000000..a49f7ea1fb --- /dev/null +++ b/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 diff --git a/railties/lib/rails/generators/rails/serializer/serializer_generator.rb b/railties/lib/rails/generators/rails/serializer/serializer_generator.rb new file mode 100644 index 0000000000..2118906227 --- /dev/null +++ b/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 diff --git a/railties/lib/rails/generators/rails/serializer/templates/serializer.rb b/railties/lib/rails/generators/rails/serializer/templates/serializer.rb new file mode 100644 index 0000000000..30c058c7e9 --- /dev/null +++ b/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 -%>
\ No newline at end of file diff --git a/railties/lib/rails/generators/test_unit/serializer/serializer_generator.rb b/railties/lib/rails/generators/test_unit/serializer/serializer_generator.rb new file mode 100644 index 0000000000..533c032c3b --- /dev/null +++ b/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 diff --git a/railties/lib/rails/generators/test_unit/serializer/templates/unit_test.rb b/railties/lib/rails/generators/test_unit/serializer/templates/unit_test.rb new file mode 100644 index 0000000000..0b1bbdcaa5 --- /dev/null +++ b/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 -%> diff --git a/railties/test/generators/serializer_generator_test.rb b/railties/test/generators/serializer_generator_test.rb new file mode 100644 index 0000000000..2afaa65693 --- /dev/null +++ b/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 |