<commit_before># This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.
# (c) <NAME>

class ChatroomsController < ApplicationController
  before_filter :authenticate_user!
  before_filter :user_is_owner, :only => [:edit, :update, :destroy]
  before_filter :user_is_allowed, :only => [:show, :full]

  # GET /chatrooms
  # GET /chatrooms.json
  respond_to :html, :json
  def index
    @chatrooms = current_user.chatrooms.all
    respond_with @chatrooms
  end

  # GET /chatrooms/1
  # GET /chatrooms/1.json
  def show
    @msgs = @chatroom.msgs.last(20).reverse
    respond_with @chatroom
  end

  def full
    @msgs = @chatroom.msgs.reverse
    respond_with @chatroom
  end

  # GET /chatrooms/new
  # GET /chatrooms/new.json
  def new
    @chatroom = current_user.chatrooms.new
    respond_with @chatroom
  end

  # GET /chatrooms/1/edit
  def edit
  end

  # POST /chatrooms
  # POST /chatrooms.json
  def create
    @chatroom = current_user.chatrooms.build(params[:chatroom])

    respond_to do |format|
      if current_user.chatrooms << @chatroom
        format.html { redirect_to @chatroom, notice: 'Chatroom was successfully created.' }
        format.json { render json: @chatroom, status: :created, location: @chatroom }
      else
        format.html { render action: "new" }
        format.json { render json: @chatroom.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /chatrooms/1
  # PUT /chatrooms/1.json
  def update
    respond_to do |format|
      if @chatroom.update_attributes(params[:chatroom])
        format.html { redirect_to @chatroom, notice: 'Chatroom was successfully updated.' }
        format.json { head :ok }
      else
        format.html { render action: "edit" }
        format.json { render json: @chatroom.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /chatrooms/1
  # DELETE /chatrooms/1.json
  def destroy
    @chatroom.destroy

    respond_to do |format|
      format.html { redirect_to chatrooms_url, alert: "Chatroom deleted. :(" }
      format.json { head :ok }
    end
  end

private
  def user_is_owner
    @chatroom = Chatroom.find(params[:id])
    redirect_to root_path, :flash => { :notice => "You don't own that" } unless current_user.chatrooms.include? @chatroom
  end

  def user_is_allowed
    @chatroom = Chatroom.find(params[:id])
    redirect_to root_path, :flash => { :notice => "You don't have permission to see this" } unless @chatroom.users.include? current_user
  end
end

<commit_msg>Pre-load users and last_message in chatrooms list<commit_after># This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.
# (c) <NAME>

class ChatroomsController < ApplicationController
  before_filter :authenticate_user!
  before_filter :user_is_owner, :only => [:edit, :update, :destroy]
  before_filter :user_is_allowed, :only => [:show, :full]

  # GET /chatrooms
  # GET /chatrooms.json
  respond_to :html, :json
  def index
    @chatrooms = current_user.chatrooms.includes(:users, :last_message).all
    respond_with @chatrooms
  end

  # GET /chatrooms/1
  # GET /chatrooms/1.json
  def show
    @msgs = @chatroom.msgs.last(20).reverse
    respond_with @chatroom
  end

  def full
    @msgs = @chatroom.msgs.reverse
    respond_with @chatroom
  end

  # GET /chatrooms/new
  # GET /chatrooms/new.json
  def new
    @chatroom = current_user.chatrooms.new
    respond_with @chatroom
  end

  # GET /chatrooms/1/edit
  def edit
  end

  # POST /chatrooms
  # POST /chatrooms.json
  def create
    @chatroom = current_user.chatrooms.build(params[:chatroom])

    respond_to do |format|
      if current_user.chatrooms << @chatroom
        format.html { redirect_to @chatroom, notice: 'Chatroom was successfully created.' }
        format.json { render json: @chatroom, status: :created, location: @chatroom }
      else
        format.html { render action: "new" }
        format.json { render json: @chatroom.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /chatrooms/1
  # PUT /chatrooms/1.json
  def update
    respond_to do |format|
      if @chatroom.update_attributes(params[:chatroom])
        format.html { redirect_to @chatroom, notice: 'Chatroom was successfully updated.' }
        format.json { head :ok }
      else
        format.html { render action: "edit" }
        format.json { render json: @chatroom.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /chatrooms/1
  # DELETE /chatrooms/1.json
  def destroy
    @chatroom.destroy

    respond_to do |format|
      format.html { redirect_to chatrooms_url, alert: "Chatroom deleted. :(" }
      format.json { head :ok }
    end
  end

private
  def user_is_owner
    @chatroom = Chatroom.find(params[:id])
    redirect_to root_path, :flash => { :notice => "You don't own that" } unless current_user.chatrooms.include? @chatroom
  end

  def user_is_allowed
    @chatroom = Chatroom.find(params[:id])
    redirect_to root_path, :flash => { :notice => "You don't have permission to see this" } unless @chatroom.users.include? current_user
  end
end


{'fraction_non_alphanumeric': 0.10340314136125654, 'fraction_numerical': 0.0048616305160807775, 'mean_word_length': 3.0832061068702292, 'pattern_counts': {'":': 0, '<': 11, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2963857', 'n_tokens_mistral': 1767, 'n_tokens_neox': 1723, 'n_words': 485}