Skip to content

Commit

Permalink
added story model
Browse files Browse the repository at this point in the history
  • Loading branch information
fredguest committed Apr 26, 2012
1 parent 7968622 commit cd3e161
Show file tree
Hide file tree
Showing 20 changed files with 368 additions and 0 deletions.
1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--colour
12 changes: 12 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,15 @@ gem 'jquery-rails'

# To use debugger
# gem 'ruby-debug19', :require => 'ruby-debug'

group :development do
gem "rspec-rails"
end

group :test do
gem "rspec-rails"
end




15 changes: 15 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ GEM
coffee-script-source
execjs
coffee-script-source (1.3.1)
diff-lcs (1.1.3)
erubis (2.7.0)
execjs (1.3.0)
multi_json (~> 1.0)
Expand Down Expand Up @@ -79,6 +80,19 @@ GEM
rake (0.9.2.2)
rdoc (3.12)
json (~> 1.4)
rspec (2.9.0)
rspec-core (~> 2.9.0)
rspec-expectations (~> 2.9.0)
rspec-mocks (~> 2.9.0)
rspec-core (2.9.0)
rspec-expectations (2.9.1)
diff-lcs (~> 1.1.3)
rspec-mocks (2.9.0)
rspec-rails (2.9.0)
actionpack (>= 3.0)
activesupport (>= 3.0)
railties (>= 3.0)
rspec (~> 2.9.0)
sass (3.1.16)
sass-rails (3.2.5)
railties (~> 3.2.0)
Expand Down Expand Up @@ -106,6 +120,7 @@ DEPENDENCIES
coffee-rails (~> 3.2.1)
jquery-rails
rails (= 3.2.3)
rspec-rails
sass-rails (~> 3.2.3)
sqlite3
uglifier (>= 1.0.3)
3 changes: 3 additions & 0 deletions app/assets/javascripts/stories.js.coffee
Original file line number Diff line number Diff line change
@@ -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/
56 changes: 56 additions & 0 deletions app/assets/stylesheets/scaffolds.css.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
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 changes: 3 additions & 0 deletions app/assets/stylesheets/stories.css.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the stories controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
83 changes: 83 additions & 0 deletions app/controllers/stories_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
class StoriesController < ApplicationController
# GET /stories
# GET /stories.json
def index
@stories = Story.all

respond_to do |format|
format.html # index.html.erb
format.json { render json: @stories }
end
end

# GET /stories/1
# GET /stories/1.json
def show
@story = Story.find(params[:id])

respond_to do |format|
format.html # show.html.erb
format.json { render json: @story }
end
end

# GET /stories/new
# GET /stories/new.json
def new
@story = Story.new

respond_to do |format|
format.html # new.html.erb
format.json { render json: @story }
end
end

# GET /stories/1/edit
def edit
@story = Story.find(params[:id])
end

# POST /stories
# POST /stories.json
def create
@story = Story.new(params[:story])

respond_to do |format|
if @story.save
format.html { redirect_to @story, notice: 'Story was successfully created.' }
format.json { render json: @story, status: :created, location: @story }
else
format.html { render action: "new" }
format.json { render json: @story.errors, status: :unprocessable_entity }
end
end
end

# PUT /stories/1
# PUT /stories/1.json
def update
@story = Story.find(params[:id])

respond_to do |format|
if @story.update_attributes(params[:story])
format.html { redirect_to @story, notice: 'Story was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @story.errors, status: :unprocessable_entity }
end
end
end

# DELETE /stories/1
# DELETE /stories/1.json
def destroy
@story = Story.find(params[:id])
@story.destroy

respond_to do |format|
format.html { redirect_to stories_url }
format.json { head :no_content }
end
end
end
2 changes: 2 additions & 0 deletions app/helpers/stories_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module StoriesHelper
end
4 changes: 4 additions & 0 deletions app/models/story.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Story < ActiveRecord::Base
attr_accessible :content, :story_id, :twitter_handle
validates_presence_of :twitter_handle
end
29 changes: 29 additions & 0 deletions app/views/stories/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<%= form_for(@story) do |f| %>
<% if @story.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@story.errors.count, "error") %> prohibited this story from being saved:</h2>

<ul>
<% @story.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>

<div class="field">
<%= f.label :content %><br />
<%= f.text_area :content %>
</div>
<div class="field">
<%= f.label :twitter_handle %><br />
<%= f.text_field :twitter_handle %>
</div>
<div class="field">
<%= f.label :story_id %><br />
<%= f.number_field :story_id %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
6 changes: 6 additions & 0 deletions app/views/stories/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<h1>Editing story</h1>

<%= render 'form' %>

<%= link_to 'Show', @story %> |
<%= link_to 'Back', stories_path %>
27 changes: 27 additions & 0 deletions app/views/stories/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<h1>Listing stories</h1>

<table>
<tr>
<th>Content</th>
<th>Twitter handle</th>
<th>Story</th>
<th></th>
<th></th>
<th></th>
</tr>

<% @stories.each do |story| %>
<tr>
<td><%= story.content %></td>
<td><%= story.twitter_handle %></td>
<td><%= story.story_id %></td>
<td><%= link_to 'Show', story %></td>
<td><%= link_to 'Edit', edit_story_path(story) %></td>
<td><%= link_to 'Destroy', story, confirm: 'Are you sure?', method: :delete %></td>
</tr>
<% end %>
</table>

<br />

<%= link_to 'New Story', new_story_path %>
5 changes: 5 additions & 0 deletions app/views/stories/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<h1>New story</h1>

<%= render 'form' %>

<%= link_to 'Back', stories_path %>
20 changes: 20 additions & 0 deletions app/views/stories/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<p id="notice"><%= notice %></p>

<p>
<b>Content:</b>
<%= @story.content %>
</p>

<p>
<b>Twitter handle:</b>
<%= @story.twitter_handle %>
</p>

<p>
<b>Story:</b>
<%= @story.story_id %>
</p>


<%= link_to 'Edit', edit_story_path(@story) %> |
<%= link_to 'Back', stories_path %>
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
VegastechApp::Application.routes.draw do
resources :stories

# The priority is based upon order of creation:
# first created -> highest priority.

Expand Down
16 changes: 16 additions & 0 deletions db/migrate/20120426025626_create_stories.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class CreateStories < ActiveRecord::Migration
def change
create_table :stories do |t|
t.text :content
t.string :twitter_handle
t.integer :story_id

t.timestamps
end

add_index :stories, :twitter_handle
add_index :stories, :story_id
add_index :stories, :created_at

end
end
28 changes: 28 additions & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# encoding: UTF-8
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20120426025626) do

create_table "stories", :force => true do |t|
t.text "content"
t.string "twitter_handle"
t.integer "story_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end

add_index "stories", ["created_at"], :name => "index_stories_on_created_at"
add_index "stories", ["story_id"], :name => "index_stories_on_story_id"
add_index "stories", ["twitter_handle"], :name => "index_stories_on_twitter_handle"

end
13 changes: 13 additions & 0 deletions spec/models/story_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require 'spec_helper'

describe Story do
context "when performing validations" do
it "should not be valid without a twitter handle" do
story = Story.new
story.should be_invalid

end

end

end
11 changes: 11 additions & 0 deletions spec/requests/stories_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'spec_helper'

describe "Stories" do
describe "GET /stories" do
it "works! (now write some real specs)" do
# Run the generator again with the --webrat flag if you want to use webrat methods/matchers
get stories_path
response.status.should be(200)
end
end
end
Loading

0 comments on commit cd3e161

Please sign in to comment.