<commit_before>class MembershipsController < ApplicationController

  skip_before_filter :authenticate_user, :only => %w(show update)
  before_filter :authenticate_user_with_code_or_session, :only => %w(show update)

  cache_sweeper :membership_sweeper, :only => %w(create update destroy batch)

  def show
    # allow email links to work (since they will be GET requests)
    if params[:email]
      update
    else
      raise ActionController::UnknownAction, t('No_action_to_show')
    end
  end

  def index
    @group = Group.find(params[:group_id])
    @can_edit = @logged_in.can_edit?(@group)
    if @logged_in.can_see?(@group)
      @requests = @group.membership_requests
    else
      render :text => t('not_authorized'), :layout => true, :status => 401
    end
  end

  # join group
  def create
    @group = Group.find(params[:group_id])
    @person = Person.find(params[:id])
    if @logged_in.can_edit?(@group) or not @group.approval_required_to_join?
      @group.memberships.create(:person => @person)
    elsif me?
      @group.membership_requests.create(:person => @person)
      flash[:warning] = t('groups.request_sent')
    end
    redirect_back
  end

  def update
    @group = Group.find(params[:group_id])
    # email on/off
    if params[:email]
      @person = Person.find(params[:id])
      if @logged_in.can_edit?(@group) or @logged_in.can_edit?(@person)
        @group.set_options_for @person, {:get_email => (params[:email] == 'on')}
        flash[:notice] = t('groups.email_settings_changed')
        redirect_back
      else
        render :text => t('There_was_an_error'), :layout => true, :status => 500
      end
    # promote/demote
    elsif @logged_in.can_edit?(@group)
      @membership = @group.memberships.find_or_create_by_person_id(params[:id])
      @membership.update_attribute :admin, !params[:promote].nil?
      flash[:notice] = t('groups.user_settings_saved')
      redirect_back
    else
      render :text => t('not_authorized'), :layout => true, :status => 401
    end
  end

  # leave group
  def destroy
    @group = Group.find(params[:group_id])
    @membership = @group.memberships.find_by_person_id(params[:id])
    if @logged_in.can_edit?(@group) or @membership.person == @logged_in
      if @group.last_admin?(@membership.person)
        flash[:warning] = t('groups.last_admin_remove', :name => @membership.person.name)
      else
        @membership.destroy
      end
    end
    respond_to do |format|
      format.html { redirect_back }<commit_msg>Fix bug deleting group membership for deleted person.<commit_after>class MembershipsController < ApplicationController

  skip_before_filter :authenticate_user, :only => %w(show update)
  before_filter :authenticate_user_with_code_or_session, :only => %w(show update)

  cache_sweeper :membership_sweeper, :only => %w(create update destroy batch)

  def show
    # allow email links to work (since they will be GET requests)
    if params[:email]
      update
    else
      raise ActionController::UnknownAction, t('No_action_to_show')
    end
  end

  def index
    @group = Group.find(params[:group_id])
    @can_edit = @logged_in.can_edit?(@group)
    if @logged_in.can_see?(@group)
      @requests = @group.membership_requests
    else
      render :text => t('not_authorized'), :layout => true, :status => 401
    end
  end

  # join group
  def create
    @group = Group.find(params[:group_id])
    @person = Person.find(params[:id])
    if @logged_in.can_edit?(@group) or not @group.approval_required_to_join?
      @group.memberships.create(:person => @person)
    elsif me?
      @group.membership_requests.create(:person => @person)
      flash[:warning] = t('groups.request_sent')
    end
    redirect_back
  end

  def update
    @group = Group.find(params[:group_id])
    # email on/off
    if params[:email]
      @person = Person.find(params[:id])
      if @logged_in.can_edit?(@group) or @logged_in.can_edit?(@person)
        @group.set_options_for @person, {:get_email => (params[:email] == 'on')}
        flash[:notice] = t('groups.email_settings_changed')
        redirect_back
      else
        render :text => t('There_was_an_error'), :layout => true, :status => 500
      end
    # promote/demote
    elsif @logged_in.can_edit?(@group)
      @membership = @group.memberships.find_or_create_by_person_id(params[:id])
      @membership.update_attribute :admin, !params[:promote].nil?
      flash[:notice] = t('groups.user_settings_saved')
      redirect_back
    else
      render :text => t('not_authorized'), :layout => true, :status => 401
    end
  end

  # leave group
  def destroy
    @group = Group.find(params[:group_id])
    @membership = @group.memberships.find_by_person_id(params[:id])
    if @logged_in.can_edit?(@group) or @membership.try(:person) == @logged_in
      if @membership.person and @group.last_admin?(@membership.person)
        flash[:warning] = t('groups.last_admin_remove', :name => @membership.person.name)
      else
        @membership.destroy
      end
    end
    respond_to do |format|
      format.html { redirect_back }
{'fraction_non_alphanumeric': 0.1281194733739438, 'fraction_numerical': 0.0035370406759677735, 'mean_word_length': 3.536541889483066, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 35, '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': '5924383', 'n_tokens_mistral': 1690, 'n_tokens_neox': 1638, 'n_words': 341}