2013-01-05 21:21:11 +00:00
|
|
|
class TopicsController < ApplicationController
|
2014-01-29 03:46:58 +00:00
|
|
|
include TopicsHelper
|
|
|
|
|
2014-07-27 19:57:35 +00:00
|
|
|
before_filter :require_user, only: [:create, :update, :destroy]
|
2013-01-05 21:21:11 +00:00
|
|
|
|
|
|
|
respond_to :html, :js, :json
|
2014-02-05 17:37:21 +00:00
|
|
|
|
2014-01-29 03:46:58 +00:00
|
|
|
# GET /topics/autocomplete_topic
|
|
|
|
def autocomplete_topic
|
|
|
|
@current = current_user
|
|
|
|
term = params[:term]
|
|
|
|
if term && !term.empty?
|
2014-02-14 03:32:51 +00:00
|
|
|
# !connor term here needs to have .downcase
|
2014-02-15 17:52:56 +00:00
|
|
|
@topics = Topic.where('LOWER("name") like ?', term.downcase + '%').order('"name"')
|
2014-02-05 01:28:06 +00:00
|
|
|
|
|
|
|
#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)) }
|
2014-01-29 03:46:58 +00:00
|
|
|
else
|
2014-02-05 01:28:06 +00:00
|
|
|
@topics = []
|
2014-01-29 03:46:58 +00:00
|
|
|
end
|
2014-02-05 01:28:06 +00:00
|
|
|
render json: autocomplete_array_json(@topics)
|
2014-01-29 03:46:58 +00:00
|
|
|
end
|
2013-01-05 21:21:11 +00:00
|
|
|
|
|
|
|
# GET topics/:id
|
|
|
|
def show
|
|
|
|
@current = current_user
|
|
|
|
@topic = Topic.find(params[:id]).authorize_to_show(@current)
|
|
|
|
|
|
|
|
if @topic
|
|
|
|
@relatives = @topic.network_as_json(@current).html_safe
|
|
|
|
else
|
|
|
|
redirect_to root_url and return
|
|
|
|
end
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.html { respond_with(@topic, @user) }
|
2014-07-27 19:57:35 +00:00
|
|
|
#format.json { respond_with(@relatives) }
|
|
|
|
format.json { render :json => @topic }
|
2013-01-05 21:21:11 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-07-27 19:57:35 +00:00
|
|
|
# POST /topics
|
|
|
|
# POST /topics.json
|
2013-01-05 21:21:11 +00:00
|
|
|
def create
|
2014-07-27 19:57:35 +00:00
|
|
|
@topic = Topic.new(params[:topic])
|
2013-01-05 21:21:11 +00:00
|
|
|
|
|
|
|
respond_to do |format|
|
2014-07-27 19:57:35 +00:00
|
|
|
if @topic.save
|
|
|
|
format.json { render json: @topic, status: :created }
|
|
|
|
else
|
|
|
|
format.json { render json: @topic.errors, status: :unprocessable_entity }
|
|
|
|
end
|
2013-01-05 21:21:11 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-07-27 19:57:35 +00:00
|
|
|
# PUT /topics/1
|
|
|
|
# PUT /topics/1.json
|
2013-01-05 21:21:11 +00:00
|
|
|
def update
|
2014-07-27 19:57:35 +00:00
|
|
|
@topic = Topic.find(params[:id])
|
|
|
|
|
2014-02-09 23:54:45 +00:00
|
|
|
respond_to do |format|
|
2014-07-27 19:57:35 +00:00
|
|
|
if @topic.update_attributes(params[:topic])
|
|
|
|
format.json { head :no_content }
|
2013-01-05 21:21:11 +00:00
|
|
|
else
|
2014-07-27 19:57:35 +00:00
|
|
|
format.json { render json: @topic.errors, status: :unprocessable_entity }
|
2013-01-05 21:21:11 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# 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|
|
2013-04-26 04:07:29 +00:00
|
|
|
synapse.mappings.each do |m|
|
2014-02-26 20:23:29 +00:00
|
|
|
|
|
|
|
@map = m.map
|
|
|
|
@map.touch(:updated_at)
|
|
|
|
|
2013-04-26 04:07:29 +00:00
|
|
|
#push notify to anyone viewing same map in realtime (see mapping.rb to understand the 'message' action)
|
2014-02-04 17:08:09 +00:00
|
|
|
m.message 'destroy',@current.id
|
2013-04-26 04:07:29 +00:00
|
|
|
|
|
|
|
m.delete
|
|
|
|
end
|
|
|
|
|
2013-01-05 21:21:11 +00:00
|
|
|
synapse.delete
|
|
|
|
end
|
|
|
|
|
|
|
|
@mappings.each do |mapping|
|
2014-02-26 20:23:29 +00:00
|
|
|
|
|
|
|
@map = mapping.map
|
|
|
|
@map.touch(:updated_at)
|
|
|
|
|
2013-04-26 04:07:29 +00:00
|
|
|
#push notify to anyone viewing a map with this topic in realtime (see mapping.rb to understand the 'message' action)
|
2014-02-04 17:08:09 +00:00
|
|
|
mapping.message 'destroy',@current.id
|
2013-04-26 04:07:29 +00:00
|
|
|
|
2013-01-05 21:21:11 +00:00
|
|
|
mapping.delete
|
|
|
|
end
|
|
|
|
|
|
|
|
@topic.delete
|
|
|
|
end
|
|
|
|
|
2014-07-27 19:57:35 +00:00
|
|
|
respond_to do |format|
|
|
|
|
format.js { render :json => "success" }
|
2013-01-05 21:21:11 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|