2016-02-03 13:38:41 +00:00
|
|
|
class TopicsController < ApplicationController
|
|
|
|
include TopicsHelper
|
|
|
|
|
2016-02-28 09:48:18 +00:00
|
|
|
before_action :require_user, only: [:create, :update, :destroy]
|
2016-03-11 23:13:22 +00:00
|
|
|
after_action :verify_authorized, except: :autocomplete_topic
|
2016-03-11 14:10:31 +00:00
|
|
|
|
2016-02-03 13:38:41 +00:00
|
|
|
respond_to :html, :js, :json
|
|
|
|
|
|
|
|
# GET /topics/autocomplete_topic
|
|
|
|
def autocomplete_topic
|
|
|
|
term = params[:term]
|
|
|
|
if term && !term.empty?
|
2016-03-11 14:10:31 +00:00
|
|
|
@topics = policy_scope(Topic.where('LOWER("name") like ?', term.downcase + '%')).order('"name"')
|
2016-02-03 13:38:41 +00:00
|
|
|
else
|
|
|
|
@topics = []
|
|
|
|
end
|
|
|
|
render json: autocomplete_array_json(@topics)
|
|
|
|
end
|
|
|
|
|
|
|
|
# GET topics/:id
|
|
|
|
def show
|
2016-03-11 14:10:31 +00:00
|
|
|
@topic = Topic.find(params[:id])
|
2016-03-12 00:10:30 +00:00
|
|
|
authorize @topic
|
2016-02-03 13:38:41 +00:00
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.html {
|
2016-03-12 00:10:30 +00:00
|
|
|
@alltopics = ([@topic] + policy_scope(Topic.relatives(@topic.id)))
|
|
|
|
@allsynapses = policy_scope(Synapse.for_topic(@topic.id))
|
2016-03-11 14:10:31 +00:00
|
|
|
|
|
|
|
@allcreators = @alltopics.map(&:user).uniq
|
|
|
|
@allcreators += @allsynapses.map(&:user).uniq
|
2016-02-03 13:38:41 +00:00
|
|
|
|
|
|
|
respond_with(@allsynapses, @alltopics, @allcreators, @topic)
|
|
|
|
}
|
|
|
|
format.json { render json: @topic }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# GET topics/:id/network
|
|
|
|
def network
|
2016-03-11 14:10:31 +00:00
|
|
|
@topic = Topic.find(params[:id])
|
2016-03-12 00:10:30 +00:00
|
|
|
authorize @topic
|
2016-02-03 13:38:41 +00:00
|
|
|
|
2016-03-11 14:10:31 +00:00
|
|
|
@alltopics = [@topic] + policy_scope(@topic.relatives)
|
|
|
|
@allsynapses = policy_scope(@topic.synapses)
|
|
|
|
|
|
|
|
@allcreators = @alltopics.map(&:user).uniq
|
|
|
|
@allcreators += @allsynapses.map(&:user).uniq
|
2016-02-03 13:38:41 +00:00
|
|
|
|
|
|
|
@json = Hash.new()
|
|
|
|
@json['topic'] = @topic
|
|
|
|
@json['creators'] = @allcreators
|
|
|
|
@json['relatives'] = @alltopics
|
|
|
|
@json['synapses'] = @allsynapses
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.json { render json: @json }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# GET topics/:id/relative_numbers
|
|
|
|
def relative_numbers
|
2016-03-11 14:10:31 +00:00
|
|
|
@topic = Topic.find(params[:id])
|
|
|
|
authorize @topic
|
2016-02-03 13:38:41 +00:00
|
|
|
|
2016-03-11 14:10:31 +00:00
|
|
|
topicsAlreadyHas = params[:network] ? params[:network].split(',').map(&:to_i) : []
|
2016-02-03 13:38:41 +00:00
|
|
|
|
2016-03-11 14:10:31 +00:00
|
|
|
@alltopics = policy_scope(@topic.relatives).to_a.uniq
|
|
|
|
@alltopics.delete_if! do |topic|
|
|
|
|
topicsAlreadyHas.index(topic.id) != nil
|
|
|
|
end
|
2016-02-03 13:38:41 +00:00
|
|
|
|
2016-03-11 14:10:31 +00:00
|
|
|
@json = Hash.new(0)
|
2016-02-03 13:38:41 +00:00
|
|
|
@alltopics.each do |t|
|
2016-03-11 14:10:31 +00:00
|
|
|
@json[t.metacode.id] += 1
|
2016-02-03 13:38:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
respond_to do |format|
|
2016-03-11 14:10:31 +00:00
|
|
|
format.json { render json: @json }
|
2016-02-03 13:38:41 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# GET topics/:id/relatives
|
|
|
|
def relatives
|
2016-03-11 14:10:31 +00:00
|
|
|
@topic = Topic.find(params[:id])
|
2016-03-12 00:10:30 +00:00
|
|
|
authorize @topic
|
2016-03-11 14:10:31 +00:00
|
|
|
|
|
|
|
topicsAlreadyHas = params[:network] ? params[:network].split(',').map(&:to_i) : []
|
|
|
|
|
|
|
|
alltopics = policy_scope(@topic.relatives).to_a.uniq.delete_if do |topic|
|
|
|
|
topicsAlreadyHas.index(topic.id.to_s) != nil
|
|
|
|
end
|
|
|
|
|
|
|
|
#find synapses between topics in alltopics array
|
|
|
|
allsynapses = policy_scope(@topic.synapses)
|
|
|
|
synapse_ids = (allsynapses.map(&:topic1_id) + allsynapses.map(&:topic2_id)).uniq
|
|
|
|
allsynapses.delete_if! do |synapse|
|
|
|
|
synapse_ids.index(synapse.id) != nil
|
|
|
|
end
|
|
|
|
|
|
|
|
creatorsAlreadyHas = params[:creators] ? params[:creators].split(',').map(&:to_i) : []
|
|
|
|
allcreators = (alltopics.map(&:user) + allsynapses.map(&:user)).uniq.delete_if do |user|
|
|
|
|
creatorsAlreadyHas.index(user.id) != nil
|
|
|
|
end
|
|
|
|
|
|
|
|
@json = Hash.new()
|
|
|
|
@json['topics'] = alltopics
|
|
|
|
@json['synapses'] = allsynapses
|
|
|
|
@json['creators'] = allcreators
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.json { render json: @json }
|
|
|
|
end
|
2016-02-03 13:38:41 +00:00
|
|
|
end
|
|
|
|
|
2016-03-11 14:10:31 +00:00
|
|
|
# POST /topics
|
|
|
|
# POST /topics.json
|
|
|
|
def create
|
|
|
|
@topic = Topic.new(topic_params)
|
2016-03-12 00:10:30 +00:00
|
|
|
authorize @topic
|
2016-03-11 14:10:31 +00:00
|
|
|
|
|
|
|
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
|
2016-02-03 13:38:41 +00:00
|
|
|
end
|
2016-03-11 14:10:31 +00:00
|
|
|
end
|
2016-02-03 13:38:41 +00:00
|
|
|
|
2016-03-11 14:10:31 +00:00
|
|
|
# PUT /topics/1
|
|
|
|
# PUT /topics/1.json
|
|
|
|
def update
|
|
|
|
@topic = Topic.find(params[:id])
|
2016-03-12 00:10:30 +00:00
|
|
|
authorize @topic
|
2016-03-11 14:10:31 +00:00
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
if @topic.update_attributes(topic_params)
|
|
|
|
format.json { head :no_content }
|
|
|
|
else
|
|
|
|
format.json { render json: @topic.errors, status: :unprocessable_entity }
|
|
|
|
end
|
2016-02-03 13:38:41 +00:00
|
|
|
end
|
2016-03-11 14:10:31 +00:00
|
|
|
end
|
2016-02-03 13:38:41 +00:00
|
|
|
|
2016-03-11 14:10:31 +00:00
|
|
|
# DELETE topics/:id
|
|
|
|
def destroy
|
|
|
|
@topic = Topic.find(params[:id])
|
2016-03-12 00:10:30 +00:00
|
|
|
authorize @topic
|
2016-02-03 13:38:41 +00:00
|
|
|
|
2016-03-11 14:10:31 +00:00
|
|
|
@topic.delete
|
|
|
|
respond_to do |format|
|
|
|
|
format.json { head :no_content }
|
2016-02-03 13:38:41 +00:00
|
|
|
end
|
2016-03-11 14:10:31 +00:00
|
|
|
end
|
2016-02-03 13:38:41 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def topic_params
|
|
|
|
params.require(:topic).permit(:id, :name, :desc, :link, :permission, :user_id, :metacode_id)
|
|
|
|
end
|
|
|
|
end
|