forked from rails-api/active_model_serializers
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
91f9317
commit 7377464
Showing
9 changed files
with
154 additions
and
0 deletions.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
lib/active_model/serializer/generators/resource_override.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
require 'rails/generators' | ||
require 'rails/generators/rails/resource/resource_generator' | ||
|
||
module Rails | ||
module Generators | ||
class ResourceGenerator | ||
def add_serializer | ||
invoke 'serializer' | ||
end | ||
end | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
37 changes: 37 additions & 0 deletions
37
lib/active_model/serializer/generators/serializer/serializer_generator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
module Rails | ||
module Generators | ||
class SerializerGenerator < NamedBase | ||
source_root File.expand_path('../templates', __FILE__) | ||
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 | ||
|
||
private | ||
|
||
def attributes_names | ||
[:id] + 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 (ns = Rails::Generators.namespace) && ns.const_defined?(:ApplicationSerializer) || | ||
defined?(::ApplicationSerializer) | ||
'ApplicationSerializer' | ||
else | ||
'ActiveModel::Serializer' | ||
end | ||
end | ||
end | ||
end | ||
end |
8 changes: 8 additions & 0 deletions
8
lib/active_model/serializer/generators/serializer/templates/serializer.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<% module_namespacing do -%> | ||
class <%= class_name %>Serializer < <%= parent_class_name %> | ||
attributes <%= attributes_names.map(&:inspect).join(", ") %> | ||
<% association_names.each do |attribute| -%> | ||
has_one :<%= attribute %> | ||
<% end -%> | ||
end | ||
<% end -%> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
module ActiveModel | ||
class Railtie < Rails::Railtie | ||
initializer 'generators' do |app| | ||
require 'rails/generators' | ||
require 'active_model/serializer/generators/serializer/serializer_generator' | ||
Rails::Generators.configure!(app.config.generators) | ||
require 'active_model/serializer/generators/resource_override' | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
require 'test_helper' | ||
require 'rails' | ||
require 'test_app' | ||
require 'rails/generators/rails/resource/resource_generator' | ||
require 'active_model/serializer/generators/resource_override' | ||
|
||
class ResourceGeneratorTest < Rails::Generators::TestCase | ||
destination File.expand_path('../../../tmp', __FILE__) | ||
setup :prepare_destination, :copy_routes | ||
|
||
tests Rails::Generators::ResourceGenerator | ||
arguments %w(account) | ||
|
||
def test_serializer_file_is_generated | ||
run_generator | ||
|
||
assert_file 'app/serializers/account_serializer.rb', /class AccountSerializer < ActiveModel::Serializer/ | ||
end | ||
|
||
private | ||
|
||
def copy_routes | ||
config_dir = File.join(destination_root, 'config') | ||
FileUtils.mkdir_p(config_dir) | ||
File.write(File.join(config_dir, 'routes.rb'), 'Rails.application.routes.draw { }') | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
require 'test_helper' | ||
require 'rails' | ||
require 'test_app' | ||
require 'active_model/serializer/generators/serializer/serializer_generator' | ||
|
||
class SerializerGeneratorTest < Rails::Generators::TestCase | ||
destination File.expand_path('../../../tmp', __FILE__) | ||
setup :prepare_destination | ||
|
||
tests Rails::Generators::SerializerGenerator | ||
arguments %w(account name:string description:text business:references) | ||
|
||
def test_generates_a_serializer_with_attributes_and_associations | ||
run_generator | ||
assert_file 'app/serializers/account_serializer.rb', /class AccountSerializer < ActiveModel::Serializer/ do |serializer| | ||
assert_match(/attributes :id, :name, :description/, serializer) | ||
assert_match(/has_one :business/, serializer) | ||
end | ||
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_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 | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
class TestApp < Rails::Application | ||
if Rails.version.to_s.first >= '4' | ||
config.eager_load = false | ||
config.secret_key_base = 'abc123' | ||
end | ||
end | ||
|
||
TestApp.load_generators |