Sha256: c89252e9c1c2c3c06fd1d161d9cb64df63e932740cd09c154121a362c2c825e2

Contents?: true

Size: 1.72 KB

Versions: 69

Compression:

Stored size: 1.72 KB

Contents

require_relative "generator_mixin"

module Graphiti
  class ApiTestGenerator < ::Rails::Generators::Base
    include GeneratorMixin

    source_root File.expand_path("templates", __dir__)

    argument :resource, type: :string
    class_option :actions,
      type: :array,
      default: nil,
      aliases: ["--actions", "-a"],
      desc: 'Array of controller actions, e.g. "index show destroy"'

    desc "Generates rspec request specs at spec/api"
    def generate
      generate_api_specs
    end

    private

    def var
      dir.singularize
    end

    def dir
      @resource.gsub("Resource", "").underscore.pluralize
    end

    def generate_api_specs
      if actions?("index")
        to = File.join("spec", ApplicationResource.endpoint_namespace, dir, "index_spec.rb")
        template("index_request_spec.rb.erb", to)
      end

      if actions?("show")
        to = File.join("spec", ApplicationResource.endpoint_namespace, dir, "show_spec.rb")
        template("show_request_spec.rb.erb", to)
      end

      if actions?("create")
        to = File.join("spec", ApplicationResource.endpoint_namespace, dir, "create_spec.rb")
        template("create_request_spec.rb.erb", to)
      end

      if actions?("update")
        to = File.join("spec", ApplicationResource.endpoint_namespace, dir, "update_spec.rb")
        template("update_request_spec.rb.erb", to)
      end

      if actions?("destroy")
        to = File.join("spec", ApplicationResource.endpoint_namespace, dir, "destroy_spec.rb")
        template("destroy_request_spec.rb.erb", to)
      end
    end

    def resource_class
      @resource.constantize
    end

    def type
      resource_class.type
    end

    def model_class
      resource_class.model
    end
  end
end

Version data entries

69 entries across 69 versions & 1 rubygems

Version Path
graphiti-1.7.6 deprecated_generators/graphiti/api_test_generator.rb
graphiti-1.7.5 deprecated_generators/graphiti/api_test_generator.rb
graphiti-1.7.4 deprecated_generators/graphiti/api_test_generator.rb
graphiti-1.7.3 deprecated_generators/graphiti/api_test_generator.rb
graphiti-1.7.2 deprecated_generators/graphiti/api_test_generator.rb
graphiti-1.7.1 deprecated_generators/graphiti/api_test_generator.rb
graphiti-1.7.0 deprecated_generators/graphiti/api_test_generator.rb
graphiti-1.6.4 deprecated_generators/graphiti/api_test_generator.rb
graphiti-1.6.3 deprecated_generators/graphiti/api_test_generator.rb
graphiti-1.6.2 deprecated_generators/graphiti/api_test_generator.rb
graphiti-1.6.1 deprecated_generators/graphiti/api_test_generator.rb
graphiti-1.6.0 deprecated_generators/graphiti/api_test_generator.rb
graphiti-1.5.3 deprecated_generators/graphiti/api_test_generator.rb
graphiti-1.5.2 deprecated_generators/graphiti/api_test_generator.rb
graphiti-1.5.1 deprecated_generators/graphiti/api_test_generator.rb
graphiti-1.5.0 deprecated_generators/graphiti/api_test_generator.rb
graphiti-1.4.0 deprecated_generators/graphiti/api_test_generator.rb
graphiti-1.3.9 deprecated_generators/graphiti/api_test_generator.rb
graphiti-1.3.8 deprecated_generators/graphiti/api_test_generator.rb
graphiti-1.3.7 deprecated_generators/graphiti/api_test_generator.rb