-
-
Save xenda/4486522 to your computer and use it in GitHub Desktop.
Revisions
-
xenda revised this gist
Jan 8, 2013 . 1 changed file with 9 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,10 +1,16 @@ class Entry < ActiveRecord::Base ITEMS_PER_PAGE = 50 belongs_to :subreddit def self.page_for(subreddit, page) if subreddit entries = subreddit.entries else entries = Entry.all end pages, extra = entries.count.divmod(ITEMS_PER_PAGE) pages += 1 if extra > 0 -
xenda revised this gist
Jan 8, 2013 . 1 changed file with 25 additions and 15 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,24 +1,34 @@ class Entry < ActiveLOL ITEMS_PER_PAGE = 50 belongs_to :subreddit # just guessing def self.page_for(subreddit, page) entries = subreddit.entries pages, extra = entries.count.divmod(ITEMS_PER_PAGE) pages += 1 if extra > 0 page = 1 if page < 1 skip = (page - 1) * ITEMS_PER_PAGE data = entries.limit(ITEMS_PER_PAGE).offset(skip).order("id DESC") next_page = page < pages ? page + 1 : page prev_page = page > 1 ? page - 1 : page next_page = "/entries/?page=#{next_page}" prev_page = "/entries/?page=#{prev_page}" {page: page, next: next_page, prev: prev_page, data: data} end end class EntriesController < ApplicationController def index subreddit = Subreddit.find(params[:subreddit) if subreddit render json: Entry.page_for(subreddit, params[:page]) else #something bad end end -
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,24 @@ class EntriesController < ApplicationController ITEMS_PER_PAGE = 50 def index subreddit = params[:subreddit].to_i subreddit = Subreddit.find_by_id(subreddit) where_filter = {} if subreddit where_filter = { subreddit_id: subreddit.id } end pages, extra = Entry.where(where_filter).count.divmod(ITEMS_PER_PAGE) pages += 1 if extra > 0 page = params[:page].to_i page = page < 1 ? 1 : page skip = (page - 1) * ITEMS_PER_PAGE data = Entry.where(where_filter).limit(ITEMS_PER_PAGE).offset(skip).order("id DESC") next_page = page < pages ? page + 1 : page prev_page = page > 1 ? page - 1 : page next_page = "/entries/?page=#{next_page}" prev_page = "/entries/?page=#{prev_page}" render json: {page: page, next: next_page, prev: prev_page, data: data} end end