Sha256: 78d0657c95734aa4026327324cd46664fd49d5d02823e8056ed97b31b7789e2f

Contents?: true

Size: 1.58 KB

Versions: 13

Compression:

Stored size: 1.58 KB

Contents

require 'generators/rspec'
require 'rails/generators/resource_helpers'

module Rspec
  module Generators
    class JbuilderGenerator < Base
      include ::Rails::Generators::ResourceHelpers

      argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"

      def generate_view_specs
        source_paths << File.expand_path("../templates", __FILE__)
        copy_view :index
        copy_view :show
      end

      protected

      def copy_view(view)
        template "#{view}_spec.rb",
                 File.join("spec/views", controller_file_path, "#{view}.json.jbuilder_spec.rb")
      end

       # support for namespaced-resources
      def ns_file_name
        ns_parts.empty? ? file_name : "#{ns_parts[0].underscore}_#{ns_parts[1].singularize.underscore}"
      end

      # support for namespaced-resources
      def ns_table_name
        ns_parts.empty? ? table_name : "#{ns_parts[0].underscore}/#{ns_parts[1].tableize}"
      end

      def ns_parts
        @ns_parts ||= begin
                        matches = ARGV[0].to_s.match(/\A(\w+)(?:\/|::)(\w+)/)
                        matches ? [matches[1], matches[2]] : []
                      end
      end

      def value_for(attribute)
        raw_value_for(attribute).inspect
      end

      def raw_value_for(attribute)
        case attribute.type
        when :string
          attribute.name.titleize
        when :integer
          @attribute_id_map ||= {}
          @attribute_id_map[attribute] ||= @attribute_id_map.keys.size.next
        else
          attribute.default
        end
      end

   end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
jbuilder_rspec_generator-1.3.0 lib/generators/rspec/jbuilder_generator.rb
jbuilder_rspec_generator-1.2.0 lib/generators/rspec/jbuilder_generator.rb
jbuilder_rspec_generator-1.1.0 lib/generators/rspec/jbuilder_generator.rb
jbuilder_rspec_generator-1.0.9 lib/generators/rspec/jbuilder_generator.rb
jbuilder_rspec_generator-1.0.8 lib/generators/rspec/jbuilder_generator.rb
jbuilder_rspec_generator-1.0.7 lib/generators/rspec/jbuilder_generator.rb
jbuilder_rspec_generator-1.0.6 lib/generators/rspec/jbuilder_generator.rb
jbuilder_rspec_generator-1.0.5 lib/generators/rspec/jbuilder_generator.rb
jbuilder_rspec_generator-1.0.4 lib/generators/rspec/jbuilder_generator.rb
jbuilder_rspec_generator-1.0.3 lib/generators/rspec/jbuilder_generator.rb
jbuilder_rspec_generator-1.0.2 lib/generators/rspec/jbuilder_generator.rb
jbuilder_rspec_generator-1.0.1 lib/generators/rspec/jbuilder_generator.rb
jbuilder_rspec_generator-1.0.0 lib/generators/rspec/jbuilder_generator.rb