Permalink
Browse files

+phrases_controller

  • Loading branch information...
1 parent 0451c5b commit 6316271699e1eedd02d21bb86b6492ee843f8259 @DarTSeNSe committed Dec 10, 2011
@@ -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/
@@ -187,4 +187,59 @@ ul.users {
.users li {
list-style: none;
+}
+
+h1.phrase {
+ margin-bottom: 0.3em;
+}
+
+table.phrases {
+ margin-top: 1em;
+}
+
+table.phrases tr {
+ height: 70px;
+}
+
+table.phrases tr td.gravatar {
+ border-top: 1px solid #ccc;
+ vertical-align: top;
+ width: 50px;
+}
+
+table.phrases tr td.phrase {
+ border-top: 1px solid #ccc;
+ vertical-align: top;
+ padding-top: 10px;
+}
+
+table.phrases tr td.phrase span.timestamp {
+ display: block;
+ font-size: 85%;
+ color: #666;
+}
+
+div.user_info img {
+ padding-right: 0.1em;
+}
+
+div.user_info a {
+ text-decoration: none;
+}
+
+div.user_info span.user_name {
+ position: absolute;
+}
+
+div.user_info span.phrases {
+ font-size: 80%;
+}
+
+form.new_phrase {
+ margin-bottom: 2em;
+}
+
+form.new_phrase textarea {
+ height: 4em;
+ margin-bottom: 0;
}
@@ -0,0 +1,3 @@
+// Place all the styles related to the Phrases controller here.
+// They will automatically be included in application.css.
+// You can use Sass (SCSS) here: http://sass-lang.com/
@@ -2,6 +2,7 @@ class PagesController < ApplicationController
def home
@title = "Home"
+ @phrase = Phrase.new if signed_in?
end
def contact
@@ -0,0 +1,27 @@
+class PhrasesController < ApplicationController
+ before_filter :authenticate, :only => [:create, :destroy]
+ before_filter :authorized_user, :only => :destroy
+
+def create
+ @phrase = current_user.phrases.build(params[:phrase])
+ if @phrase.save
+ flash[:success] = "Phrase created!"
+ redirect_to root_path
+ else
+ render 'pages/home'
+ end
+ end
+
+ def destroy
+ @phrase.destroy
+ redirect_back_or root_path
+ end
+
+ private
+
+ def authorized_user
+ @phrase = current_user.phrases.find_by_id(params[:id])
+ redirect_to root_path if @phrase.nil?
+ end
+
+end
@@ -7,6 +7,7 @@ class UsersController < ApplicationController
def show
@user = User.find(params[:id])
+ @phrases = @user.phrases.paginate(:page => params[:page])
@title = @user.name
end
@@ -0,0 +1,2 @@
+module PhrasesHelper
+end
@@ -26,15 +26,20 @@ def current_user?(user)
user == current_user
end
- def deny_access
- redirect_to signin_path, :notice => "Please sign in to access this page."
- end
-
def redirect_back_or(default)
redirect_to(session[:return_to] || default)
clear_return_to
end
+ def authenticate
+ deny_access unless signed_in?
+ end
+
+ def deny_access
+ store_location
+ redirect_to signin_path, :notice => "Please sign in to access this page."
+ end
+
private
def user_from_remember_token
View
@@ -16,4 +16,6 @@ class Phrase < ActiveRecord::Base
:length => { :maximum => 16 }
validates :user_id, :presence => true
validates :language_id, :presence => true
+
+ default_scope :order => 'phrase.created_at DESC'
end
@@ -1,6 +1,22 @@
+<% if signed_in? %>
+ <table class="front" summary="For signed-in users">
+ <tr>
+ <td class="main">
+ <h1 class="phrase">Your phrase:</h1>
+ <%= render 'shared/phrase_form' %>
+ </td>
+ <td class="sidebar round">
+ <%= render 'shared/user_info' %>
+ </td>
+ </tr>
+ </table>
+<% else %>
+
<h1>SimTrans</h1>
<p>
This is the home page for the Simple translation service
</p>
-<%= link_to "Sign up now!", signup_path, :class => "signup_button round" %>
+<%= link_to "Sign up now!", signup_path, :class => "signup_button round" %>
+
+<% end %>
@@ -0,0 +1,8 @@
+<tr>
+ <td class="phrase">
+ <span class="content"><%= phrase.content %></span>
+ <span class="timestamp">
+ Posted <%= time_ago_in_words(phrase.created_at) %> ago.
+ </span>
+ </td>
+</tr>
@@ -0,0 +1,9 @@
+<%= form_for @phrase do |f| %>
+ <%= render 'shared/error_messages', :object => f.object %>
+ <div class="field">
+ <%= f.text_area :phrase %>
+ </div>
+ <div class="actions">
+ <%= f.submit "Submit" %>
+ </div>
+<% end %>
@@ -0,0 +1,11 @@
+<div class="user_info">
+ <a href="<%= user_path(current_user) %>">
+ <%= gravatar_for current_user, :size => 30 %>
+ <span class="user_name">
+ <%= current_user.name %>
+ </span>
+ <span class="phrases">
+ <%= pluralize(current_user.phrases.count, "phrase") %>
+ </span>
+ </a>
+</div>
@@ -5,6 +5,12 @@
<%= gravatar_for @user %>
<%= @user.name %>
</h1>
+ <% unless @user.phrases.empty? %>
+ <table class="phrases" summary="User phrases">
+ <%= render @phrases %>
+ </table>
+ <%= will_paginate @phrases %>
+ <% end %>
</td>
<td class="sidebar round">
<strong>Name</strong> <%= @user.name %><br />
View
@@ -1,6 +1,8 @@
Simtrans::Application.routes.draw do
+
resources :users
resources :sessions, :only => [:new, :create, :destroy]
+ resources :phrases, :only => [:create, :destroy]
match '/signup', :to => 'users#new'
match '/signin', :to => 'sessions#new'
@@ -0,0 +1,26 @@
+require 'spec_helper'
+
+describe PhrasesController do
+
+ describe "GET 'new'" do
+ it "should be successful" do
+ get 'new'
+ response.should be_success
+ end
+ end
+
+ describe "GET 'create'" do
+ it "should be successful" do
+ get 'create'
+ response.should be_success
+ end
+ end
+
+ describe "GET 'show'" do
+ it "should be successful" do
+ get 'show'
+ response.should be_success
+ end
+ end
+
+end
@@ -0,0 +1,15 @@
+require 'spec_helper'
+
+# Specs in this file have access to a helper object that includes
+# the PhrasesHelper. For example:
+#
+# describe PhrasesHelper do
+# describe "string concat" do
+# it "concats two strings with spaces" do
+# helper.concat_strings("this","that").should == "this that"
+# end
+# end
+# end
+describe PhrasesHelper do
+ pending "add some examples to (or delete) #{__FILE__}"
+end
@@ -0,0 +1,5 @@
+require 'spec_helper'
+
+describe "phrases/create.html.erb" do
+ pending "add some examples to (or delete) #{__FILE__}"
+end
@@ -0,0 +1,5 @@
+require 'spec_helper'
+
+describe "phrases/new.html.erb" do
+ pending "add some examples to (or delete) #{__FILE__}"
+end
@@ -0,0 +1,5 @@
+require 'spec_helper'
+
+describe "phrases/show.html.erb" do
+ pending "add some examples to (or delete) #{__FILE__}"
+end

0 comments on commit 6316271

Please sign in to comment.