Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Wenzowski committed Oct 13, 2011
1 parent 2848f3b commit 4bf12a1
Show file tree
Hide file tree
Showing 14 changed files with 307 additions and 7 deletions.
88 changes: 88 additions & 0 deletions app/controllers/posts_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
class PostsController < ApplicationController
before_filter :authenticate_user!

# GET /posts
# GET /posts.xml
def index
@posts = Post.all

respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @posts }
end
end

# GET /posts/1
# GET /posts/1.xml
def show
@post = Post.find(params[:id])

respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @post }
end
end

# GET /posts/new
# GET /posts/new.xml
def new
@post = Post.new

respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @post }
end
end

# GET /posts/1/edit
def edit
@post = Post.find(params[:id])
@post.user = current_user
@post.lock! unless @post.locked?
end

# POST /posts
# POST /posts.xml
def create
@post = Post.new(params[:post])

respond_to do |format|
if @post.save
format.html { redirect_to(@post, :notice => 'Post was successfully created.') }
format.xml { render :xml => @post, :status => :created, :location => @post }
else
format.html { render :action => "new" }
format.xml { render :xml => @post.errors, :status => :unprocessable_entity }
end
end
end

# PUT /posts/1
# PUT /posts/1.xml
def update
@post = Post.find(params[:id])
@post.user = current_user

respond_to do |format|
if @post.update_attributes(params[:post])
format.html { redirect_to(@post, :notice => 'Post was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @post.errors, :status => :unprocessable_entity }
end
end
end

# DELETE /posts/1
# DELETE /posts/1.xml
def destroy
@post = Post.find(params[:id])
@post.destroy

respond_to do |format|
format.html { redirect_to(posts_url) }
format.xml { head :ok }
end
end
end
4 changes: 4 additions & 0 deletions app/models/lock.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Lock < ActiveRecord::Base
belongs_to :user
belongs_to :lockable, polymorphic: true
end
26 changes: 26 additions & 0 deletions app/models/post.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Post < ActiveRecord::Base
attr_accessor :user
has_many :locks, as: :lockable
validate :check_lock

def lock
@lock ||= self.locks.find(:first, order: 'created_at DESC')
end

def lock!
self.locks << Lock.new(user: @user) if @user
end

def locked?
return false unless self.lock
true if self.lock.created_at > 1.hour.ago
end

private

def check_lock
if self.locked?
errors.add(:locks, "Sorry, the post is locked by #{self.lock.user.email}") if self.locked? && self.lock.user != @user
end
end
end
1 change: 1 addition & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ class User < ActiveRecord::Base
:token_authenticatable

attr_accessible :email, :password, :password_confirmation
has_many :locks
end
25 changes: 25 additions & 0 deletions app/views/posts/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<%= form_for(@post) do |f| %>
<% if @post.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2>

<ul>
<% @post.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>

<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :body %><br />
<%= f.text_field :body %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
6 changes: 6 additions & 0 deletions app/views/posts/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<h1>Editing post</h1>

<%= render 'form' %>
<%= link_to 'Show', @post %> |
<%= link_to 'Back', posts_path %>
25 changes: 25 additions & 0 deletions app/views/posts/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<h1>Listing posts</h1>

<table>
<tr>
<th>Title</th>
<th>Body</th>
<th></th>
<th></th>
<th></th>
</tr>

<% @posts.each do |post| %>
<tr>
<td><%= post.title %></td>
<td><%= post.body %></td>
<td><%= link_to 'Show', post %></td>
<td><%= link_to 'Edit', edit_post_path(post) %></td>
<td><%= link_to 'Destroy', post, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>

<br />

<%= link_to 'New Post', new_post_path %>
5 changes: 5 additions & 0 deletions app/views/posts/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<h1>New post</h1>

<%= render 'form' %>
<%= link_to 'Back', posts_path %>
15 changes: 15 additions & 0 deletions app/views/posts/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<p id="notice"><%= notice %></p>

<p>
<b>Title:</b>
<%= @post.title %>
</p>

<p>
<b>Body:</b>
<%= @post.body %>
</p>


<%= link_to 'Edit', edit_post_path(@post) %> |
<%= link_to 'Back', posts_path %>
6 changes: 5 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
DeviseExample::Application.routes.draw do
resources :locks

resources :posts

devise_for :users, :admin

resources :home, :only => :index
resources :admins, :only => :index

root :to => 'home#index'
root :to => 'posts#index'

match '/token' => 'home#token', :as => :token
end
14 changes: 14 additions & 0 deletions db/migrate/20111013044839_create_posts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class CreatePosts < ActiveRecord::Migration
def self.up
create_table :posts do |t|
t.string :title
t.string :body

t.timestamps
end
end

def self.down
drop_table :posts
end
end
15 changes: 15 additions & 0 deletions db/migrate/20111013045407_create_locks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class CreateLocks < ActiveRecord::Migration
def self.up
create_table :locks do |t|
t.integer :user_id
t.integer :lockable_id
t.string :lockable_type

t.timestamps
end
end

def self.down
drop_table :locks
end
end
28 changes: 22 additions & 6 deletions db/schema.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# encoding: UTF-8
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
Expand All @@ -10,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20100615195947) do
ActiveRecord::Schema.define(:version => 20111013045407) do

create_table "admins", :force => true do |t|
t.string "email", :default => "", :null => false
Expand All @@ -19,21 +20,36 @@
t.datetime "updated_at"
end

create_table "locks", :force => true do |t|
t.integer "user_id"
t.integer "lockable_id"
t.string "lockable_type"
t.datetime "created_at"
t.datetime "updated_at"
end

create_table "posts", :force => true do |t|
t.string "title"
t.string "body"
t.datetime "created_at"
t.datetime "updated_at"
end

create_table "users", :force => true do |t|
t.string "email", :default => "", :null => false
t.string "encrypted_password", :limit => 128, :default => "", :null => false
t.string "email", :default => "", :null => false
t.string "encrypted_password", :limit => 128, :default => "", :null => false
t.string "confirmation_token"
t.datetime "confirmed_at"
t.datetime "confirmation_sent_at"
t.string "reset_password_token"
t.string "remember_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", :default => 0
t.integer "sign_in_count", :default => 0
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.integer "failed_attempts", :default => 0
t.integer "failed_attempts", :default => 0
t.string "unlock_token"
t.datetime "locked_at"
t.datetime "created_at"
Expand Down
56 changes: 56 additions & 0 deletions public/stylesheets/scaffold.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
body { background-color: #fff; color: #333; }

body, p, ol, ul, td {
font-family: verdana, arial, helvetica, sans-serif;
font-size: 13px;
line-height: 18px;
}

pre {
background-color: #eee;
padding: 10px;
font-size: 11px;
}

a { color: #000; }
a:visited { color: #666; }
a:hover { color: #fff; background-color:#000; }

div.field, div.actions {
margin-bottom: 10px;
}

#notice {
color: green;
}

.field_with_errors {
padding: 2px;
background-color: red;
display: table;
}

#error_explanation {
width: 450px;
border: 2px solid red;
padding: 7px;
padding-bottom: 0;
margin-bottom: 20px;
background-color: #f0f0f0;
}

#error_explanation h2 {
text-align: left;
font-weight: bold;
padding: 5px 5px 5px 15px;
font-size: 12px;
margin: -7px;
margin-bottom: 0px;
background-color: #c00;
color: #fff;
}

#error_explanation ul li {
font-size: 12px;
list-style: square;
}

0 comments on commit 4bf12a1

Please sign in to comment.