Skip to content

Commit

Permalink
Merge pull request #18 from hanami/feature/pagination
Browse files Browse the repository at this point in the history
Add pagination gem for show page
  • Loading branch information
davydovanton committed Jun 3, 2017
2 parents 488e1c7 + 9d2b121 commit 1522248
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 4 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ gem 'slim'
gem 'hanami-bootstrap', '0.4'
gem 'sass'

gem 'hanami-pagination'

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

Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ GEM
hanami-utils (~> 1.0)
rom-repository (~> 1.3)
rom-sql (~> 1.2)
hanami-pagination (0.1.0)
hanami-model (~> 1.0)
hanami-router (1.0.0)
hanami-utils (~> 1.0)
http_router (= 0.11.2)
Expand Down Expand Up @@ -223,6 +225,7 @@ DEPENDENCIES
hanami (= 1.0.0)
hanami-bootstrap (= 0.4)
hanami-model (= 1.0.0)
hanami-pagination
hanami-serializer!
hiredis
mock_redis
Expand Down
2 changes: 1 addition & 1 deletion apps/api/controllers/contributors/show.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def call(params)
def responce_hash(contributor)
return ERROR_RESPONCE unless contributor

commits = CommitRepository.new.all_for_contributor(contributor.id).map(&:to_h)
commits = CommitRepository.new.all_for_contributor(contributor.id).to_a.map(&:to_h)

{
status: :ok,
Expand Down
4 changes: 3 additions & 1 deletion apps/web/controllers/contributors/show.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
module Web::Controllers::Contributors
class Show
include Web::Action
include Hanami::Pagination::Action

expose :contributor, :commits

def call(params)
@contributor = ContributorRepository.new.find_by_github(params[:id])
@commits = CommitRepository.new.all_for_contributor(@contributor.id)
@commits = all_for_page(CommitRepository.new.all_for_contributor(@contributor.id))
end
end
end
14 changes: 14 additions & 0 deletions apps/web/templates/contributors/show.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,17 @@ table.table.table-hover
td = link_to commit.title, commit.url, target: '_blank'
td = project_name(commit)

- if show_pagination?
nav aria-label="Page navigation"
ul class="pagination"
- if pager.prev_page
li
a href="#{prev_page_url}" aria-label="Previous"
span aria-hidden="true" «
- pager.pages_range.each do |i|
li class="#{active_class(i)}"
a href="/contributors/#{contributor.github}/?page=#{i}" = i
- if pager.next_page
li
a href="#{next_page_url}" aria-label="Next"
span aria-hidden="true" »
9 changes: 9 additions & 0 deletions apps/web/views/contributors/show.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
module Web::Views::Contributors
class Show
include Web::View
include Hanami::Pagination::View

def active_class(page)
'active' if pager.current_page?(page)
end

def project_name(commit)
matcher = commit.url.match(%r{github.com/hanami/([\w.]+)/})
Expand All @@ -10,5 +15,9 @@ def project_name(commit)
def title
"Commits for #{contributor.github}"
end

def show_pagination?
pager.pager.total_pages > 1
end
end
end
1 change: 1 addition & 0 deletions config/initializers/enable_pagination.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CommitRepository.enable_pagination!
2 changes: 1 addition & 1 deletion lib/contributors/repositories/commit_repository.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class CommitRepository < Hanami::Repository
def all_for_contributor(contributor_id)
commits.where(contributor_id: contributor_id).to_a
commits.where(contributor_id: contributor_id)
end
end
2 changes: 1 addition & 1 deletion spec/contributors/services/add_new_commits_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
VCR.use_cassette("commits") do
described_class.new.call

commits = commit_repo.all_for_contributor(contributor.id)
commits = commit_repo.all_for_contributor(contributor.id).to_a

expect(commits).not_to be_empty
expect(commits.first.url).to be
Expand Down

0 comments on commit 1522248

Please sign in to comment.