Sha256: 3096bb8fa3dc1925fde3868084d75645db206eca4ed219c57b6b524f9e0c44a4

Contents?: true

Size: 1.53 KB

Versions: 40

Compression:

Stored size: 1.53 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 = "spec/api/v1/#{dir}/index_spec.rb"
        template('index_request_spec.rb.erb', to)
      end

      if actions?('show')
        to = "spec/api/v1/#{dir}/show_spec.rb"
        template('show_request_spec.rb.erb', to)
      end

      if actions?('create')
        to = "spec/api/v1/#{dir}/create_spec.rb"
        template('create_request_spec.rb.erb', to)
      end

      if actions?('update')
        to = "spec/api/v1/#{dir}/update_spec.rb"
        template('update_request_spec.rb.erb', to)
      end

      if actions?('destroy')
        to = "spec/api/v1/#{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

40 entries across 40 versions & 1 rubygems

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