Skip to content

Commit

Permalink
Rating room
Browse files Browse the repository at this point in the history
  • Loading branch information
hoangcnpd01980 committed Sep 11, 2019
1 parent 0364437 commit 6530c05
Show file tree
Hide file tree
Showing 35 changed files with 335 additions and 90 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ gem "jquery-ui-rails"
gem "bootstrap-datepicker-rails", "~> 1.8", ">= 1.8.0.1"
gem "country_select", "~> 4.0"
gem "jquery-validation-rails"
gem "ratyrate"

group :development, :test do
gem "byebug", platforms: %i[mri mingw x64_mingw]
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ GEM
activesupport (>= 5.0)
i18n
polyamorous (= 2.3.0)
ratyrate (1.2.2.alpha)
rb-fsevent (0.10.3)
rb-inotify (0.10.0)
ffi (~> 1.0)
Expand Down Expand Up @@ -386,6 +387,7 @@ DEPENDENCIES
puma (~> 3.11)
rails (~> 5.2.3)
ransack
ratyrate
rspec-rails (~> 3.8)
rubocop (~> 0.74.0)
sass-rails (~> 5.0)
Expand Down
Binary file added app/assets/images/big-star.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/assets/images/cancel-off.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/assets/images/cancel-on.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/assets/images/mid-star.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
//= require bootstrap-datepicker
//= require jquery.validate
//= require jquery.validate.additional-methods
//= require jquery.raty
//= require ratyrate

/*global toastr*/
toastr.options = {
Expand Down
119 changes: 58 additions & 61 deletions app/assets/javascripts/jquery.raty.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
*
* The MIT License
*
* author: Washington Botelho
* github: wbotelhos/raty
* version: 2.9.0
* @author : Washington Botelho
* @doc : http://wbotelhos.com/raty
* @version : 2.7.0
*
*/

;
(function($) {
'use strict';

Expand All @@ -19,7 +20,7 @@

methods.destroy.call(this.self);

this.opt = $.extend(true, {}, $.fn.raty.defaults, options, this.self.data());
this.opt = $.extend(true, {}, $.fn.raty.defaults, options);

methods._adjustCallback.call(this);
methods._adjustNumber.call(this);
Expand Down Expand Up @@ -58,7 +59,7 @@
},

_adjustCallback: function() {
var options = ['number', 'readOnly', 'score', 'scoreName', 'target', 'path'];
var options = ['number', 'readOnly', 'score', 'scoreName', 'target'];

for (var i = 0; i < options.length; i++) {
if (typeof this.opt[options[i]] === 'function') {
Expand Down Expand Up @@ -234,9 +235,8 @@
},

_bindOver: function() {
var
that = this,
action = that.opt.half ? 'mousemove.raty' : 'mouseover.raty';
var that = this,
action = that.opt.half ? 'mousemove.raty' : 'mouseover.raty';

that.stars.on(action, function(evt) {
var score = methods._getScoreByPosition.call(that, evt, this);
Expand Down Expand Up @@ -288,9 +288,8 @@
},

_createCancel: function() {
var
icon = this.opt.path + this.opt.cancelOff,
cancel = $('<' + this.opt.starType + ' />', { title: this.opt.cancelHint, 'class': this.opt.cancelClass });
var icon = this.opt.path + this.opt.cancelOff,
cancel = $('<' + this.opt.starType + ' />', { title: this.opt.cancelHint, 'class': this.opt.cancelClass });

if (this.opt.starType === 'img') {
cancel.attr({ src: icon, alt: 'x' });
Expand Down Expand Up @@ -462,16 +461,15 @@
return this.opt.score && this.opt.score >= i ? 'starOn' : 'starOff';
},

_resetTitle: function() {
_resetTitle: function(star) {
for (var i = 0; i < this.opt.number; i++) {
this.stars[i].title = methods._getHint.call(this, i + 1);
}
},

_roundHalfScore: function(score) {
var
integer = parseInt(score, 10),
decimal = methods._getFirstDecimal.call(this, score);
_roundHalfScore: function(score) {
var integer = parseInt(score, 10),
decimal = methods._getFirstDecimal.call(this, score);

if (decimal !== 0) {
decimal = decimal > 5 ? 1 : 0.5;
Expand All @@ -483,16 +481,16 @@
_roundStars: function(score, evt) {
var
decimal = (score % 1).toFixed(2),
name ;
name ;

if (evt || this.move) {
name = decimal > 0.5 ? 'starOn' : 'starHalf';
} else if (decimal > this.opt.round.down) { // Up: [x.76 .. x.99]
} else if (decimal > this.opt.round.down) { // Up: [x.76 .. x.99]
name = 'starOn';

if (this.opt.halfShow && decimal < this.opt.round.up) { // Half: [x.26 .. x.75]
name = 'starHalf';
} else if (decimal < this.opt.round.full) { // Down: [x.00 .. x.5]
} else if (decimal < this.opt.round.full) { // Down: [x.00 .. x.5]
name = 'starOff';
}
}
Expand All @@ -503,7 +501,7 @@
star = this.stars[Math.ceil(score) - 1];

methods._setIcon.call(this, star, icon);
} // Full down: [x.00 .. x.25]
} // Full down: [x.00 .. x.25]
},

_setIcon: function(star, icon) {
Expand Down Expand Up @@ -615,9 +613,8 @@

destroy: function() {
return this.each(function() {
var
self = $(this),
raw = self.data('raw');
var self = $(this),
raw = self.data('raw');

if (raw) {
self.off('.raty').empty().css({ cursor: raw.style.cursor }).removeData('readonly');
Expand All @@ -628,9 +625,8 @@
},

getScore: function() {
var
score = [],
value ;
var score = [],
value ;

this.each(function() {
value = this.score.val();
Expand Down Expand Up @@ -673,7 +669,7 @@

if (self.data('readonly') !== readonly) {
if (readonly) {
self.off('.raty').children(this.opt.starType).off('.raty');
self.off('.raty').children('img').off('.raty');

methods._lock.call(this);
} else {
Expand Down Expand Up @@ -725,39 +721,40 @@
};

$.fn.raty.defaults = {
cancel: false,
cancelClass: 'raty-cancel',
cancelHint: 'Cancel this rating!',
cancelOff: 'cancel-off.png',
cancelOn: 'cancel-on.png',
cancelPlace: 'left',
click: undefined,
half: false,
halfShow: true,
hints: ['bad', 'poor', 'regular', 'good', 'gorgeous'],
iconRange: undefined,
mouseout: undefined,
mouseover: undefined,
noRatedMsg: 'Not rated yet!',
number: 5,
numberMax: 20,
path: undefined,
precision: false,
readOnly: false,
round: { down: 0.25, full: 0.6, up: 0.76 },
score: undefined,
scoreName: 'score',
single: false,
space: true,
starHalf: 'star-half.png',
starOff: 'star-off.png',
starOn: 'star-on.png',
starType: 'img',
target: undefined,
targetFormat: '{score}',
targetKeep: false,
targetScore: undefined,
targetText: '',
targetType: 'hint'
cancel : false,
cancelClass : 'raty-cancel',
cancelHint : 'Cancel this rating!',
cancelOff : 'cancel-off.png',
cancelOn : 'cancel-on.png',
cancelPlace : 'left',
click : undefined,
half : false,
halfShow : true,
hints : ['bad', 'poor', 'regular', 'good', 'gorgeous'],
iconRange : undefined,
mouseout : undefined,
mouseover : undefined,
noRatedMsg : 'Not rated yet!',
number : 5,
numberMax : 20,
path : undefined,
precision : false,
readOnly : false,
round : { down: 0.25, full: 0.6, up: 0.76 },
score : undefined,
scoreName : 'score',
single : false,
space : true,
starHalf : 'star-half.png',
starOff : 'star-off.png',
starOn : 'star-on.png',
starType : 'img',
target : undefined,
targetFormat : '{score}',
targetKeep : false,
targetScore : undefined,
targetText : '',
targetType : 'hint'
};

})(jQuery);
62 changes: 62 additions & 0 deletions app/assets/javascripts/ratyrate.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
$.fn.raty.defaults.half = false;
$.fn.raty.defaults.halfShow = true;
$.fn.raty.defaults.path = "/assets";
$.fn.raty.defaults.cancel = false;

$(function(){
$(".star").each(function() {
var $readonly = ($(this).attr('data-readonly') == 'true');
var $half = ($(this).attr('data-enable-half') == 'true');
var $halfShow = ($(this).attr('data-half-show') == 'true');
var $single = ($(this).attr('data-single') == 'true');
$(this).raty({
score: function() {
return $(this).attr('data-rating')
},
number: function() {
return $(this).attr('data-star-count')
},
half: $half,
halfShow: $halfShow,
single: $single,
path: $(this).attr('data-star-path'),
starOn: $(this).attr('data-star-on'),
starOff: $(this).attr('data-star-off'),
starHalf: $(this).attr('data-star-half'),
cancel: $(this).attr('data-cancel'),
cancelPlace: $(this).attr('data-cancel-place'),
cancelHint: $(this).attr('data-cancel-hint'),
cancelOn: $(this).attr('data-cancel-on'),
cancelOff: $(this).attr('data-cancel-off'),
noRatedMsg: $(this).attr('data-no-rated-message'),
round: $(this).attr('data-round'),
space: $(this).attr('data-space'),
target: $(this).attr('data-target'),
targetText: $(this).attr('data-target-text'),
targetType: $(this).attr('data-target-type'),
targetFormat: $(this).attr('data-target-format'),
targetScoret: $(this).attr('data-target-score'),
readOnly: $readonly,
click: function(score, evt) {
var _this = this;
if (score == null) { score = 0; }
$.post('<%= Rails.application.class.routes.url_helpers.rate_path %>',
{
score: score,
dimension: $(this).attr('data-dimension'),
id: $(this).attr('data-id'),
klass: $(this).attr('data-classname')
},
function(data) {
if(data) {
// success code goes here ...

if ($(_this).attr('data-disable-after-rate') == 'true') {
$(_this).raty('set', { readOnly: true, score: score });
}
}
});
}
});
});
});
22 changes: 11 additions & 11 deletions app/controllers/bookings_controller.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
# frozen_string_literal: true

class BookingsController < ApplicationController
def new
return redirect_to root_path, warning: t(".warning") unless check_params_exists?

@booking = Booking.new
@room = Room.find params[:room_id]
@code_vouchers = @room.price.vouchers.map{|c| c.code}
@code_vouchers = @room.price.vouchers.map(&:code)
return redirect_to root_path, warning: t(".warning") unless valid_date?

check_valid_params
check_valid_guest
@night = ((@checkout - @checkin)/60/60/24).to_i.abs
@night = ((@checkout - @checkin) / 60 / 60 / 24).to_i.abs
end

def create
Expand All @@ -25,8 +29,8 @@ def create

def booking_params
params.require(:booking).permit :checkin, :checkout, :room_id, :number_guest,
:name_booking, :phone_booking, :email_booking, :name_booked, :phone_booked,
:email_booked, :country_code, :trip_purpose, :request, :intend_time, :voucher_id
:name_booking, :phone_booking, :email_booking, :name_booked, :phone_booked,
:email_booked, :country_code, :trip_purpose, :request, :intend_time, :voucher_id
end

def check_params_exists?
Expand All @@ -40,18 +44,14 @@ def check_valid_params
end

def check_valid_guest
if @room.guest < params[:guest].to_i.abs || !(params[:guest] =~ /\A[-+]?[0-9]+\z/)
return redirect_to root_path, warning: t(".warning")
end
redirect_to root_path, warning: t(".warning") if @room.guest < params[:guest].to_i.abs || params[:guest] !~ /\A[-+]?[0-9]+\z/
end

def valid_date?
date_format = "%d/%m/%Y"
if DateTime.strptime(params[:checkin], date_format) && DateTime.strptime(params[:checkout], date_format)
return true
end
return true if DateTime.strptime(params[:checkin], date_format) && DateTime.strptime(params[:checkout], date_format)
rescue ArgumentError
return false
false
end

def save_booking
Expand Down
8 changes: 5 additions & 3 deletions app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

class CommentsController < ApplicationController
before_action :load_room, only: %i(create destroy)
before_action :load_room, only: %i[create destroy]
before_action :load_comment, only: :destroy

def create
Expand All @@ -19,7 +21,7 @@ def destroy
if @comment.destroy
respond_to do |format|
format.html { redirect_to request.referrer }
format.js { }
format.js {}
end
else
flash[:danger] = t "controller.reviews.delete_review_fail"
Expand All @@ -30,7 +32,7 @@ def destroy
private

def comment_params
params.require(:comment).permit :rate, :content
params.require(:comment).permit :content
end

def load_room
Expand Down
Loading

0 comments on commit 6530c05

Please sign in to comment.