Skip to content

Commit

Permalink
Add user gramposts
Browse files Browse the repository at this point in the history
  • Loading branch information
sophiaxc committed Apr 29, 2012
1 parent c1d45de commit 1e4dfbc
Show file tree
Hide file tree
Showing 29 changed files with 448 additions and 29 deletions.
34 changes: 34 additions & 0 deletions app/assets/stylesheets/custom.css.scss
Expand Up @@ -184,3 +184,37 @@ input, textarea, select, .uneditable-input {
}
}
}

/* gramposts */

.gramposts {
list-style: none;
margin: 10px 0 0 0;

li {
padding: 10px 0;
border-top: 1px solid #e8e8e8;
}
}

.title {
display: block;
font-weight: bold;
}

.content {
display: block;
}
.timestamp {
color: $grayLight;
}
.gravatar {
float: left;
margin-right: 10px;
}
aside {
textarea {
height: 100px;
margin-bottom: 5px;
}
}
29 changes: 29 additions & 0 deletions app/controllers/gramposts_controller.rb
@@ -0,0 +1,29 @@
class GrampostsController < ApplicationController
before_filter :signed_in_user, only: [:create, :destroy]
before_filter :correct_user, only: :destroy

def create
@grampost = current_user.gramposts.build(params[:grampost])
if @grampost.save
flash[:success] = "Grampost created!"
redirect_to root_path
else
@feed_items = []
render 'static_pages/home'
end
end

def destroy
@grampost.destroy
redirect_back_or root_path
end

def index
end

private
def correct_user
@grampost = current_user.gramposts.find_by_id(params[:id])
redirect_to root_path if @grampost.nil?
end
end
4 changes: 4 additions & 0 deletions app/controllers/static_pages_controller.rb
@@ -1,5 +1,9 @@
class StaticPagesController < ApplicationController
def home
if signed_in?
@grampost = current_user.gramposts.build
@feed_items = current_user.feed.paginate(page: params[:page])
end
end

def help
Expand Down
8 changes: 1 addition & 7 deletions app/controllers/users_controller.rb
Expand Up @@ -28,6 +28,7 @@ def update

def show
@user = User.find(params[:id])
@gramposts = @user.gramposts.paginate(page: params[:page])
end

def new
Expand All @@ -47,13 +48,6 @@ def create

private

def signed_in_user
unless signed_in?
store_location
redirect_to signin_path, notice: "Please sign in."
end
end

def correct_user
@user = User.find(params[:id])
redirect_to(root_path) unless current_user?(@user)
Expand Down
15 changes: 15 additions & 0 deletions app/helpers/gramposts_helper.rb
@@ -0,0 +1,15 @@
module GrampostsHelper

def wrap(description)
sanitize(raw(description.split.map{ |s| wrap_long_string(s) }.join(' ')))
end

private

def wrap_long_string(text, max_width = 30)
zero_width_space = "&#8203;"
regex = /.{1,#{max_width}}/
(text.length < max_width) ? text :
text.scan(regex).join(zero_width_space)
end
end
7 changes: 7 additions & 0 deletions app/helpers/sessions_helper.rb
Expand Up @@ -35,6 +35,13 @@ def store_location
session[:return_to] = request.fullpath
end

def signed_in_user
unless signed_in?
store_location
redirect_to signin_path, notice: "Please sign in."
end
end

private

def user_from_remember_token
Expand Down
21 changes: 21 additions & 0 deletions app/models/grampost.rb
@@ -0,0 +1,21 @@
# == Schema Information
#
# Table name: gramposts
#
# id :integer not null, primary key
# title :string(255)
# description :string(255)
# user_id :integer
# created_at :datetime not null
# updated_at :datetime not null
#

class Grampost < ActiveRecord::Base
attr_accessible :title, :description
belongs_to :user

validates :user_id, presence: true
validates :title, presence: true, length: { maximum: 140 }

default_scope order: 'gramposts.created_at DESC'
end
18 changes: 13 additions & 5 deletions app/models/user.rb
Expand Up @@ -2,16 +2,20 @@
#
# Table name: users
#
# id :integer not null, primary key
# name :string(255)
# email :string(255)
# created_at :datetime not null
# updated_at :datetime not null
# id :integer not null, primary key
# name :string(255)
# email :string(255)
# created_at :datetime not null
# updated_at :datetime not null
# password_digest :string(255)
# remember_token :string(255)
# admin :boolean default(FALSE)
#

class User < ActiveRecord::Base
attr_accessible :name, :email, :password, :password_confirmation
has_secure_password
has_many :gramposts, dependent: :destroy

before_save { |user| user.email = email.downcase }
before_save :create_remember_token
Expand All @@ -24,6 +28,10 @@ class User < ActiveRecord::Base
validates :password, length: { minimum: 6 }
validates :password_confirmation, presence: true

def feed
Grampost.where("user_id = ?", id)
end

private

def create_remember_token
Expand Down
12 changes: 12 additions & 0 deletions app/views/gramposts/_grampost.html.erb
@@ -0,0 +1,12 @@
<li>
<span class="title"><%= grampost.title %></span>
<span class="content"><%= wrap(grampost.description) %></span>
<span class="timestamp">
Posted <%= time_ago_in_words(grampost.created_at) %> ago.
</span>
<% if current_user?(grampost.user) %>
<%= link_to "delete", grampost, method: :delete,
confirm: "You sure?",
title: grampost.title %>
<% end %>
</li>
6 changes: 3 additions & 3 deletions app/views/shared/_error_messages.html.erb
@@ -1,10 +1,10 @@
<% if @user.errors.any? %>
<% if object.errors.any? %>
<div id="error_explanation">
<div class="alert alert-error">
The form contains <%= pluralize(@user.errors.count, "error") %>.
The form contains <%= pluralize(object.errors.count, "error") %>.
</div>
<ul>
<% @user.errors.full_messages.each do |msg| %>
<% object.errors.full_messages.each do |msg| %>
<li>* <%= msg %></li>
<% end %>
</ul>
Expand Down
6 changes: 6 additions & 0 deletions app/views/shared/_feed.html.erb
@@ -0,0 +1,6 @@
<% if @feed_items.any? %>
<ol class="gramposts">
<%= render partial: 'shared/feed_item', collection: @feed_items %>
</ol>
<%= will_paginate @feed_items %>
<% end %>
16 changes: 16 additions & 0 deletions app/views/shared/_feed_item.html.erb
@@ -0,0 +1,16 @@
<li id="<%= feed_item.id %>">
<%= link_to gravatar_for(feed_item.user), feed_item.user %>
<span class="user">
<%= link_to feed_item.user.name, feed_item.user %>
</span>
<span class="title"><%= feed_item.title %></span>
<span class="content"><%= wrap(feed_item.description) %></span>
<span class="timestamp">
Posted <%= time_ago_in_words(feed_item.created_at) %> ago.
</span>
<% if current_user?(feed_item.user) %>
<%= link_to "delete", feed_item, method: :delete,
confirm: "You sure?",
title: feed_item.title %>
<% end %>
</li>
8 changes: 8 additions & 0 deletions app/views/shared/_grampost_form.html.erb
@@ -0,0 +1,8 @@
<%= form_for(@grampost) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field">
<%= f.text_field :title, placeholder: "Grampost title..." %>
<%= f.text_area :description, placeholder: "Describe the details of your grampost..." %>
</div>
<%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>
9 changes: 9 additions & 0 deletions app/views/shared/_user_info.html.erb
@@ -0,0 +1,9 @@
<a href="<%= user_path(current_user) %>">
<%= gravatar_for current_user, size: 52 %>
</a>
<h1>
<%= current_user.name %>
</h1>
<span>
<%= link_to "view my profile", current_user %>
</span>
39 changes: 28 additions & 11 deletions app/views/static_pages/home.html.erb
@@ -1,13 +1,30 @@
<div class="center hero-unit">
<h1>Welcome to Gramlist</h1>
<% if signed_in? %>
<div class="row">
<aside class="span4">
<section>
<%= render 'shared/user_info' %>
</section>
<section>
<%= render 'shared/grampost_form' %>
</section>
</aside>
<div class="span8">
<h3>Grampost Feed</h3>
<%= render 'shared/feed' %>
</div>
</div>
<% else %>
<div class="center hero-unit">
<h1>Welcome to Gramlist</h1>

<h2>
This is the home page for the
<a href="http://railstutorial.org/">Ruby on Rails Tutorial</a>
sample application.
</h2>

<%= link_to "Sign up now!", signup_path, class: "btn btn-large btn-primary" %>
</div>
<h2>
This is the home page for the
<a href="http://railstutorial.org/">Ruby on Rails Tutorial</a>
sample application.
</h2>
<%= link_to "Sign up now!", signup_path, class: "btn btn-large btn-primary" %>
</div>

<%= link_to image_tag("rails.png", alt: "Rails"), 'http://rubyonrails.org/' %>
<%= link_to image_tag("rails.png", alt: "Rails"), 'http://rubyonrails.org/' %>
<% end %>
2 changes: 1 addition & 1 deletion app/views/users/new.html.erb
Expand Up @@ -4,7 +4,7 @@
<div class="row">
<div class="span6 offset3">
<%= form_for(@user) do |f| %>
<%= render 'shared/error_messages' %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.label :name %>
<%= f.text_field :name %>
Expand Down
9 changes: 9 additions & 0 deletions app/views/users/show.html.erb
Expand Up @@ -8,4 +8,13 @@
</h1>
</section>
</aside>
<div class="span8">
<% if @user.gramposts.any? %>
<h3>Gramposts</h3>
<ol class="gramposts">
<%= render @gramposts %>
</ol>
<%= will_paginate @gramposts %>
<% end %>
</div>
</div>
2 changes: 1 addition & 1 deletion config/application.rb
Expand Up @@ -54,7 +54,7 @@ class Application < Rails::Application
# This will create an empty whitelist of attributes available for mass-assignment for all models
# in your app. As such, your models will need to explicitly whitelist or blacklist accessible
# parameters by using an attr_accessible or attr_protected declaration.
# config.active_record.whitelist_attributes = true
config.active_record.whitelist_attributes = true

# Enable the asset pipeline
config.assets.enabled = true
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
@@ -1,6 +1,7 @@
Gramlist::Application.routes.draw do
resources :users
resources :sessions, only: [:new, :create, :destroy]
resources :gramposts, only: [:create, :destroy, :index]

root to: 'static_pages#home'
match '/signup', to: 'users#new'
Expand Down
12 changes: 12 additions & 0 deletions db/migrate/20120424042957_create_gramposts.rb
@@ -0,0 +1,12 @@
class CreateGramposts < ActiveRecord::Migration
def change
create_table :gramposts do |t|
t.string :title
t.string :description
t.integer :user_id

t.timestamps
end
add_index :gramposts, [:user_id, :created_at]
end
end
12 changes: 11 additions & 1 deletion db/schema.rb
Expand Up @@ -11,7 +11,17 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20120422232839) do
ActiveRecord::Schema.define(:version => 20120424042957) do

create_table "gramposts", :force => true do |t|
t.string "title"
t.string "description"
t.integer "user_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end

add_index "gramposts", ["user_id", "created_at"], :name => "index_gramposts_on_user_id_and_created_at"

create_table "users", :force => true do |t|
t.string "name"
Expand Down
8 changes: 8 additions & 0 deletions lib/tasks/sample_data.rake
Expand Up @@ -15,5 +15,13 @@ namespace :db do
password: password,
password_confirmation: password)
end

users = User.all(limit: 6)
50.times do
title = Faker::Lorem.sentence(2)
description = Faker::Lorem.sentence(5)
users.each { |user| user.gramposts.create!(title: title,
description: description) }
end
end
end
6 changes: 6 additions & 0 deletions spec/factories.rb
Expand Up @@ -9,4 +9,10 @@
admin true
end
end

factory :grampost do
title "Post Title"
description "Lorem Ipsum"
user
end
end

0 comments on commit 1e4dfbc

Please sign in to comment.