Skip to content

support ActiveModel::Serializers #148 #213

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ your...
- Fixture files
- Tests and Specs
- Object Daddy exemplars
- ActiveModel::Serializers model serializers
- Machinist blueprints
- Fabrication fabricators
- Thoughtbot's factory_girl factories, i.e. the (spec|test)/factories/<model>_factory.rb files
Expand Down Expand Up @@ -165,6 +166,8 @@ you can do so with a simple environment variable, instead of editing the
Place the annotations at the top (before) or the bottom (after) of any test files
--pr, --position-in-routes [before|after]
Place the annotations at the top (before) or the bottom (after) of the routes.rb file
--ps, --position-in-serializers [before|after]
Place the annotations at the top (before) or the bottom (after) of any serializer file
-r, --routes Annotate routes.rb with the output of 'rake routes'
-v, --version Show the current version of this gem
-m, --show-migration Include the migration version number in the annotation
Expand Down Expand Up @@ -232,6 +235,7 @@ extra carefully, and consider using one.
- SpatialAdapter: http://github.com/pdeffendol/spatial_adapter
- PostgisAdapter: http://github.com/nofxx/postgis_adapter
- PostGISAdapter: https://github.com/dazuma/activerecord-postgis-adapter
- ActiveModel::Serializer: https://github.com/rails-api/active_model_serializers


== License
Expand Down
8 changes: 7 additions & 1 deletion bin/annotate
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ OptionParser.new do |opts|
"Place the annotations at the top (before) or the bottom (after) of the model/test/fixture/factory/routes file(s)") do |p|
ENV['position'] = p
[
'position_in_class','position_in_factory','position_in_fixture','position_in_test', 'position_in_routes'
'position_in_class','position_in_factory','position_in_fixture','position_in_test', 'position_in_routes', 'position_in_serializer'
].each do |key|
ENV[key] = p unless(has_set_position[key])
end
Expand Down Expand Up @@ -72,6 +72,12 @@ OptionParser.new do |opts|
has_set_position['position_in_routes'] = true
end

opts.on('--ps', '--position-in-serializer [before|after]', ['before', 'after'],
"Place the annotations at the top (before) or the bottom (after) of any serializer file") do |p|
ENV['position_in_routes'] = p
has_set_position['serializer'] = true
end

opts.on('-r', '--routes',
"Annotate routes.rb with the output of 'rake routes'") do
target = {
Expand Down
3 changes: 2 additions & 1 deletion lib/annotate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ module Annotate
#
POSITION_OPTIONS=[
:position_in_routes, :position_in_class, :position_in_test,
:position_in_fixture, :position_in_factory, :position,
:position_in_fixture, :position_in_factory, :position_in_serializer,
:position,
]
FLAG_OPTIONS=[
:show_indexes, :simple_indexes, :include_version, :exclude_tests,
Expand Down
18 changes: 17 additions & 1 deletion lib/annotate/annotate_models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ module AnnotateModels
FABRICATORS_TEST_DIR = File.join("test", "fabricators")
FABRICATORS_SPEC_DIR = File.join("spec", "fabricators")

# ActiveModel::Serializers https://github.com/rails-api/active_model_serializers
SERIALIZERS_DIR = File.join("app", "serializers")

TEST_PATTERNS = [
File.join(UNIT_TEST_DIR, "%MODEL_NAME%_test.rb"),
File.join(MODEL_TEST_DIR, "%MODEL_NAME%_test.rb"),
Expand All @@ -55,6 +58,10 @@ module AnnotateModels
File.join(FABRICATORS_SPEC_DIR, "%MODEL_NAME%_fabricator.rb"),
]

SERIALIZER_PATTERNS = [
File.join(SERIALIZERS_DIR, "%MODEL_NAME%_serializer.rb")
]

# Don't show limit (#) on these column types
# Example: show "integer" instead of "integer(4)"
NO_LIMIT_COL_TYPES = ["integer", "boolean"]
Expand Down Expand Up @@ -286,9 +293,11 @@ def remove_annotation_of_file(file_name)
# :position_in_test<Symbol>:: where to place the annotated section in test/spec file(s)
# :position_in_fixture<Symbol>:: where to place the annotated section in fixture file
# :position_in_factory<Symbol>:: where to place the annotated section in factory file
# :position_in_serializer<Symbol>:: where to place the annotated section in serializer file
# :exclude_tests<Symbol>:: whether to skip modification of test/spec files
# :exclude_fixtures<Symbol>:: whether to skip modification of fixture files
# :exclude_factories<Symbol>:: whether to skip modification of factory files
# :exclude_serializers<Symbol>:: whether to skip modification of serializer files
#
def annotate(klass, file, header, options={})
begin
Expand Down Expand Up @@ -323,6 +332,13 @@ def annotate(klass, file, header, options={})
detect { |result| result } || did_annotate
end

unless options[:exclude_serializers]
did_annotate = SERIALIZER_PATTERNS.
map { |file| resolve_filename(file, model_name, table_name) }.
map { |file| annotate_one_file(file, info, :position_in_serializer, options_with_position(options, :position_in_serializer)) }.
detect { |result| result } || did_annotate
end

return did_annotate
rescue Exception => e
puts "Unable to annotate #{file}: #{e.message}"
Expand Down Expand Up @@ -457,7 +473,7 @@ def remove_annotations(options={})
model_file_name = File.join(model_dir, file)
deannotated_klass = true if(remove_annotation_of_file(model_file_name))

(TEST_PATTERNS + FIXTURE_PATTERNS + FACTORY_PATTERNS).
(TEST_PATTERNS + FIXTURE_PATTERNS + FACTORY_PATTERNS + SERIALIZER_PATTERNS).
map { |file| resolve_filename(file, model_name, table_name) }.
each do |file|
if File.exist?(file)
Expand Down
43 changes: 22 additions & 21 deletions lib/generators/annotate/templates/auto_annotate_models.rake
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,28 @@ if Rails.env.development?
# You can override any of these by setting an environment variable of the
# same name.
Annotate.set_defaults({
'position_in_routes' => "before",
'position_in_class' => "before",
'position_in_test' => "before",
'position_in_fixture' => "before",
'position_in_factory' => "before",
'show_indexes' => "true",
'simple_indexes' => "false",
'model_dir' => "app/models",
'include_version' => "false",
'require' => "",
'exclude_tests' => "false",
'exclude_fixtures' => "false",
'exclude_factories' => "false",
'ignore_model_sub_dir' => "false",
'skip_on_db_migrate' => "false",
'format_bare' => "true",
'format_rdoc' => "false",
'format_markdown' => "false",
'sort' => "false",
'force' => "false",
'trace' => "false",
'position_in_routes' => "before",
'position_in_class' => "before",
'position_in_test' => "before",
'position_in_fixture' => "before",
'position_in_factory' => "before",
'position_in_serializer' => "before",
'show_indexes' => "true",
'simple_indexes' => "false",
'model_dir' => "app/models",
'include_version' => "false",
'require' => "",
'exclude_tests' => "false",
'exclude_fixtures' => "false",
'exclude_factories' => "false",
'ignore_model_sub_dir' => "false",
'skip_on_db_migrate' => "false",
'format_bare' => "true",
'format_rdoc' => "false",
'format_markdown' => "false",
'sort' => "false",
'force' => "false",
'trace' => "false",
})
end

Expand Down
43 changes: 22 additions & 21 deletions spec/integration/rails_4.1.1/lib/tasks/auto_annotate_models.rake
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,28 @@ if Rails.env.development?
# You can override any of these by setting an environment variable of the
# same name.
Annotate.set_defaults({
'position_in_routes' => "before",
'position_in_class' => "before",
'position_in_test' => "before",
'position_in_fixture' => "before",
'position_in_factory' => "before",
'show_indexes' => "true",
'simple_indexes' => "false",
'model_dir' => "app/models",
'include_version' => "false",
'require' => "",
'exclude_tests' => "false",
'exclude_fixtures' => "false",
'exclude_factories' => "false",
'ignore_model_sub_dir' => "false",
'skip_on_db_migrate' => "false",
'format_bare' => "true",
'format_rdoc' => "false",
'format_markdown' => "false",
'sort' => "false",
'force' => "false",
'trace' => "false",
'position_in_routes' => "before",
'position_in_class' => "before",
'position_in_test' => "before",
'position_in_fixture' => "before",
'position_in_factory' => "before",
'position_in_serializer' => "before",
'show_indexes' => "true",
'simple_indexes' => "false",
'model_dir' => "app/models",
'include_version' => "false",
'require' => "",
'exclude_tests' => "false",
'exclude_fixtures' => "false",
'exclude_factories' => "false",
'ignore_model_sub_dir' => "false",
'skip_on_db_migrate' => "false",
'format_bare' => "true",
'format_rdoc' => "false",
'format_markdown' => "false",
'sort' => "false",
'force' => "false",
'trace' => "false",
})
end

Expand Down