Initial commit
This commit is contained in:
BIN
app/assets/images/rails.png
Normal file
BIN
app/assets/images/rails.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 6.5 KiB |
15
app/assets/javascripts/application.js
Normal file
15
app/assets/javascripts/application.js
Normal file
@@ -0,0 +1,15 @@
|
||||
// This is a manifest file that'll be compiled into application.js, which will include all the files
|
||||
// listed below.
|
||||
//
|
||||
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
|
||||
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
|
||||
//
|
||||
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
|
||||
// the compiled file.
|
||||
//
|
||||
// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
|
||||
// GO AFTER THE REQUIRES BELOW.
|
||||
//
|
||||
//= require jquery
|
||||
//= require jquery_ujs
|
||||
//= require_tree .
|
||||
3
app/assets/javascripts/posts.js.coffee
Normal file
3
app/assets/javascripts/posts.js.coffee
Normal file
@@ -0,0 +1,3 @@
|
||||
# Place all the behaviors and hooks related to the matching controller here.
|
||||
# All this logic will automatically be available in application.js.
|
||||
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
|
||||
13
app/assets/stylesheets/application.css
Normal file
13
app/assets/stylesheets/application.css
Normal file
@@ -0,0 +1,13 @@
|
||||
/*
|
||||
* This is a manifest file that'll be compiled into application.css, which will include all the files
|
||||
* listed below.
|
||||
*
|
||||
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
|
||||
* or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
|
||||
*
|
||||
* You're free to add application-wide styles to this file and they'll appear at the top of the
|
||||
* compiled file, but it's generally better to create a new file per style scope.
|
||||
*
|
||||
*= require_self
|
||||
*= require_tree .
|
||||
*/
|
||||
3
app/assets/stylesheets/posts.css.scss
Normal file
3
app/assets/stylesheets/posts.css.scss
Normal file
@@ -0,0 +1,3 @@
|
||||
// Place all the styles related to the Posts controller here.
|
||||
// They will automatically be included in application.css.
|
||||
// You can use Sass (SCSS) here: http://sass-lang.com/
|
||||
69
app/assets/stylesheets/scaffolds.css.scss
Normal file
69
app/assets/stylesheets/scaffolds.css.scss
Normal file
@@ -0,0 +1,69 @@
|
||||
body {
|
||||
background-color: #fff;
|
||||
color: #333;
|
||||
font-family: verdana, arial, helvetica, sans-serif;
|
||||
font-size: 13px;
|
||||
line-height: 18px;
|
||||
}
|
||||
|
||||
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;
|
||||
&:visited {
|
||||
color: #666;
|
||||
}
|
||||
&:hover {
|
||||
color: #fff;
|
||||
background-color: #000;
|
||||
}
|
||||
}
|
||||
|
||||
div {
|
||||
&.field, &.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;
|
||||
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;
|
||||
}
|
||||
ul li {
|
||||
font-size: 12px;
|
||||
list-style: square;
|
||||
}
|
||||
}
|
||||
3
app/controllers/application_controller.rb
Normal file
3
app/controllers/application_controller.rb
Normal file
@@ -0,0 +1,3 @@
|
||||
class ApplicationController < ActionController::Base
|
||||
protect_from_forgery
|
||||
end
|
||||
96
app/controllers/posts_controller.rb
Normal file
96
app/controllers/posts_controller.rb
Normal file
@@ -0,0 +1,96 @@
|
||||
class PostsController < ApplicationController
|
||||
load_and_authorize_resource
|
||||
|
||||
#####
|
||||
## Comment this line to allow guest users to edit and supply random user_ids
|
||||
before_filter :authorize_update_for_real, :only => :update
|
||||
#####
|
||||
|
||||
# GET /posts
|
||||
# GET /posts.json
|
||||
def index
|
||||
#@posts = Post.all
|
||||
|
||||
respond_to do |format|
|
||||
format.html # index.html.erb
|
||||
format.json { render json: @posts }
|
||||
end
|
||||
end
|
||||
|
||||
# GET /posts/1
|
||||
# GET /posts/1.json
|
||||
def show
|
||||
#@post = Post.find(params[:id])
|
||||
|
||||
respond_to do |format|
|
||||
format.html # show.html.erb
|
||||
format.json { render json: @post }
|
||||
end
|
||||
end
|
||||
|
||||
# GET /posts/new
|
||||
# GET /posts/new.json
|
||||
def new
|
||||
#@post = Post.new
|
||||
|
||||
respond_to do |format|
|
||||
format.html # new.html.erb
|
||||
format.json { render json: @post }
|
||||
end
|
||||
end
|
||||
|
||||
# GET /posts/1/edit
|
||||
def edit
|
||||
#@post = Post.find(params[:id])
|
||||
end
|
||||
|
||||
# POST /posts
|
||||
# POST /posts.json
|
||||
def create
|
||||
#@post = Post.new(params[:post])
|
||||
|
||||
respond_to do |format|
|
||||
if @post.save
|
||||
format.html { redirect_to posts_url, notice: 'Post was successfully created.' }
|
||||
format.json { render json: @post, status: :created, location: @post }
|
||||
else
|
||||
format.html { render action: "new" }
|
||||
format.json { render json: @post.errors, status: :unprocessable_entity }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def authorize_update_for_real
|
||||
@post = Post.find(params[:id])
|
||||
@post.assign_attributes(params[:post])
|
||||
authorize! :edit, @post
|
||||
end
|
||||
|
||||
# PUT /posts/1
|
||||
# PUT /posts/1.json
|
||||
def update
|
||||
#@post = Post.find(params[:id])
|
||||
|
||||
respond_to do |format|
|
||||
if @post.update_attributes(params[:post])
|
||||
format.html { redirect_to posts_url, notice: 'Post was successfully updated.' }
|
||||
format.json { head :no_content }
|
||||
else
|
||||
format.html { render action: "edit" }
|
||||
format.json { render json: @post.errors, status: :unprocessable_entity }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# DELETE /posts/1
|
||||
# DELETE /posts/1.json
|
||||
def destroy
|
||||
#@post = Post.find(params[:id])
|
||||
@post.destroy
|
||||
|
||||
respond_to do |format|
|
||||
format.html { redirect_to posts_url }
|
||||
format.json { head :no_content }
|
||||
end
|
||||
end
|
||||
end
|
||||
2
app/helpers/application_helper.rb
Normal file
2
app/helpers/application_helper.rb
Normal file
@@ -0,0 +1,2 @@
|
||||
module ApplicationHelper
|
||||
end
|
||||
2
app/helpers/posts_helper.rb
Normal file
2
app/helpers/posts_helper.rb
Normal file
@@ -0,0 +1,2 @@
|
||||
module PostsHelper
|
||||
end
|
||||
0
app/mailers/.gitkeep
Normal file
0
app/mailers/.gitkeep
Normal file
0
app/models/.gitkeep
Normal file
0
app/models/.gitkeep
Normal file
10
app/models/ability.rb
Normal file
10
app/models/ability.rb
Normal file
@@ -0,0 +1,10 @@
|
||||
class Ability
|
||||
include CanCan::Ability
|
||||
|
||||
def initialize user
|
||||
user ||= User.new # guest access
|
||||
can :manage, Post, user_id: user.id
|
||||
can :read, Post
|
||||
end
|
||||
|
||||
end
|
||||
4
app/models/post.rb
Normal file
4
app/models/post.rb
Normal file
@@ -0,0 +1,4 @@
|
||||
class Post < ActiveRecord::Base
|
||||
belongs_to :user
|
||||
attr_accessible :body, :title, :user_id
|
||||
end
|
||||
11
app/models/user.rb
Normal file
11
app/models/user.rb
Normal file
@@ -0,0 +1,11 @@
|
||||
class User < ActiveRecord::Base
|
||||
# Include default devise modules. Others available are:
|
||||
# :token_authenticatable, :confirmable,
|
||||
# :lockable, :timeoutable and :omniauthable
|
||||
devise :database_authenticatable, :registerable,
|
||||
:recoverable, :rememberable, :trackable, :validatable
|
||||
|
||||
# Setup accessible (or protected) attributes for your model
|
||||
attr_accessible :email, :password, :password_confirmation, :remember_me
|
||||
# attr_accessible :title, :body
|
||||
end
|
||||
16
app/views/layouts/application.html.erb
Normal file
16
app/views/layouts/application.html.erb
Normal file
@@ -0,0 +1,16 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>CancanAttrTest</title>
|
||||
<%= stylesheet_link_tag "application", :media => "all" %>
|
||||
<%= javascript_include_tag "application" %>
|
||||
<%= csrf_meta_tags %>
|
||||
</head>
|
||||
<body>
|
||||
<p class="notice"><%= notice %></p>
|
||||
<p class="alert"><%= alert %></p>
|
||||
|
||||
<%= yield %>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
29
app/views/posts/_form.html.erb
Normal file
29
app/views/posts/_form.html.erb
Normal file
@@ -0,0 +1,29 @@
|
||||
<%= 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 :user_id %><br />
|
||||
<%= f.text_field :user_id %>
|
||||
</div>
|
||||
<div class="field">
|
||||
<%= f.label :title %><br />
|
||||
<%= f.text_field :title %>
|
||||
</div>
|
||||
<div class="field">
|
||||
<%= f.label :body %><br />
|
||||
<%= f.text_area :body %>
|
||||
</div>
|
||||
<div class="actions">
|
||||
<%= f.submit %>
|
||||
</div>
|
||||
<% end %>
|
||||
6
app/views/posts/edit.html.erb
Normal file
6
app/views/posts/edit.html.erb
Normal file
@@ -0,0 +1,6 @@
|
||||
<h1>Editing post</h1>
|
||||
|
||||
<%= render 'form' %>
|
||||
|
||||
<%= link_to 'Show', @post %> |
|
||||
<%= link_to 'Back', posts_path %>
|
||||
27
app/views/posts/index.html.erb
Normal file
27
app/views/posts/index.html.erb
Normal file
@@ -0,0 +1,27 @@
|
||||
<h1>Listing posts</h1>
|
||||
|
||||
<table>
|
||||
<tr>
|
||||
<th>User</th>
|
||||
<th>Title</th>
|
||||
<th>Body</th>
|
||||
<th></th>
|
||||
<th></th>
|
||||
<th></th>
|
||||
</tr>
|
||||
|
||||
<% @posts.each do |post| %>
|
||||
<tr>
|
||||
<td><%= post.user_id %></td>
|
||||
<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, method: :delete, data: { confirm: 'Are you sure?' } %></td>
|
||||
</tr>
|
||||
<% end %>
|
||||
</table>
|
||||
|
||||
<br />
|
||||
|
||||
<%= link_to 'New Post', new_post_path %>
|
||||
5
app/views/posts/new.html.erb
Normal file
5
app/views/posts/new.html.erb
Normal file
@@ -0,0 +1,5 @@
|
||||
<h1>New post</h1>
|
||||
|
||||
<%= render 'form' %>
|
||||
|
||||
<%= link_to 'Back', posts_path %>
|
||||
20
app/views/posts/show.html.erb
Normal file
20
app/views/posts/show.html.erb
Normal file
@@ -0,0 +1,20 @@
|
||||
<p id="notice"><%= notice %></p>
|
||||
|
||||
<p>
|
||||
<b>User:</b>
|
||||
<%= @post.user %>
|
||||
</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 %>
|
||||
Reference in New Issue
Block a user