Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class RoomsController < ApplicationController
- before_action :set_room, except: [:index, :new, :create]
- before_action :authenticate_user!, except: [:show]
- before_action :is_authorised, only: [:listing, :pricing, :description, :photo_upload, :amenities, :location, :update]
- def index
- @rooms = current_user.rooms
- end
- def new
- @hotels = current_user.hotels
- @room = current_user.rooms.build
- end
- def create
- @room = current_user.rooms.build(room_params)
- if @room.save
- redirect_to listing_room_path(@room), notice: "Сохранено..."
- else
- flash[:alert] = "Что-то пошло не так..."
- render :new
- end
- end
- def show
- @pictures = @room.pictures
- @guest_reviews = @room.guest_reviews
- end
- def listing
- @hotels = current_user.hotels
- end
- def pricing
- end
- def description
- end
- def photo_upload
- @pictures = @room.pictures
- end
- def amenities
- end
- def location
- end
- def update
- new_params = room_params
- new_params = room_params.merge(active: 1) if is_ready_room
- if @room.update(new_params)
- flash[:notice] = "Сохранено..."
- else
- flash[:alert] = "Что-то пошло не так..."
- end
- redirect_back(fallback_location: request.referer)
- end
- # --- Reservations ---
- def preload
- today = Date.today
- reservations = @room.reservations.where("(start_date >= ? OR end_date >= ?) AND status = ?", today, today, 1)
- unavailable_dates = @room.calendars.where("status = ? AND day > ?", 1, today)
- special_dates = @room.calendars.where("status = ? AND day > ? AND price <> ?", 0, today, @room.price)
- render json: {
- reservations: reservations,
- unavailable_dates: unavailable_dates,
- special_dates: special_dates
- }
- end
- def preview
- start_date = Date.parse(params[:start_date])
- end_date = Date.parse(params[:end_date])
- output = {
- conflict: is_conflict(start_date, end_date, @room)
- }
- render json: output
- end
- private
- def is_conflict(start_date, end_date, room)
- check = room.reservations.where("(? < start_date AND end_date < ?) AND status = ?", start_date, end_date, 1)
- check_2 = room.calendars.where("day BETWEEN ? AND ? AND status = ?", start_date, end_date, 1).limit(1)
- check.size > 0 || check_2.size > 0 ? true : false
- end
- def set_room
- @room = Room.find(params[:id])
- end
- def is_authorised
- redirect_to root_path, alert: "У вас нет доступа" unless current_user.id == @room.user_id
- end
- def is_ready_room
- !@room.active && !@room.price.blank? && !@room.listing_name.blank? && !@room.pictures.blank?
- end
- def room_params
- params.require(:room).permit(:home_type, :room_type, :accommodate, :bed_room, :bath_room, :listing_name, :summary, :address, :is_tv, :is_kitchen, :is_air, :is_heating, :is_internet, :price, :active, :instant, :hotel_id)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement