Skip to content

Commit

Permalink
switch to activerecord, since the old version of sqlite on dreamhost …
Browse files Browse the repository at this point in the history
…doesn't like sequel's sql.
  • Loading branch information
robrasmussen committed Dec 29, 2008
1 parent e594a90 commit 69b5494
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 33 deletions.
27 changes: 8 additions & 19 deletions lib/post.rb
Expand Up @@ -3,26 +3,9 @@
$LOAD_PATH.unshift File.dirname(__FILE__) + '/../vendor/syntax'
require 'syntax/convertors/html'

class Post < Sequel::Model
unless table_exists?
set_schema do
primary_key :id
text :title
text :body
text :body_html
text :summary_html
text :slug
text :tags
timestamp :created_at
end
create_table
end
class Post < ActiveRecord::Base

before_save do
self.body_html = to_html(body)
# Maruku will occasionlly escape a trailing </p>, easier to strip it out than to figure out why.
self.summary_html = to_html(summary).gsub("&lt;/p&gt;", "")
end
before_save :cache_html

def url
d = created_at
Expand Down Expand Up @@ -77,4 +60,10 @@ def split_content(string)
end
[ to_html(show.join("\n\n")), hide.size > 0 ]
end

def cache_html
self.body_html = to_html(body)
# Maruku will occasionlly escape a trailing </p>, easier to strip it out than to figure out why.
self.summary_html = to_html(summary).gsub("&lt;/p&gt;", "")
end
end
19 changes: 9 additions & 10 deletions main.rb
@@ -1,11 +1,10 @@
require 'rubygems'
require 'sinatra'

$LOAD_PATH.unshift File.dirname(__FILE__) + '/vendor/sequel'
require 'sequel'
require 'activerecord'

configure do
Sequel.connect(ENV['DATABASE_URL'] || 'sqlite://blog.db')
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => "blog.db")

require 'ostruct'
Blog = OpenStruct.new(
Expand Down Expand Up @@ -45,12 +44,12 @@ def auth
### Public

get '/' do
posts = Post.reverse_order(:created_at).limit(10)
posts = Post.all(:order => "created_at DESC", :limit => 10)
erb :index, :locals => { :posts => posts }, :layout => false
end

get '/past/:year/:month/:day/:slug/' do
post = Post.filter(:slug => params[:slug]).first
post = Post.find_all_by_slug(params[:slug]).first
stop [ 404, "Page not found" ] unless post
@title = post.title
erb :post, :locals => { :post => post }
Expand All @@ -61,20 +60,20 @@ def auth
end

get '/past' do
posts = Post.reverse_order(:created_at)
posts = Post.all(:order => "created_at DESC")
@title = "Archive"
erb :archive, :locals => { :posts => posts }
end

get '/past/tags/:tag' do
tag = params[:tag]
posts = Post.filter(:tags.like("%#{tag}%")).reverse_order(:created_at).limit(30)
posts = Post.all(:conditions => ["tags like ?", "%#{tag}%"], :order => "created_at DESC", :limit => 30)
@title = "Posts tagged #{tag}"
erb :tagged, :locals => { :posts => posts, :tag => tag }
end

get '/feed' do
@posts = Post.reverse_order(:created_at).limit(10)
@posts = Post.all(:order => "created_at DESC", :limit => 10)
content_type 'application/atom+xml', :charset => 'utf-8'
builder :feed
end
Expand Down Expand Up @@ -108,14 +107,14 @@ def auth

get '/past/:year/:month/:day/:slug/edit' do
auth
post = Post.filter(:slug => params[:slug]).first
post = Post.find_all_by_slug(params[:slug]).first
stop [ 404, "Page not found" ] unless post
erb :edit, :locals => { :post => post, :url => post.url }
end

post '/past/:year/:month/:day/:slug/' do
auth
post = Post.filter(:slug => params[:slug]).first
post = Post.find_all_by_slug(params[:slug]).first
stop [ 404, "Page not found" ] unless post
post.title = params[:title]
post.tags = params[:tags]
Expand Down
12 changes: 9 additions & 3 deletions spec/base.rb
@@ -1,11 +1,17 @@
require 'rubygems'
require 'spec'

$LOAD_PATH.unshift File.join(File.dirname(__FILE__), "..", "vendor", "sequel")
require 'sequel'
require 'activerecord'

Sequel.sqlite
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")

ActiveRecord::Schema.define do
create_table :posts do |t|
t.text :title, :body, :body_html, :summary_html, :slug, :tags
t.timestamp :created_at
end
end

$LOAD_PATH.unshift(File.dirname(__FILE__) + '/../lib')
require 'post'

Expand Down
2 changes: 1 addition & 1 deletion spec/post_spec.rb
Expand Up @@ -37,7 +37,7 @@
@post.title = 'hello'
@post.body = 'world'
@post.save
Post.filter(:title => 'hello').first.body.should == 'world'
Post.find_all_by_title('hello').first.body.should == 'world'
end

it "generates a slug from the title (but saved to db on first pass so that url never changes)" do
Expand Down

0 comments on commit 69b5494

Please sign in to comment.