diff --git a/Gemfile b/Gemfile index 9c5bbd7..c7058bf 100644 --- a/Gemfile +++ b/Gemfile @@ -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 diff --git a/Gemfile.lock b/Gemfile.lock index b69799c..3c9f558 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -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 @@ -125,3 +129,4 @@ DEPENDENCIES sqlite3 turbolinks uglifier (>= 1.3.0) + will_paginate diff --git a/app/assets/javascripts/posts.js b/app/assets/javascripts/posts.js deleted file mode 100644 index c10f1e3..0000000 --- a/app/assets/javascripts/posts.js +++ /dev/null @@ -1,22 +0,0 @@ -$(document).ready(function(e) { - - function getTag(){ - return($('#tag_id').find(":selected")[0]); - } - - $("#save_tag").on('click', 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(); - }); - }); -}); \ No newline at end of file diff --git a/app/assets/javascripts/posts_show.js b/app/assets/javascripts/posts_show.js new file mode 100644 index 0000000..6820a86 --- /dev/null +++ b/app/assets/javascripts/posts_show.js @@ -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"); + }); + }); +}); \ No newline at end of file diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index c8fbe3e..5f49c7e 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -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 diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index dcd8ce4..5ce39a9 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -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 @@ -21,7 +21,6 @@ def new @post = Post.new end - #TODO def tag tag = Tag.find(params[:tag_id]) @post.tags << tag diff --git a/app/controllers/tags_controller.rb b/app/controllers/tags_controller.rb index 9bd199f..2dd3420 100644 --- a/app/controllers/tags_controller.rb +++ b/app/controllers/tags_controller.rb @@ -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 diff --git a/app/models/comment.rb b/app/models/comment.rb index 4e76c5b..9a0d68a 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -1,3 +1,9 @@ class Comment < ActiveRecord::Base belongs_to :post + + self.per_page = 3 + + def to_s + self.body + end end diff --git a/app/models/post.rb b/app/models/post.rb index 0f6c0a8..852bcad 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -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| diff --git a/app/models/tag.rb b/app/models/tag.rb index 4708182..372a99e 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -3,5 +3,5 @@ class Tag < ActiveRecord::Base validates :name, uniqueness: true - + self.per_page = 5 end diff --git a/app/views/comments/index.html.erb b/app/views/comments/index.html.erb index 2f2ece2..4ea34c1 100644 --- a/app/views/comments/index.html.erb +++ b/app/views/comments/index.html.erb @@ -1,15 +1,6 @@

Listing comments

- - - - - - - - - <% @comments.each do |comment| %> @@ -21,6 +12,7 @@ <% end %>
Body
+<%= will_paginate @comments %>
diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 95368c3..6e7e0a2 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -3,7 +3,8 @@ Blog <%= 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 %> diff --git a/app/views/posts/index.html.erb b/app/views/posts/index.html.erb index f6de752..2fe8c4c 100644 --- a/app/views/posts/index.html.erb +++ b/app/views/posts/index.html.erb @@ -1,15 +1,7 @@

Listing posts

- - - - - - - - - +
Body
<% @posts.each do |post| %> @@ -18,10 +10,11 @@ - <% end %> + <% end %>
<%= link_to 'Edit', edit_post_path(post) %> <%= link_to 'Destroy', post, method: :delete, data: { confirm: 'Are you sure?' } %>
+<%= will_paginate @posts %> + -
<%= link_to 'New Post', new_post_path %> diff --git a/app/views/posts/show.html.erb b/app/views/posts/show.html.erb index 2596528..b70114c 100644 --- a/app/views/posts/show.html.erb +++ b/app/views/posts/show.html.erb @@ -4,9 +4,10 @@ Body: <%= @post.body %>

+
Current Tags: <% @post_tags.each do |tag| %> - <%= tag.name %> + | <%= tag.name %> <% end %>
@@ -25,10 +26,14 @@ Comments <% @comments.each do |comment| %>
<%= comment.body %>
- <% end %> + <% end %> + <%= will_paginate @comments %> + -<%= render "comments/form" %> - +
+ Your Next Comment + <%= render "comments/form" %> +
<%= link_to 'Edit', edit_post_path(@post) %> | <%= link_to 'Back', posts_path %> diff --git a/app/views/tags/index.html.erb b/app/views/tags/index.html.erb index a9b3e94..05ba270 100644 --- a/app/views/tags/index.html.erb +++ b/app/views/tags/index.html.erb @@ -1,15 +1,6 @@

Listing tags

- - - - - - - - - <% @tags.each do |tag| %> @@ -19,8 +10,10 @@ <% end %> +
Name
<%= link_to 'Destroy', tag, method: :delete, data: { confirm: 'Are you sure?' } %>
+<%= will_paginate @tags %>
diff --git a/db/seeds.rb b/db/seeds.rb index 4edb1e8..9b13c36 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -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