<commit_before>module Actions
  class ProposeEvidenceItem
    include Actions::Transactional
    attr_reader :evidence_item, :originating_user, :direct_attributes, :relational_attributes

    def initialize(direct_attributes, relational_attributes, originating_user)
      @direct_attributes = direct_attributes
      @relational_attributes = relational_attributes
      @originating_user = originating_user
    end

    private
    def execute
      direct_attributes[:status] = 'submitted'
      evidence_item = EvidenceItem.new(direct_attributes).tap do |item|
        item.variant = get_variant(relational_attributes)
        item.disease = get_disease(relational_attributes)
        item.source = get_source(relational_attributes)
        item.drugs = get_drugs(relational_attributes)
        item.save
      end
      Event.create(
        action: 'submitted',
        originating_user: originating_user,
        subject: evidence_item
      )
      evidence_item.subscribe_user(originating_user)
      @evidence_item = evidence_item
    end

    def get_variant(params)
      if variant = Variant.joins(:gene).where(genes: { id: params[:gene][:id] }, name: params[:variant][:name].upcase).first
        variant
      else
        Variant.create(gene: Gene.find_by(id: params[:gene][:id]), name: params[:variant][:name].upcase, description: '')
      end
    end

    def get_disease(params)
      if params[:noDoid]
        Disease.create(name: params[:disease_name])
      else
        Disease.find_by(id: params[:disease][:id])
      end
    end

    def get_source(params)
      pubmed_id = params[:pubmed_id]
      if found_source = Source.find_by(pubmed_id: pubmed_id)
        found_source
      elsif (citation = Scrapers::PubMed.get_citation_from_pubmed_id(pubmed_id)).present?
        Source.create(description: citation, pubmed_id: pubmed_id)
      end
    end

    def get_drugs(params)<commit_msg>fully populate new sources added via propose evidence<commit_after>module Actions
  class ProposeEvidenceItem
    include Actions::Transactional
    attr_reader :evidence_item, :originating_user, :direct_attributes, :relational_attributes

    def initialize(direct_attributes, relational_attributes, originating_user)
      @direct_attributes = direct_attributes
      @relational_attributes = relational_attributes
      @originating_user = originating_user
    end

    private
    def execute
      direct_attributes[:status] = 'submitted'
      evidence_item = EvidenceItem.new(direct_attributes).tap do |item|
        item.variant = get_variant(relational_attributes)
        item.disease = get_disease(relational_attributes)
        item.source = get_source(relational_attributes)
        item.drugs = get_drugs(relational_attributes)
        item.save
      end
      Event.create(
        action: 'submitted',
        originating_user: originating_user,
        subject: evidence_item
      )
      evidence_item.subscribe_user(originating_user)
      @evidence_item = evidence_item
    end

    def get_variant(params)
      if variant = Variant.joins(:gene).where(genes: { id: params[:gene][:id] }, name: params[:variant][:name].upcase).first
        variant
      else
        Variant.create(gene: Gene.find_by(id: params[:gene][:id]), name: params[:variant][:name].upcase, description: '')
      end
    end

    def get_disease(params)
      if params[:noDoid]
        Disease.create(name: params[:disease_name])
      else
        Disease.find_by(id: params[:disease][:id])
      end
    end

    def get_source(params)
      pubmed_id = params[:pubmed_id]
      if found_source = Source.find_by(pubmed_id: pubmed_id)
        found_source
      else
        Source.new(pubmed_id: pubmed_id).tap do |source|
          Scrapers::PubMed.populate_source_fields(source)
        end
      end
    end

    def get_drugs(params)
{'fraction_non_alphanumeric': 0.08984679304076863, 'fraction_numerical': 0.0, 'mean_word_length': 3.28476084538376, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '813574', 'n_tokens_mistral': 1182, 'n_tokens_neox': 1117, 'n_words': 210}