Sha256: 21ba43ccf558f5f9b4449fcf1935a80c4d1b70bd9e9fc1a87327835e16f7dac7

Contents?: true

Size: 1.6 KB

Versions: 23

Compression:

Stored size: 1.6 KB

Contents

class User::RoomsController < UserController
  before_action :find_room, only: [:show, :join, :leave, :destroy]
  before_action :find_hub, only: [:join, :leave, :destroy]

  def index
    @rooms = Room.paginate page: params[:page], per_page: 10
    @rooms_with_hub = @rooms.map do |room|
      OpenStruct.new room: room, hub: HubFinder.new(room).find
    end
  end

  def new
    @room = Room.new
  end

  def create
    @room = Room.new params_room
    if @room.save
      emit @room, :created, data: {room: {id: @room.id, name: @room.name}}
      redirect_to user_rooms_path, notice: "Room has been created"
    else
      render :new
    end
  end

  def show
    render
  end

  def join
    @hub.add_member current_user
    emit @room, :member_joined, data: {
      room_id: @room.id,
      member: {
        id: current_user.id,
        username: current_user.username,
      }
    }
    redirect_to user_room_url(id: params[:id])
  end

  def leave
    @hub.del_member current_user
    emit @room, :member_left, data: {
      room_id: @room.id,
      member: {id: current_user.id}
    }
    redirect_to user_rooms_path
  end

  def destroy
    if @hub.raw_members.any?
      redirect_to :back, alert: 'Only empty room can be deleted'
      return
    end
    del_hub @hub
    @room.destroy
    emit @room, :destroyed, data: {room_id: @room.id}
    redirect_to user_rooms_path, notice: 'Room has been deleted'
  end

  protected

    def params_room
      params.require(:room).permit :name
    end

    def find_room
      @room = Room.find params[:id]
    end

    def find_hub
      @hub = HubFinder.new(@room).find
    end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
loco-rails-1.3.2 test/dummy/app/controllers/user/rooms_controller.rb
loco-rails-1.3.1 test/dummy/app/controllers/user/rooms_controller.rb
loco-rails-1.3.0 test/dummy/app/controllers/user/rooms_controller.rb