diff --git a/Gemfile b/Gemfile index 8ebb0cb6..481f94cd 100644 --- a/Gemfile +++ b/Gemfile @@ -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' diff --git a/Gemfile.lock b/Gemfile.lock index cd2f369a..d90abc8e 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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 @@ -442,6 +450,7 @@ DEPENDENCIES hanami (= 1.1.0) hanami-fabrication hanami-model (= 1.1.0) + hanami-pagination! hanami-serializer! hanami-webpack! hiredis diff --git a/apps/web/assets/stylesheets/_tasks.scss b/apps/web/assets/stylesheets/_tasks.scss index 3dfa06bb..8fa99414 100644 --- a/apps/web/assets/stylesheets/_tasks.scss +++ b/apps/web/assets/stylesheets/_tasks.scss @@ -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; } diff --git a/apps/web/controllers/tasks/index.rb b/apps/web/controllers/tasks/index.rb index 1af395cd..184a20e0 100644 --- a/apps/web/controllers/tasks/index.rb +++ b/apps/web/controllers/tasks/index.rb @@ -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 @@ -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 diff --git a/apps/web/templates/tasks/index.html.slim b/apps/web/templates/tasks/index.html.slim index 59ced661..950dbb44 100644 --- a/apps/web/templates/tasks/index.html.slim +++ b/apps/web/templates/tasks/index.html.slim @@ -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 diff --git a/apps/web/views/tasks/index.rb b/apps/web/views/tasks/index.rb index adb914c3..8942cac6 100644 --- a/apps/web/views/tasks/index.rb +++ b/apps/web/views/tasks/index.rb @@ -1,6 +1,7 @@ module Web::Views::Tasks class Index include Web::View + include Hanami::Pagination::View def title 'OSSBoard: tasks' diff --git a/config/initializers/enable_pagination.rb b/config/initializers/enable_pagination.rb new file mode 100644 index 00000000..07585511 --- /dev/null +++ b/config/initializers/enable_pagination.rb @@ -0,0 +1 @@ +TaskRepository.enable_pagination! \ No newline at end of file diff --git a/lib/ossboard/repositories/task_repository.rb b/lib/ossboard/repositories/task_repository.rb index f6708b7d..eed0e052 100644 --- a/lib/ossboard/repositories/task_repository.rb +++ b/lib/ossboard/repositories/task_repository.rb @@ -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) diff --git a/spec/web/controllers/tasks/index_spec.rb b/spec/web/controllers/tasks/index_spec.rb index a1322e99..93aeafc6 100644 --- a/spec/web/controllers/tasks/index_spec.rb +++ b/spec/web/controllers/tasks/index_spec.rb @@ -155,6 +155,36 @@ expect(action.tasks.map(&:status)).to all(eq('in progress')) end end + + context 'when are on the first page' do + 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