class Effective::Generators::RouteGenerator

Public Instance Methods

create_route() click to toggle source
# File lib/generators/effective/route_generator.rb, line 26
def create_route
  blocks = []

  Effective::CodeWriter.new(resource.routes_file) do |w|
    resource.namespaces.each do |namespace|
      index = nil

      w.within(blocks.last) do
        index = w.first { |line, depth| depth == 1 && line == "namespace :#{namespace} do"}
      end

      index ? (blocks << index) : break
    end

    content = resource.namespaces[blocks.length..-1].map { |namespace| "namespace :#{namespace} do"} +
      [resources].flatten + (['end'] * (resource.namespaces.length - blocks.length))

    w.within(blocks.last) do
      if content.length == 1 && w.find { |line, depth| depth == 1 && line == content.first }
        say_status :identical, content.first, :blue
      else
        w.insert_after_last(content, content_depth: 0) { |line, depth| depth == 1 && line.start_with?('resources') } ||
        w.insert_before_last(content, content_depth: 0) { |line, depth| depth == 1 && line.start_with?('root') } ||
        w.insert_before_last(content, content_depth: 0) { |line, depth| line == 'end' }

        say_status :route, content.join("\n\t\t")
      end
    end
  end
end
invoke_route() click to toggle source
# File lib/generators/effective/route_generator.rb, line 22
def invoke_route
  say_status :invoke, :route, :white
end
validate_resource() click to toggle source
# File lib/generators/effective/route_generator.rb, line 18
def validate_resource
  exit unless resource_valid?
end

Private Instance Methods

resources() click to toggle source
# File lib/generators/effective/route_generator.rb, line 59
def resources
  @resources ||= (
    resources = "resources :#{resource.plural_name}"

    if ((crud_actions - ['show']) == invoked_actions)
      resources << ', except: [:show]'
    elsif (crud_actions - invoked_actions).present?
      resources << ', only: ['
      resources << (crud_actions & invoked_actions).map { |action| ':' + action }.join(', ')
      resources << ']'
    end

    if non_crud_actions.present?
      [resources + ' do'] + non_crud_actions.map { |action| "get :#{action}, on: :member" } + ['end']
    else
      resources
    end
  )
end