factor maps#index into 4 separate functions

This commit is contained in:
Devin Howard 2016-02-13 17:42:36 +08:00
parent baca4aac83
commit 8ef847bd6d
2 changed files with 55 additions and 36 deletions

View file

@ -6,47 +6,66 @@ class MapsController < ApplicationController
autocomplete :map, :name, :full => true, :extra_data => [:user_id] autocomplete :map, :name, :full => true, :extra_data => [:user_id]
# GET /explore/active # GET /explore/active
# GET /explore/featured def activemaps
# GET /explore/mapper/:id
def index
return redirect_to activemaps_url if request.path == "/explore"
@current = current_user @current = current_user
@maps = []
page = params[:page].present? ? params[:page] : 1 page = params[:page].present? ? params[:page] : 1
if request.path.index("/explore/active") != nil
@maps = Map.where("maps.permission != ?", "private").order("updated_at DESC").page(page).per(20) @maps = Map.where("maps.permission != ?", "private").order("updated_at DESC").page(page).per(20)
@request = "active" @request = "active"
elsif request.path.index("/explore/featured") != nil
@maps = Map.where("maps.featured = ? AND maps.permission != ?", true, "private").order("updated_at DESC").page(page).per(20) redirect_to root_url and return if authenticated?
respond_to do |format|
format.html { respond_with(@maps, @request, @user) }
format.json { render json: @maps }
end
end
# GET /explore/featured
def featuredmaps
@current = current_user
page = params[:page].present? ? params[:page] : 1
@maps = Map.where("maps.featured = ? AND maps.permission != ?", true, "private")
.order("updated_at DESC").page(page).per(20)
@request = "featured" @request = "featured"
elsif request.path.index('/explore/mine') != nil # looking for maps by me
respond_to do |format|
format.html { respond_with(@maps, @request, @user) }
format.json { render json: @maps }
end
end
# GET /explore/mine
def mymaps
return redirect_to activemaps_url if !authenticated? return redirect_to activemaps_url if !authenticated?
@current = current_user
page = params[:page].present? ? params[:page] : 1
# don't need to exclude private maps because they all belong to you # don't need to exclude private maps because they all belong to you
@maps = Map.where("maps.user_id = ?", @current.id).order("updated_at DESC").page(page).per(20) @maps = Map.where("maps.user_id = ?", @current.id).order("updated_at DESC").page(page).per(20)
@request = "you" @request = "you"
elsif request.path.index('/explore/mapper/') != nil # looking for maps by a mapper
respond_to do |format|
format.html { respond_with(@maps, @request, @user) }
format.json { render json: @maps }
end
end
# GET /explore/mapper/:id
def usermaps
@current = current_user
page = params[:page].present? ? params[:page] : 1
@user = User.find(params[:id]) @user = User.find(params[:id])
@maps = Map.where("maps.user_id = ? AND maps.permission != ?", @user.id, "private").order("updated_at DESC").page(page).per(20) @maps = Map.where("maps.user_id = ? AND maps.permission != ?", @user.id, "private").order("updated_at DESC").page(page).per(20)
@request = "mapper" @request = "mapper"
end
respond_to do |format| respond_to do |format|
format.html { format.html { respond_with(@maps, @request, @user) }
if @request == "active" && authenticated?
redirect_to root_url and return
end
respond_with(@maps, @request, @user)
}
format.json { render json: @maps } format.json { render json: @maps }
end end
end end
# GET maps/:id # GET maps/:id
def show def show
@current = current_user @current = current_user
@map = Map.find(params[:id]).authorize_to_show(@current) @map = Map.find(params[:id]).authorize_to_show(@current)
@ -72,7 +91,6 @@ class MapsController < ApplicationController
# GET maps/:id/contains # GET maps/:id/contains
def contains def contains
@current = current_user @current = current_user
@map = Map.find(params[:id]).authorize_to_show(@current) @map = Map.find(params[:id]).authorize_to_show(@current)

View file

@ -20,11 +20,12 @@ Metamaps::Application.routes.draw do
get 'topics/:id/relative_numbers', to: 'topics#relative_numbers', as: :relative_numbers get 'topics/:id/relative_numbers', to: 'topics#relative_numbers', as: :relative_numbers
get 'topics/:id/relatives', to: 'topics#relatives', as: :relatives get 'topics/:id/relatives', to: 'topics#relatives', as: :relatives
get 'explore/active', to: 'maps#index', as: :activemaps resources :maps, except: [:index, :new, :edit]
get 'explore/featured', to: 'maps#index', as: :featuredmaps get 'explore/active', to: 'maps#activemaps'
get 'explore/mine', to: 'maps#index', as: :mymaps get 'explore/featured', to: 'maps#featuredmaps'
get 'explore/mapper/:id', to: 'maps#index', as: :usermaps get 'explore/mine', to: 'maps#mymaps'
resources :maps, except: [:new, :edit] get 'explore/mapper/:id', to: 'maps#usermaps'
get 'maps/:id/contains', to: 'maps#contains', as: :contains get 'maps/:id/contains', to: 'maps#contains', as: :contains
post 'maps/:id/upload_screenshot', to: 'maps#screenshot', as: :screenshot post 'maps/:id/upload_screenshot', to: 'maps#screenshot', as: :screenshot