Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ gem 'coffee-rails', '~> 4.0.0'
# Use jquery as the JavaScript library
gem 'jquery-rails'

gem "faker"
gem "will_paginate"

gem 'pry'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
Expand Down
5 changes: 5 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ GEM
coffee-script-source (1.6.3)
erubis (2.7.0)
execjs (2.0.2)
faker (1.2.0)
i18n (~> 0.5)
hike (1.2.3)
i18n (0.6.5)
jbuilder (1.5.2)
Expand Down Expand Up @@ -110,12 +112,14 @@ GEM
uglifier (2.3.1)
execjs (>= 0.3.0)
json (>= 1.8.0)
will_paginate (3.0.5)

PLATFORMS
ruby

DEPENDENCIES
coffee-rails (~> 4.0.0)
faker
jbuilder (~> 1.2)
jquery-rails
pry
Expand All @@ -125,3 +129,4 @@ DEPENDENCIES
sqlite3
turbolinks
uglifier (>= 1.3.0)
will_paginate
22 changes: 0 additions & 22 deletions app/assets/javascripts/posts.js

This file was deleted.

22 changes: 22 additions & 0 deletions app/assets/javascripts/posts_show.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
jQuery(function(e) {
function getTag(){
return($('#tag_id').find(":selected")[0]);
};

$(document).on('click', "#save_tag", function (e) {
e.preventDefault();
var tag = getTag();

$.ajax({
dataType: "json",
type: "POST",
url: $(this).data('url'),
data: { tag_id: tag.value }
})
.done(function(){
$("#current_tags").append(" | " + tag.text);
tag.remove();
console.log("Adding Tag");
});
});
});
2 changes: 1 addition & 1 deletion app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class CommentsController < ApplicationController
# GET /comments
# GET /comments.json
def index
@comments = Comment.all
@comments = Comment.paginate(:page => params[:page]).all
end

# GET /comments/1
Expand Down
5 changes: 2 additions & 3 deletions app/controllers/posts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ class PostsController < ApplicationController
# GET /posts
# GET /posts.json
def index
@posts = Post.all
@posts = Post.paginate(:page => params[:page]).all
end

# GET /posts/1
# GET /posts/1.json
def show
@comment = Comment.new(:post_id => @post.id)
@comments = @post.comments
@comments = @post.comments.paginate(:page => params[:page]).order('id DESC')
@post_tags = @post.tags
@tags = @post.unassociated_tags
end
Expand All @@ -21,7 +21,6 @@ def new
@post = Post.new
end

#TODO
def tag
tag = Tag.find(params[:tag_id])
@post.tags << tag
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/tags_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class TagsController < ApplicationController
# GET /tags
# GET /tags.json
def index
@tags = Tag.all
@tags = Tag.paginate(:page => params[:page]).all
end

# GET /tags/1
Expand Down
6 changes: 6 additions & 0 deletions app/models/comment.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
class Comment < ActiveRecord::Base
belongs_to :post

self.per_page = 3

def to_s
self.body
end
end
2 changes: 2 additions & 0 deletions app/models/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ class Post < ActiveRecord::Base
has_many :comments
has_and_belongs_to_many :tags

self.per_page = 2

def unassociated_tags
absent_tags = Tag.all - tags
absent_tags.inject([]) do |memo, tag|
Expand Down
2 changes: 1 addition & 1 deletion app/models/tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ class Tag < ActiveRecord::Base

validates :name, uniqueness: true


self.per_page = 5
end
10 changes: 1 addition & 9 deletions app/views/comments/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
<h1>Listing comments</h1>

<table>
<thead>
<tr>
<th>Body</th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>

<tbody>
<% @comments.each do |comment| %>
<tr>
Expand All @@ -21,6 +12,7 @@
<% end %>
</tbody>
</table>
<%= will_paginate @comments %>

<br>

Expand Down
3 changes: 2 additions & 1 deletion app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
<head>
<title>Blog</title>
<%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
<%= javascript_include_tag "application", "data-turbolinks-track" => true %>
<%= javascript_include_tag "application", params[:controller] if Blog::Application.assets.find_asset("#{params[:controller]}.js") %>
<%= javascript_include_tag "application", "#{params[:controller]}_#{params[:action]}" if Blog::Application.assets.find_asset("#{params[:controller]}_#{params[:action]}.js") %>
<%= csrf_meta_tags %>
</head>
<body>
Expand Down
15 changes: 4 additions & 11 deletions app/views/posts/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
<h1>Listing posts</h1>

<table>
<thead>
<tr>
<th>Body</th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>

<table>
<tbody>
<% @posts.each do |post| %>
<tr>
Expand All @@ -18,10 +10,11 @@
<td><%= link_to 'Edit', edit_post_path(post) %></td>
<td><%= link_to 'Destroy', post, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
<% end %>
</tbody>
</table>
<%= will_paginate @posts %>


<br>

<%= link_to 'New Post', new_post_path %>
13 changes: 9 additions & 4 deletions app/views/posts/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
<strong>Body:</strong>
<%= @post.body %>
</p>

<div id="current_tags"><strong>Current Tags:</strong>
<% @post_tags.each do |tag| %>
<%= tag.name %>
| <%= tag.name %>
<% end %>
</div>

Expand All @@ -25,10 +26,14 @@
<strong>Comments</strong>
<% @comments.each do |comment| %>
<div><%= comment.body %></div>
<% end %>
<% end %>
<%= will_paginate @comments %>

</div>

<%= render "comments/form" %>

<div>
<strong>Your Next Comment</strong>
<%= render "comments/form" %>
</div>
<%= link_to 'Edit', edit_post_path(@post) %> |
<%= link_to 'Back', posts_path %>
11 changes: 2 additions & 9 deletions app/views/tags/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
<h1>Listing tags</h1>

<table>
<thead>
<tr>
<th>Name</th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>

<tbody>
<% @tags.each do |tag| %>
<tr>
Expand All @@ -19,8 +10,10 @@
<td><%= link_to 'Destroy', tag, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>

</tbody>
</table>
<%= will_paginate @tags %>

<br>

Expand Down
17 changes: 13 additions & 4 deletions db/seeds.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
# This file should contain all the record creation needed to seed the database with its default values.
# The data can then be loaded with the rake db:seed (or created alongside the db with db:setup).
#
# Examples:
#
# cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }])
# Mayor.create(name: 'Emanuel', city: cities.first)
# generate 100 posts with 5 to 10 comments each and 100 tags

100.times do
post = Post.create(:body=>Faker::Lorem.paragraphs.join("\n"))

rand(5..10).times do
Comment.create(:post_id=>post.id, :body=> Faker::Lorem.sentence)
end
end

100.times do
Tag.create(:name => Faker::Lorem.word)
end