Created
February 4, 2009 06:22
-
-
Save kematzy/57985 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CONFUSED SINATRA APP | |
Basic code is this: [http://gist.github.com/57920] with the following at the top | |
of the file: | |
# set the root of the whole app | |
APP_ROOT = File.expand_path(File.dirname(__FILE__)) unless defined?(APP_ROOT) | |
# running on Edge?, if so grab that version | |
$LOAD_PATH.unshift "#{APP_ROOT}/vendor/sinatra/lib" if test(?d, "#{APP_ROOT}/vendor/sinatra") | |
require 'rubygems' | |
..... | |
... and as the last line of file | |
MyApp.run! | |
============================================= | |
IN THE CLI... (OS X Terminal) | |
## start app | |
$ ruby myapp.rb | |
== Sinatra/0.9.0.4 has taken the stage on 4567 for development with backup from Mongrel | |
## output after loading http://localhost:4567/about | |
./myapp.rb:23: warning: already initialized constant Settings | |
./myapp.rb:24: warning: already initialized constant Page | |
== Sinatra/0.9.0.4 has taken the stage on 4567 for development with backup from Mongrel | |
== Someone is already performing on port 4567! | |
127.0.0.1 - - [04/Feb/2009 14:19:36] "GET /about/ HTTP/1.1" 200 2321 0.0042 | |
./myapp.rb:23: warning: already initialized constant Settings | |
./myapp.rb:24: warning: already initialized constant Page | |
== Sinatra/0.9.0.4 has taken the stage on 4567 for development with backup from Mongrel | |
== Someone is already performing on port 4567! | |
./myapp.rb:23: warning: already initialized constant Settings | |
./myapp.rb:24: warning: already initialized constant Page | |
== Sinatra/0.9.0.4 has taken the stage on 4567 for development with backup from Mongrel | |
== Someone is already performing on port 4567! | |
./myapp.rb:23: warning: already initialized constant Settings | |
./myapp.rb:24: warning: already initialized constant Page | |
== Sinatra/0.9.0.4 has taken the stage on 4567 for development with backup from Mongrel | |
== Someone is already performing on port 4567! | |
127.0.0.1 - - [04/Feb/2009 14:19:36] "GET /favicon.ico HTTP/1.1" 404 420 0.0222 | |
127.0.0.1 - - [04/Feb/2009 14:19:36] "GET /js/jquery.js HTTP/1.1" 200 31033 0.1562 | |
./myapp.rb:23: warning: already initialized constant Settings | |
./myapp.rb:24: warning: already initialized constant Page | |
== Sinatra/0.9.0.4 has taken the stage on 4567 for development with backup from Mongrel | |
== Someone is already performing on port 4567! | |
127.0.0.1 - - [04/Feb/2009 14:19:36] "GET /images/sinatra-logo.gif HTTP/1.1" 200 5337 0.0042 | |
127.0.0.1 - - [04/Feb/2009 14:19:36] "GET /stylesheets/screen.css HTTP/1.1" 200 15919 0.2258 | |
./myapp.rb:23: warning: already initialized constant Settings | |
./myapp.rb:24: warning: already initialized constant Page | |
== Sinatra/0.9.0.4 has taken the stage on 4567 for development with backup from Mongrel | |
== Someone is already performing on port 4567! | |
127.0.0.1 - - [04/Feb/2009 14:19:36] "GET /stylesheets/images/grid.png HTTP/1.1" 404 436 0.0006 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## filepath: lib/helpers.rb | |
module Sinatra | |
# a basic convenience method for outputting the Sinatra version text on page | |
# Usage: | |
# <b>Usage:</b> | |
# >> Sinatra.version | |
# => "Sinatra vX.XX" | |
# | |
def self.version; "Sinatra v#{self::VERSION}"; end; | |
# Application wide helpers | |
# | |
# | |
# | |
module Helpers | |
# Add your own helpers here | |
## | |
## Pre-defined helpers | |
## | |
# | |
def host | |
request.host | |
end | |
# | |
def path_info | |
request.path_info | |
end | |
def path_info_to_form_id | |
request.path_info.sub(/^\//,'').gsub(/[^A-Za-z0-9_-]/,'').to_s | |
end | |
# | |
def path_controller_name | |
pi = request.path_info | |
(pi == '/') ? 'home' : pi.split('/')[1].gsub(/[^A-Za-z0-9_-]/,'').to_s | |
end | |
# | |
def path_action_name | |
request.path_info.split('/')[2].gsub(/[^A-Za-z0-9_-]/,'').to_s rescue nil | |
end | |
# | |
def page_body(format=:erb, options={}) | |
prefix = options.delete(:prefix) | |
id = prefix.nil? ? path_controller_name : "#{prefix}_#{path_controller_name}" | |
c = path_action_name.nil? ? nil : path_action_name | |
if format == :haml | |
{ :id => id, :class => c } | |
else | |
out = "<body" | |
out += %{ id="#{id}"} unless id.nil? | |
out += %{ class="#{c}"} unless c.nil? | |
out += ">" | |
end | |
end | |
# | |
def page_header | |
erb :'shared/header', :layout => false | |
end | |
# | |
def page_nav_bar | |
erb :'shared/navbar', :layout => false | |
end | |
# | |
def page_footer | |
erb :'shared/footer', :layout => false | |
end | |
# | |
# __METHOD_DESC__ | |
# | |
# <b>Usage:</b> | |
# >> | |
# => | |
# | |
def page_debug(options={}) | |
options = {:env => :development, :enabled => true }.merge(options) | |
if Sinatra::Base.environment == options[:env] && options[:enabled] | |
erb :'shared/debug', :layout => false | |
end | |
end | |
## Next two methods based on the Rails YearAfterYear plugin by Robby Russell, MIT license. | |
# Renders the current year: ie: 2008 | |
# <b>Usage:</b> | |
# >> current_year | |
# => 2008 | |
# | |
def current_year | |
Time.now.strftime('%Y') | |
end | |
# Returns the range of years | |
# | |
# <b>Usage:</b> | |
# >> year_range | |
# => 2008 (current year) | |
# | |
# >> year_range(current_year) | |
# => 2008 (current year) | |
# | |
# >> year_range(2005) | |
# => 2005-2008 (current year) | |
# | |
def year_range(start_year=nil) | |
if start_year.nil? | |
current_year | |
elsif start_year.to_s == current_year | |
current_year | |
else | |
start_year.to_s + '-' + current_year | |
end | |
end | |
end #/module Helpers | |
# class EventContext | |
# include Helpers | |
# end | |
end #/module Sinatra |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## filepath: app/models/models.rb | |
%w(dm-core dm-serializer dm-validations dm-timestamps).each { |lib| require lib } | |
class Post | |
include DataMapper::Resource | |
include DataMapper::Serialize | |
property :id, Serial, :key => true | |
property :post_category_id, Integer | |
property :title, String | |
property :subheading, String | |
property :body, Text | |
# when was this record created/updated | |
property :created_at, DateTime | |
property :updated_at, DateTime | |
belongs_to :post_category | |
has n, :comments | |
end | |
class Comment | |
include DataMapper::Resource | |
include DataMapper::Serialize | |
property :id, Serial, :key => true | |
property :post_id, Integer | |
property :body, Text | |
# when was this record created/updated | |
property :created_at, DateTime | |
property :updated_at, DateTime | |
belongs_to :post | |
end | |
class PostCategory | |
include DataMapper::Resource | |
property :id, Integer, :serial => true | |
property :name, String | |
# when was this record created/updated | |
property :created_at, DateTime | |
property :updated_at, DateTime | |
has n, :posts | |
end | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## filepath: myapp.rb | |
# set the root of the whole app | |
APP_ROOT = File.expand_path(File.dirname(__FILE__)) unless defined?(APP_ROOT) | |
# # running on Edge?, if so grab that version | |
# $LOAD_PATH.unshift "#{APP_ROOT}/vendor/sinatra/lib" if test(?d, "#{APP_ROOT}/vendor/sinatra") | |
require 'rubygems' | |
%w(sinatra/base compass ostruct ).each {|g| require g } | |
class MyApp < Sinatra::Default | |
enable :static, :logging | |
set :app_file, __FILE__ | |
set :public, "#{root}/public" | |
set :views, "#{root}/app/views" | |
set :haml, :format => :html4, :attr_wrapper => '"' | |
set :sass, :style => :compact | |
load "#{root}/app/models/models.rb" | |
load "#{root}/lib/helpers.rb" | |
## Import the Site Configs | |
::Settings = ::OpenStruct.new(YAML.load_file("#{root}/config/settings.yml")) unless defined?(::Settings) | |
::Page = ::OpenStruct.new(:title => "Undefined") unless defined?(::Page) | |
configure do | |
::DataMapper.setup(:default, "sqlite3:db/#{environment}.sqlite3") | |
::DataMapper.auto_upgrade! | |
::DataMapper.logger.set_log('log/dm.log', :debug) | |
end | |
enable :reload, :run if development? | |
# helpers | |
alias_method :h, :escape_html | |
get '/' do | |
@title = 'HOME' | |
@posts = Post.all | |
erb :index | |
# erb :index, :layout => :application | |
end | |
get '/about/?' do | |
erb :about | |
end | |
get '/sinatra' do | |
out = "Powered by Sinatra v#{Sinatra::VERSION} running in '#{Sinatra::Base.environment}' mode.\n" | |
erb( out ) | |
end | |
get "/stylesheets/*.css", :layout => false do | |
content_type 'text/css', :charset => 'utf-8' | |
# Use views/stylesheets & blueprint's stylesheet dirs in the Sass load path | |
sass "stylesheets/#{params[:splat]}".to_sym, { | |
:sass => { | |
:style => :compact, | |
:load_paths => ( | |
[ File.join(APP_ROOT, 'app', 'views', 'stylesheets') ] + Compass::Frameworks::ALL.map { |f| f.stylesheets_directory } | |
) | |
} | |
} | |
end | |
end | |
MyApp.run! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## filepath: config/settings.yml | |
### SinatraApp Settings | |
site_title: 'Default Site Title' | |
client_name: 'Client Name Ltd.' | |
header_h1: 'Client Name' | |
meta_description: "Edit this text to make a meta description." | |
meta_keywords: "add, your, website, keywords, here, as, a, comma, delimited, string" | |
## GENERAL INFORMATION:: | |
# DEV CREDIT | |
dev_name: "kematzy" | |
dev_url: "kematzy.com" | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## filepath: lib/sinatra.rb | |
module Sinatra | |
# ripped from cschneid [http://github.com/cschneid/irclogger/blob/master/lib/partials.rb] | |
module Partials | |
# | |
# | |
def partial(template, *args) | |
options = args.extract_options! | |
options.merge!(:layout => false) | |
if collection = options.delete(:collection) then | |
collection.inject([]) do |buffer, member| | |
buffer << erb(template, options.merge(:layout =>false, :locals => {template.to_sym => member})) | |
end.join("\n") | |
else | |
erb(template, options) | |
end | |
end #/partial | |
end #/module Partials | |
end #/module Sinatra |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## All the files in the views dir | |
## filepath: app/views/layout.erb | |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> | |
<html lang="en"> | |
<head> | |
<meta http-equiv="Content-type" content="text/html; charset=utf-8"> | |
<meta name="Content-Language" content="en-us"> | |
<title><%= ::Page.title %> » <%= Settings.site_title %></title> | |
<meta name="author" content="<%= Settings.dev_name %>"> | |
<meta name="description" content="<%= Settings.meta_description %>"> | |
<meta name="keywords" content="<%= Settings.meta_keywords %>"> | |
<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon"> | |
<meta name="Copyright" content="(c) <%#= year_range %> Copyright content: <%= Settings.client_name %>; Copyright design: <%= Settings.dev_url %>"> | |
<!-- (c) <%= year_range %> Copyright content: <%= Settings.client_name %>; Copyright design: <%= Settings.dev_url %> All Rights Reserved. --> | |
<%#= page_cached_timestamp %> | |
<link rel="stylesheet" href="/stylesheets/screen.css" type="text/css" media="screen" charset="utf-8"> | |
<script src="/js/jquery.js" type="text/javascript" charset="utf-8"></script> | |
</head> | |
<%= page_body(:erb) %> | |
<!-- div.container --> | |
<div class="container showgrid"> | |
<%= page_header %> | |
<%= page_nav_bar %> | |
<%#= page_msg_bar %> | |
<%= yield %> | |
<%= page_footer %> | |
</div> | |
<!-- /div.container --> | |
<p id="credit"> | |
<a href="http://<%= Settings.dev_url %>/" title="designed and developed by <%= Settings.dev_url %>" rel="external" onclick="this.target='_blank'">developed by <strong><%= Settings.dev_name %></strong></a> | |
<br> | |
powered by <a href="http://sinatrarb.com/" rel="external" title="learn more about this fantastic framework [opens in new window]"><%= Sinatra.version %></a> | |
</p> | |
<%= page_debug(:enabled => false) %> | |
</body> | |
</html> | |
============ | |
## filepath: app/views/index.erb | |
<% ::Page.title = "HOME" %> | |
<h2><%= ::Page.title %></h2> | |
<p>This is the HOME page</p> | |
============ | |
## filepath: app/views/about.erb | |
<% ::Page.title = "About" %> | |
<h2><%= ::Page.title %></h2> | |
<p>This is the about page</p> | |
============ | |
## filepath: app/views/stylesheets/screen.sass | |
/* SITE SPECIFIC STUFF | |
body | |
:margin 2em auto | |
:background-color #C9D5DD | |
:font-family "Lucida Grande","Lucida Sans Unicode", Helvetica, Arial, Verdana, sans-serif | |
:font-size 70% | |
.container | |
:width 960px | |
:background #fff | |
:border-top 1px solid #aaa | |
:border-right 1px solid #aaa | |
:border-bottom 2px solid #999 | |
:border-left 1px solid #aaa | |
#header | |
:float left | |
:width 400px | |
:padding-right 280px | |
:padding-left 280px | |
:margin-right 0 | |
:text-align center | |
img#logo | |
:margin 1em 0 0.8em 0 | |
h1 | |
:display none | |
#footer | |
:border-top 1px solid #dedede | |
:background-color #fefefe | |
:margin 2em 0 0 0 | |
:padding 0 | |
p | |
:padding 1em 0 0 40px | |
#main-content | |
:float left | |
:width 960px | |
:margin-top 4em | |
:margin-right 0px | |
:padding-left 40px | |
:padding-right 40px | |
#nav p | |
:text-align center | |
a | |
:text-decoration none | |
:color #879BBF | |
a:hover | |
:color #637491 | |
:border-bottom 2px solid #ccc | |
/* /END SITE SPECIFIC STUFF | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment