Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'rubygems'
- require 'camping'
- require 'camping/db'
- require 'camping/session'
- require 'redcloth'
- require 'mime/types'
- require 'lib/taggable'
- Camping.goes :Blog
- module Blog
- include Camping::Session
- class AuthenticationException < StandardError; end
- module Controllers
- class Index < R '/'
- def get
- @entries = Entry.find_latest_10
- render :index
- end
- end
- class Archives
- def get
- @entries = Entry.find_all
- render :all
- end
- end
- class Post < R '/p/([a-z0-9\-]+)/edit', '/post'
- def get(p=nil)
- render :error_404 unless logged_in?
- unless p.blank?
- begin
- @entry = Entry.find_by_permalink(p)
- rescue ActiveRecord::RecordNotFound
- render :error_404
- else
- render :edit
- end
- else
- render :post
- end
- end
- def post(p=nil)
- render :error_404 unless logged_in?
- end
- end
- class View < R '/p/([a-z0-9\-]+)'
- def get(p)
- @entry = Entry.find_by_permalink(p)
- rescue ActiveRecord::RecordNotFound
- render :error_404
- else
- render :view
- end
- end
- class Login < R '/sign-in'
- def get
- render :login
- end
- def post
- begin
- @user = User.authenticate(input.email, input.password)
- @state.user = @user.id
- rescue AuthenticationException => e
- @error = e.message
- render :login
- else
- redirect Index
- end
- end
- end
- class Static < R '/static/(.+)'
- PATH = File.dirname(__FILE__)
- def get(f)
- @headers['Content-type'] = (MIME::Types.type_for(f)[0] || 'text/plain')
- unless File.readable? File.join(PATH, 'static', f)
- render :error_404
- else
- File.read(File.join(PATH, 'static', f))
- end
- end
- end
- class Logout < R '/signout'
- def get
- @state.user = nil
- redirect Index
- end
- end
- protected
- def logged_in?
- !@state.user.blank? == true
- end
- end
- module Views
- def index
- end
- def login
- end
- def error_404
- h1 'Oops'
- p 'Whatever you were looking for? It ain\'t here.'
- end
- def layout
- html do
- head do
- title 'blog'
- link :href => R(Static, 'style.css'), :rel => "stylesheet", :type => "text/css"
- end
- body do
- h1 do
- a 'blog', :href => R(Index)
- end
- div.menu! do
- ul do
- li { a 'home', :href => R(Index) }
- li { a 'archives', :href => R(Archives) }
- li { a 'about', :href => R(Static, 'about.html') }
- end
- end
- hr
- self << yield
- hr
- p 'Copyright (c) 2008 Cameron Cox'
- end
- end
- end
- end
- module Models
- class Tag < Base
- validates_uniqueness_of :name
- validates_presence_of :name
- end
- class Comment < Base
- belongs_to :entry
- validates_presence_of :email, :name, :body
- validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
- # validates_format_of :website, :with => /\A\Z/i, :if => proc { |comment| !comment.website.blank? } # -- lazy ass :D
- end
- class Entry < Base
- acts_as_taggable :tag_class_name => 'Blog::Models::Tag'
- belongs_to :user
- has_many :comments
- def self.find_latest_10
- find(:all, :limit => 10, :order => "created_at DESC")
- end
- def find_all
- find(:all, :order => "created_at DESC")
- end
- def self.find_by_permalink(permalink)
- entry = find(:first, :conditions => ["permalink = ?", permalink])
- raise ActiveRecord::RecordNotFound, "No Entry with permalink=#{permalink}." unless entry
- entry
- end
- end
- class User < Base
- validates_presence_of :username, :email
- validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
- validates_uniqueness_of :username, :email
- validates_presence_of :password, :password_confirmation, :if => :password_required?
- validates_confirmation_of :password, :if => :password_required?
- validates_length_of :password, :minimum => 6, :if => :password_required?
- after_validation :hash_password_if_neccessary
- attr_accessor :password, :password_confirmation
- def self.authenticate(email, password)
- user = find(:first, :conditions => ["email = ?", email])
- raise AuthenticationException.new('User does not exist.') unless user
- raise AuthenticationException.new('Incorrect Password.') unless user.verify_password(password)
- user
- end
- def verify_password(password)
- self.hashed_password == hash_password(password)
- end
- protected
- def self.crypt(data, salt)
- Digest::SHA1.hexdigest("--#{data}--#{salt}--")
- end
- def hash_password(password)
- self.class.crypt(password, self.salt)
- end
- def hash_password_if_neccessary
- self.salt ||= self.class.crypt(self.object_id, Time.now.to_s)
- self.hashed_password = hash_password(self.password)
- end
- def password_required?
- hashed_password.blank? || !password.blank?
- end
- end
- class CreateBlogTables < V 1.0
- def self.up
- create_table :blog_tags do |t|
- t.string :name
- t.timestamps
- end
- create_table :blog_tags_entries, :id => false do |t|
- t.integer :tag_id, :entry_id
- end
- create_table :blog_users do |t|
- t.string :email, :username, :hashed_password, :salt
- t.timestamps
- end
- create_table :blog_entries do |t|
- t.string :permalink, :title
- t.text :body
- t.integer :user_id
- t.timestamps
- end
- create_table :blog_comments do |t|
- t.string :email, :name, :website
- t.text :body
- t.integer :entry_id
- t.timestamps
- end
- end
- def self.down
- drop_table :blog_tags
- drop_table :blog_tags_entries
- drop_table :blog_users
- drop_table :blog_entries
- drop_table :blog_comments
- end
- end
- end
- def Blog.create
- Models.create_schema
- end
- # protected
- # def render(m, layout = true)
- # content = Erubis::Eruby.new(IO.read(File.join(File.dirname(__FILE__), 'templates', "#{m}.erb"))).result(binding)
- # content = Erubis::Eruby.new(IO.read(File.join(File.dirname(__FILE__), 'templates', 'layout.erb'))).result(binding) if layout
- #
- # return content
- # end
- end
Add Comment
Please, Sign In to add comment