Sha256: 5e22573cbf338f85664ee817e5ad22be810002d2fc6dc756acefef0ebeb56613

Contents?: true

Size: 1.29 KB

Versions: 11

Compression:

Stored size: 1.29 KB

Contents

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

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

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

    argument :resource, type: :string
    argument :attributes, type: :array, default: [], banner: "field[:type][:index] field[:type][:index]"
    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_resource_specs
    end

    private

    def var
      dir.singularize
    end

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

    def generate_resource_specs
      if actions?('create', 'update', 'destroy')
        to = "spec/resources/#{var}/writes_spec.rb.rb"
        template('resource_writes_spec.rb.erb', to)
      end

      if actions?('index', 'show')
        to = "spec/resources/#{var}/reads_spec.rb.rb"
        template('resource_reads_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

11 entries across 11 versions & 1 rubygems

Version Path
graphiti-1.0.alpha.17 lib/generators/graphiti/resource_test_generator.rb
graphiti-1.0.alpha.16 lib/generators/graphiti/resource_test_generator.rb
graphiti-1.0.alpha.15 lib/generators/graphiti/resource_test_generator.rb
graphiti-1.0.alpha.14 lib/generators/graphiti/resource_test_generator.rb
graphiti-1.0.alpha.12 lib/generators/graphiti/resource_test_generator.rb
graphiti-1.0.alpha.11 lib/generators/graphiti/resource_test_generator.rb
graphiti-1.0.alpha.10 lib/generators/graphiti/resource_test_generator.rb
graphiti-1.0.alpha.9 lib/generators/graphiti/resource_test_generator.rb
graphiti-1.0.alpha.8 lib/generators/graphiti/resource_test_generator.rb
graphiti-1.0.alpha.7 lib/generators/graphiti/resource_test_generator.rb
graphiti-1.0.alpha.6 lib/generators/graphiti/resource_test_generator.rb