Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
iafonov committed Jan 26, 2009
0 parents commit 29ea4ed
Show file tree
Hide file tree
Showing 28 changed files with 476 additions and 0 deletions.
1 change: 1 addition & 0 deletions README
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Small blog written in Ruby (Sinatra, Haml+Sass, Activerecord)
46 changes: 46 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
require 'rubygems'
require 'spec/rake/spectask'
require 'sinatra'


Spec::Rake::SpecTask.new do |t|
t.ruby_opts = ['-rtest/unit']
t.spec_files = FileList['test/**/*_test.rb']
end



namespace 'db' do
task :create do
require 'activerecord'

ActiveRecord::Base.establish_connection(
:adapter => 'mysql',
:encoding => 'utf8',
:username => 'root',
:host => 'localhost',
:socket => '/var/lib/mysql/mysql.sock',
:database => 'blog_development'
)

ActiveRecord::Migration.create_table :posts do |t|
t.string :title
t.text :body

t.timestamps
end

ActiveRecord::Migration.create_table :comments do |t|
t.text :body
t.integer :post_id

t.timestamps
end
end
end

namespace 'gems' do
task :install do

end
end
3 changes: 3 additions & 0 deletions about.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
get '/about' do
haml :about_me
end
55 changes: 55 additions & 0 deletions blog.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
require 'rubygems'
require 'sinatra'
require 'activerecord'

require 'schema/schema.rb'

require 'util/flash.rb'
require 'util/sessions.rb'

get '/blog' do
@posts = Post.all()
haml :blog, :layout => :layout_blog
end

get '/posts/add' do secure
haml :post_add
end

post '/posts/add' do secure
Post.create(:title => params[:title], :body => params[:body])
set_flash 'New post created'
redirect '/blog'
end

get '/posts/edit/:id' do secure
@post = Post.find(params[:id])
haml :post_edit
end

post '/posts/edit/:id' do secure
post = Post.find(params[:id])
post.title = params[:title]
post.body = params[:body]
post.save
set_flash 'Post updated'
redirect '/blog'
end

get '/posts/delete/:id' do secure
Post.delete(params[:id])
set_flash 'Post was deleted'
redirect '/blog'
end

get '/posts/:id' do
@post = Post.find(params[:id])
haml :post_view, :layout => :layout_blog
end

post '/posts/:id/add_comment' do
post = Post.find(params[:id])
post.comments.create(:body => params[:body])
set_flash 'Comment added'
redirect '/posts/' + params[:id]
end
9 changes: 9 additions & 0 deletions config/rackup.ru
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
set :app_file, File.expand_path(File.dirname(__FILE__) + '/../main.rb')
set :public, File.expand_path(File.dirname(__FILE__) + '/../public')
set :views, File.expand_path(File.dirname(__FILE__) + '/../views')
set :env, :production
disable :run, :reload

require File.dirname(__FILE__) + "/../main"

run Sinatra.application
39 changes: 39 additions & 0 deletions configuration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
configure :development do
ActiveRecord::Base.establish_connection(
:adapter => 'mysql',
:encoding => 'utf8',
:username => 'root',
:host => 'localhost',
:socket => '/var/lib/mysql/mysql.sock',
:database => 'blog_development'
)

enable :sessions
end

configure :test do
ActiveRecord::Base.establish_connection(
:adapter => 'mysql',
:encoding => 'utf8',
:username => 'root',
:host => 'localhost',
:socket => '/var/lib/mysql/mysql.sock',
:database => 'blog_development'
)

enable :sessions
end

configure :development_home do #home
ActiveRecord::Base.establish_connection(
:adapter => 'mysql',
:encoding => 'utf8',
:username => 'root',
:password => '1',
:host => 'localhost',
:socket => '/var/run/mysqld/mysqld.sock',
:database => 'blog_development'
)

enable :sessions
end
3 changes: 3 additions & 0 deletions gallery.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
get '/gallery' do
haml :gallery
end
15 changes: 15 additions & 0 deletions helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'rubygems'
require 'haml'

helpers do
def partial(page, options={})
haml page, options.merge!(:layout => false)
end

def for_me(text)
if (@is_admin) then
engine = Haml::Engine.new(text)
return engine.render
end
end
end
20 changes: 20 additions & 0 deletions login.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
get '/login' do
haml :login
end

post '/login' do
if authenticate(params["password"])
redirect '/'
else
'Fuck you bitch!'
end
end

get '/logout' do
logout
redirect '/'
end

get '/version' do
Sinatra::VERSION
end
27 changes: 27 additions & 0 deletions main.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require 'rubygems'
require 'sinatra'
require 'activerecord'

require 'util/flash.rb'

load 'configuration.rb'
load 'helpers.rb'

before do
@flash = get_flash
@is_admin = session["i_am"]
end

get '/style.css' do
content_type 'text/css'
sass :style
end

get '/' do
redirect '/about'
end

load 'login.rb'
load 'about.rb'
load 'gallery.rb'
load 'blog.rb'
Binary file added public/images/formbg.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/logo.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/me.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/me2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions schema/schema.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Post < ActiveRecord::Base
has_many :comments
end

class Comment < ActiveRecord::Base
belongs_to :post
end
14 changes: 14 additions & 0 deletions test/spec_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require 'rubygems'
require 'sinatra'
require 'sinatra/test/rspec'

load 'main.rb'

describe 'My fucking site' do
it 'should show the about me page with my nice face' do
get '/blog'
print @response.inspect
@response.should be_ok
end

end
11 changes: 11 additions & 0 deletions util/flash.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'sinatra'

def get_flash
flash = request.cookies["flash"]
response.delete_cookie("flash")
return flash
end

def set_flash(message)
set_cookie("flash", :value => message, :path => '/')
end
16 changes: 16 additions & 0 deletions util/sessions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
def authenticate(password)
if 'fuck' == password then
session["i_am"] = true
return true
else
return false
end
end

def logout
session["i_am"] = false
end

def secure
redirect '/' if not @is_admin
end
5 changes: 5 additions & 0 deletions views/_navigation.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#navigation
%a{ :id => "t-about", :href => '/about' } About Me
%a{ :id => "t-blog", :href => '/blog' } Blog
%a{ :id => "t-gallery", :href => '/gallery' } Gallery
= for_me "%a{ :href => '/logout' } Logout"
43 changes: 43 additions & 0 deletions views/about_me.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
!!!
%html
%meta{"http-equiv" => 'Content-Type', "content" => 'text/html'}
%head
%title Igor Afonov
%link{ :rel=>"stylesheet", :href=>"/style.css", :type=>"text/css"}
%body{ :id => "about" }
= partial :_navigation
%p
%img{ :id => 'me', :src => '/me.jpg', :align => 'right' }
.mark Education
%p I was born in Donetsk in 1985. In 2007 I've graduated from Donetsk National Technical University (I've recieved master degree in CS and bachelor degree in buisness managment)......
%p I'm working as software developer..... Software development for me is more than just a work - it's my hobby. Specific areas of my professional interest include:
.mark Interests
Java - I have 2 years expirience in working with C++ (I've developed an enterprise 3D CAD system) and C++. Migrating from C++ to Java was like migrating from ... to ...
%ul
%li Java itself
%li Dependency injection containers (Google Guice, Tapestry IoC). I have expirience in building my own, custom IoC container based on annotations.
%li Dynamic languages built on top of JVM (Groovy, JRuby).
%li Web-frameworks: Tapestry 5, custom made frameworks....
Ruby - the main feature of ruby that attracts me is its outstanding flexibilty and code expressiveness....
%ul
%li
Ligtweight web-framework Sinatra (This site is powered by Sinatra,
%a{ :href => "/" } Read more...
)
%li Haml+Sass template engine
C++
%ul
%li I've worked for 1.5 years with C++ building 3D CAD application
%p
%p
You can contact me by
%a{:href => "mailto:afonov@gmail.com"} afonov@gmail.com
12 changes: 12 additions & 0 deletions views/blog.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
- for post in @posts
.post
.mark 22.01.2008
.post-title
%a{:href => "posts/#{post.id}"}= post.title
.post-body= post.body
.post-footer
%a{:href => "posts/#{post.id}#comments"}= "Comments(#{post.comments.count})"
= for_me "%a{ :href => 'posts/edit/#{post.id}' } Edit"
= for_me "%a{ :href => 'posts/delete/#{post.id}' } Delete"

= for_me "%a{ :href => 'posts/add' } Add new post"
8 changes: 8 additions & 0 deletions views/gallery.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
!!!
%html
%meta{"http-equiv" => 'Content-Type', "content" => 'text/html'}
%head
%title Micro blog
%link{ :rel=>"stylesheet", :href=>"/style.css", :type=>"text/css"}
%body{ :id => "gallery" }
= partial :_navigation
10 changes: 10 additions & 0 deletions views/layout_blog.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
!!!
%html
%meta{"http-equiv" => 'Content-Type', "content" => 'text/html'}
%head
%link{ :rel=>"stylesheet", :href=>"/style.css", :type=>"text/css"}
%body{ :id => "blog" }
= partial :_navigation
%br
#flash= @flash
= yield
4 changes: 4 additions & 0 deletions views/login.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<h2>Hi %username%!</h2>
%form{:method => "post"}
%input{:name => "password", :type => "password"}
%input{:type => "submit", :value => "Go!"}
10 changes: 10 additions & 0 deletions views/post_add.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
%h3 Adding new post:

%form{ :action => "add", :method => "post"}
Title:
%input{:name => "title"}
%br
Body:
%textarea{:name=>"body", :cols => 80, :rows => 40}
%br
%input{:type=>"submit"}
Loading

0 comments on commit 29ea4ed

Please sign in to comment.