Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add node to post.

  • Loading branch information...
commit f2f1efb9e4216818cba5d5c2f577f2a8e04f3dee 1 parent e29acd8
xmonkey authored
1  Gemfile
View
@@ -2,6 +2,7 @@ source 'https://rubygems.org'
gem 'rails', '3.2.3'
gem 'bootstrap-sass'
+gem 'twitter'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
9 Gemfile.lock
View
@@ -42,6 +42,8 @@ GEM
erubis (2.7.0)
execjs (1.4.0)
multi_json (~> 1.0)
+ faraday (0.8.4)
+ multipart-post (~> 1.1)
hike (1.2.1)
i18n (0.6.1)
journey (1.0.4)
@@ -55,6 +57,7 @@ GEM
treetop (~> 1.4.8)
mime-types (1.19)
multi_json (1.3.6)
+ multipart-post (1.1.5)
pg (0.14.1)
polyglot (0.3.3)
rack (1.4.1)
@@ -87,6 +90,7 @@ GEM
railties (~> 3.2.0)
sass (>= 3.1.10)
tilt (~> 1.3)
+ simple_oauth (0.2.0)
sprockets (2.1.3)
hike (~> 1.2)
rack (~> 1.0)
@@ -97,6 +101,10 @@ GEM
treetop (1.4.10)
polyglot
polyglot (>= 0.3.1)
+ twitter (4.4.1)
+ faraday (~> 0.8)
+ multi_json (~> 1.3)
+ simple_oauth (~> 0.2)
tzinfo (0.3.33)
uglifier (1.3.0)
execjs (>= 0.3.0)
@@ -114,4 +122,5 @@ DEPENDENCIES
rails (= 3.2.3)
sass-rails (~> 3.2.3)
sqlite3
+ twitter
uglifier (>= 1.0.3)
7 app/controllers/nodes_controller.rb
View
@@ -0,0 +1,7 @@
+class NodesController < ApplicationController
+ def show
+ @node = Node.find(params[:id])
+ @posts = @node.posts
+ render "posts/index"
+ end
+end
2  app/controllers/posts_controller.rb
View
@@ -13,7 +13,7 @@ def new
end
def create
- @post = Post.create(params[:post])
+ @post = Post.new(params[:post])
@post.user = current_user
if @post.save
redirect_to posts_path
4 app/models/node.rb
View
@@ -0,0 +1,4 @@
+class Node < ActiveRecord::Base
+ attr_accessible :name
+ has_many :posts
+end
12 app/models/post.rb
View
@@ -2,10 +2,20 @@ class Post < ActiveRecord::Base
has_many :comments
has_many :votes
belongs_to :user
- attr_accessible :title, :url
+ belongs_to :node
+ attr_accessible :title, :url, :node_name
validates :title, :presence => true
validates :url, :presence => true
def votes_count
votes.where(:upvote => true).count - votes.where(:upvote => false).count
end
+
+ def node_name
+ node.name if self.node
+ end
+
+ def node_name=(name)
+ self.node =Node.find_or_create_by_name(name) unless name.blank?
+ end
+
end
4 app/views/posts/new.html.erb
View
@@ -17,6 +17,10 @@
<%= f.label :url %><br />
<%= f.text_field :url %>
</div>
+ <div class="field">
+ <%= f.label :node_name %><br />
+ <%= f.text_field :node_name %>
+ </div>
<div class="actions">
<%= f.submit %>
</div>
6 app/views/posts/show.html.erb
View
@@ -1,5 +1,11 @@
<h3><%= link_to @post.title, url_link(@post) %> </h3>
+<% unless @post.node_name.blank? %>
+ node: <%= link_to @post.node_name, @post.node %>
+<% end %>
+
+
<p><%= pluralize(@post.comments.count, ' comment') %> </p>
+
<ul>
<% @post.comments.each do |comment| %>
<li>
2  config/routes.rb
View
@@ -12,4 +12,6 @@
resources :comments, :only => :create
resources :votes, :only => :create
end
+
+ resources :nodes, :only => :show
end
5 db/migrate/20121218140444_add_twitter_to_user.rb
View
@@ -0,0 +1,5 @@
+class AddTwitterToUser < ActiveRecord::Migration
+ def change
+ add_column :users, :twitter, :string
+ end
+end
9 db/migrate/20121218140844_create_nodes.rb
View
@@ -0,0 +1,9 @@
+class CreateNodes < ActiveRecord::Migration
+ def change
+ create_table :nodes do |t|
+ t.string :name
+
+ t.timestamps
+ end
+ end
+end
5 db/migrate/20121218141949_add_node_to_post.rb
View
@@ -0,0 +1,5 @@
+class AddNodeToPost < ActiveRecord::Migration
+ def change
+ add_column :posts, :node_id, :integer
+ end
+end
10 db/schema.rb
View
@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20121218131337) do
+ActiveRecord::Schema.define(:version => 20121218141949) do
create_table "comments", :force => true do |t|
t.text "content"
@@ -23,12 +23,19 @@
add_index "comments", ["post_id"], :name => "index_comments_on_post_id"
+ create_table "nodes", :force => true do |t|
+ t.string "name"
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ end
+
create_table "posts", :force => true do |t|
t.string "url"
t.string "title"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "user_id"
+ t.integer "node_id"
end
create_table "users", :force => true do |t|
@@ -36,6 +43,7 @@
t.string "name"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
+ t.string "twitter"
end
create_table "votes", :force => true do |t|
7 test/fixtures/nodes.yml
View
@@ -0,0 +1,7 @@
+# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html
+
+one:
+ name: MyString
+
+two:
+ name: MyString
7 test/unit/node_test.rb
View
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class NodeTest < ActiveSupport::TestCase
+ # test "the truth" do
+ # assert true
+ # end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.