<commit_before>class SpeciesVariationObservationsController < ApplicationController
  
  before_action :login_required
  before_action :admin_required, only: :destroy
  before_action :set_species_variation_observation, only: [:show, :edit, :update, :destroy]

  def index
    @species_variation_observations = SpeciesVariationObservation.order(observed_on: :desc).page(params[:page]).includes(:plot)
  end

  def show
  end

  def new
    @species_variation_observation = SpeciesVariationObservation.new
    @plots = Plot.order(:plot_id)
    @plants = Plant.order(Arel.sql('LOWER(common_name)'))
  end

  def edit
    @plots = Plot.order(:plot_id)
    @plants = Plant.order(Arel.sql('LOWER(common_name)'))
  end

  def create
    @species_variation_observation = SpeciesVariationObservation.new(species_variation_observation_params)
    @species_variation_observation.user = current_user
    respond_to do |format|
      if @species_variation_observation.save
        format.html { redirect_to @species_variation_observation, flash: {success: 'Species variation observation was successfully created.' } }
        format.json { render :show, status: :created, location: @species_variation_observation }
      else
        @plots = Plot.order(:plot_id)
        @plants = Plant.order(Arel.sql('LOWER(common_name)'))
        format.html { render :new }
        format.json { render json: @species_variation_observation.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
    respond_to do |format|
      if @species_variation_observation.update(species_variation_observation_params)
        format.html { redirect_to @species_variation_observation, flash: {success: 'Species variation observation was successfully updated.' } }
        format.json { render :show, status: :ok, location: @species_variation_observation }
      else
        @plots = Plot.order(:plot_id)
        @plants = Plant.order(Arel.sql('LOWER(common_name)'))
        format.html { render :edit }
        format.json { render json: @species_variation_observation.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    @species_variation_observation.destroy
    respond_to do |format|
      format.html { redirect_to species_variation_observations_url, flash: { sucess: 'Species variation observation was successfully deleted.' } }
      format.json { head :no_content }
    end
  end

  private

    def set_species_variation_observation
      @species_variation_observation = SpeciesVariationObservation.find(params[:id])
    end

    def species_variation_observation_params
      params.require(:species_variation_observation).permit(:observed_on, :plot_id, :plant_id, :average_height, :average_width, :description, photo: [])
    end

end
<commit_msg>add plant to include in species variation observation controller to reduce db queries from view<commit_after>class SpeciesVariationObservationsController < ApplicationController
  
  before_action :login_required
  before_action :admin_required, only: :destroy
  before_action :set_species_variation_observation, only: [:show, :edit, :update, :destroy]

  def index
    @species_variation_observations = SpeciesVariationObservation.order(observed_on: :desc).page(params[:page]).includes(:plot, :plant)
  end

  def show
  end

  def new
    @species_variation_observation = SpeciesVariationObservation.new
    @plots = Plot.order(:plot_id)
    @plants = Plant.order(Arel.sql('LOWER(common_name)'))
  end

  def edit
    @plots = Plot.order(:plot_id)
    @plants = Plant.order(Arel.sql('LOWER(common_name)'))
  end

  def create
    @species_variation_observation = SpeciesVariationObservation.new(species_variation_observation_params)
    @species_variation_observation.user = current_user
    respond_to do |format|
      if @species_variation_observation.save
        format.html { redirect_to @species_variation_observation, flash: {success: 'Species variation observation was successfully created.' } }
        format.json { render :show, status: :created, location: @species_variation_observation }
      else
        @plots = Plot.order(:plot_id)
        @plants = Plant.order(Arel.sql('LOWER(common_name)'))
        format.html { render :new }
        format.json { render json: @species_variation_observation.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
    respond_to do |format|
      if @species_variation_observation.update(species_variation_observation_params)
        format.html { redirect_to @species_variation_observation, flash: {success: 'Species variation observation was successfully updated.' } }
        format.json { render :show, status: :ok, location: @species_variation_observation }
      else
        @plots = Plot.order(:plot_id)
        @plants = Plant.order(Arel.sql('LOWER(common_name)'))
        format.html { render :edit }
        format.json { render json: @species_variation_observation.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    @species_variation_observation.destroy
    respond_to do |format|
      format.html { redirect_to species_variation_observations_url, flash: { sucess: 'Species variation observation was successfully deleted.' } }
      format.json { head :no_content }
    end
  end

  private

    def set_species_variation_observation
      @species_variation_observation = SpeciesVariationObservation.find(params[:id])
    end

    def species_variation_observation_params
      params.require(:species_variation_observation).permit(:observed_on, :plot_id, :plant_id, :average_height, :average_width, :description, photo: [])
    end

end

{'fraction_non_alphanumeric': 0.08897554058844381, 'fraction_numerical': 0.0, 'mean_word_length': 4.263992537313433, 'pattern_counts': {'":': 0, '<': 5, '<?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': '979066', 'n_tokens_mistral': 1736, 'n_tokens_neox': 1643, 'n_words': 351}