<commit_before># encoding: UTF-8
class IdeasController < ApplicationController
  before_filter :authenticate_login!
  before_filter :require_account!
  before_filter :cleanup_session
  before_filter :map_state_names, only:[:create, :update]
  before_filter :map_no_category, only:[:create, :update]

  ValidAngles = %w(discussable vettable votable pickable approvable signoffable buildable followed)
  DefaultAngle = ValidAngles.last

  ValidOrders  = %w(rating activity progress creation size)
  DefaultOrder = {
    'discussable' => 'activity',
    'vettable'    => 'activity',
    'votable'     => 'activity',
    'pickable'    => 'rating',
    'approvable'  => 'activity',
    'buildable'   => 'progress',
    'signoffable' => 'activity',
    'followed'    => 'activity'
  }

  ValidFilters = %w(all authored commented vetted backed)
  DefaultFilter = ValidFilters.first

  ValidViews = %w(cards board)
  DefaultView = ValidViews.first

  def index
    @angle    = set_angle_from_params
    @order    = set_order_from_params_and_angle
    @filter   = set_filter_from_params_and_angle
    @category = set_category_from_params_and_angle
    @view     = set_view_from_params

    @ideas = current_account.ideas.
      includes(:vettings).
      send(:"#{@angle}_by", current_user).
      send(:"by_#{@order}")
    @ideas = @ideas.send(:"#{@filter}_by", current_user) unless @filter == 'all'

    if @category == 'none'
      @ideas = @ideas.where(category: nil)
    elsif @category != 'all'
      @ideas = @ideas.where(category: @category)
    end
  end

  def show
    @idea = find_idea(params[:id])
    self.current_account = @idea.account

    @just_submitted = session.delete(:just_submitted)
  end

  def new
    @idea = current_user.ideas.new
  end

  def create
    @idea = current_user.ideas.new(params[:idea])
    @idea.account = current_account
    if @idea.save
      session[:just_submitted] = true
      redirect_to @idea
    else
      render :action => 'new'
    end
  end

  def edit
    @idea = find_idea(params[:id])
  end

  def update
    @idea = find_idea(params[:id])

    # specifics on state changes
    if state = params[:idea].andand[:state]
      # auto-set product manager when picking
      if state == Idea.state_value(:picked)
        @idea.product_manager = current_user
      end

      if @idea.draft? && state == Idea.state_value(:submitted)
        # this is a draft promoted to submitted
        session[:just_submitted] = true
      end
    end

    if @idea.update_attributes(params[:idea])
      current_user.bookmarked_ideas.add!(@idea)
      redirect_to @idea, notice:_("Successfully updated idea.")
    else
      session.delete(:just_submitted)
      render :action => 'edit'
    end
  end

  def destroy
    @idea = Idea.find(params[:id])
    @idea.destroy
    redirect_to ideas_url, :notice => "Successfully destroyed idea."
  end


  private

  def find_idea(id)
    idea = Idea.where(account_id:current_login.accounts.value_of(:id)).find(id)
    self.current_account = idea.account
    return idea
  end

  def cleanup_session
    session.delete :ideas_angle  unless session[:ideas_angle].kind_of?(String)
    session.delete :ideas_order  unless session[:ideas_order].kind_of?(Hash)
    session.delete :ideas_filter unless session[:ideas_filter].kind_of?(Hash)
  end

  def set_angle_from_params
    params[:angle] =
    session[:ideas_angle] = begin<commit_msg>Dries up controller<commit_after># encoding: UTF-8
class IdeasController < ApplicationController
  before_filter :authenticate_login!
  before_filter :require_account!
  before_filter :cleanup_session
  before_filter :map_state_names, only:[:create, :update]
  before_filter :map_no_category, only:[:create, :update]

  ValidAngles = %w(discussable vettable votable pickable approvable signoffable buildable followed)
  DefaultAngle = ValidAngles.last

  ValidOrders  = %w(rating activity progress creation size)
  DefaultOrder = {
    'discussable' => 'activity',
    'vettable'    => 'activity',
    'votable'     => 'activity',
    'pickable'    => 'rating',
    'approvable'  => 'activity',
    'buildable'   => 'progress',
    'signoffable' => 'activity',
    'followed'    => 'activity'
  }

  ValidFilters = %w(all authored commented vetted backed)
  DefaultFilter = ValidFilters.first

  ValidViews = %w(cards board)
  DefaultView = ValidViews.first

  def index
    @angle    = set_angle_from_params
    @order    = set_order_from_params_and_angle
    @filter   = set_filter_from_params_and_angle
    @category = set_category_from_params_and_angle
    @view     = set_view_from_params

    @ideas = current_account.ideas.
      includes(:vettings).
      send(:"#{@angle}_by", current_user).
      send(:"by_#{@order}")
    @ideas = @ideas.send(:"#{@filter}_by", current_user) unless @filter == 'all'

    if @category == 'none'
      @ideas = @ideas.where(category: nil)
    elsif @category != 'all'
      @ideas = @ideas.where(category: @category)
    end
  end

  def show
    @idea = find_idea(params[:id])
    self.current_account = @idea.account

    @just_submitted = session.delete(:just_submitted)
  end

  def new
    @idea = current_user.ideas.new
  end

  def create
    @idea = current_user.ideas.new(params[:idea])
    @idea.account = current_account
    if @idea.save
      session[:just_submitted] = true
      redirect_to @idea
    else
      render :action => 'new'
    end
  end

  def edit
    @idea = find_idea(params[:id])
  end

  def update
    @idea = find_idea(params[:id])

    # specifics on state changes
    if state = params[:idea].andand[:state]
      # auto-set product manager when picking
      if state == Idea.state_value(:picked)
        @idea.product_manager = current_user
      end

      if @idea.draft? && state == Idea.state_value(:submitted)
        # this is a draft promoted to submitted
        session[:just_submitted] = true
      end
    end

    if @idea.update_attributes(params[:idea])
      current_user.bookmarked_ideas.add!(@idea)
      redirect_to @idea, notice:_("Successfully updated idea.")
    else
      session.delete(:just_submitted)
      render :action => 'edit'
    end
  end

  def destroy
    @idea = Idea.find(params[:id])
    @idea.destroy
    redirect_to ideas_url, :notice => "Successfully destroyed idea."
  end


  private

  def find_idea(id)
    idea = Idea.where(account_id:current_login.accounts.value_of(:id)).find(id)
    self.current_account = idea.account
    return idea
  end

  def cleanup_session
    {
      ideas_angle:  String,
      ideas_order:  Hash,
      ideas_filter: Hash
    }.each_pair do |session_key, type|
      next if session[session_key].kind_of?(type)
      session.delete(session_key)
    end
  end

  def set_angle_from_params
    params[:angle] =
    session[:ideas_angle] = begin
{'fraction_non_alphanumeric': 0.11204111600587371, 'fraction_numerical': 0.0002936857562408223, 'mean_word_length': 3.3326972010178118, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 25, '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': '346906', 'n_tokens_mistral': 2228, 'n_tokens_neox': 2179, 'n_words': 476}