class Schofield::Generators::Routes

Public Class Methods

add_routes(levels, depth=3) click to toggle source
# File lib/generators/schofield/routes.rb, line 7
def self.add_routes levels, depth=3
  childless = []
  levels.each do |level|
    if level.join?
      @joins << level
    elsif level.nests?
      @routes << route_string([level], depth)
      add_routes(level.nested_levels, depth+1)
      @routes << "#{'  ' * depth}end" + (depth == 3 ? "\n" : '')
    else
      childless << level
    end
  end
  @routes << route_string(childless, depth) if childless.any?
end
generate() click to toggle source
# File lib/generators/schofield/routes.rb, line 32
def self.generate
  @routes = [
    "  namespace :admin do\n",
    "    root :to => 'home#index'",
    "    match '/:locale' => 'home#index'\n",
    "    scope '/:locale', :locale => /\#{I18n.available_locales.join('|')}/, :shallow_path => '/:locale' do\n"
  ]
  @joins = []
  add_routes(Levels.all.select(&:routes?))
  @routes.push "\n      resources :#{@joins.map{ |s| s.name.tableize }.join(', :')}, :only => [:create, :destroy]" if @joins.any?
  @routes.push "\n      resources :#{Levels.sortables.map{ |s| s.tableize }.join(', :')}, :only => [] do\n        post :sort, :on => :collection\n      end" if Levels.sortables.any?
  @routes.push '    end'
  @routes.push '  end'
  @routes.join("\n") + "\n\n"
end
route_string(levels, depth) click to toggle source
# File lib/generators/schofield/routes.rb, line 23
def self.route_string levels, depth
  string  = '  ' * depth
  string += "resources "
  string += ':' + levels.map{ |level| level.name.tableize }.join(', :')
  string += ", "
  string += ":except => #{depth == 3 ? ':edit' : '[:index, :edit]'}"
  string += levels.length == 1 && levels.first.nests? ? ', :shallow => true do' : ''
end