ontologies_controller.rb revision 06cbff1e355ede78f5fa530b57a93e4dbc533840
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga#
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga# Controller for ontologies
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga#
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznagaclass OntologiesController < InheritedResources::Base
c498d9365aa68b789215f4f356a9935f70b0fb3bmb
c498d9365aa68b789215f4f356a9935f70b0fb3bmb include FilesHelper
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga belongs_to :repository, finder: :find_by_path!
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga has_pagination
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga has_scope :search, :state
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga actions :index, :show, :edit, :update, :destroy
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga respond_to :html, except: %i(show)
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga before_filter :check_write_permission, except: [:index, :show, :oops_state]
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga before_filter :check_read_permissions
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga def index
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga if in_repository?
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga @search_response = paginate_for(parent.ontologies)
c498d9365aa68b789215f4f356a9935f70b0fb3bmb @count = end_of_association_chain.total_count
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga @repository_id = parent.id
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga render :index_repository
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga else
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga @search_response = paginate_for(Ontology.scoped)
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga @count = resource_class.count
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga render :index_global
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga end
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga end
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga def new
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga @ontology_version = build_resource.versions.build
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga @c_vertices = Category.first.roots.first.children rescue []
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga end
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga def edit
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga @ontology = resource
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga @c_vertices = Category.first.roots.first.children rescue []
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga end
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga def update
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga resource.category_ids = user_selected_categories
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga params[:ontology].try(:except!, :iri)
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga super
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga end
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga def create
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga @version = build_resource.versions.first
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga @version.user = current_user
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga super
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi resource.category_ids = user_selected_categories
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga end
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga def show
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga @content_object = :ontology
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga if !params[:repository_id]
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga # redirect for legacy routing
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi ontology = Ontology.find params[:id]
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi redirect_to [ontology.repository, ontology]
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi return
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi end
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi respond_to do |format|
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga format.html do
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga if !resource.distributed?
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga default_kind = resource.symbols.groups_by_kind.first.kind
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga redirect_to locid_for(resource, :symbols, kind: default_kind)
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga else
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga redirect_to locid_for(resource, :children)
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga end
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga end
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga end
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga end
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga def destroy
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga if resource.is_imported?
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga flash[:error] = "Can't delete #{Settings.OMS.with_indefinite_article}
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga that is imported by another one."
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga redirect_to resource_chain
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga else
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga resource.destroy_with_parent(current_user)
c498d9365aa68b789215f4f356a9935f70b0fb3bmb destroy!
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga end
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga end
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga def retry_failed
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga scope = end_of_association_chain
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga if id = params[:id]
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga # retry a specific ontology
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga scope = scope.where(id: id)
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga end
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga scope.retry_failed
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga redirect_to (id ? [parent, scope.first!, :ontology_versions] : [parent, :ontologies])
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga end
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga def oops_state
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga respond_to do |format|
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga format.json do
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga respond_with resource.current_version.try(:request)
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga end
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga end
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga end
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga protected
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga def check_read_permissions
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga unless params[:action] == 'index'
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga authorize!(:show, Repository.find_by_path(params[:repository_id]))
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga end
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga end
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga def build_resource
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga @ontology ||= begin
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga type = (params[:ontology] || {}).delete(:type)
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga clazz = type=='DistributedOntology' ? DistributedOntology : SingleOntology
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga @ontology = clazz.new params[:ontology]
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga @ontology.repository = parent
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga @ontology
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga end
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga end
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga def check_write_permission
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga authorize! :write, parent
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga end
c498d9365aa68b789215f4f356a9935f70b0fb3bmb
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga def build_categories_tree(vertex)
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga @a ||= []
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga vertex.children.each { |child| build_categories_tree(child) unless child.children.empty?; @a << child }
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga @a
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga end
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga def user_selected_categories
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga params[:category_ids].keys unless params[:category_ids].nil?
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga end
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga helper_method :repository
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga def repository
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga parent
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga end
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznagaend
d00f0155af9a9a671eb08a0dc30f5ea0a379c36cayznaga