<commit_before>#
# Controller for Links
#
class LinksController < InheritedResources::Base

  respond_to :json, :xml
  has_pagination
  has_scope :search
  belongs_to :ontology, :optional => true
  load_and_authorize_resource :except => [:index, :show]
  before_filter :check_read_permissions

  def index
    super do |format|
      format.html do
        @search = params[:search]
        @search = nil if @search.blank?
      end
    end
  end

  def new
    @version = build_resource.versions.build
  end

  def create
    @version = build_resource.versions.first
    @version.source = Ontology.find(params[:link][:source_id]).current_version
    @version.target = Ontology.find(params[:link][:target_id]).current_version
    super
  end

  def update_version
    @version = resource.current_version.dup
    @version.version_number = @version.version_number + 1
    @version.save
    @version.ontology.update_version!(to: @version)
    redirect_to edit_link_link_version_path(resource, @version)
  end


  private

  def collection
    restrict_by_permission = proc do |link|
      can?(:show, link.source.repository) && can?(:show, link.target.repository)
    end
    if params[:ontology_id]
      ontology_id = params[:ontology_id]
      @links = Link.with_ontology_reference(ontology_id).
        joins(:source => :logic).order('logics.name DESC')
      collection = @links
    else
      collection = super
    end
    paginate_for(collection.select(&restrict_by_permission))
  end

  def build_resource
    @link ||= Link.new params[:link]
  end

  def check_read_permissions
    unless params[:action] == 'index'
      if resource.source
        authorize! :show, resource.source.repository
      end
      if resource.target
        authorize! :show, resource.target.repository
      end
    end
  end
end<commit_msg>optimize links controller further<commit_after>#
# Controller for Links
#
class LinksController < InheritedResources::Base

  respond_to :json, :xml
  has_pagination
  has_scope :search
  belongs_to :ontology, :optional => true
  load_and_authorize_resource :except => [:index, :show]
  before_filter :check_read_permissions

  def index
    super do |format|
      format.html do
        @search = params[:search]
        @search = nil if @search.blank?
      end
    end
  end

  def new
    @version = build_resource.versions.build
  end

  def create
    @version = build_resource.versions.first
    @version.source = Ontology.find(params[:link][:source_id]).current_version
    @version.target = Ontology.find(params[:link][:target_id]).current_version
    super
  end

  def update_version
    @version = resource.current_version.dup
    @version.version_number = @version.version_number + 1
    @version.save
    @version.ontology.update_version!(to: @version)
    redirect_to edit_link_link_version_path(resource, @version)
  end


  private

  def collection
    restrict_by_permission = proc do |link|
      can?(:show, link.source.repository) && can?(:show, link.target.repository)
    end
    if params[:ontology_id]
      collection = Link.with_ontology_reference(params[:ontology_id])
    else
      collection = super
    end
    @links = collection = collection.
        joins(:source => :logic).order('logics.name DESC')
    paginate_for(collection.select(&restrict_by_permission))
  end

  def build_resource
    @link ||= Link.new params[:link]
  end

  def check_read_permissions
    unless params[:action] == 'index'
      if resource.source
        authorize! :show, resource.source.repository
      end
      if resource.target
        authorize! :show, resource.target.repository
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.0970210440010932, 'fraction_numerical': 0.0005465974309920743, 'mean_word_length': 3.362336114421931, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6419962', 'n_tokens_mistral': 1131, 'n_tokens_neox': 1106, 'n_words': 247}