Sha256: fcfee8c031ac816dc28e4e2ce31065e70b7244f32ff2fc5d9c665275a22d7a84

Contents?: true

Size: 1.5 KB

Versions: 48

Compression:

Stored size: 1.5 KB

Contents

require 'rails/generators'
require 'rails/generators/rails/resource_route/resource_route_generator'

module Incline::Extensions
  module ResourceRouteGenerator

    ##
    # Overrides the add_resource_route method.
    def self.included(base)
      base.class_eval do
        undef add_resource_route

        ##
        # Adds a resource route with additional :api and :locate actions.
        def add_resource_route
          return if options[:actions].present?

          # iterates over all namespaces and opens up blocks
          regular_class_path.each_with_index do |namespace, index|
            write("namespace :#{namespace} do", index + 1)
          end

          # inserts the primary resource with api routes as well.
          pad = '  ' * (route_length + 1)
          write <<-EOR, 0
#{pad}resources :#{file_name.pluralize} do
#{pad}  member do
#{pad}    post :locate
#{pad}  end
#{pad}  collection do
#{pad}    match :api, via: [ :get, :post ]
#{pad}  end
#{pad}end
          EOR

          # ends blocks
          regular_class_path.each_index do |index|
            write("end", route_length - index)
          end

          # route prepends two spaces onto the front of the string that is passed, this corrects that.
          # Also it adds a \n to the end of each line, as route already adds that
          # we need to correct that too.
          route route_string[2..-2]
        end

      end
    end

  end
end

Rails::Generators::ResourceRouteGenerator.include Incline::Extensions::ResourceRouteGenerator

Version data entries

48 entries across 48 versions & 1 rubygems

Version Path
incline-0.3.14 lib/incline/extensions/resource_route_generator.rb
incline-0.3.13 lib/incline/extensions/resource_route_generator.rb
incline-0.3.12 lib/incline/extensions/resource_route_generator.rb
incline-0.3.11 lib/incline/extensions/resource_route_generator.rb
incline-0.3.10 lib/incline/extensions/resource_route_generator.rb
incline-0.3.9 lib/incline/extensions/resource_route_generator.rb
incline-0.3.8 lib/incline/extensions/resource_route_generator.rb
incline-0.3.7 lib/incline/extensions/resource_route_generator.rb
incline-0.3.6 lib/incline/extensions/resource_route_generator.rb
incline-0.3.5 lib/incline/extensions/resource_route_generator.rb
incline-0.3.4 lib/incline/extensions/resource_route_generator.rb
incline-0.3.3 lib/incline/extensions/resource_route_generator.rb
incline-0.3.2 lib/incline/extensions/resource_route_generator.rb
incline-0.3.1 lib/incline/extensions/resource_route_generator.rb
incline-0.3.0 lib/incline/extensions/resource_route_generator.rb
incline-0.2.36 lib/incline/extensions/resource_route_generator.rb
incline-0.2.35 lib/incline/extensions/resource_route_generator.rb
incline-0.2.34 lib/incline/extensions/resource_route_generator.rb
incline-0.2.28 lib/incline/extensions/resource_route_generator.rb
incline-0.2.27 lib/incline/extensions/resource_route_generator.rb