• Stars
    star
    194
  • Rank 193,191 (Top 4 %)
  • Language
    Ruby
  • Created over 14 years ago
  • Updated almost 12 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Extends Sinatra with ActiveRecord helper methods and Rake tasks (now maintained by @holman).

Sinatra ActiveRecord Extension

The official fork is now at: http://github.com/janko-m/sinatra-activerecord

This one is now closed.


Extends Sinatra with a extension methods and Rake tasks for dealing with a SQL database using the ActiveRecord ORM.

Install the sinatra-activerecord gem along with one of the database adapters:

sudo gem install activerecord
sudo gem install sinatra-activerecord -s http://gemcutter.org
sudo gem install sqlite3
sudo gem install mysql
sudo gem install postgres

adding this to your Rakefile

# require your app file first
require 'sinatra-ar-exmple-app'
require 'sinatra/activerecord/rake'

In terminal, test that it works

$ rake -T
rake db:create_migration  # create an ActiveRecord migration in ./db/migrate
rake db:migrate           # migrate your database

Now you can create a migration

$ rake db:create_migration NAME=create_foos

This will create a migration file in ./db/migrate, ready for editing

class CreateFoos < ActiveRecord::Migration
  def self.up
    create_table :foos do |t|
      t.string :name
    end
  end

  def self.down
    drop_table :foos
  end
end

run the migration

$ rake db:migrate

I like to split models out into a separate database.rb file and then require it from the main app file, but you can plop the following code in about anywhere and it'll work just fine:

require 'sinatra'
require 'sinatra/activerecord'

# Establish the database connection; or, omit this and use the DATABASE_URL
# environment variable or the default sqlite://<environment>.db as the connection string:
set :database, 'sqlite://foo.db'

# At this point, you can access the ActiveRecord::Base class using the
# "database" object:
puts "the foos table doesn't exist" if !database.table_exists?('foos')

# models just work ...
class Foo < ActiveRecord::Base
end

# see:
Foo.all

# access the models within the context of an HTTP request
get '/foos/:id' do
  @foo = Foo.find(params[:id])
  erb :foos
end

NOTE about the rip-off

This Code and README.md is a heavy adaption of rtomayko's sinatra-sequel

Copyright (c) 2009 Blake Mizerany

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

More Repositories

1

pat

Go
1,413
star
2

roundup

eliminate bugs and weeds from shell scripts
Shell
424
star
3

noeqd

A fault-tolerant network service for meaningful GUID generation
Go
340
star
4

perks

Effective Computation of Things
Go
182
star
5

pq

Go
179
star
6

assert

Asserts to Go testing
Go
142
star
7

sinatra-dj

a simple piglatin app to show off sinatra + DJ
Ruby
81
star
8

aws4

A Go package for AWS Signature version 4
Go
73
star
9

sinatra-redis

A light extension for using redis with Sinatra
Ruby
60
star
10

lizzy

An DSL for creating AMQP agents; quickly.
Ruby
53
star
11

vendor

Vendor copies go dependencies to ./vendor
Go
52
star
12

onis

runtime object introspection for ruby process
Ruby
44
star
13

pq.go

Go
39
star
14

sinatra-captcha

Quick, simple, fast captcha for Sinatra
Ruby
37
star
15

mc

Binary Memcached client for Go with SASL support
Go
34
star
16

heroku-sinatra-app

Ruby
32
star
17

aws

An old simple AWS client in Go (use bmizerany/aws4 for more up to date aws usage).
Go
26
star
18

sinatra-any

Granular before filters for sinatra
Ruby
24
star
19

redis-erl

Minimilast Redis Client for Erlang
Erlang
21
star
20

swirl

API version agnostic EC2 client
Ruby
20
star
21

beldam

clean, easy, ec2 wrapper
Ruby
14
star
22

logplex

Go
14
star
23

swirl-node

JavaScript
13
star
24

em-swirl

An evented Ruby EC2 driver for EventMachine
Ruby
12
star
25

recho

echo(1) in ruby
Ruby
11
star
26

nodefinder

Erlang node finders
11
star
27

noeq.go

Go client for noeqd
Go
10
star
28

pqx

Go
9
star
29

sinatra-slaushed

redis powered authentication for Sinatra
Ruby
9
star
30

frylock

Ruby + CLI + DSL = Command line apps made easy! (Not ready yet)
Ruby
8
star
31

supermemo

SM2 application
Ruby
7
star
32

inotify-ffi

Ruby
7
star
33

guns

It has two pipes; got tickets to the show?
Ruby
7
star
34

noeq

A command line client for noeqd
Go
6
star
35

lpx

Logplex framing parser
Go
6
star
36

lively-docs

Escape the hell of `rake rdoc` .. edit .. `rake rdoc` .. edit .. etc.
5
star
37

pat.go

Now at bmizerany/pat
Go
5
star
38

WAck

Ack! for the web.
Ruby
5
star
39

heroku-docs

Documentation for Heroku, in the form of a Sinatra app serving markdown text files.
Ruby
5
star
40

jabberlang

Erlang jabber client
5
star
41

sinatra-template-vendor

Ruby
4
star
42

domainy

for getting the base of a domain
Ruby
4
star
43

RedisPl

Redis Perl Bindings (from svn://svn.rot13.org/Redis/)
Perl
3
star
44

lameetup

Ruby
3
star
45

sinatra-sassacache

Extends Sinatra with a route to compile and cache Sass templates
3
star
46

test

a test for work
3
star
47

attrubates

Arrtubates sets meta on meta for objects and adds additional rendering helpers for ActionController::Base in Rails.
Ruby
3
star
48

lorem-me

Hacker's Lorem Ipsum Genereator with Gists.
Ruby
3
star
49

sinatra-ext-pres

Sinatra Extensions Presentation
Ruby
2
star
50

schemafinder

2
star
51

fido

he fetches
Shell
2
star
52

hk

Fast Heroku client
Go
2
star
53

Elephant

An Adium Plugin for keeping realtime backup/full-text search of all chats
Objective-C
2
star
54

pages

Go
2
star
55

aws.go

Go
2
star
56

doozer-bench

bechmark the doozerz
Go
1
star
57

muni

Quick muni notifications for us SF dwellers
1
star
58

bang

banger
Ruby
1
star
59

mc.go

Go
1
star
60

onis-web

1
star
61

vips

Go
1
star
62

fsh

Fuck syntax highlighting. The correct default for the Atom (atom.io) editor.
CSS
1
star
63

theherms

example - don't look
1
star