Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create pagination for the index page #198 #201

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ gem 'slim'
gem 'hanami-webpack', github: 'samuelsimoes/hanami-webpack'
gem 'sass'
gem 'relative_time'
gem 'hanami-pagination', github: 'davydovanton/hanami-pagination'

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

Expand Down
9 changes: 9 additions & 0 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 @@ -442,6 +450,7 @@ DEPENDENCIES
hanami (= 1.1.0)
hanami-fabrication
hanami-model (= 1.1.0)
hanami-pagination!
hanami-serializer!
hanami-webpack!
hiredis
Expand Down
27 changes: 27 additions & 0 deletions apps/web/assets/stylesheets/_tasks.scss
Original file line number Diff line number Diff line change
Expand Up @@ -543,3 +543,30 @@
margin: 0 -10px -7px;
}
}

.pagination {
text-align: center;
padding: 0.3em;
cursor: default; }
.pagination a, .pagination span {
padding: 0.2em 0.5em; }
.pagination .disabled {
color: #aaaaaa; }
.pagination .pagination-current-page {
font-style: normal;
font-weight: bold;
color: red; }
.pagination a {
border: 1px solid #dddddd;
color: #214CFD;
text-decoration: none; }
.pagination a:hover, .pagination a:focus {
border-color: #003366;
background: #214CFD;
color: white; }
.pagination .pagination-previous-page, .pagination .pagination-next-page {
border-width: 2px; }
.pagination .pagination-previous-page {
margin-right: 1em; }
.pagination .pagination-next-page {
margin-left: 1em; }
8 changes: 7 additions & 1 deletion apps/web/controllers/tasks/index.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
module Web::Controllers::Tasks
class Index
include Web::Action
include Hanami::Pagination::Action
expose :tasks

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

private
Expand All @@ -29,5 +31,9 @@ def with_language(search_params)
def status
Task::VALID_STATUSES.values.include?(params[:status]) ? params[:status] : 'in progress'
end

def limit
50
end
end
end
4 changes: 4 additions & 0 deletions apps/web/templates/tasks/index.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
.tasks__language-selector
= select_tasks_by_language

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

.tasks__new
- if current_user.registred?
= link_to_new_task
Expand Down
1 change: 1 addition & 0 deletions apps/web/views/tasks/index.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module Web::Views::Tasks
class Index
include Web::View
include Hanami::Pagination::View

def title
'OSSBoard: tasks'
Expand Down
1 change: 1 addition & 0 deletions config/initializers/enable_pagination.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TaskRepository.enable_pagination!
6 changes: 5 additions & 1 deletion lib/ossboard/repositories/task_repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ def all_from_date_counted_by_status_and_day(from)
end

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

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

def assigned_tasks_for_user(user)
Expand Down
30 changes: 30 additions & 0 deletions spec/web/controllers/tasks/index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,36 @@
expect(action.tasks.map(&:status)).to all(eq('in progress'))
end
end

context 'when are on the first page' do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's simplify specs here. For this we can use DI (dependency injection and wiki) for repository instance:

So, I found a problem in pagination davydovanton/hanami-pagination#10, thanks for this! 💯 🌟 🎉

let(:params) { { lang: 'ruby', status: 'done', page: 1} }

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

it 'returns 50 tasks on page' do
expect(action.tasks).to all(be_a(Task))
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
48.times { |i| Fabricate.create(:task, title: "title ##{i}", approved: true, status: 'done', lang: 'ruby') }
action.call(params)
end

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