Skip to content

Commit

Permalink
Create pagination for the index page ossboard-org#198
Browse files Browse the repository at this point in the history
  • Loading branch information
IlinDmitry committed Jan 10, 2018
1 parent c1efdff commit 52cc7c6
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 113 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ gem 'slim'
gem 'hanami-webpack', github: 'samuelsimoes/hanami-webpack'
gem 'sass'
gem 'relative_time'
gem 'hanami-pagination'
gem 'hanami-pagination', github: 'davydovanton/hanami-pagination'

gem 'hanami-serializer', github: 'davydovanton/hanami-serializer'

Expand Down
12 changes: 9 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ GIT
hanami-controller (> 0.4.0, < 2)
newrelic_rpm

GIT
remote: https://github.com/davydovanton/hanami-pagination.git
revision: fe0079b9a00898a4133a6ca982c1f893fd0b843f
specs:
hanami-pagination (0.1.0)
hanami-helpers (~> 1.1)
hanami-model (~> 1.0)

GIT
remote: https://github.com/davydovanton/hanami-serializer.git
revision: d5f10ef199a73fd5816c01d8dd66d13115e00212
Expand Down Expand Up @@ -192,8 +200,6 @@ GEM
rom (~> 3.3, >= 3.3.3)
rom-repository (~> 1.4)
rom-sql (~> 1.3, >= 1.3.5)
hanami-pagination (0.1.0)
hanami-model (~> 1.0)
hanami-router (1.1.0)
hanami-utils (~> 1.1)
http_router (= 0.11.2)
Expand Down Expand Up @@ -444,7 +450,7 @@ DEPENDENCIES
hanami (= 1.1.0)
hanami-fabrication
hanami-model (= 1.1.0)
hanami-pagination
hanami-pagination!
hanami-serializer!
hanami-webpack!
hiredis
Expand Down
13 changes: 5 additions & 8 deletions apps/web/assets/stylesheets/_tasks.scss
Original file line number Diff line number Diff line change
Expand Up @@ -548,11 +548,11 @@
text-align: center;
padding: 0.3em;
cursor: default; }
.pagination a, .pagination span, .pagination em {
.pagination a, .pagination span {
padding: 0.2em 0.5em; }
.pagination .disabled {
color: #aaaaaa; }
.pagination .current {
.pagination .pagination-current-page {
font-style: normal;
font-weight: bold;
color: red; }
Expand All @@ -564,12 +564,9 @@
border-color: #003366;
background: #214CFD;
color: white; }
.pagination .page_info {
color: #aaaaaa;
padding-top: 0.8em; }
.pagination .previous_page, .pagination .next_page {
.pagination .pagination-previous-page, .pagination .pagination-next-page {
border-width: 2px; }
.pagination .previous_page {
.pagination .pagination-previous-page {
margin-right: 1em; }
.pagination .next_page {
.pagination .pagination-next-page {
margin-left: 1em; }
6 changes: 3 additions & 3 deletions apps/web/controllers/tasks/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ class Index
expose :tasks

def call(params)
repo = TaskRepository.new.find_by(search_params)
@tasks = all_for_page(repo)
relation = TaskRepository.new.search_relation(search_params)
@tasks = all_for_page(relation)
end

private
Expand All @@ -33,7 +33,7 @@ def status
end

def limit
10
50
end
end
end
2 changes: 1 addition & 1 deletion apps/web/templates/tasks/index.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

#pagination
- if pager.all_pages.count > 1
= pagination
= paginate(:tasks)

.tasks__new
- if current_user.registred?
Expand Down
80 changes: 0 additions & 80 deletions apps/web/views/tasks/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,85 +77,5 @@ def author_information(author, task)
text(" • #{task.lang}")
end
end

def pagination
html.div(class: 'pagination') do

if pager.prev_page.nil?
span(class: 'disabled previous_page')
text '← Previous'
else
a(href: page_url(pager.prev_page) + '&status=' + params[:status]) do
text '← Previous'
end
end

total_pages = pager.all_pages.count

first_in_range = pager.pages_range.first

if 1 < first_in_range
a(href: page_url(1) + '&status=' + params[:status]) do
1
end
end

if 2 < first_in_range
a(href: page_url(2) + '&status=' + params[:status]) do
2
end
end

if 1 < first_in_range || 2 < first_in_range
span(class: 'gap') do
text '...'
end
end

pager.pages_range.each do |page_number|

if pager.current_page?(page_number)
em(class: 'current') do
text page_number
end
else
a(href: page_url(page_number) + '&status=' + params[:status]) do
page_number
end
end

end

last_in_range = pager.pages_range[-1]

if last_in_range < total_pages
span(class: 'gap') do
text '...'
end

if last_in_range != total_pages-1
a(href: page_url(total_pages-1) + '&status=' + params[:status]) do
total_pages-1
end
end

a(href: page_url(total_pages) + '&status=' + params[:status]) do
total_pages
end

end

if pager.next_page.nil?
span(class: 'disabled next_page')
text 'Next →'
else
a(href: page_url(pager.next_page) + '&status=' + params[:status]) do
text 'Next →'
end
end

end
end

end
end
4 changes: 4 additions & 0 deletions lib/ossboard/repositories/task_repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ def all_from_date_counted_by_status_and_day(from)
end

def find_by(params = {})
search_relation(params).to_a
end

def search_relation(params)
tasks.where(params).order { id.desc }.map_to(Task)
end

Expand Down
16 changes: 8 additions & 8 deletions spec/ossboard/repositories/task_repository_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@

context 'when params is empty' do
it 'returns array of closed tasks' do
result = repo.find_by(approved: true).to_a
result = repo.find_by(approved: true)
expect(result).to all(be_a(Task))
expect(result.count).to eq 3
end
end

context 'when status is closed' do
it 'returns array of closed tasks' do
result = repo.find_by(status: 'closed', approved: true).to_a
result = repo.find_by(status: 'closed', approved: true)
expect(result).to all(be_a(Task))
expect(result.count).to eq 1
expect(result.first.status).to eq 'closed'
Expand All @@ -27,7 +27,7 @@

context 'when status is done' do
it 'returns array of done tasks' do
result = repo.find_by(status: 'done', approved: true).to_a
result = repo.find_by(status: 'done', approved: true)
expect(result).to all(be_a(Task))
expect(result.count).to eq 1
expect(result.first.status).to eq 'done'
Expand All @@ -36,7 +36,7 @@

context 'when status is in progress' do
it 'returns array of in progress tasks' do
result = repo.find_by(status: 'in progress', approved: true).to_a
result = repo.find_by(status: 'in progress', approved: true)
expect(result).to all(be_a(Task))
expect(result.count).to eq 1
expect(result.first.status).to eq 'in progress'
Expand All @@ -45,7 +45,7 @@

context 'when lang is ruby' do
it 'returns array of ruby language tasks' do
result = repo.find_by(lang: 'ruby', approved: true).to_a
result = repo.find_by(lang: 'ruby', approved: true)
expect(result).to all(be_a(Task))
expect(result.count).to eq 1
expect(result.first.lang).to eq 'ruby'
Expand All @@ -54,7 +54,7 @@

context 'when lang is haskell' do
it 'returns array of haskell language tasks' do
result = repo.find_by(lang: 'haskell', approved: true).to_a
result = repo.find_by(lang: 'haskell', approved: true)
expect(result).to all(be_a(Task))
expect(result.count).to eq 1
expect(result.first.lang).to eq 'haskell'
Expand All @@ -63,7 +63,7 @@

context 'when lang is unknown' do
it 'returns array of unknown language tasks' do
result = repo.find_by(lang: 'unknown', approved: true).to_a
result = repo.find_by(lang: 'unknown', approved: true)
expect(result).to all(be_a(Task))
expect(result.count).to eq 1
expect(result.first.lang).to eq 'unknown'
Expand All @@ -72,7 +72,7 @@

context 'when status is in progress and lang is ruby' do
it 'returns array of in progress, ruby tasks' do
result = repo.find_by(status: 'in progress', lang: 'ruby', approved: true).to_a
result = repo.find_by(status: 'in progress', lang: 'ruby', approved: true)
expect(result).to all(be_a(Task))
expect(result.count).to eq 1
expect(result.first.lang).to eq 'ruby'
Expand Down
13 changes: 4 additions & 9 deletions spec/web/controllers/tasks/index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -160,24 +160,22 @@
let(:params) { { lang: 'ruby', status: 'done', page: 1} }

before do
8.times { |i| Fabricate.create(:task, title: "title ##{i}", approved: true, status: 'done', lang: 'ruby') }
48.times { |i| Fabricate.create(:task, title: "title ##{i}", approved: true, status: 'done', lang: 'ruby') }
action.call(params)
end

it 'returns 10 tasks on page' do

it 'returns 50 tasks on page' do
expect(action.tasks).to all(be_a(Task))
expect(action.tasks.count).to eq 10
expect(action.tasks.count).to eq 50
expect(action.tasks.map(&:status)).to all(eq('done'))
end

end

context 'when are on the second page' do
let(:params) { { lang: 'ruby', status: 'done', page: 2} }

before do
8.times { |i| Fabricate.create(:task, title: "title ##{i}", approved: true, status: 'done', lang: 'ruby') }
48.times { |i| Fabricate.create(:task, title: "title ##{i}", approved: true, status: 'done', lang: 'ruby') }
action.call(params)
end

Expand All @@ -186,10 +184,7 @@
expect(action.tasks.count).to eq 1
expect(action.tasks.map(&:status)).to all(eq('done'))
end

end


end
end
end

0 comments on commit 52cc7c6

Please sign in to comment.