factor out map_scope function

This commit is contained in:
Devin Howard 2016-09-26 00:28:23 +08:00
parent bb87c9c2db
commit f75ad41a82

View file

@ -9,8 +9,7 @@ class ExploreController < ApplicationController
# GET /explore/active
def active
@maps = policy_scope(Map).order(updated_at: :desc)
.page(params[:page]).per(20)
@maps = map_scope(Map)
respond_to do |format|
format.html do
@ -24,8 +23,7 @@ class ExploreController < ApplicationController
# GET /explore/featured
def featured
@maps = policy_scope(Map).where(featured: true).order(updated_at: :desc)
.page(params[:page]).per(20)
@maps = map_scope(Map.where(featured: true))
respond_to do |format|
format.html { respond_with(@maps, @user) }
@ -35,8 +33,7 @@ class ExploreController < ApplicationController
# GET /explore/mine
def mine
@maps = policy_scope(Map).where(user_id: current_user.id)
.order(updated_at: :desc).page(params[:page]).per(20)
@maps = map_scope(Map.where(user_id: current_user.id))
respond_to do |format|
format.html { respond_with(@maps, @user) }
@ -46,8 +43,7 @@ class ExploreController < ApplicationController
# GET /explore/shared
def shared
@maps = policy_scope(Map).where(id: current_user.shared_maps.map(&:id))
.order(updated_at: :desc).page(params[:page]).per(20)
@maps = map_scope(Map.where(id: current_user.shared_maps.map(&:id)))
respond_to do |format|
format.html { respond_with(@maps, @user) }
@ -57,9 +53,7 @@ class ExploreController < ApplicationController
# GET /explore/starred
def starred
stars = current_user.stars.map(&:map_id)
@maps = policy_scope(Map).where(id: stars).order(updated_at: :desc)
.page(params[:page]).per(20)
@maps = map_scope(Map.where(id: current_user.stars.map(&:map_id)))
respond_to do |format|
format.html { respond_with(@maps, @user) }
@ -70,8 +64,7 @@ class ExploreController < ApplicationController
# GET /explore/mapper/:id
def mapper
@user = User.find(params[:id])
@maps = policy_scope(Map.where(user: @user))
.order(updated_at: :desc).page(params[:page]).per(20)
@maps = map_scope(Map.where(user: @user))
respond_to do |format|
format.html { respond_with(@maps, @user) }
@ -81,6 +74,10 @@ class ExploreController < ApplicationController
private
def map_scope(scope)
policy_scope(scope).order(updated_at: :desc).page(params[:page]).per(20)
end
def authorize_explore
authorize :Explore
end