examples/blog.rb in camping-2.1.532 vs examples/blog.rb in camping-3.0.0
- old
+ new
@@ -12,28 +12,28 @@
include Camping::Session
module Models
class Post < Base
belongs_to :user
-
+
before_save do |record|
cloth = RedCloth.new(record.body)
cloth.hard_breaks = false
record.html_body = cloth.to_html
end
end
-
+
class Comment < Base; belongs_to :user; end
class User < Base; end
class BasicFields < V 1.1
def self.up
create_table :blog_posts, :force => true do |t|
t.integer :user_id, :null => false
t.string :title, :limit => 255
t.text :body, :html_body
- t.timestamps
+ t.timestamps
end
create_table :blog_users, :force => true do |t|
t.string :username, :password
end
create_table :blog_comments, :force => true do |t|
@@ -42,11 +42,11 @@
t.text :body, :html_body
t.timestamps
end
User.create :username => 'admin', :password => 'camping'
end
-
+
def self.down
drop_table :blog_posts
drop_table :blog_users
drop_table :blog_comments
end
@@ -65,21 +65,21 @@
def get
require_login!
@post = Post.new
render :add
end
-
+
def post
require_login!
post = Post.create(:title => input.post_title, :body => input.post_body,
:user_id => @state.user_id)
redirect PostN, post
end
end
class PostN
- def get(post_id)
+ def get(post_id)
@post = Post.find(post_id)
render :view
end
end
@@ -91,30 +91,30 @@
end
def post(post_id)
require_login!
@post = Post.find(post_id)
- @post.update_attributes :title => input.post_title, :body => input.post_body
- redirect PostN, @post
+ @post.update :title => input.post_title, :body => input.post_body
+ redirect PostN, @post
end
end
class Login
def get
render :login
end
-
+
def post
@user = User.find_by_username_and_password(input.username, input.password)
if @user
@state.user_id = @user.id
redirect R(Index)
else
@info = 'Wrong username or password.'
end
-
+
render :login
end
end
class Logout
@@ -126,21 +126,21 @@
class Style < R '/styles\.css'
STYLE = File.read(__FILE__).gsub(/.*__END__/m, '')
def get
- @headers['Content-Type'] = 'text/css; charset=utf-8'
+ @headers['content-type'] = 'text/css; charset=utf-8'
STYLE
end
end
end
-
+
module Helpers
def logged_in?
!!@state.user_id
end
-
+
def require_login!
unless logged_in?
redirect Controllers::Login
throw :halt
end
@@ -150,30 +150,30 @@
module Views
def layout
html do
head do
title 'My Blog'
- link :rel => 'stylesheet', :type => 'text/css',
+ link :rel => 'stylesheet', :type => 'text/css',
:href => '/styles.css', :media => 'screen'
end
body do
h1 { a 'My Blog', :href => R(Index) }
-
+
div.wrapper! do
- text yield
+ self << yield
end
-
+
hr
-
+
p.footer! do
if logged_in?
_admin_menu
else
a 'Login', :href => R(Login)
text ' to the adminpanel'
end
- text ' – Powered by '
+ text! ' – Powered by '
a 'Camping', :href => 'http://camping.rubyforge.org/'
end
end
end
end
@@ -194,14 +194,14 @@
end
def login
h2 'Login'
p.info @info if @info
-
+
form :action => R(Login), :method => 'post' do
input :name => 'to', :type => 'hidden', :value => @to if @to
-
+
label 'Username', :for => 'username'
input :name => 'username', :id => 'username', :type => 'text'
label 'Password', :for => 'password'
input :name => 'password', :id => 'password', :type => 'password'
@@ -222,35 +222,35 @@
_post(@post)
end
# partials
def _admin_menu
- text [['Log out', R(Logout)], ['New', R(PostNew)]].map { |name, to|
- capture { a name, :href => to}
+ text! [['Log out', R(Logout)], ['New', R(PostNew)]].map { |name, to|
+ mab { a name, :href => to}
}.join(' – ')
end
def _post(post)
h2 { a post.title, :href => R(PostN, post) }
p.info do
- text "Written by <strong>#{post.user.username}</strong> "
+ text! "Written by <strong>#{post.user.username}</strong> "
text post.updated_at.strftime('%B %M, %Y @ %H:%M ')
_post_menu(post)
end
- text post.html_body
+ text! post.html_body
end
-
+
def _post_menu(post)
if logged_in?
a '(edit)', :href => R(Edit, post)
end
end
def _form(post, opts)
form({:method => 'post'}.merge(opts)) do
label 'Title', :for => 'post_title'
- input :name => 'post_title', :id => 'post_title', :type => 'text',
+ input :name => 'post_title', :id => 'post_title', :type => 'text',
:value => post.title
label 'Body', :for => 'post_body'
textarea post.body, :name => 'post_body', :id => 'post_body'
@@ -279,16 +279,16 @@
h1, h2, h3, h4 {
font-family: Georgia, serif;
font-weight: normal;
}
-h1 {
+h1 {
background-color: #EEE;
border-bottom: 5px solid #6F812D;
- outline: 5px solid #9CB441;
+ outline: 5px solid #9CB441;
font-weight: normal;
- font-size: 3em;
+ font-size: 3em;
padding: 0.5em 0;
text-align: center;
}
h2 {
@@ -299,11 +299,11 @@
h1 a { color: #143D55; text-decoration: none }
h1 a:hover { color: #143D55; text-decoration: underline }
h2 a { color: #287AA9; text-decoration: none }
h2 a:hover { color: #287AA9; text-decoration: underline }
-#wrapper {
+#wrapper {
margin: 3em auto;
width: 700px;
}
p {
@@ -331,32 +331,32 @@
color: #9CB441;
}
hr {
border-width: 5px 0;
- border-style: solid;
+ border-style: solid;
border-color: #9CB441;
border-bottom-color: #6F812D;
- height: 0;
+ height: 0;
}
-p#footer {
+p#footer {
font-size: 0.9em;
- margin: 0;
+ margin: 0;
padding: 1em;
text-align: center;
}
-label {
+label {
display: block;
width: 100%;
}
input, textarea {
- padding: 5px;
+ padding: 5px;
margin-bottom: 1em;
margin-right: 490px;
- width: 200px;
+ width: 200px;
}
input.submit {
width: auto;
}