-
Notifications
You must be signed in to change notification settings - Fork 34
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
base: master
Are you sure you want to change the base?
Changes from 6 commits
b2edf7b
4e6f285
8d5c8e5
a79ec28
f7166c2
c1efdff
52cc7c6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) | ||
repo = TaskRepository.new.find_by(search_params) | ||
@tasks = all_for_page(repo) | ||
end | ||
|
||
private | ||
|
@@ -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 | ||
10 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's make limit as 50? |
||
end | ||
end | ||
end |
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' | ||
|
@@ -76,5 +77,85 @@ def author_information(author, task) | |
text(" • #{task.lang}") | ||
end | ||
end | ||
|
||
def pagination | ||
html.div(class: 'pagination') do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we really need all this code without any specs here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wrote this code, because pagination function was not available in hanami-pagination gem. |
||
|
||
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
TaskRepository.enable_pagination! |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,7 +29,7 @@ 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 | ||
tasks.where(params).order { id.desc }.map_to(Task) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. WDYT if we split this code into two methods: def find_by(params = {})
search_relation(params).to_a
end
def search_relation(params)
tasks.where(params).order { id.desc }.map_to(Task)
end |
||
end | ||
|
||
def assigned_tasks_for_user(user) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -155,6 +155,41 @@ | |
expect(action.tasks.map(&:status)).to all(eq('in progress')) | ||
end | ||
end | ||
|
||
context 'when are on the first page' do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
So, I found a problem in pagination davydovanton/hanami-pagination#10, thanks for this! 💯 🌟 🎉 |
||
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') } | ||
action.call(params) | ||
end | ||
|
||
it 'returns 10 tasks on page' do | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. missing space here |
||
expect(action.tasks).to all(be_a(Task)) | ||
expect(action.tasks.count).to eq 10 | ||
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') } | ||
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 | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could you cut unnecessary spaces here and format specs? thanks! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, i will focus on it more. |
||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's not a
repo
🤔let's rename it to
relation