Module: Padrino::Routing::ClassMethods

### Home Classes Methods

### In Files

- lib/padrino-core/application/routing.rb

### Methods

- #add_filter
- #after
- #before
- #compiled_router
- #construct_filter
- #controller
- #controllers
- #deferred_routes
- #delete
- #get
- #head
- #link
- #options
- #parent
- #patch
- #post
- #process_path_for_parent_params
- #put
- #rebase_url
- #recognize_path
- #reset_router!
- #router
- #unlink
- #url
- #url_for
- #urls

### Files

- README.rdoc

### Class/Module Index

Quicksearch

- Padrino
- Padrino::Application
- Padrino::ApplicationLoadError
- Padrino::ApplicationSetup
- Padrino::ApplicationSetup::ClassMethods
- Padrino::AuthenticityToken
- Padrino::Cli
- Padrino::Cli::Adapter
- Padrino::Cli::Base
- Padrino::Cli::Launcher
- Padrino::Filter
- Padrino::Flash
- Padrino::Flash::Helpers
- Padrino::Flash::Storage
- Padrino::Loader
- Padrino::Logger
- Padrino::Logger::Colorize
- Padrino::Logger::Extensions
- Padrino::Logger::Rack
- Padrino::Module
- Padrino::Mounter
- Padrino::Mounter::ApplicationWrapper
- Padrino::Mounter::MounterException
- Padrino::ParamsProtection
- Padrino::ParamsProtection::ClassMethods
- Padrino::ParamsProtection::InstanceMethods
- Padrino::Reloader
- Padrino::Reloader::Rack
- Padrino::Reloader::Storage
- Padrino::Router
- Padrino::Routing
- Padrino::Routing::ClassMethods
- Padrino::Routing::InstanceMethods
- Padrino::Routing::Parent
- Padrino::Routing::UnrecognizedException
- Padrino::Server
- Padrino::ShowExceptions
- Padrino::Tasks
- HttpRouter
- HttpRouter::Node
- HttpRouter::Node::FreeRegex
- HttpRouter::Node::Glob
- HttpRouter::Node::Path
- HttpRouter::Node::SpanningRegex
- HttpRouter::Request
- HttpRouter::Route
- Sinatra
- Sinatra::Base
- Sinatra::Request
- Kernel
- Object
- PadrinoTasks

No matching classes.

# Padrino::Routing::ClassMethods

Class methods responsible for enhanced routing for controllers.

### Constants

CONTROLLER_OPTIONS

### Public Instance Methods

add_filter(type, &block) click to toggle source

Adds a filter hook to a request.

    # File lib/padrino-core/application/routing.rb, line 166
    def add_filter(type, &block)
      filters[type] << block
    end

after(*args, &block) click to toggle source

Add an after filter hook.

@see construct_filter

    # File lib/padrino-core/application/routing.rb, line 159
    def after(*args, &block)
      add_filter :after, &(args.empty? ? block : construct_filter(*args, &block))
    end

before(*args, &block) click to toggle source

Add a before filter hook.

@see construct_filter

    # File lib/padrino-core/application/routing.rb, line 150
    def before(*args, &block)
      add_filter :before, &(args.empty? ? block : construct_filter(*args, &block))
    end

compiled_router() click to toggle source

    # File lib/padrino-core/application/routing.rb, line 266
    def compiled_router
      if @deferred_routes
        deferred_routes.each do |routes|
          routes.each do |(route, dest)|
            route.to(dest)
            route.before_filters.flatten!
            route.after_filters.flatten!
          end
        end
        @deferred_routes = nil
        router.sort!
      end
      router
    end

construct_filter(*args, &block) click to toggle source

Creates a filter to process before/after the matching route.

@param [Array] args

@example We are be able to filter with String path

    before('/') { 'only to :index' }
    get(:index} { 'foo' } # => filter match only before this.
    get(:main) { 'bar' }

@example is the same of

    before(:index) { 'only to :index' }
    get(:index} { 'foo' } # => filter match only before this.
    get(:main) { 'bar' }

@example it works only for the given controller

    controller :foo do
      before(:index) { 'only to for :foo_index' }
      get(:index} { 'foo' } # => filter match only before this.
      get(:main) { 'bar' }
    end

    controller :bar do
      before(:index) { 'only to for :bar_index' }
      get(:index} { 'foo' } # => filter match only before this.
      get(:main) { 'bar' }
    end

@example if filters based on a symbol or regexp

    before :index, /main/ do; ... end
    # => match only path that are  +/+ or contains +main+

@example filtering everything except an occurrence

    before :except => :index do; ...; end

@example you can also filter using a request param

    before :agent => /IE/ do; ...; end
    # => match +HTTP_USER_AGENT+ containing +IE+

@see www.padrinorb.com/guides/controllers#route-filters

    # File lib/padrino-core/application/routing.rb, line 211
    def construct_filter(*args, &block)
      options = args.extract_options!
      if except = options.delete(:except)
        fail "You cannot use :except with other options specified" unless args.empty? && options.empty?
        options = Array(except).extract_options!
      end
      Filter.new(!except, @_controller, options, Array(except || args), &block)
    end

controller(*args, &block) click to toggle source

Method to organize our routes in a better way.

@param [Array] args

    Controller arguments.

@yield []

    The given block will be used to define the routes within the
    Controller.

@example

    controller :admin do
      get :index do; ...; end
      get :show, :with => :id  do; ...; end
    end

    url(:admin_index) # => "/admin"
    url(:admin_show, :id => 1) # "/admin/show/1"

@example Using named routes follow the sinatra way:

    controller "/admin" do
      get "/index" do; ...; end
      get "/show/:id" do; ...; end
    end

@example Supply `:provides` to all controller routes:

    controller :provides => [:html, :xml, :json] do
      get :index do; "respond to html, xml and json"; end
      post :index do; "respond to html, xml and json"; end
      get :foo do; "respond to html, xml and json"; end
    end

@example Specify parent resources in padrino with the `:parent` option on the controller:

    controllers :product, :parent => :user do
      get :index do
        # url is generated as "/user/#{params[:user_id]}/product"
        # url_for(:product, :index, :user_id => 5) => "/user/5/product"
      end
      get :show, :with => :id do
        # url is generated as "/user/#{params[:user_id]}/product/show/#{params[:id]}"
        # url_for(:product, :show, :user_id => 5, :id => 10) => "/user/5/product/show/10"
      end
    end

@example Specify conditions to run for all routes:

    controller :conditions => {:protect => true} do
      def self.protect(protected)
        condition do
          halt 403, "No secrets for you!" unless params[:key] == "s3cr3t"
        end if protected
      end

      # This route will only return "secret stuff" if the user goes to
      # `/private?key=s3cr3t`.
      get("/private") { "secret stuff" }

      # And this one, too!
      get("/also-private") { "secret stuff" }

      # But you can override the conditions for each route as needed.
      # This route will be publicly accessible without providing the
      # secret key.
      get :index, :protect => false do
        "Welcome!"
      end
    end

@example Supply default values:

    controller :lang => :de do
      get :index, :map => "/:lang" do; "params[:lang] == :de"; end
    end

In a controller, before and after filters are scoped and don't

    affect other controllers or the main app.

In a controller, layouts are scoped and don't affect other

    controllers or the main app.

@example

    controller :posts do
      layout :post
      before { foo }
      after  { bar }
    end

    # File lib/padrino-core/application/routing.rb, line 136
    def controller(*args, &block)
      if block_given?
        with_new_options(*args) { instance_eval(&block) }
      else
        include(*args) if extensions.any?
      end
    end

Also aliased as: controllers
controllers(*args, &block) click to toggle source
Alias for: controller
deferred_routes() click to toggle source

    # File lib/padrino-core/application/routing.rb, line 281
    def deferred_routes
      @deferred_routes ||= ROUTE_PRIORITY.map{[]}
    end

delete(path, *args, &block) click to toggle source

    # File lib/padrino-core/application/routing.rb, line 352
    def delete(path, *args, &block)  route 'DELETE',  path, *args, &block end

get(path, *args, &block) click to toggle source

    # File lib/padrino-core/application/routing.rb, line 342
    def get(path, *args, &block)
      conditions = @conditions.dup
      route('GET', path, *args, &block)

      @conditions = conditions
      route('HEAD', path, *args, &block)
    end

head(path, *args, &block) click to toggle source

    # File lib/padrino-core/application/routing.rb, line 353
    def head(path, *args, &block)    route 'HEAD',    path, *args, &block end

link(path, *args, &block) click to toggle source

    # File lib/padrino-core/application/routing.rb, line 356
    def link(path, *args, &block)    route 'LINK',    path, *args, &block end

options(path, *args, &block) click to toggle source

    # File lib/padrino-core/application/routing.rb, line 354
    def options(path, *args, &block) route 'OPTIONS', path, *args, &block end

parent(name, options={}) click to toggle source

Provides many parents with shallowing.

@param [Symbol] name

    The parent name.

@param [Hash] options

    Additional options.

@example

    controllers :product do
      parent :shop, :optional => true, :map => "/my/stand"
      parent :category, :optional => true
      get :show, :with => :id do
        # generated urls:
        #   "/product/show/#{params[:id]}"
        #   "/my/stand/#{params[:shop_id]}/product/show/#{params[:id]}"
        #   "/my/stand/#{params[:shop_id]}/category/#{params[:category_id]}/product/show/#{params[:id]}"
        # url_for(:product, :show, :id => 10) => "/product/show/10"
        # url_for(:product, :show, :shop_id => 5, :id => 10) => "/my/stand/5/product/show/10"
        # url_for(:product, :show, :shop_id => 5, :category_id => 1, :id => 10) => "/my/stand/5/category/1/product/show/10"
      end
    end

    # File lib/padrino-core/application/routing.rb, line 244
    def parent(name, options={})
      defaults = { :optional => false, :map => name.to_s }
      options = defaults.merge(options)
      @_parent = Array(@_parent) unless @_parent.is_a?(Array)
      @_parent << Parent.new(name, options)
    end

patch(path, *args, &block) click to toggle source

    # File lib/padrino-core/application/routing.rb, line 355
    def patch(path, *args, &block)   route 'PATCH',   path, *args, &block end

post(path, *args, &block) click to toggle source

    # File lib/padrino-core/application/routing.rb, line 351
    def post(path, *args, &block)    route 'POST',    path, *args, &block end

process_path_for_parent_params(path, parent_params) click to toggle source

Processes the existing path and prepends the 'parent' parameters onto the route Used for calculating path in route method.

    # File lib/padrino-core/application/routing.rb, line 374
    def process_path_for_parent_params(path, parent_params)
      parent_prefix = parent_params.flatten.compact.uniq.map do |param|
        map  = (param.respond_to?(:map) && param.map ? param.map : param.to_s)
        part = "#{map}/:#{param.to_s.singularize}_id/"
        part = "(#{part})" if param.respond_to?(:optional) && param.optional?
        part
      end

      [parent_prefix, path].flatten.join("")
    end

put(path, *args, &block) click to toggle source

    # File lib/padrino-core/application/routing.rb, line 350
    def put(path, *args, &block)     route 'PUT',     path, *args, &block end

rebase_url(url) click to toggle source

    # File lib/padrino-core/application/routing.rb, line 359
    def rebase_url(url)
      if url.start_with?('/')
        new_url = ''
        new_url << conform_uri(ENV['RACK_BASE_URI']) if ENV['RACK_BASE_URI']
        new_url << conform_uri(uri_root) if defined?(uri_root)
        new_url << url
      else
        url.blank? ? '/' : url
      end
    end

recognize_path(path) click to toggle source

Recognize a given path.

@param [String] path

    Path+Query to parse

@return [Symbol, Hash]

    Returns controller and query params.

@example Giving a controller like:

    controller :foo do
      get :bar, :map => 'foo-bar-:id'; ...; end
    end

@example You should be able to reverse:

    MyApp.url(:foo_bar, :id => :mine)
    # => /foo-bar-mine

@example Into this:

    MyApp.recognize_path('foo-bar-mine')
    # => [:foo_bar, :id => :mine]

    # File lib/padrino-core/application/routing.rb, line 312
    def recognize_path(path)
      responses = @router.recognize(Rack::MockRequest.env_for(path))
      responses = responses[0] if responses[0].is_a?(Array)
      [responses[0].path.route.name, responses[0].params]
    end

reset_router!() click to toggle source

    # File lib/padrino-core/application/routing.rb, line 285
    def reset_router!
      @deferred_routes = nil
      router.reset!
    end

router() click to toggle source

Using HttpRouter, for features and configurations.

@example

    router.add('/greedy/:greed')
    router.recognize('/simple')

@see github.com/joshbuddy/http_router

    # File lib/padrino-core/application/routing.rb, line 260
    def router
      @router ||= HttpRouter.new
      block_given? ? yield(@router) : @router
    end

Also aliased as: urls
unlink(path, *args, &block) click to toggle source

    # File lib/padrino-core/application/routing.rb, line 357
    def unlink(path, *args, &block)  route 'UNLINK',  path, *args, &block end

url(*args) click to toggle source

Instance method for url generation.

@option options [String] :fragment

    An addition to url to identify a portion of requested resource (i.e #something).

@option options [String] :anchor

    Synonym for fragment.

@example

    url(:show, :id => 1)
    url(:show, :name => 'test', :id => 24)
    url(:show, 1)
    url(:controller_name, :show, :id => 21)
    url(:controller_show, :id => 29)
    url(:index, :fragment => 'comments')

    # File lib/padrino-core/application/routing.rb, line 334
    def url(*args)
      params = args.extract_options!
      fragment = params.delete(:fragment) || params.delete(:anchor)
      path = make_path_with_params(args, value_to_param(params.symbolize_keys))
      rebase_url(fragment ? path << '#' << fragment : path)
    end

Also aliased as: url_for
url_for(*args) click to toggle source
Alias for: url
urls() click to toggle source
Alias for: router

[Validate]

Generated with the Darkfish Rdoc Generator 2.