Sha256: 8a6a8eea9468a26cedba440d3c2e0d378fa3f1ae7e293df81df88a30114fe744

Contents?: true

Size: 1.03 KB

Versions: 8

Compression:

Stored size: 1.03 KB

Contents

# frozen_string_literal: true

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.erb', File.join('app/serializers', class_path, "#{file_name}_serializer.rb")
      end

      private

      def attributes_names
        [:id] + attributes.reject(&:reference?).map! { |a| a.name.to_sym }
      end

      def association_names
        attributes.select(&:reference?).map! { |a| a.name.to_sym }
      end

      def parent_class_name
        if options[:parent]
          options[:parent]
        elsif 'ApplicationSerializer'.safe_constantize
          'ApplicationSerializer'
        else
          'ActiveModel::Serializer'
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 2 rubygems

Version Path
active_model_serializers-0.10.15 lib/generators/rails/serializer_generator.rb
active_model_serializers-0.10.14 lib/generators/rails/serializer_generator.rb
active_model_serializers-0.10.13 lib/generators/rails/serializer_generator.rb
active_model_serializers-0.10.12 lib/generators/rails/serializer_generator.rb
active_model_serializers-0.10.11 lib/generators/rails/serializer_generator.rb
active_model_serializers-0.10.10 lib/generators/rails/serializer_generator.rb
active_model_serializers_custom-0.10.90 lib/generators/rails/serializer_generator.rb
active_model_serializers-0.10.9 lib/generators/rails/serializer_generator.rb