Skip to content

Commit

Permalink
better user home pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
sethherr committed Nov 3, 2017
1 parent 803e079 commit 5b53e5e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
11 changes: 8 additions & 3 deletions app/controllers/welcome_controller.rb
Expand Up @@ -20,12 +20,17 @@ def goodbye
end

def user_home
bikes = current_user.bikes
page = params[:page] || 1
@locks_active_tab = params[:active_tab] == 'locks'
@per_page = params[:per_page] || 20
paginated_bikes = Kaminari.paginate_array(bikes).page(page).per(@per_page)
@bikes = BikeDecorator.decorate_collection(paginated_bikes)
# If there are over 100 bikes created by the user, we'll have problems loading and sorting them
if current_user.creation_states.limit(101).count > 100
bikes = current_user.rough_approx_bikes.page(page).per(@per_page)
@bikes = bikes.decorate
else
bikes = current_user.bikes.page(page).per(@per_page)
@bikes = bikes.decorate
end
@locks = LockDecorator.decorate_collection(current_user.locks)
end

Expand Down
4 changes: 4 additions & 0 deletions app/models/user.rb
Expand Up @@ -212,6 +212,10 @@ def bikes(user_hidden=true)
Bike.unscoped.where(id: bike_ids(user_hidden))
end

def rough_approx_bikes # Rough fix for users with large numbers of bikes
Bike.includes(:ownerships).where(ownerships: { current: true, user_id: id })
end

def bike_ids(user_hidden=true)
ows = ownerships.where(example: false).where(current: true)
if user_hidden
Expand Down

0 comments on commit 5b53e5e

Please sign in to comment.