Skip to content
This repository has been archived by the owner on Jun 26, 2019. It is now read-only.

Commit

Permalink
Add pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
huacnlee committed Mar 22, 2016
1 parent 15e1d75 commit 363486f
Show file tree
Hide file tree
Showing 18 changed files with 132 additions and 54 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ group :development, :test do
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.1.0'
gem 'jquery-rails'
gem 'minitest'

gem 'devise'

gem 'factory_girl_rails'
gem 'simplecov'
end
10 changes: 10 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ PATH
github-markup (>= 1.3.0)
rails (~> 4.2.0)
redcarpet (>= 3.3.0)
will_paginate (>= 3.0.0)

GEM
remote: https://rubygems.org/
Expand Down Expand Up @@ -63,6 +64,7 @@ GEM
responders
thread_safe (~> 0.1)
warden (~> 1.2.3)
docile (1.1.5)
erubis (2.7.0)
execjs (2.6.0)
factory_girl (4.5.0)
Expand Down Expand Up @@ -128,6 +130,11 @@ GEM
sprockets (>= 2.8, < 4.0)
sprockets-rails (>= 2.0, < 4.0)
tilt (>= 1.1, < 3)
simplecov (0.11.2)
docile (~> 1.1.0)
json (~> 1.8)
simplecov-html (~> 0.10.0)
simplecov-html (0.10.0)
sprockets (3.5.2)
concurrent-ruby (~> 1.0)
rack (> 1, < 3)
Expand All @@ -145,6 +152,7 @@ GEM
json (>= 1.8.0)
warden (1.2.6)
rack (>= 1.0)
will_paginate (3.1.0)

PLATFORMS
ruby
Expand All @@ -155,9 +163,11 @@ DEPENDENCIES
factory_girl_rails
homeland!
jquery-rails
minitest
mysql2
rails (= 4.2.6)
sass-rails (~> 5.0)
simplecov
uglifier (>= 1.3.0)

BUNDLED WITH
Expand Down
5 changes: 3 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ begin
rescue LoadError
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
end
Bundler::GemHelper.install_tasks

load 'rails/tasks/statistics.rake'
APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
load 'rails/tasks/engine.rake'

Bundler::GemHelper.install_tasks

1 change: 1 addition & 0 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
- 编写测试用例
- 管理后台
- API
- 权限
Expand Down
3 changes: 3 additions & 0 deletions app/controllers/homeland/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ module Homeland
class ApplicationController < ::ApplicationController
helper_method :current_user

helper Homeland::ActionView::TagHelpers
helper Homeland::ActionView::WillPaginate

alias_method :origin_current_user, Homeland.config.current_user_method.to_sym
alias_method :origin_authenticate_user!, Homeland.config.authenticate_user_method.to_sym
def current_user
Expand Down
8 changes: 4 additions & 4 deletions app/controllers/homeland/topics_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@ class TopicsController < Homeland::ApplicationController
before_filter :authenticate_user!, only: [:new, :edit, :create, :update, :destroy, :reply]

def index
@topics = Topic.latest.includes(:user).limit(10)
@topics = Topic.latest.includes(:user).page(params[:page])

set_seo_meta(t("homeland.nav.latest"))
end

def node
@node = Node.find(params[:id])
@topics = @node.topics.latest.includes(:user).limit(50)
@topics = @node.topics.latest.includes(:user).page(params[:page])

render action: "index"
end

%w(recent features).each do |action|
define_method(action) do
@topics = Topic.send(action).includes(:user, :node).limit(20)
@topics = Topic.send(action).includes(:user, :node).page(params[:page])

set_seo_meta(t("homeland.nav.#{action}"))

Expand All @@ -28,7 +28,7 @@ def node

def show
@topic = Topic.find(params[:id])
@replies = @topic.replies.includes(:user)
@replies = @topic.replies.includes(:user).page(params[:page])

set_seo_meta(@topic.title)
end
Expand Down
43 changes: 0 additions & 43 deletions app/helpers/homeland/application_helper.rb

This file was deleted.

14 changes: 14 additions & 0 deletions app/models/homeland/concerns/pagination.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require 'will_paginate'
require 'will_paginate/active_record'

module Homeland
module Concerns
module Pagination
extend ActiveSupport::Concern

included do
self.per_page = Homeland.config.per_page
end
end
end
end
1 change: 1 addition & 0 deletions app/models/homeland/reply.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ class Reply < ActiveRecord::Base
include Homeland::Concerns::SoftDelete
include Homeland::Concerns::MarkdownBody
include Homeland::Concerns::UserMethods
include Homeland::Concerns::Pagination

belongs_to :user, class_name: Homeland.config.user_class.to_s
belongs_to :topic, class_name: 'Homeland::Topic'
Expand Down
1 change: 1 addition & 0 deletions app/models/homeland/topic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ class Topic < ActiveRecord::Base
include Homeland::Concerns::SoftDelete
include Homeland::Concerns::MarkdownBody
include Homeland::Concerns::UserMethods
include Homeland::Concerns::Pagination

belongs_to :user, class_name: Homeland.config.user_class.to_s
belongs_to :last_reply_user, class_name: Homeland.config.user_class.to_s
Expand Down
2 changes: 2 additions & 0 deletions app/views/homeland/topics/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@
<% end %>
</tbody>
</table>

<%= homeland_paginate @topics %>
</div>
1 change: 1 addition & 0 deletions homeland.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ Gem::Specification.new do |s|
s.add_dependency 'activerecord', '~> 4.2.0'
s.add_dependency 'github-markup', '>= 1.3.0'
s.add_dependency 'redcarpet', '>= 3.3.0'
s.add_dependency 'will_paginate', '>= 3.0.0'
end
4 changes: 3 additions & 1 deletion lib/homeland.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
require "homeland/engine"
require 'homeland/configuration'
require 'homeland/markup'
require 'homeland/action_view/tag_helpers'
require 'homeland/action_view/will_paginate'

module Homeland
class << self
def config
return @config if defined?(@config)
@config = Configuration.new
@config.app_name = 'Homeland'
@config.per_size = 32
@config.per_page = 32
@config.user_class = 'User'
@config.user_name_method = 'name'
@config.user_avatar_method = nil
Expand Down
45 changes: 45 additions & 0 deletions lib/homeland/action_view/tag_helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
module Homeland
module ActionView
module TagHelpers
DEFAULT_AVATAR = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAPAAAADwCAMAAAAJixmgAAAAFVBMVEWkpKSnp6eqqqq3t7fS0tLV1dXZ2dmshcKEAAAAtklEQVR4Ae3XsRGAAAjAQFRk/5HtqaTz5H+DlInvAQAAAAAAAAAAAAAAAAAAAACymiveO6o7BQsWLFiwYMGCBS8PFixYsGDBggULFixYsGDBggULFixYsGDBggULFixYsGDBc4IFCxYsWLBgwYIFC14ZfOeAPRQ8IliwYMGCBQsWLFiwYMGCBQsWLFiwYMGCBQsWLFiwYMGCBQsWLFiwYMGCBQv+JQAAAAAAAAAAAAAAAAAAAOAB4KJfdHmj+kwAAAAASUVORK5CYII='

def homeland_render_errors(target)
if target.errors.present?
render partial: '/homeland/shared/error_messages', locals: { target: target }
end
end

def homeland_timeago(time, options = {})
options[:class] ||= "timeago"
content_tag(:abbr, time.to_s, options.merge(:title => time.getutc.iso8601)) if time
end

def homeland_node_tag(node)
return '' if node.blank?

label = [node.badge_html, content_tag(:span, node.name, class: 'name')].join(' ')

link_to raw(label), homeland.node_topics_path(node.id), class: 'node'
end

def homeland_user_name_tag(obj)
return '' if obj.blank?
return '' if obj.user_name.blank?

link_to obj.user_name, obj.user_profile_url, class: 'user-name'
end

def homeland_user_avatar_tag(obj, opts = {})
default = image_tag(DEFAULT_AVATAR, class: 'avatar media-object')
return default if obj.blank?
if obj.user_avatar_url.blank?
img = default
else
img = image_tag(obj.user_avatar_url, class: 'avatar media-object')
end

link_to img, obj.user_profile_url, title: obj.user_name, class: 'user-avatar'
end
end
end
end
38 changes: 38 additions & 0 deletions lib/homeland/action_view/will_paginate.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require 'will_paginate/view_helpers/action_view'

module Homeland
module ActionView
module WillPaginate
def homeland_paginate(collection = nil, options = {})
options, collection = collection, nil if collection.is_a? Hash
# Taken from original will_paginate code to handle if the helper is not passed a collection object.
collection ||= infer_collection_from_controller
options[:renderer] ||= BootstrapLinkRenderer
options[:inner_window] ||= 2
will_paginate(collection, options).try :html_safe
end

class BootstrapLinkRenderer < ::WillPaginate::ActionView::LinkRenderer
protected

def html_container(html)
container_attributes[:class] = 'pagination'
tag(:ul, html, container_attributes)
end

def page_number(page)
tag :li, link(page, page, rel: rel_value(page), class: 'page-link'), class: (page == current_page ? 'page-item active' : 'page-item')
end

def gap
tag :li, link('&hellip;'.html_safe, '#'), class: 'page-item disabled'
end

def previous_or_next_page(page, text, classname)
tag :li, link(text, page || '#', class: 'page-link'),
class: [(classname[0..3] if @options[:page_links]), ("page-item #{classname}" if @options[:page_links]), ('page-item disabled' unless page)].join(' ')
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/homeland/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,6 @@ class Configuration
attr_accessor :app_name

# pagination size, default: 32
attr_accessor :per_size
attr_accessor :per_page
end
end
2 changes: 1 addition & 1 deletion test/dummy/config/boot.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../../../Gemfile', __FILE__)

require 'bundler/setup' # Set up gems listed in the Gemfile.
4 changes: 2 additions & 2 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
ENV['RAILS_ENV'] ||= 'test'

require 'rails'
require File.expand_path('../dummy/config/environment', __FILE__)
require 'rails/test_help'
require 'factory_girl_rails'

class ActiveSupport::TestCase
Expand Down

0 comments on commit 363486f

Please sign in to comment.