Skip to content

Commit ebb8ea2

Browse files
committed
Add generators for serializers.
1 parent 28bcda4 commit ebb8ea2

File tree

6 files changed

+142
-0
lines changed

6 files changed

+142
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Description:
2+
Generates a serializer for the given resource with tests.
3+
4+
Example:
5+
`rails generate serializer Account name created_at`
6+
7+
For TestUnit it creates:
8+
Serializer: app/serializers/account_serializer.rb
9+
TestUnit: test/unit/account_serializer_test.rb
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
module Rails
2+
module Generators
3+
class SerializerGenerator < NamedBase
4+
check_class_collision :suffix => "Serializer"
5+
6+
argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
7+
8+
class_option :parent, :type => :string, :desc => "The parent class for the generated serializer"
9+
10+
def create_serializer_file
11+
template 'serializer.rb', File.join('app/serializers', class_path, "#{file_name}_serializer.rb")
12+
end
13+
14+
hook_for :test_framework
15+
16+
private
17+
18+
def attributes_names
19+
attributes.select { |attr| !attr.reference? }.map { |a| a.name.to_sym }
20+
end
21+
22+
def association_names
23+
attributes.select { |attr| attr.reference? }.map { |a| a.name.to_sym }
24+
end
25+
26+
def parent_class_name
27+
if options[:parent]
28+
options[:parent]
29+
elsif (n = Rails::Generators.namespace) && n.const_defined?(:ApplicationSerializer)
30+
"ApplicationSerializer"
31+
elsif Object.const_defined?(:ApplicationSerializer)
32+
"ApplicationSerializer"
33+
else
34+
"ActiveModel::Serializer"
35+
end
36+
end
37+
end
38+
end
39+
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<% module_namespacing do -%>
2+
class <%= class_name %>Serializer < <%= parent_class_name %>
3+
<% if attributes.any? -%> attributes <%= attributes_names.map(&:inspect).join(", ") %>
4+
<% end -%>
5+
<% association_names.each do |attribute| -%>
6+
has_one :<%= attribute %>
7+
<% end -%>
8+
end
9+
<% end -%>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
require 'rails/generators/test_unit'
2+
3+
module TestUnit
4+
module Generators
5+
class SerializerGenerator < Base
6+
check_class_collision :suffix => "SerializerTest"
7+
8+
def create_test_files
9+
template 'unit_test.rb', File.join('test/unit', class_path, "#{file_name}_serializer_test.rb")
10+
end
11+
end
12+
end
13+
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
require 'test_helper'
2+
3+
<% module_namespacing do -%>
4+
class <%= class_name %>SerializerTest < ActiveSupport::TestCase
5+
# test "the truth" do
6+
# assert true
7+
# end
8+
end
9+
<% end -%>
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
require 'generators/generators_test_helper'
2+
require 'rails/generators/rails/serializer/serializer_generator'
3+
4+
class SerializerGeneratorTest < Rails::Generators::TestCase
5+
include GeneratorsTestHelper
6+
arguments %w(account name:string description:text business:references)
7+
8+
def test_generates_a_serializer
9+
run_generator
10+
assert_file "app/serializers/account_serializer.rb", /class AccountSerializer < ActiveModel::Serializer/
11+
end
12+
13+
def test_generates_a_namespaced_serializer
14+
run_generator ["admin/account"]
15+
assert_file "app/serializers/admin/account_serializer.rb", /class Admin::AccountSerializer < ActiveModel::Serializer/
16+
end
17+
18+
def test_uses_application_serializer_if_one_exists
19+
Object.const_set(:ApplicationSerializer, Class.new)
20+
run_generator
21+
assert_file "app/serializers/account_serializer.rb", /class AccountSerializer < ApplicationSerializer/
22+
ensure
23+
Object.send :remove_const, :ApplicationSerializer
24+
end
25+
26+
def test_uses_namespace_application_serializer_if_one_exists
27+
Object.const_set(:SerializerNamespace, Module.new)
28+
SerializerNamespace.const_set(:ApplicationSerializer, Class.new)
29+
Rails::Generators.namespace = SerializerNamespace
30+
run_generator
31+
assert_file "app/serializers/serializer_namespace/account_serializer.rb",
32+
/module SerializerNamespace\n class AccountSerializer < ApplicationSerializer/
33+
ensure
34+
Object.send :remove_const, :SerializerNamespace
35+
Rails::Generators.namespace = nil
36+
end
37+
38+
def test_uses_given_parent
39+
Object.const_set(:ApplicationSerializer, Class.new)
40+
run_generator ["Account", "--parent=MySerializer"]
41+
assert_file "app/serializers/account_serializer.rb", /class AccountSerializer < MySerializer/
42+
ensure
43+
Object.send :remove_const, :ApplicationSerializer
44+
end
45+
46+
def test_generates_attributes_and_associations
47+
run_generator
48+
assert_file "app/serializers/account_serializer.rb" do |serializer|
49+
assert_match(/^ attributes :name, :description$/, serializer)
50+
assert_match(/^ has_one :business$/, serializer)
51+
end
52+
end
53+
54+
def test_with_no_attributes_does_not_add_extra_space
55+
run_generator ["account"]
56+
assert_file "app/serializers/account_serializer.rb", /class AccountSerializer < ActiveModel::Serializer\nend/
57+
end
58+
59+
def test_invokes_default_test_framework
60+
run_generator
61+
assert_file "test/unit/account_serializer_test.rb", /class AccountSerializerTest < ActiveSupport::TestCase/
62+
end
63+
end

0 commit comments

Comments
 (0)