Skip to content

Commit

Permalink
topic index and new action.
Browse files Browse the repository at this point in the history
  • Loading branch information
chloerei committed Sep 23, 2011
1 parent 9d9432c commit e8a4b11
Show file tree
Hide file tree
Showing 13 changed files with 121 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Gemfile
Expand Up @@ -11,6 +11,8 @@ gem 'bson_ext', "~> 1.3"
gem 'haml'
gem 'css-bootstrap-rails'
gem 'client_side_validations'
gem 'kaminari'
gem 'gravtastic'

# Gems used only for assets and not required
# in production environments by default.
Expand All @@ -35,5 +37,8 @@ gem 'jquery-rails'
group :test do
# Pretty printed test output
gem 'turn', :require => false
end

group :test, :development do
gem 'factory_girl_rails'
end
5 changes: 5 additions & 0 deletions Gemfile.lock
Expand Up @@ -46,12 +46,15 @@ GEM
factory_girl_rails (1.2.0)
factory_girl (~> 2.1.0)
railties (>= 3.0.0)
gravtastic (3.1.0)
haml (3.1.3)
hike (1.2.1)
i18n (0.6.0)
jquery-rails (1.0.14)
railties (~> 3.0)
thor (~> 0.14)
kaminari (0.12.4)
rails (>= 3.0.0)
libv8 (3.3.10.2)
mail (2.3.0)
i18n (>= 0.4.0)
Expand Down Expand Up @@ -119,8 +122,10 @@ DEPENDENCIES
client_side_validations
css-bootstrap-rails
factory_girl_rails
gravtastic
haml
jquery-rails
kaminari
mongoid (~> 2.2)
rails (= 3.1.1.rc1)
therubyracer
Expand Down
14 changes: 14 additions & 0 deletions app/assets/stylesheets/code_campo.css
Expand Up @@ -9,3 +9,17 @@ footer div > p {text-align: center;}
#login-form, #signup-form {margin: 90px auto 70px; width: 380px; padding: 20px;}

.field_with_errors label.message {color: red; font-weight: normal; display: inline-block;}

#main {margin-top: 20px;}
table.item-list {border: none; margin-bottom: 0;}
table.item-list th, table.item-list td {vertical-align: middle;}
table.item-list td.avatar {padding-right: 0; width: 48px; line-height: 0;}
table.item-list td.avatar img {height: 48px; width: 48px;}
table.item-list td span.domain {font-weight: bold;}
table.item-list td h3 {font-size: 16px; padding-bottom: 11px; line-height: 18px;}
table.item-list td.reply-count a {float: right; color: #FFF; background-color: #BFBFBF; padding: 2px 9px; -moz-border-radius: 10px; border-radius: 10px; font-size: 11px; font-weight: bold; text-decoration: none;}
table.item-list td.reply-count a:hover {background-color: #AAA;}
table.item-list th + th, table.item-list td + td {border-left: none;}

.tags a {color: white; font-weight: bold; font-size: 11px; padding: 1px 4px; background-color: #BFBFBF; border-radius: 3px; -moz-border-radius: 3px;}
.tags a:hover {text-decoration: none; background-color: #AAA;}
4 changes: 4 additions & 0 deletions app/controllers/application_controller.rb
Expand Up @@ -5,6 +5,10 @@ class ApplicationController < ActionController::Base

protected

def require_logined
redirect_to login_url unless logined?
end

def current_user
@current_user ||= login_from_session unless defined?(@current_user)
@current_user
Expand Down
11 changes: 11 additions & 0 deletions app/controllers/topics_controller.rb
@@ -0,0 +1,11 @@
class TopicsController < ApplicationController
before_filter :require_logined, :except => [:index]

def index
@topics = Topic.order_by([[:actived_at, :desc]]).page(params[:page]).per(15)
end

def new
@topic = Topic.new
end
end
13 changes: 13 additions & 0 deletions app/models/topic.rb
@@ -0,0 +1,13 @@
class Topic
include Mongoid::Document
include Mongoid::Timestamps

field :title
field :content
field :tags, :type => Array
field :actived_at, :type => DateTime

belongs_to :user

validates :title, :content, :user, :presence => true
end
4 changes: 4 additions & 0 deletions app/models/user.rb
@@ -1,6 +1,8 @@
class User
include Mongoid::Document
include ActiveModel::SecurePassword
include Gravtastic
gravtastic :rating => 'G', :size => 48

field :name
field :email
Expand All @@ -13,4 +15,6 @@ class User
validates :email, :format => {:with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/, :message => 'Invalid Email address'}
validates :password, :password_confirmation, :presence => true, :on => :create
validates :password, :length => {:minimum => 6}

has_many :topics
end
28 changes: 28 additions & 0 deletions app/views/topics/index.haml
@@ -0,0 +1,28 @@
#main
.container
.row
#mainbar.span12
#topics.box
%table.item-list
- @topics.each do |topic|
%tr
%td.avatar
= image_tag topic.user.gravatar_url
%td
%h3
%a{:href => '#'} This is a title
.info
- if topic.tags.present?
%span.tags
- topic.tags.each do |tag|
%a{:href => '#'}= tag
%span.author
by
%a{:href => '#'}= topic.user.name
%span.time
at
= time_ago_in_words topic.created_at
ago.
%td.reply-count
%a{:href => '#'} 0
#sidebar.span4
Empty file added app/views/topics/new.haml
Empty file.
2 changes: 2 additions & 0 deletions config/routes.rb
Expand Up @@ -6,4 +6,6 @@
delete 'logout' => 'user_sessions#destroy', :as => :logout
resources :users, :only => [:create]
resources :user_sessions, :only => [:create]

resources :topics
end
10 changes: 10 additions & 0 deletions test/factories/topics.rb
@@ -0,0 +1,10 @@
# Read about factories at http://github.com/thoughtbot/factory_girl

FactoryGirl.define do
factory :topic do
title 'title'
content 'content'
tags %w(ruby programing)
association :user
end
end
21 changes: 21 additions & 0 deletions test/functional/topics_controller_test.rb
@@ -0,0 +1,21 @@
require 'test_helper'

class TopicsControllerTest < ActionController::TestCase
def setup
@user = Factory :user
end

test "should get index" do
get :index
assert_response :success, @response.body
end

test "should get new topic page" do
get :new
assert_redirected_to login_url

login_as @user
get :new
assert_response :success, @response.body
end
end
4 changes: 4 additions & 0 deletions test/unit/topic_test.rb
@@ -0,0 +1,4 @@
require 'test_helper'

class TopicTest < ActiveSupport::TestCase
end

0 comments on commit e8a4b11

Please sign in to comment.