Sha256: 14351f11eb9ccf0cfb7d209b6292660b382559e58ae2a3c9a8416ce834d4a32a

Contents?: true

Size: 1.72 KB

Versions: 1

Compression:

Stored size: 1.72 KB

Contents

module Schofield

  module Generators

    class Routes

      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

      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

      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, :delete]" 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

    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
schofield-0.3.0 lib/generators/schofield/routes.rb