Implement Event Ratings

This commit is contained in:
shlok007 2016-09-22 20:06:46 -04:00 committed by Shlok Srivastava
parent 5be1ad52ee
commit 20f4853487
67 changed files with 1298 additions and 378 deletions

View file

@ -138,6 +138,7 @@ class Ability
# ids of all the conferences for which the user has the 'organizer' role
conf_ids_for_organizer = Conference.with_role(:organizer, user).pluck(:id)
can :manage, VotableField, conference_id: conf_ids_for_organizer
can :manage, Resource, conference_id: conf_ids_for_organizer
can [:new, :create], Conference if user.has_role?(:organizer, :any)
can :manage, Conference, id: conf_ids_for_organizer
@ -192,6 +193,7 @@ class Ability
# ids of all the conferences for which the user has the 'cfp' role
conf_ids_for_cfp = Conference.with_role(:cfp, user).pluck(:id)
can :manage, VotableField, conference_id: conf_ids_for_cfp
can [:index, :show, :update], Resource, conference_id: conf_ids_for_cfp
can :manage, Event, program: { conference_id: conf_ids_for_cfp }
can :manage, EventType, program: { conference_id: conf_ids_for_cfp }
@ -220,7 +222,6 @@ class Ability
end
can [:index, :revert_object, :revert_attribute], PaperTrail::Version, item_type: 'Event', conference_id: conf_ids_for_cfp
can [:index, :revert_object, :revert_attribute], PaperTrail::Version, item_type: 'Vote', conference_id: conf_ids_for_cfp
can [:index, :revert_object, :revert_attribute], PaperTrail::Version do |version|
version.item_type == 'Commercial' && conf_ids_for_cfp.include?(version.conference_id) &&
(version.object.to_s.include?('Event') || version.object_changes.to_s.include?('Event'))

View file

@ -0,0 +1,4 @@
class AverageCache < ActiveRecord::Base
belongs_to :rater, class_name: 'User'
belongs_to :rateable, polymorphic: true
end

View file

@ -22,7 +22,7 @@ class Conference < ActiveRecord::Base
has_many :supporters, through: :ticket_purchases, source: :user
has_many :tickets, dependent: :destroy
has_many :resources, dependent: :destroy
has_many :votable_fields, dependent: :destroy
has_many :lodgings, dependent: :destroy
has_many :registrations, dependent: :destroy
has_many :participants, through: :registrations, source: :user

View file

@ -1,5 +1,8 @@
class Event < ActiveRecord::Base
include ActiveRecord::Transitions
scope :vote, -> (votable_fields) { votable_fields.each { |field| ratyrate_rateable field.title } }
has_paper_trail on: [:create, :update], ignore: [:updated_at, :guid, :week], meta: { conference_id: :conference_id }
acts_as_commentable
@ -15,8 +18,6 @@ class Event < ActiveRecord::Base
has_one :submitter_event_user, -> { where(event_role: 'submitter') }, class_name: 'EventUser'
has_one :submitter, through: :submitter_event_user, source: :user
has_many :votes, dependent: :destroy
has_many :voters, through: :votes, source: :user
has_many :commercials, as: :commercialable, dependent: :destroy
belongs_to :event_type
@ -92,33 +93,9 @@ class Event < ActiveRecord::Base
registrations.count < max_attendees
end
##
# Finds the rating of the user for the event
# ====Returns
# * +integer+ -> the rating of the user for the event
def user_rating(user)
(vote = votes.find_by(user: user)) ? vote.rating : 0
end
##
# Checks if the event has votes
# If a user is provided, it checks if the event has votes by the user
# ====Returns
# * +true+ -> If the event has votes (optionally, by the user)
# * +false+ -> If the event does not have any votes (optionally, by the user)
def voted?(user=nil)
return votes.where(user: user).any? if user
votes.any?
end
def average_rating
@total_rating = 0
votes.each do |vote|
@total_rating = @total_rating + vote.rating
end
@total = votes.size
@total_rating > 0 ? number_with_precision(@total_rating / @total.to_f, precision: 2, strip_insignificant_zeros: true) : 0
def ended?
timezone = program.conference.timezone
Time.now.in_time_zone(timezone) > event_schedules.find_by(schedule: program.selected_schedule).end_time
end
# get event speakers with the event sumbmitter at the first position

View file

@ -0,0 +1,3 @@
class OverallAverage < ActiveRecord::Base
belongs_to :rateable, polymorphic: true
end

View file

@ -53,11 +53,11 @@ class Program < ActiveRecord::Base
accepts_nested_attributes_for :difficulty_levels, allow_destroy: true
# validates :conference_id, presence: true, uniqueness: true
validates :rating, numericality: { greater_than_or_equal_to: 0, less_than_or_equal_to: 10, only_integer: true }
validates :schedule_interval, numericality: { greater_than_or_equal_to: 5, less_than_or_equal_to: 60 }, presence: true
validate :schedule_interval_divisor_60
validate :voting_start_date_before_end_date
validate :voting_dates_exist
validate :voting_dates_exist_for_blind_voting
validate :voting_dates_exist_for_rating_enabled
after_create :create_event_types
after_create :create_difficulty_levels
@ -93,10 +93,10 @@ class Program < ActiveRecord::Base
end
##
# Checks if both voting_start_date and voting_end_date are set
# Checks if both voting_start_date and voting_end_date are set when blind voting is enabled
# ====Returns
# Errors when the condition is not true
def voting_dates_exist
def voting_dates_exist_for_blind_voting
errors.add(:voting_start_date, 'must be set, when blind voting is enabled') if blind_voting && !voting_start_date && !voting_end_date
errors.add(:voting_end_date, 'must be set, when blind voting is enabled') if blind_voting && !voting_start_date && !voting_end_date
@ -106,6 +106,15 @@ class Program < ActiveRecord::Base
errors.add(:voting_start_date, 'must be set, when voting_end_date is set') if voting_end_date && !voting_start_date
end
##
# Checks if both voting_start_date and voting_end_date are set when rating is enabled
# ====Returns
# Errors when the condition is not true
def voting_dates_exist_for_rating_enabled
errors.add(:voting_start_date, 'must be set, when voting is enabled') if rating_enabled && !voting_start_date && !voting_end_date
errors.add(:voting_end_date, 'must be set, when voting is enabled') if rating_enabled && !voting_start_date && !voting_end_date
end
##
# Checks if voting_start_date is before voting_end_date
# ====Returns
@ -115,16 +124,7 @@ class Program < ActiveRecord::Base
end
##
# Checcks if the program has rating enabled
#
# ====Returns
# * +false+ -> If rating is not enabled
# * +true+ -> If rating is enabled
def rating_enabled?
rating && rating > 0
end
##
# Checks if the call for papers for the conference is currently open
#
# ====Returns

4
app/models/rate.rb Normal file
View file

@ -0,0 +1,4 @@
class Rate < ActiveRecord::Base
belongs_to :rater, class_name: 'User'
belongs_to :rateable, polymorphic: true
end

View file

@ -0,0 +1,3 @@
class RatingCache < ActiveRecord::Base
belongs_to :cacheable, polymorphic: true
end

View file

@ -5,6 +5,7 @@ class UserDisabled < StandardError
end
class User < ActiveRecord::Base
ratyrate_rater
rolify
has_many :users_roles
has_many :roles, through: :users_roles, dependent: :destroy
@ -47,8 +48,6 @@ class User < ActiveRecord::Base
has_many :ticket_purchases, dependent: :destroy
has_many :payments, dependent: :destroy
has_many :tickets, through: :ticket_purchases, source: :ticket
has_many :votes, dependent: :destroy
has_many :voted_events, through: :votes, source: :events
has_many :subscriptions, dependent: :destroy
accepts_nested_attributes_for :roles

View file

@ -0,0 +1,20 @@
class VotableField < ActiveRecord::Base
belongs_to :conference
validates :title, :votable_type, :stars, presence: true
validates :title, uniqueness: {scope: :votable_type, message: 'already exsists for the selected votable type'}
VALID_VOTABLE_TYPES = %w(Event).freeze
# ratyrate does not allow criterias to have spaces in them
validate :no_spaces_in_title
validate :correct_votable_type
private
def no_spaces_in_title
errors.add(:title, 'should not have spaces') unless title.match(/\s/).nil?
end
def correct_votable_type
errors.add(:votable_type, "should be one of the following: #{VALID_VOTABLE_TYPES.join(', ')}") unless VALID_VOTABLE_TYPES.include? votable_type
end
end

View file

@ -1,14 +1,2 @@
class Vote < ActiveRecord::Base
belongs_to :user
belongs_to :event
has_paper_trail ignore: [:updated_at], meta: { conference_id: :conference_id }
delegate :name, to: :user
private
def conference_id
event.program.conference_id
end
end