Added RelatedContentsController
This commit is contained in:
43
app/controllers/related_contents_controller.rb
Normal file
43
app/controllers/related_contents_controller.rb
Normal file
@@ -0,0 +1,43 @@
|
||||
class RelatedContentsController < ApplicationController
|
||||
RELATIONABLE_MODELS = %w{proposals debates}
|
||||
VALID_URL = /#{Setting['url']}\/.*\/.*/
|
||||
|
||||
skip_authorization_check
|
||||
|
||||
def create
|
||||
if relationable_object && related_object
|
||||
@relationable.relate_content(@related)
|
||||
|
||||
flash[:success] = t('related_content.success')
|
||||
else
|
||||
flash[:error] = t('related_content.error', url: Setting['url'])
|
||||
end
|
||||
|
||||
redirect_to @relationable
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def valid_url?
|
||||
params[:url].match(VALID_URL)
|
||||
end
|
||||
|
||||
def relationable_object
|
||||
@relationable = (params[:relationable_klass].singularize.camelize.constantize).find_by_id(params[:relationable_id])
|
||||
end
|
||||
|
||||
def related_object
|
||||
begin
|
||||
if valid_url?
|
||||
url = params[:url]
|
||||
|
||||
related_klass = url.match(/\/(#{RELATIONABLE_MODELS.join("|")})\//)[0].gsub("/", "")
|
||||
related_id = url.match(/\/[0-9]+/)[0].gsub("/", "")
|
||||
|
||||
@related = (related_klass.singularize.camelize.constantize).find_by_id(related_id)
|
||||
end
|
||||
rescue
|
||||
nil
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -462,6 +462,8 @@ Rails.application.routes.draw do
|
||||
root to: "dashboard#index"
|
||||
end
|
||||
|
||||
resources :related_contents, only: [:create]
|
||||
|
||||
# GraphQL
|
||||
get '/graphql', to: 'graphql#query'
|
||||
post '/graphql', to: 'graphql#query'
|
||||
|
||||
Reference in New Issue
Block a user