Sha256: c6b7f7f9f504b52212b3f10d48e74f4a795eda137a4bbf8744015f86e229fbc5

Contents?: true

Size: 1.13 KB

Versions: 10

Compression:

Stored size: 1.13 KB

Contents

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) ||
               (Object.const_get(:ApplicationSerializer) rescue nil)
          'ApplicationSerializer'
        else
          'ActiveModel::Serializer'
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
active_model_serializers-0.9.9 lib/active_model/serializer/generators/serializer/serializer_generator.rb
active_model_serializers-0.9.8 lib/active_model/serializer/generators/serializer/serializer_generator.rb
active_model_serializers-0.9.7 lib/active_model/serializer/generators/serializer/serializer_generator.rb
active_model_serializers-0.9.6 lib/active_model/serializer/generators/serializer/serializer_generator.rb
active_model_serializers-0.9.5 lib/active_model/serializer/generators/serializer/serializer_generator.rb
active_model_serializers-0.9.4 lib/active_model/serializer/generators/serializer/serializer_generator.rb
active_model_serializers-0.9.3 lib/active_model/serializer/generators/serializer/serializer_generator.rb
active_model_serializers-0.9.2 lib/active_model/serializer/generators/serializer/serializer_generator.rb
active_model_serializers-0.9.1 lib/active_model/serializer/generators/serializer/serializer_generator.rb
active_model_serializers-0.9.0 lib/active_model/serializer/generators/serializer/serializer_generator.rb