├── .gems
├── .gitignore
├── views
├── root.haml
└── layout.haml
├── lib
└── profile.rb
├── config.ru
├── Gemfile
├── spec
├── application_spec.rb
├── profile_spec.rb
└── spec_helper.rb
├── application.rb
├── script
└── console
├── Rakefile
├── environment.rb
├── public
└── main.css
├── MIT-LICENSE
├── README.rdoc
└── Gemfile.lock
/.gems:
--------------------------------------------------------------------------------
1 | data_mapper -v 1.0.0
2 | dm-postgres-adapter
3 | sinatra -v 1.0
4 |
5 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.db
2 | *.sqlite3
3 | .bundle/*
4 | log/*
5 | *.swp
6 | *.swo
7 |
--------------------------------------------------------------------------------
/views/root.haml:
--------------------------------------------------------------------------------
1 | %h2 Main Page
2 | %p Here is some text.
3 | %p Here is a link.
4 | %p ttys!
5 |
--------------------------------------------------------------------------------
/lib/profile.rb:
--------------------------------------------------------------------------------
1 | # example model file
2 | class Profile
3 | include DataMapper::Resource
4 |
5 | property :id, Serial
6 | property :name, String
7 | property :created_at, DateTime
8 | property :updated_at, DateTime
9 |
10 | validates_presence_of :name
11 | end
12 |
--------------------------------------------------------------------------------
/config.ru:
--------------------------------------------------------------------------------
1 | require File.join(File.dirname(__FILE__), 'application')
2 |
3 | set :run, false
4 | set :environment, :production
5 |
6 | FileUtils.mkdir_p 'log' unless File.exists?('log')
7 | log = File.new("log/sinatra.log", "a+")
8 | $stdout.reopen(log)
9 | $stderr.reopen(log)
10 |
11 | run Sinatra::Application
12 |
--------------------------------------------------------------------------------
/Gemfile:
--------------------------------------------------------------------------------
1 | source :rubygems
2 | gem 'sinatra', '>= 1.0'
3 | gem 'rake'
4 | gem 'data_mapper'
5 | gem 'dm-core'
6 | gem 'dm-sqlite-adapter'
7 | gem 'dm-timestamps'
8 | gem 'dm-validations'
9 | gem 'dm-aggregates'
10 | gem 'dm-migrations'
11 | gem 'haml'
12 |
13 | group :test do
14 | gem 'rspec', :require => 'spec'
15 | gem 'rack-test'
16 | end
17 |
--------------------------------------------------------------------------------
/spec/application_spec.rb:
--------------------------------------------------------------------------------
1 | require "#{File.dirname(__FILE__)}/spec_helper"
2 |
3 | describe 'main application' do
4 | include Rack::Test::Methods
5 |
6 | def app
7 | Sinatra::Application.new
8 | end
9 |
10 | specify 'should show the default index page' do
11 | get '/'
12 | last_response.should be_ok
13 | end
14 |
15 | specify 'should have more specs' do
16 | pending
17 | end
18 | end
19 |
--------------------------------------------------------------------------------
/views/layout.haml:
--------------------------------------------------------------------------------
1 | !!!
2 | %html
3 | %head
4 | %title= @title || SiteConfig.title
5 | %link{:href => '/main.css', :rel => 'stylesheet', :type => 'text/css'}
6 | %body
7 | #container
8 | #header
9 | #logo
10 | %h1
11 | %a{:href => '/'}= SiteConfig.title
12 | #content= yield
13 | #footer
14 | %p#legal= "— © #{Time.now.strftime('%Y')} #{SiteConfig.author} —"
15 |
--------------------------------------------------------------------------------
/spec/profile_spec.rb:
--------------------------------------------------------------------------------
1 | require "#{File.dirname(__FILE__)}/spec_helper"
2 |
3 | describe 'profile' do
4 | before(:each) do
5 | @profile = Profile.new(:name => 'test user')
6 | end
7 |
8 | specify 'should be valid' do
9 | @profile.should be_valid
10 | end
11 |
12 | specify 'should require a name' do
13 | @profile = Profile.new
14 | @profile.should_not be_valid
15 | @profile.errors[:name].should include("Name must not be blank")
16 | end
17 | end
18 |
--------------------------------------------------------------------------------
/application.rb:
--------------------------------------------------------------------------------
1 | require 'rubygems'
2 | require 'bundler/setup'
3 | require 'sinatra'
4 | require File.join(File.dirname(__FILE__), 'environment')
5 |
6 | configure do
7 | set :views, "#{File.dirname(__FILE__)}/views"
8 | end
9 |
10 | error do
11 | e = request.env['sinatra.error']
12 | Kernel.puts e.backtrace.join("\n")
13 | 'Application error'
14 | end
15 |
16 | helpers do
17 | # add your helpers here
18 | end
19 |
20 | # root page
21 | get '/' do
22 | haml :root
23 | end
24 |
--------------------------------------------------------------------------------
/script/console:
--------------------------------------------------------------------------------
1 | #! /usr/bin/env ruby
2 | # This console script adapted from:rake
3 | # http://barkingiguana.com/blog/2009/01/25/scriptconsole-for-your-application/
4 |
5 | libs = []
6 | libs << "irb/completion"
7 | libs << File.dirname(__FILE__) + '/../environment.rb'
8 |
9 | command_line = []
10 | command_line << "irb"
11 | command_line << libs.inject("") { |acc, lib| acc + %( -r "#{lib}") }
12 | command_line << "--simple-prompt"
13 | command = command_line.join(" ")
14 |
15 | puts "Welcome to the sinatra console interface."
16 | exec command
17 |
--------------------------------------------------------------------------------
/spec/spec_helper.rb:
--------------------------------------------------------------------------------
1 | require 'rubygems'
2 | require 'bundler'
3 | Bundler.setup(:default, :test)
4 | require 'sinatra'
5 | require 'rspec'
6 | require 'rack/test'
7 |
8 | # set test environment
9 | Sinatra::Base.set :environment, :test
10 | Sinatra::Base.set :run, false
11 | Sinatra::Base.set :raise_errors, true
12 | Sinatra::Base.set :logging, false
13 |
14 | require File.join(File.dirname(__FILE__), '../application')
15 |
16 | # establish in-memory database for testing
17 | DataMapper.setup(:default, "sqlite3::memory:")
18 |
19 | RSpec.configure do |config|
20 | # reset database before each example is run
21 | config.before(:each) { DataMapper.auto_migrate! }
22 | end
23 |
--------------------------------------------------------------------------------
/Rakefile:
--------------------------------------------------------------------------------
1 | require 'rubygems'
2 | require 'bundler/setup'
3 | require 'rspec/core/rake_task'
4 |
5 | task :default => :test
6 | task :test => :spec
7 |
8 | if !defined?(RSpec)
9 | puts "spec targets require RSpec"
10 | else
11 | desc "Run all examples"
12 | RSpec::Core::RakeTask.new(:spec) do |t|
13 | #t.pattern = 'spec/**/*_spec.rb' # not needed this is default
14 | t.rspec_opts = ['-cfs']
15 | end
16 | end
17 |
18 | namespace :db do
19 | desc 'Auto-migrate the database (destroys data)'
20 | task :migrate => :environment do
21 | DataMapper.auto_migrate!
22 | end
23 |
24 | desc 'Auto-upgrade the database (preserves data)'
25 | task :upgrade => :environment do
26 | DataMapper.auto_upgrade!
27 | end
28 | end
29 |
30 | task :environment do
31 | require File.join(File.dirname(__FILE__), 'environment')
32 | end
33 |
--------------------------------------------------------------------------------
/environment.rb:
--------------------------------------------------------------------------------
1 | require 'rubygems'
2 | require 'bundler/setup'
3 | require 'dm-core'
4 | require 'dm-timestamps'
5 | require 'dm-validations'
6 | require 'dm-aggregates'
7 | require 'dm-migrations'
8 | require 'haml'
9 | require 'ostruct'
10 |
11 | require 'sinatra' unless defined?(Sinatra)
12 |
13 | configure do
14 | SiteConfig = OpenStruct.new(
15 | :title => 'Your Application Name',
16 | :author => 'Your Name',
17 | :url_base => 'http://localhost:4567/'
18 | )
19 |
20 | # load models
21 | $LOAD_PATH.unshift("#{File.dirname(__FILE__)}/lib")
22 | Dir.glob("#{File.dirname(__FILE__)}/lib/*.rb") { |lib| require File.basename(lib, '.*') }
23 |
24 | DataMapper.setup(:default, (ENV["DATABASE_URL"] || "sqlite3:///#{File.expand_path(File.dirname(__FILE__))}/#{Sinatra::Base.environment}.db"))
25 | end
26 |
--------------------------------------------------------------------------------
/public/main.css:
--------------------------------------------------------------------------------
1 | body {
2 | background: #FFF;
3 | color: #1B1B1B;
4 | font-family: Verdana, Arial, Helvetica, sans-serif;
5 | }
6 | #container {
7 | margin: 0 auto;
8 | width: 800px;
9 | }
10 | #header {
11 | padding: 0.5em 0 0 0;
12 | margin: 0;
13 | text-align: center;
14 | border-bottom: 3px solid #000;
15 | }
16 | #content {
17 | padding: 0;
18 | margin: 0;
19 | }
20 | #footer {
21 | padding: 0;
22 | margin: 0;
23 | font-size: 0.7em;
24 | text-align: center;
25 | border-top: 3px solid #000;
26 | }
27 | h1, h2, h3, h4, h5, h6 {
28 | margin-top: 0em;
29 | margin-bottom: .25em;
30 | font-weight: bold;
31 | }
32 | h1 { font-size: 2.2em; }
33 | h2 { font-size: 1.6em; }
34 | h3 { font-size: 1.4em; }
35 | h4 { font-size: 1.3em; }
36 | h5 { font-size: 1.2em; }
37 | h6 { font-size: 1.1em; }
38 | p {
39 | margin-bottom: 1em;
40 | line-height: 1.3;
41 | }
42 | a {
43 | color: #095EAE;
44 | font-weight: bold;
45 | text-decoration: none;
46 | }
47 | a:hover {
48 | text-decoration: underline;
49 | }
50 | #logo a:hover {
51 | text-decoration: none;
52 | }
53 |
--------------------------------------------------------------------------------
/MIT-LICENSE:
--------------------------------------------------------------------------------
1 | Copyright (c) 2009 Nick Plante
2 |
3 | Permission is hereby granted, free of charge, to any person obtaining
4 | a copy of this software and associated documentation files (the
5 | "Software"), to deal in the Software without restriction, including
6 | without limitation the rights to use, copy, modify, merge, publish,
7 | distribute, sublicense, and/or sell copies of the Software, and to
8 | permit persons to whom the Software is furnished to do so, subject to
9 | the following conditions:
10 |
11 | The above copyright notice and this permission notice shall be
12 | included in all copies or substantial portions of the Software.
13 |
14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 |
--------------------------------------------------------------------------------
/README.rdoc:
--------------------------------------------------------------------------------
1 | = Sinatra Application Template
2 |
3 | A base Sinatra application template. Just fork and build. Yay!
4 | Includes Bundler, DataMapper, RSpec2, and Haml, all ready to go.
5 |
6 | Works with both Ruby 1.8.7 and Ruby 1.9.2.
7 |
8 | == Configuration
9 |
10 | Dependencies and all configuration is done in environment.rb. Your database is also set up here. DataMapper will use sqlite3 by default. Tests use the sqlite3-memory adapter (no configuration needed).
11 |
12 | Add your controller actions in application.rb. Views for these actions are placed in the views directory. Static files, including a stock stylesheet, go in the public directory. Models go in the lib directory and are auto-loaded.
13 |
14 | == Testing
15 |
16 | Add your specs in spec; just require spec_helper.rb to pre-configure the test environment. A number of samples are provided (including a sample model, which can be removed). To run the specs:
17 |
18 | rake spec
19 |
20 | == Getting Started
21 |
22 | bundle install
23 | rake db:migrate
24 | ruby application.rb
25 |
26 | == Thanks
27 |
28 | This project includes contributions from the following developers:
29 |
30 | * garrensmith
31 | * bryanwoods
32 | * flexd
33 | * mcollina
34 |
35 | (c) 2011 Nick Plante. This code is distributed under the MIT license.
36 |
--------------------------------------------------------------------------------
/Gemfile.lock:
--------------------------------------------------------------------------------
1 | GEM
2 | remote: http://rubygems.org/
3 | specs:
4 | addressable (2.2.6)
5 | bcrypt-ruby (2.1.4)
6 | data_mapper (1.1.0)
7 | dm-aggregates (= 1.1.0)
8 | dm-constraints (= 1.1.0)
9 | dm-core (= 1.1.0)
10 | dm-migrations (= 1.1.0)
11 | dm-serializer (= 1.1.0)
12 | dm-timestamps (= 1.1.0)
13 | dm-transactions (= 1.1.0)
14 | dm-types (= 1.1.0)
15 | dm-validations (= 1.1.0)
16 | data_objects (0.10.6)
17 | addressable (~> 2.1)
18 | diff-lcs (1.1.2)
19 | dm-aggregates (1.1.0)
20 | dm-core (~> 1.1.0)
21 | dm-constraints (1.1.0)
22 | dm-core (~> 1.1.0)
23 | dm-core (1.1.0)
24 | addressable (~> 2.2.4)
25 | dm-do-adapter (1.1.0)
26 | data_objects (~> 0.10.2)
27 | dm-core (~> 1.1.0)
28 | dm-migrations (1.1.0)
29 | dm-core (~> 1.1.0)
30 | dm-serializer (1.1.0)
31 | dm-core (~> 1.1.0)
32 | fastercsv (~> 1.5.4)
33 | json (~> 1.4.6)
34 | dm-sqlite-adapter (1.1.0)
35 | dm-do-adapter (~> 1.1.0)
36 | do_sqlite3 (~> 0.10.2)
37 | dm-timestamps (1.1.0)
38 | dm-core (~> 1.1.0)
39 | dm-transactions (1.1.0)
40 | dm-core (~> 1.1.0)
41 | dm-types (1.1.0)
42 | bcrypt-ruby (~> 2.1.4)
43 | dm-core (~> 1.1.0)
44 | fastercsv (~> 1.5.4)
45 | json (~> 1.4.6)
46 | stringex (~> 1.2.0)
47 | uuidtools (~> 2.1.2)
48 | dm-validations (1.1.0)
49 | dm-core (~> 1.1.0)
50 | do_sqlite3 (0.10.6)
51 | data_objects (= 0.10.6)
52 | fastercsv (1.5.4)
53 | haml (3.1.2)
54 | json (1.4.6)
55 | rack (1.3.0)
56 | rack-test (0.6.0)
57 | rack (>= 1.0)
58 | rake (0.9.2)
59 | rspec (2.6.0)
60 | rspec-core (~> 2.6.0)
61 | rspec-expectations (~> 2.6.0)
62 | rspec-mocks (~> 2.6.0)
63 | rspec-core (2.6.4)
64 | rspec-expectations (2.6.0)
65 | diff-lcs (~> 1.1.2)
66 | rspec-mocks (2.6.0)
67 | sinatra (1.2.6)
68 | rack (~> 1.1)
69 | tilt (< 2.0, >= 1.2.2)
70 | stringex (1.2.1)
71 | tilt (1.3.2)
72 | uuidtools (2.1.2)
73 |
74 | PLATFORMS
75 | ruby
76 |
77 | DEPENDENCIES
78 | data_mapper
79 | dm-aggregates
80 | dm-core
81 | dm-migrations
82 | dm-sqlite-adapter
83 | dm-timestamps
84 | dm-validations
85 | haml
86 | rack-test
87 | rake
88 | rspec
89 | sinatra (>= 1.0)
90 |
--------------------------------------------------------------------------------