Skip to content

Commit

Permalink
Merge pull request #511 from cbdr/bugfix/AR-66-Broken-Pagination-Headers
Browse files Browse the repository at this point in the history
AR-66: Broken Pagination Headers
  • Loading branch information
Alex Tharp authored Jul 11, 2017
2 parents 7b2f3b1 + 254713d commit e2b50ee
Show file tree
Hide file tree
Showing 11 changed files with 28 additions and 42 deletions.
8 changes: 4 additions & 4 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ gem 'cortex-exceptions', '= 0.0.4'
gem 'cortex-plugins-core', '= 0.12.4'

# API
gem 'grape', '~> 0.17'
gem 'grape-entity', '~> 0.6.0'
gem 'grape-swagger', '~> 0.27.1'
gem 'grape', '~> 0.19.2'
gem 'grape-entity', '~> 0.6.1'
gem 'grape-swagger', '~> 0.27.2'

# Authorization
gem 'six', '~> 0.2.0'
Expand All @@ -36,7 +36,7 @@ gem 'image_optim_pack', '~> 0.4.0'
gem 'acts-as-taggable-on', '~> 4.0'
gem 'bcrypt', '~> 3.1.11'
gem 'kaminari', '~> 0.17.0'
gem 'grape-kaminari', git: 'https://github.com/toastercup/grape-kaminari.git', branch: 'set-paginate-headers-extraction'
gem 'grape-kaminari', '~> 0.1.9'
gem 'elasticsearch-model', '~> 5.0'
gem 'elasticsearch-rails', '~> 5.0'
gem 'paranoia', '~> 2.3'
Expand Down
22 changes: 8 additions & 14 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,6 @@ GIT
awesome_nested_set (3.1.2)
activerecord (>= 4.0.0, < 5.1)

GIT
remote: https://github.com/toastercup/grape-kaminari.git
revision: 661388050eaa97f70006b74fe300e19760b8a132
branch: set-paginate-headers-extraction
specs:
grape-kaminari (0.1.9)
grape
kaminari

GIT
remote: https://github.com/triloch/rails-observers.git
revision: ea30390cb07b4a37dd2f9d03966c89c1372f9dbe
Expand Down Expand Up @@ -251,7 +242,10 @@ GEM
grape-entity (0.6.1)
activesupport (>= 5.0.0)
multi_json (>= 1.3.2)
grape-swagger (0.27.1)
grape-kaminari (0.1.9)
grape
kaminari
grape-swagger (0.27.2)
grape (>= 0.16.2)
guard (2.14.1)
formatador (>= 0.2.4)
Expand Down Expand Up @@ -640,10 +634,10 @@ DEPENDENCIES
font-awesome-sass (~> 4.7.0)
foreman
gon (~> 6.1.0)
grape (~> 0.17)
grape-entity (~> 0.6.0)
grape-kaminari!
grape-swagger (~> 0.27.1)
grape (~> 0.19.2)
grape-entity (~> 0.6.1)
grape-kaminari (~> 0.1.9)
grape-swagger (~> 0.27.2)
guard-jasmine (~> 2.1)
guard-rspec (~> 4.7)
haml (~> 5.0)
Expand Down
3 changes: 1 addition & 2 deletions app/api/v1/resources/media.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ class Media < Grape::API
require_scope! 'view:media'

@media = ::GetMultipleMedia.call(params: declared(clean_params(params), include_missing: false), tenant: current_tenant).media
set_paginate_headers(@media)
::V1::Entities::Media.represent @media.to_a
::V1::Entities::Media.represent paginate(@media).records
end

desc 'Show media tags'
Expand Down
17 changes: 8 additions & 9 deletions app/api/v1/resources/posts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ class Posts < Grape::API
require_scope! 'view:posts'
authorize! :view, ::Post
@posts = ::GetPosts.call(params: declared(clean_params(params), include_missing: false), tenant: current_tenant).posts
set_paginate_headers(@posts)
::V1::Entities::Post.represent @posts.to_a
::V1::Entities::Post.represent paginate(@posts).records
end

desc 'Show published posts', { entity: ::V1::Entities::Post, nickname: "postFeed" }
Expand All @@ -37,13 +36,14 @@ class Posts < Grape::API
params_hash = Digest::MD5.hexdigest(declared(params).to_s)
cache_key = "feed-#{last_updated_at}-#{current_tenant.id}-#{params_hash}"

posts_page = ::Rails.cache.fetch(cache_key, expires_in: 30.minutes, race_condition_ttl: 10) do
posts = ::Rails.cache.fetch(cache_key, expires_in: 30.minutes, race_condition_ttl: 10) do
posts = ::GetPosts.call(params: declared(clean_params(params), include_missing: false), tenant: current_tenant, published: true).posts
set_paginate_headers(posts)
::V1::Entities::Post.represent posts.to_a
paginated_posts = paginate(posts).records.to_a
{records: paginated_posts, headers: header}
end

posts_page
header.merge!(posts[:headers])
::V1::Entities::Post.represent posts[:records]
end

desc 'Show all published posts', { entity: ::V1::Entities::Post, nickname: "allPostFeed" }
Expand All @@ -53,7 +53,7 @@ class Posts < Grape::API
authorize! :view, ::Post

posts = ::GetPosts.call(params: declared(clean_params(params), include_missing: false), tenant: current_tenant, published: true).posts
posts_page = ::V1::Entities::Post.represent posts.to_a
::V1::Entities::Post.represent paginate(posts).records
end

desc 'Show published post authors'
Expand All @@ -70,8 +70,7 @@ class Posts < Grape::API
authorize! :view, post

@posts = ::GetRelatedPosts.call(post: post, params: declared(clean_params(params), include_missing: false), tenant: current_tenant, published: true).posts
set_paginate_headers(@posts)
::V1::Entities::Post.represent @posts.to_a
::V1::Entities::Post.represent paginate(@posts).records
end

desc 'Show a published post', { entity: ::V1::Entities::Post, nickname: "showFeedPost" }
Expand Down
3 changes: 1 addition & 2 deletions app/api/v1/resources/tenants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ class Tenants < Grape::API
require_scope! 'view:users'

@users = ::GetUsers.call(params: declared(clean_params(params), include_missing: false), tenant_id: params[:id]).users
set_paginate_headers(@users)
::V1::Entities::User.represent @users, full: true
::V1::Entities::User.represent paginate(@users).records, full: true
end
end
end
Expand Down
3 changes: 1 addition & 2 deletions app/api/v1/resources/webpages.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ class Webpages < Grape::API
require_scope! 'view:webpages'

@webpages = ::GetWebpages.call(params: declared(clean_params(params), include_missing: false), tenant: current_tenant).webpages
set_paginate_headers(@webpages)
::V1::Entities::Webpage.represent @webpages.to_a, full: true
::V1::Entities::Webpage.represent paginate(@webpages).records, full: true
end

desc 'Show Webpage Snippets as public feed by URL', { entity: ::V1::Entities::Webpage, nickname: 'showWebpageFeed' }
Expand Down
3 changes: 1 addition & 2 deletions app/interactors/get_multiple_media.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ def call
media = media.show_all(context.tenant)
end

media = media.page(context.params.page).per(context.params.per_page)
context.media = media.records
context.media = media
end

private
Expand Down
3 changes: 1 addition & 2 deletions app/interactors/get_posts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ def call
posts = posts.show_all(context.tenant, context.published)
end

posts = posts.page(context.params.page).per(context.params.per_page)
context.posts = posts.records
context.posts = posts
end

private
Expand Down
2 changes: 1 addition & 1 deletion app/interactors/get_related_posts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ class GetRelatedPosts
def call
related = context.post.related(context.tenant, context.published)

context.posts = related.page(context.params.page).per(context.params.per_page).records
context.posts = related
end
end
3 changes: 1 addition & 2 deletions app/interactors/get_users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ def call
users = users.show_all(context.tenant_id)
end

users = users.page(context.params.page).per(context.params.per_page)
context.users = users.records
context.users = users
end

private
Expand Down
3 changes: 1 addition & 2 deletions app/interactors/get_webpages.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ def call
webpages = webpages.show_all(context.tenant)
end

webpages = webpages.page(context.params.page).per(context.params.per_page)
context.webpages = webpages.records
context.webpages = webpages
end

private
Expand Down

0 comments on commit e2b50ee

Please sign in to comment.