<commit_before>class StudiesController < ApplicationController

  include DotGenerator
  include IndexPager

  before_filter :find_assets, :only=>[:index]
  before_filter :find_and_auth, :only=>[:edit, :update, :destroy, :show]

  before_filter :check_assays_are_not_already_associated_with_another_study,:only=>[:create,:update]

  include Seek::Publishing::GatekeeperPublish
  include Seek::Publishing::PublishingCommon

  include Seek::BreadCrumbs

  def new_object_based_on_existing_one
    @existing_study =  Study.find(params[:id])
    @study = @existing_study.clone_with_associations

    unless @study.investigation.can_edit?
       @study.investigation = nil
      flash.now[:notice] = 'The investigation of the existing study cannot be viewed, please specify your own investigation!'
    end

    render :action => "new"

  end

  def new
    @study = Study.new
    @study.create_from_asset = params[:create_from_asset]
    @study.new_link_from_assay = params[:new_link_from_assay]
    investigation = nil
    investigation = Investigation.find(params[:investigation_id]) if params[:investigation_id]
    
    if investigation
      if investigation.can_edit?
        @study.investigation = investigation
      else
        flash.now[:error] = "You do not have permission to associate the new study with the investigation '#{investigation.title}'."
      end
    end
    investigations = Investigation.all.select &:can_view?
    respond_to do |format|
      if investigations.blank?
       flash.now[:notice] = "No investigation available, you have to create a new one before creating your study!"
      end
      format.html
    end
  end

  def edit
    @study=Study.find(params[:id])
    respond_to do |format|
      format.html
      format.xml
    end
  end

  # DELETE /study/1
  # DELETE /study/1.xml
  def destroy
    
    @study.destroy

    respond_to do |format|
      format.html { redirect_to(studies_url) }
      format.xml  { head :ok }
    end
  end

  
  def update
    @study=Study.find(params[:id])

    @study.attributes = params[:study]

    if params[:sharing]
      @study.policy_or_default
      @study.policy.set_attributes_with_sharing params[:sharing], @study.projects

    end

    respond_to do |format|
      if @study.save
        flash[:notice] = 'Study was successfully updated.'
        format.html { redirect_to(@study) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @study.errors, :status => :unprocessable_entity }
      end
    end
  end

  def show
    @study=Study.find(params[:id])
    @study.create_from_asset = params[:create_from_asset]
    respond_to do |format|
      format.html
      format.xml
      format.rdf { render :template=>'rdf/show'}
      format.svg { render :text=>to_svg(@study.investigation,params[:deep]=='true',@study)}
      format.dot { render :text=>to_dot(@study.investigation,params[:deep]=='true',@study)}
      format.png { render :text=>to_png(@study.investigation,params[:deep]=='true',@study)}
    end

  end

  def create
    @study = Study.new(params[:study])

    @study.policy.set_attributes_with_sharing params[:sharing], @study.projects


  if @study.save
    if @study.new_link_from_assay=="true"
      render :partial => "assets/back_to_singleselect_parent",:locals => {:child=>@study,:parent=>"assay"}
    else
      respond_to do |format|
        flash[:notice] = 'The study was successfully created.<br/>'
        if @study.create_from_asset=="true"
          flash.now[:notice] << "Now you can create new assay by clicking 'add an assay' button"
          format.html { redirect_to study_path(:id=>@study,:create_from_asset=>@study.create_from_asset) }<commit_msg>html_safe for a notice<commit_after>class StudiesController < ApplicationController

  include DotGenerator
  include IndexPager

  before_filter :find_assets, :only=>[:index]
  before_filter :find_and_auth, :only=>[:edit, :update, :destroy, :show]

  before_filter :check_assays_are_not_already_associated_with_another_study,:only=>[:create,:update]

  include Seek::Publishing::GatekeeperPublish
  include Seek::Publishing::PublishingCommon

  include Seek::BreadCrumbs

  def new_object_based_on_existing_one
    @existing_study =  Study.find(params[:id])
    @study = @existing_study.clone_with_associations

    unless @study.investigation.can_edit?
       @study.investigation = nil
      flash.now[:notice] = 'The investigation of the existing study cannot be viewed, please specify your own investigation!'
    end

    render :action => "new"

  end

  def new
    @study = Study.new
    @study.create_from_asset = params[:create_from_asset]
    @study.new_link_from_assay = params[:new_link_from_assay]
    investigation = nil
    investigation = Investigation.find(params[:investigation_id]) if params[:investigation_id]
    
    if investigation
      if investigation.can_edit?
        @study.investigation = investigation
      else
        flash.now[:error] = "You do not have permission to associate the new study with the investigation '#{investigation.title}'."
      end
    end
    investigations = Investigation.all.select &:can_view?
    respond_to do |format|
      if investigations.blank?
       flash.now[:notice] = "No investigation available, you have to create a new one before creating your study!"
      end
      format.html
    end
  end

  def edit
    @study=Study.find(params[:id])
    respond_to do |format|
      format.html
      format.xml
    end
  end

  # DELETE /study/1
  # DELETE /study/1.xml
  def destroy
    
    @study.destroy

    respond_to do |format|
      format.html { redirect_to(studies_url) }
      format.xml  { head :ok }
    end
  end

  
  def update
    @study=Study.find(params[:id])

    @study.attributes = params[:study]

    if params[:sharing]
      @study.policy_or_default
      @study.policy.set_attributes_with_sharing params[:sharing], @study.projects

    end

    respond_to do |format|
      if @study.save
        flash[:notice] = 'Study was successfully updated.'
        format.html { redirect_to(@study) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @study.errors, :status => :unprocessable_entity }
      end
    end
  end

  def show
    @study=Study.find(params[:id])
    @study.create_from_asset = params[:create_from_asset]
    respond_to do |format|
      format.html
      format.xml
      format.rdf { render :template=>'rdf/show'}
      format.svg { render :text=>to_svg(@study.investigation,params[:deep]=='true',@study)}
      format.dot { render :text=>to_dot(@study.investigation,params[:deep]=='true',@study)}
      format.png { render :text=>to_png(@study.investigation,params[:deep]=='true',@study)}
    end

  end

  def create
    @study = Study.new(params[:study])

    @study.policy.set_attributes_with_sharing params[:sharing], @study.projects


  if @study.save
    if @study.new_link_from_assay=="true"
      render :partial => "assets/back_to_singleselect_parent",:locals => {:child=>@study,:parent=>"assay"}
    else
      respond_to do |format|
        flash[:notice] = 'The study was successfully created.<br/>'.html_safe
        if @study.create_from_asset=="true"
          flash.now[:notice] << "Now you can create new assay by clicking 'add an assay' button"
          format.html { redirect_to study_path(:id=>@study,:create_from_asset=>@study.create_from_asset) }
{'fraction_non_alphanumeric': 0.11279797576241843, 'fraction_numerical': 0.0005326941004128379, 'mean_word_length': 3.420247204237787, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 14}, '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': '4135961', 'n_tokens_mistral': 2465, 'n_tokens_neox': 2264, 'n_words': 513}