Skip to content

Commit

Permalink
Implement Generators
Browse files Browse the repository at this point in the history
  • Loading branch information
spastorino committed Oct 18, 2013
1 parent 91f9317 commit 7377464
Show file tree
Hide file tree
Showing 9 changed files with 154 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/active_model/serializer/generators/resource_override.rb
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

9 changes: 9 additions & 0 deletions lib/active_model/serializer/generators/serializer/USAGE
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
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
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 -%>
10 changes: 10 additions & 0 deletions lib/active_model/serializer/railtie.rb
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
1 change: 1 addition & 0 deletions lib/active_model_serializers.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'active_model'
require 'active_model/serializer'
require 'active_model/serializer/version'
require 'active_model/serializer/railtie' if defined?(Rails)

begin
require 'action_controller'
Expand Down
27 changes: 27 additions & 0 deletions test/integration/generators/resource_generator_test.rb
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
41 changes: 41 additions & 0 deletions test/integration/generators/serializer_generator_test.rb
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
8 changes: 8 additions & 0 deletions test/test_app.rb
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

0 comments on commit 7377464

Please sign in to comment.