<commit_before>class GroupsController < GroupBaseController
  before_filter :authenticate_user!, except: :show

  before_filter :load_resource_by_key, :except => [:create, :new]
  authorize_resource except: :create

  before_filter :ensure_group_is_setup, only: :show

  caches_action :show, :cache_path => Proc.new { |c| c.params }, unless: :user_signed_in?, :expires_in => 5.minutes

  rescue_from ActiveRecord::RecordNotFound do
    render 'application/display_error', locals: { message: t('error.group_private_or_not_found') }
  end

  #for new subgroup form
  def add_subgroup
    parent = Group.published.find(params[:id])
    @subgroup = Group.new(:parent => parent, privacy: parent.privacy)
    @subgroup.members_invitable_by = parent.members_invitable_by
  end

  def create
    @group = Group.new(permitted_params.group)
    authorize!(:create, @group)
    @group.mark_as_setup!
    if @group.save
      Measurement.increment('groups.create.success')
      @group.add_admin! current_user
      flash[:success] = t("success.group_created")
      redirect_to @group
    elsif @group.is_a_subgroup?
        @subgroup = @group
        render 'groups/add_subgroup'
    else
      Measurement.increment('groups.create.error')
      render 'form'
    end
  end

  def new
    @group = Group.new
    @group.payment_plan = 'undetermined'
  end

  def update
    if @group.update_attributes(permitted_params.group)
      if @group.is_hidden?
        @group.discussions.update_all(private: true)
      end
      Measurement.increment('groups.update.success')
      flash[:notice] = 'Group was successfully updated.'
      redirect_to @group
    else
      Measurement.increment('groups.update.error')
      render :edit
    end
  end

  def show
    @group = GroupDecorator.new @group
    @subgroups = @group.subgroups.all.select{|g| can?(:show, g) }
    @discussion = Discussion.new(group_id: @group.id)
    @discussions_with_open_motions = GroupDiscussionsViewer.for(group: @group, user: current_user).
                                                            with_open_motions.
                                                            order('motions.closing_at ASC').
                                                            preload({:current_motion => :discussion}, {:group => :parent})

    @discussions_without_open_motions = GroupDiscussionsViewer.for(group: @group, user: current_user).
                                                            without_open_motions.
                                                            order('last_comment_at DESC').
                                                            preload(:current_motion, {:group => :parent}).
                                                            page(params[:page]).per(20)

    @closed_motions = Queries::VisibleMotions.new(user: current_user, groups: @group)

    build_discussion_index_caches<commit_msg>fix decision display order in group sidebar<commit_after>class GroupsController < GroupBaseController
  before_filter :authenticate_user!, except: :show

  before_filter :load_resource_by_key, :except => [:create, :new]
  authorize_resource except: :create

  before_filter :ensure_group_is_setup, only: :show

  caches_action :show, :cache_path => Proc.new { |c| c.params }, unless: :user_signed_in?, :expires_in => 5.minutes

  rescue_from ActiveRecord::RecordNotFound do
    render 'application/display_error', locals: { message: t('error.group_private_or_not_found') }
  end

  #for new subgroup form
  def add_subgroup
    parent = Group.published.find(params[:id])
    @subgroup = Group.new(:parent => parent, privacy: parent.privacy)
    @subgroup.members_invitable_by = parent.members_invitable_by
  end

  def create
    @group = Group.new(permitted_params.group)
    authorize!(:create, @group)
    @group.mark_as_setup!
    if @group.save
      Measurement.increment('groups.create.success')
      @group.add_admin! current_user
      flash[:success] = t("success.group_created")
      redirect_to @group
    elsif @group.is_a_subgroup?
        @subgroup = @group
        render 'groups/add_subgroup'
    else
      Measurement.increment('groups.create.error')
      render 'form'
    end
  end

  def new
    @group = Group.new
    @group.payment_plan = 'undetermined'
  end

  def update
    if @group.update_attributes(permitted_params.group)
      if @group.is_hidden?
        @group.discussions.update_all(private: true)
      end
      Measurement.increment('groups.update.success')
      flash[:notice] = 'Group was successfully updated.'
      redirect_to @group
    else
      Measurement.increment('groups.update.error')
      render :edit
    end
  end

  def show
    @group = GroupDecorator.new @group
    @subgroups = @group.subgroups.all.select{|g| can?(:show, g) }
    @discussion = Discussion.new(group_id: @group.id)
    @discussions_with_open_motions = GroupDiscussionsViewer.for(group: @group, user: current_user).
                                                            with_open_motions.
                                                            order('motions.closing_at ASC').
                                                            preload({:current_motion => :discussion}, {:group => :parent})

    @discussions_without_open_motions = GroupDiscussionsViewer.for(group: @group, user: current_user).
                                                            without_open_motions.
                                                            order('last_comment_at DESC').
                                                            preload(:current_motion, {:group => :parent}).
                                                            page(params[:page]).per(20)

    @closed_motions = Queries::VisibleMotions.new(user: current_user, groups: @group).order('closed_at desc')

    build_discussion_index_caches
{'fraction_non_alphanumeric': 0.09945411122483794, 'fraction_numerical': 0.0010235414534288639, 'mean_word_length': 2.2481994459833796, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 28, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '506915', 'n_tokens_mistral': 1671, 'n_tokens_neox': 1589, 'n_words': 314}