forked from scottatron/devise_example
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
14 changed files
with
307 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |