0
0
Fork 0

initial page for the blog itself and the admin panel

This commit is contained in:
Gibheer 2011-06-24 13:28:19 +02:00
parent f875c53fb0
commit d2e8e24bd4
8 changed files with 160 additions and 0 deletions

26
Rakefile Normal file
View File

@ -0,0 +1,26 @@
$LOAD_PATH << File.expand_path(__FILE__) + '/'
require 'libs'
require 'dm-migrations'
namespace :dm do
desc 'migrate to the database model'
task :migrate do
DataMapper.auto_migrate!
end
desc 'upgrade the database to the latest model'
task :upgrade do
DataMapper.auto_upgrade!
end
desc 'fill the database with dummy data from seeds.rb'
task :seed do
require 'seeds'
end
end
desc 'open a console with all libs loaded and a database connection opened'
task :console do
sh "irb -rubygems -I#{File.expand_path(File.dirname(__FILE__))} -r libs"
end

5
admin.rb Normal file
View File

@ -0,0 +1,5 @@
class Admin < Sinatra::Base
get '/' do
'das Adminpanel! und <a href="/">zurueck</a>'
end
end

View File

@ -0,0 +1,8 @@
$LOAD_PATH << File.expand_path(File.dirname(__FILE__)) + '/'
require 'libs'
use Rack::CommonLogger
run Rack::URLMap.new({
'/' => Blog,
'/admin' => Admin
})

20
libs.rb Normal file
View File

@ -0,0 +1,20 @@
# this file loads only the libs needed for using "something"
require 'rubygems'
require 'bundler/setup'
require 'bcrypt'
require 'data_mapper'
require 'sinatra'
require 'settings'
DataMapper::Logger.new($stdout, :debug) if Configuration.environment == :development
DataMapper.setup(:default, Configuration.dbconn)
# load the models
$LOAD_PATH << File.expand_path(File.dirname(__FILE__)) + '/models'
require 'account'
require 'post'
DataMapper.finalize
require 'page'
require 'admin'

57
models/account.rb Normal file
View File

@ -0,0 +1,57 @@
class Account
include DataMapper::Resource
include DataMapper::Validate
attr_accessor :password, :password_confirmation
# Properties
property :id, Serial
property :username, String, :lazy => false
property :email, String
property :crypted_password, String, :length => 70
property :role, String
has n, :posts
# Validations
validates_presence_of :username, :email, :role
validates_uniqueness_of :username, :case_sensitive => false
validates_presence_of :password, :if => :password_required
validates_presence_of :password_confirmation, :if => :password_required
validates_length_of :password, :min => 4, :max => 40, :if => :password_required
validates_confirmation_of :password, :if => :password_required
validates_length_of :email, :min => 3, :max => 100
validates_uniqueness_of :email, :case_sensitive => false
validates_format_of :email, :with => :email_address
validates_format_of :role, :with => /[A-Za-z]/
# Callbacks
before :save, :encrypt_password
##
# This method is for authentication purpose
#
def self.authenticate(username, password)
account = first(:conditions => { :username => username }) unless username.nil?
account && account.has_password?(password) ? account : nil
end
##
# This method is used by AuthenticationHelper
#
def self.find_by_id(id)
get(id) rescue nil
end
def has_password?(password)
::BCrypt::Password.new(crypted_password) == password
end
private
def password_required
crypted_password.nil? || password.present?
end
def encrypt_password
self.crypted_password = ::BCrypt::Password.create(password) unless password.nil?
end
end

13
models/post.rb Normal file
View File

@ -0,0 +1,13 @@
class Post
include DataMapper::Resource
include DataMapper::Validate
property :id, Serial
property :title, Text, :required => true
property :written, Time, :default => lambda { Time.now }
property :released, Boolean, :default => false
property :markup, Text, :default => 'textile'
property :content, Text
belongs_to :account
end

10
page.rb Normal file
View File

@ -0,0 +1,10 @@
class Blog < Sinatra::Base
set :logging, true
get '/' do
s = '<p><a href="/admin">Adminpanel</a></p>'
Post.all(:released => true, :order => [:written.desc]).each do |post|
s += "Post: #{post.title} von #{post.account.username}<br />"
end
s
end
end

21
seeds.rb Normal file
View File

@ -0,0 +1,21 @@
gib = Account.new(
:username => 'Gibheer',
:password => 'food',
:password_confirmation => 'food',
:email => 'foo@bar.com',
:role => 'admin'
)
gib.save
storm = Account.new(
:username => 'Stormwind',
:password => 'bard',
:password_confirmation => 'bard',
:email => 'bar@foo.com',
:role => 'admin'
)
storm.save
storm.posts.new(:title => 'bar', :content => 'das ist mein post!').save
gib.posts.new(:title => 'foo', :content => 'das ist meiner', :released => true).save
gib.posts.new(:title => 'lala', :content => 'lorem ipsum und so rum').save
storm.posts.new(:title => 'erster!', :content => 'ich bin ganz oben!', :released => true).save