2013-01-05 21:21:11 +00:00
class TopicsController < ApplicationController
2014-07-31 01:10:10 +00:00
include TopicsHelper
before_filter :require_user , only : [ :create , :update , :destroy ]
respond_to :html , :js , :json
# GET /topics/autocomplete_topic
def autocomplete_topic
@current = current_user
term = params [ :term ]
if term && ! term . empty?
# !connor term here needs to have .downcase
@topics = Topic . where ( 'LOWER("name") like ?' , term . downcase + '%' ) . order ( '"name"' )
#read this next line as 'delete a topic if its private and you're either
#1. logged out or 2. logged in but not the topic creator
@topics . delete_if { | t | t . permission == " private " &&
( ! authenticated? || ( authenticated? && @current . id != t . user_id ) ) }
else
@topics = [ ]
end
render json : autocomplete_array_json ( @topics )
2013-01-05 21:21:11 +00:00
end
2014-07-31 01:10:10 +00:00
# GET topics/:id
def show
@current = current_user
@topic = Topic . find ( params [ :id ] ) . authorize_to_show ( @current )
if not @topic
redirect_to root_url and return
end
2014-08-11 22:57:34 +00:00
respond_to do | format |
format . html {
2014-11-03 13:46:22 +00:00
@alltopics = ( [ @topic ] + @topic . relatives ) . delete_if { | t | t . permission == " private " && ( ! authenticated? || ( authenticated? && @current . id != t . user_id ) ) } # should limit to topics visible to user
@allsynapses = @topic . synapses . delete_if { | s | s . permission == " private " && ( ! authenticated? || ( authenticated? && @current . id != s . user_id ) ) }
2014-08-11 22:57:34 +00:00
respond_with ( @allsynapses , @alltopics , @topic )
}
format . json { render json : @topic }
end
end
# GET topics/:id/network
def network
@current = current_user
@topic = Topic . find ( params [ :id ] ) . authorize_to_show ( @current )
if not @topic
redirect_to root_url and return
end
2014-11-03 13:46:22 +00:00
@alltopics = @topic . relatives . delete_if { | t | t . permission == " private " && ( ! authenticated? || ( authenticated? && @current . id != t . user_id ) ) }
@allsynapses = @topic . synapses . delete_if { | s | s . permission == " private " && ( ! authenticated? || ( authenticated? && @current . id != s . user_id ) ) }
2014-08-11 22:57:34 +00:00
@json = Hash . new ( )
@json [ 'topic' ] = @topic
@json [ 'relatives' ] = @alltopics
@json [ 'synapses' ] = @allsynapses
2014-07-31 01:10:10 +00:00
respond_to do | format |
2014-08-11 22:57:34 +00:00
format . json { render json : @json }
2014-07-31 01:10:10 +00:00
end
2013-01-05 21:21:11 +00:00
end
2014-07-31 01:10:10 +00:00
# POST /topics
# POST /topics.json
def create
@topic = Topic . new ( params [ :topic ] )
respond_to do | format |
if @topic . save
format . json { render json : @topic , status : :created }
else
format . json { render json : @topic . errors , status : :unprocessable_entity }
end
end
2013-01-05 21:21:11 +00:00
end
2014-07-31 01:10:10 +00:00
# PUT /topics/1
# PUT /topics/1.json
def update
@topic = Topic . find ( params [ :id ] )
respond_to do | format |
if @topic . update_attributes ( params [ :topic ] )
format . json { head :no_content }
else
format . json { render json : @topic . errors , status : :unprocessable_entity }
end
2013-04-26 04:07:29 +00:00
end
2013-01-05 21:21:11 +00:00
end
2014-07-31 01:10:10 +00:00
# DELETE topics/:id
def destroy
@current = current_user
@topic = Topic . find ( params [ :id ] ) . authorize_to_edit ( @current )
if @topic
@synapses = @topic . synapses
@mappings = @topic . mappings
@synapses . each do | synapse |
synapse . mappings . each do | m |
@map = m . map
@map . touch ( :updated_at )
m . delete
end
synapse . delete
end
@mappings . each do | mapping |
@map = mapping . map
@map . touch ( :updated_at )
mapping . delete
end
@topic . delete
end
respond_to do | format |
format . js { render :json = > " success " }
end
2013-01-05 21:21:11 +00:00
end
end