Advertisement
devyk

rooms_controller.rb

Sep 4th, 2018
2,468
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rails 3.01 KB | None | 0 0
  1. class RoomsController < ApplicationController
  2.   before_action :set_room, except: [:index, :new, :create]
  3.   before_action :authenticate_user!, except: [:show]
  4.   before_action :is_authorised, only: [:listing, :pricing, :description, :photo_upload, :amenities, :location, :update]
  5.  
  6.   def index
  7.     @rooms = current_user.rooms
  8.   end
  9.  
  10.   def new
  11.     @hotels = current_user.hotels
  12.     @room = current_user.rooms.build
  13.   end
  14.  
  15.   def create
  16.     @room = current_user.rooms.build(room_params)
  17.     if @room.save
  18.       redirect_to listing_room_path(@room), notice: "Сохранено..."
  19.     else
  20.       flash[:alert] = "Что-то пошло не так..."
  21.       render :new
  22.     end
  23.   end
  24.  
  25.   def show
  26.     @pictures = @room.pictures
  27.     @guest_reviews = @room.guest_reviews
  28.   end
  29.  
  30.   def listing
  31.     @hotels = current_user.hotels
  32.   end
  33.  
  34.   def pricing
  35.   end
  36.  
  37.   def description
  38.   end
  39.  
  40.   def photo_upload
  41.     @pictures = @room.pictures
  42.   end
  43.  
  44.   def amenities
  45.   end
  46.  
  47.   def location
  48.   end
  49.  
  50.   def update
  51.  
  52.     new_params = room_params
  53.     new_params = room_params.merge(active: 1) if is_ready_room
  54.  
  55.     if @room.update(new_params)
  56.       flash[:notice] = "Сохранено..."
  57.     else
  58.       flash[:alert] = "Что-то пошло не так..."
  59.     end
  60.     redirect_back(fallback_location: request.referer)
  61.   end
  62.  
  63.   # --- Reservations ---
  64.   def preload
  65.     today = Date.today
  66.     reservations = @room.reservations.where("(start_date >= ? OR end_date >= ?) AND status = ?", today, today, 1)
  67.     unavailable_dates = @room.calendars.where("status = ? AND day > ?", 1, today)
  68.     special_dates = @room.calendars.where("status = ? AND day > ? AND price <> ?", 0, today, @room.price)
  69.  
  70.     render json: {
  71.       reservations: reservations,
  72.       unavailable_dates: unavailable_dates,
  73.       special_dates: special_dates
  74.     }
  75.   end
  76.  
  77.   def preview
  78.     start_date = Date.parse(params[:start_date])
  79.     end_date = Date.parse(params[:end_date])
  80.  
  81.     output = {
  82.       conflict: is_conflict(start_date, end_date, @room)
  83.     }
  84.  
  85.     render json: output
  86.   end
  87.  
  88.   private
  89.     def is_conflict(start_date, end_date, room)
  90.       check = room.reservations.where("(? < start_date AND end_date < ?) AND status = ?", start_date, end_date, 1)
  91.       check_2 = room.calendars.where("day BETWEEN ? AND ? AND status = ?", start_date, end_date, 1).limit(1)
  92.  
  93.       check.size > 0 || check_2.size > 0 ? true : false
  94.     end
  95.  
  96.     def set_room
  97.       @room = Room.find(params[:id])
  98.     end
  99.  
  100.     def is_authorised
  101.       redirect_to root_path, alert: "У вас нет доступа" unless current_user.id == @room.user_id
  102.     end
  103.  
  104.     def is_ready_room
  105.       !@room.active && !@room.price.blank? && !@room.listing_name.blank? && !@room.pictures.blank?
  106.     end
  107.  
  108.     def room_params
  109.       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)
  110.     end
  111. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement