0.0
No commit activity in last 3 years
No release in over 3 years
Simple nested routes for Sinatra.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

~> 0.6.0
 Project Readme

Sinatra::Scope

Simple nested routes in sinatra.

Installation

> gem install sinatra-scope

# Gemfile
gem 'sinatra-scope'

Usage

The scope method is provided as a class method for Sinatra::Base. An example shows this better than anything else.

class Blog < Sinatra::Base
  register Sinatra::Scope

  scope :posts do
    get do
      @posts = Post.published.paginate(params)
      erb :posts
    end

    get(:new) do
      @post = Post.new
      erb(:new_post)
    end

    post do
      @post = Post.new(params[:post])
      if @post.save
        redirect "/posts/#{@post.id}"
      else
        erb(:new_post)
      end
    end

    scope ":id" do
      before { @post = Post.find(params[:id]) }

      get { erb(:post) }

      get(:edit) { erb(:edit_post) }

      put do
        if @post.update_attributes params[:post]
          redirect "/posts/#{@post.id}"
        else
          erb(:edit_post)
        end
      end

      delete do
        @post.destroy
        redirect "/posts"
      end
    end
  end
end

TODO

  • before and after in an unamed scope leak out of the scope
  • Is there a way to scope template names?
  • Track scopes as named urls