Skip to content

Commit

Permalink
added pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
enterteg committed Mar 27, 2016
1 parent 3aa928e commit 880177a
Show file tree
Hide file tree
Showing 12 changed files with 57 additions and 15 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ gem 'pg'
gem 'tinymce-rails'
gem 'dropzonejs-rails'
gem 'truncate_html', '~> 0.9.3'
gem 'lightbox2-rails'
gem 'will_paginate', '~> 3.0.6'
#------------------
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
Expand Down
5 changes: 5 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ GEM
railties (>= 4.2.0)
thor (>= 0.14, < 2.0)
json (1.8.3)
lightbox2-rails (2.7.1)
railties (>= 3.1)
loofah (2.0.3)
nokogiri (>= 1.5.9)
mail (2.6.3)
Expand Down Expand Up @@ -161,6 +163,7 @@ GEM
binding_of_caller (>= 0.7.2)
railties (>= 4.0)
sprockets-rails (>= 2.0, < 4.0)
will_paginate (3.0.7)

PLATFORMS
ruby
Expand All @@ -173,6 +176,7 @@ DEPENDENCIES
haml (~> 4.0, >= 4.0.7)
jbuilder (~> 2.0)
jquery-rails
lightbox2-rails
paperclip (~> 4.3, >= 4.3.6)
pg
rails (= 4.2.6)
Expand All @@ -185,6 +189,7 @@ DEPENDENCIES
turbolinks
uglifier (>= 1.3.0)
web-console (~> 2.0)
will_paginate (~> 3.0.6)

BUNDLED WITH
1.11.2
1 change: 1 addition & 0 deletions app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@
//= require jquery_ujs
//= require turbolinks
//= require tinymce
//= require lightbox
//= require_tree .
2 changes: 2 additions & 0 deletions app/assets/stylesheets/application.css
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
* file per style scope.
*
*= require_tree .
*= require lightbox
*= require_self
*/

25 changes: 23 additions & 2 deletions app/assets/stylesheets/posts.sass
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,32 @@ section
top: -4px
margin-right: 10px
article
font-family: 'Roboto'
padding: 20px 0px
font-weigth: 100
font-weight: 300
text-align: justify
line-height: 1.4
font-size: 1.2rem
font-size: 1.1rem
.pagination
text-align: center
padding: 10px 0px
font-size: 1.3rem
a
color: rgba(0,0,0,0.7)
text-decoration: none
padding: 8px 12px
background-color: rgba(250,50,150,0.2)
&:hover
background-color: rgba(250,50,150,0.4)
.current
color: rgba(200,50,100,1)
font-weight: bold
font-size: 1.5rem
font-style: normal
padding: 10px 15px
background-color: rgba(250,50,150,0.5)
.disabled
color: #ccc

.clear
clear: both
Expand Down
2 changes: 1 addition & 1 deletion app/assets/stylesheets/public.sass
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ body, html
margin: 0
padding: 0
font-family: 'Roboto', sans-serif
font-weight: 100
font-size: 15px
body#tinymce
padding: 10px
Expand Down Expand Up @@ -99,7 +100,6 @@ hr
position: relative
top: 50%
+transform(translateY(-50%))
+transition(0.3s ease all)
&:hover
opacity: 0.7

Expand Down
11 changes: 7 additions & 4 deletions app/controllers/public_controller.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
class PublicController < ApplicationController



rescue_from ActionView::MissingTemplate do
render :index
end

def index
@posts = Post.where.not(id: 1).limit(4).includes(:photos)
@posts = Post.where.not(id: 1).paginate(page: params[:page], per_page: 1)
end

def travels
@posts = Post.includes(:photos).travel.limit(3)
@posts = Post.travel.paginate(page: params[:page], per_page: 3)
@pins = Post.travel.pluck(:locX, :locY, :id, :title, :created_at)
@controller = 'travels'
end

def food
@posts = Post.includes(:photos).food.limit(4)
@posts = Post.food.paginate(page: params[:page], per_page: 4)
@controller = 'food'
end

def life
@posts = Post.includes(:photos).life.limit(4)
@posts = Post.life.paginate(page: params[:page], per_page: 4)
@controller = 'life'
end

Expand Down
13 changes: 9 additions & 4 deletions app/models/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,22 @@ class Post < ActiveRecord::Base
belongs_to :category
has_many :photos, dependent: :destroy

default_scope { order(created_at: :desc)}



default_scope { order(created_at: :desc) }

scope :travel, -> { where(category_id: 1)}
scope :food, -> { where(category_id: 2)}
scope :life, -> { where(category_id: 3)}
scope :travel, -> { where(category_id: 1).includes(:photos)}
scope :food, -> { where(category_id: 2).includes(:photos)}
scope :life, -> { where(category_id: 3).includes(:photos)}


validates_presence_of :title, :desc, :category_id
validates :desc, length: { minimum: 10}
validates :title, length: { minimum: 3}



def validate_travel_pin
if (self.category_id == 1 && (self.locX == nil || self.locY == nil))
errors.add(:location, "is not set")
Expand Down
2 changes: 1 addition & 1 deletion app/views/layouts/application.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
%title Blog
= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true
= javascript_include_tag 'application', 'data-turbolinks-track' => true
%link{:href => "https://fonts.googleapis.com/css?family=Raleway:200|Roboto:100", :rel => "stylesheet", :type => "text/css"}/
%link{:href => "https://fonts.googleapis.com/css?family=Raleway:200|Roboto:100,300", :rel => "stylesheet", :type => "text/css"}/
= csrf_meta_tags
%body
%header
Expand Down
2 changes: 1 addition & 1 deletion app/views/posts/_post.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
.photos.mini
-post.photos.each_with_index do |photo, i|
- break if i == 4
= link_to (photo.photo.url(:large)) do
= link_to (photo.photo.url(:large)), data: {lightbox: 'example-set'} do
= image_tag photo.photo.url(:thumb)
= link_to post do
.button.edit= 'read more...'
Expand Down
3 changes: 2 additions & 1 deletion app/views/posts/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
-if @post.photos.any?
.photos
-@post.photos.each do |photo|
= image_tag photo.photo.url(:medium)
= link_to (photo.photo.url(:large)), data: {lightbox: 'example-set'} do
= image_tag photo.photo.url(:medium)
.clear

= link_to :back do
Expand Down
4 changes: 3 additions & 1 deletion app/views/public/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@
-else
.header= 'Latest posts'
-if @posts
= render @posts
= render @posts

= will_paginate @posts, previous_label: 'prev', next_label: 'older posts', inner_window: 1, outer_window: 1

0 comments on commit 880177a

Please sign in to comment.