Sha256: aa38e4f3459393735581e58525670326e8261a737a203bd8279bb1f1cf6ad7d3

Contents?: true

Size: 1.75 KB

Versions: 21

Compression:

Stored size: 1.75 KB

Contents

$:.unshift File.dirname(__FILE__)
require 'generator_mixin'

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

    source_root File.expand_path('../templates', __FILE__)

    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

21 entries across 21 versions & 1 rubygems

Version Path
graphiti-1.0.rc.21 lib/generators/graphiti/api_test_generator.rb
graphiti-1.0.rc.19 lib/generators/graphiti/api_test_generator.rb
graphiti-1.0.rc.18 lib/generators/graphiti/api_test_generator.rb
graphiti-1.0.rc.17 lib/generators/graphiti/api_test_generator.rb
graphiti-1.0.rc.16 lib/generators/graphiti/api_test_generator.rb
graphiti-1.0.rc.15 lib/generators/graphiti/api_test_generator.rb
graphiti-1.0.rc.14 lib/generators/graphiti/api_test_generator.rb
graphiti-1.0.rc.12 lib/generators/graphiti/api_test_generator.rb
graphiti-1.0.rc.11 lib/generators/graphiti/api_test_generator.rb
graphiti-1.0.rc.10 lib/generators/graphiti/api_test_generator.rb
graphiti-1.0.rc.9 lib/generators/graphiti/api_test_generator.rb
graphiti-1.0.rc.8 lib/generators/graphiti/api_test_generator.rb
graphiti-1.0.rc.7 lib/generators/graphiti/api_test_generator.rb
graphiti-1.0.rc.6 lib/generators/graphiti/api_test_generator.rb
graphiti-1.0.rc.5 lib/generators/graphiti/api_test_generator.rb
graphiti-1.0.rc.4 lib/generators/graphiti/api_test_generator.rb
graphiti-1.0.rc.3 lib/generators/graphiti/api_test_generator.rb
graphiti-1.0.rc.2 lib/generators/graphiti/api_test_generator.rb
graphiti-1.0.rc.1 lib/generators/graphiti/api_test_generator.rb
graphiti-1.0.beta.23 lib/generators/graphiti/api_test_generator.rb