Class: Schofield::Generators::Routes

Inherits:
Object
  • Object
show all
Defined in:
lib/generators/schofield/routes.rb

Class Method Summary collapse

Class Method Details

.add_routes(levels, depth = 3) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 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

.generateObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# 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) ⇒ Object



23
24
25
26
27
28
29
30
# 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