Sha256: 3b0a07f7698eb825066817419bcec499807f67380515b6e3b3724b567de0ab54

Contents?: true

Size: 1.25 KB

Versions: 5

Compression:

Stored size: 1.25 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 generate_id_method
        RUBY_VERSION =~ /1\.8/
      end

      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]
        # Only works on 3.2
        # elsif (n = Rails::Generators.namespace) && n.const_defined?(:ApplicationSerializer)
        #   "ApplicationSerializer"
        elsif defined?(::ApplicationSerializer)
          "ApplicationSerializer"
        else
          "ActiveModel::Serializer"
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
active_model_serializers-0.8.4 lib/generators/serializer/serializer_generator.rb
active_model_serializers-0.8.3 lib/generators/serializer/serializer_generator.rb
active_model_serializers-0.8.2 lib/generators/serializer/serializer_generator.rb
active_model_serializers-0.8.1 lib/generators/serializer/serializer_generator.rb
active_model_serializers-0.8.0 lib/generators/serializer/serializer_generator.rb