diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index e4754e1e..a2da958f 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -714,3 +714,6 @@ Style/TrailingCommaInLiteral: - 'Guardfile' - 'db/migrate/20140701123203_add_events_per_week_to_conference.rb' - 'spec/models/conference_spec.rb' + +Style/FrozenStringLiteralComment: + Enabled: false diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index f0311c7a..c334cde2 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -154,7 +154,7 @@ module ApplicationHelper end def rescheduling_hint(affected_event_count) - if affected_event_count > 0 + if affected_event_count.positive? "You have #{affected_event_count} scheduled #{'event'.pluralize(affected_event_count)}. Changing the conference hours will unschedule those scheduled outside the conference hours." end end @@ -196,4 +196,23 @@ module ApplicationHelper object.picture.large.url end end + + def rating_stars(rating, max, options = {}) + Array.new(max) do |counter| + content_tag( + 'label', + '', + class: "rating#{' bright' if rating.to_f > counter}", + **options + ) + end.join.html_safe + end + + def rating_fraction(rating, max, options = {}) + content_tag( + 'span', + "#{rating}/#{max}", + **options + ) + end` end diff --git a/app/helpers/events_helper.rb b/app/helpers/events_helper.rb index 33694533..6e95ff1d 100644 --- a/app/helpers/events_helper.rb +++ b/app/helpers/events_helper.rb @@ -10,17 +10,6 @@ module EventsHelper "Registered: #{event.registrations.count}" end - def rating_stars(rating, max, options = {}) - max.times.collect do |counter| - content_tag( - 'label', - '', - class: "rating#{' bright' if rating.to_f > counter}", - **options - ) - end.join.html_safe - end - def replacement_event_notice(event_schedule) if event_schedule.present? && event_schedule.replacement? replaced_event = (event_schedule.intersecting_event_schedules.withdrawn.first || event_schedule.intersecting_event_schedules.canceled.first).event @@ -158,10 +147,10 @@ module EventsHelper event.id, event.send(attribute), url: admin_conference_program_event_path( - conference_id, - event, - event: { attribute => nil } - ), + conference_id, + event, + event: { attribute => nil } + ), method: :patch, class: 'switch-checkbox', data: { @@ -193,13 +182,13 @@ module EventsHelper } ) do content_tag('span', selection) + - content_tag('span', '', class: 'caret') + content_tag('span', '', class: 'caret') end + - content_tag('ul', class: 'dropdown-menu') do - options.collect do |option| - content_tag('li', link_to(option[0], option[1], method: :patch)) - end.join.html_safe - end + content_tag('ul', class: 'dropdown-menu') do + options.collect do |option| + content_tag('li', link_to(option[0], option[1], method: :patch)) + end.join.html_safe + end end end end diff --git a/app/models/ability.rb b/app/models/ability.rb index d6b3ef21..c3726f9d 100644 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -18,7 +18,7 @@ class Ability can [:index, :conferences], Organization can [:index], Conference can [:show], Conference do |conference| - conference.splashpage && conference.splashpage.public == true + conference&.splashpage&.public end # Can view the schedule can [:schedule, :events], Conference do |conference| diff --git a/app/models/cfp.rb b/app/models/cfp.rb index 5105a6bd..02e74abb 100644 --- a/app/models/cfp.rb +++ b/app/models/cfp.rb @@ -42,7 +42,7 @@ class Cfp < ApplicationRecord def weeks result = end_week - start_week + 1 weeks = Date.new(start_date.year, 12, 31).strftime('%W').to_i - result < 0 ? result + weeks : result + result.negative? ? result + weeks : result end ## @@ -63,7 +63,7 @@ class Cfp < ApplicationRecord def remaining_days(date = Date.today) result = (end_date - date).to_i - result > 0 ? result : 0 + result.positive? ? result : 0 end ## @@ -106,12 +106,12 @@ class Cfp < ApplicationRecord private def before_end_of_conference - if program && program.conference && program.conference.end_date && end_date && (end_date > program.conference.end_date) + if program&.conference && program.conference.end_date && end_date && (end_date > program.conference.end_date) errors .add(:end_date, "can't be after the conference end date (#{program.conference.end_date})") end - if program && program.conference && program.conference.end_date && start_date && (start_date > program.conference.end_date) + if program&.conference && program.conference.end_date && start_date && (start_date > program.conference.end_date) errors .add(:start_date, "can't be after the conference end date (#{program.conference.end_date})") end diff --git a/app/models/conference.rb b/app/models/conference.rb index 81e9ec32..f757bfe9 100644 --- a/app/models/conference.rb +++ b/app/models/conference.rb @@ -102,7 +102,7 @@ class Conference < ApplicationRecord # * +false+ -> If the user is registered # * +true+ - If the user isn't registered def user_registered? user - user.present? && registrations.where(user_id: user.id).count > 0 + user.present? && registrations.where(user_id: user.id).count.positive? end ## @@ -113,7 +113,7 @@ class Conference < ApplicationRecord event_schedules = program.event_schedules.select do |event_schedule| event_schedule.start_time.hour < start_hour || event_schedule.end_time.hour > end_hour || - (event_schedule.end_time.hour == end_hour && event_schedule.end_time.minute > 0) + (event_schedule.end_time.hour == end_hour && event_schedule.end_time.minute.positive?) end event_schedules.each(&:destroy) end @@ -151,7 +151,7 @@ class Conference < ApplicationRecord def get_submissions_per_week result = [] - if program && program.cfp && program.events + if program&.cfp && program.events submissions = program.events.select(:week).group(:week).order(:week).count start_week = program.cfp.start_week weeks = program.cfp.weeks @@ -168,7 +168,7 @@ class Conference < ApplicationRecord # * +Array+ -> e.g. 'Submitted' => [0, 3, 3, 5] -> first week 0 events, second week 3 events. def get_submissions_data result = {} - if program && program.cfp && program.events + if program&.cfp && program.events result = get_events_per_week_by_state start_week = program.cfp.start_week @@ -182,7 +182,7 @@ class Conference < ApplicationRecord result[state] = pad_array_left_not_kumulative(start_week, values) end end - result['Weeks'] = weeks > 0 ? (1..weeks).to_a : 0 + result['Weeks'] = weeks.positive? ? (1..weeks).to_a : 0 end result end @@ -256,7 +256,7 @@ class Conference < ApplicationRecord result[Ticket.find(ticket).title] = pad_array_left_not_kumulative(start_week, values) end - result['Weeks'] = weeks > 0 ? (1..weeks).to_a : 0 + result['Weeks'] = weeks.positive? ? (1..weeks).to_a : 0 end result end @@ -269,15 +269,14 @@ class Conference < ApplicationRecord def registration_weeks result = 0 weeks = 0 - if registration_period && - registration_period.start_date && + if registration_period&.start_date && registration_period.end_date weeks = Date.new(registration_period.start_date.year, 12, 31) .strftime('%W').to_i result = get_registration_end_week - get_registration_start_week + 1 end - result < 0 ? result + weeks : result + result.negative? ? result + weeks : result end ## @@ -343,7 +342,7 @@ class Conference < ApplicationRecord tracks: tracks_set?, event_types: event_types_set?, difficulty_levels: difficulty_levels_set?, - splashpage: splashpage && splashpage.public? + splashpage: splashpage&.public? } result.update( @@ -449,11 +448,11 @@ class Conference < ApplicationRecord i += 1 end end - if others > 0 + if others.positive? result['Others'] = { 'value' => others, 'color' => next_color(i) } i += 1 end - result['None'] = { 'value' => none, 'color' => next_color(i) } if none > 0 + result['None'] = { 'value' => none, 'color' => next_color(i) } if none.positive? result end @@ -719,7 +718,7 @@ class Conference < ApplicationRecord # * +True+ -> If the registration limit has been reached or exceeded # * +False+ -> If the registration limit hasn't been exceeded def registration_limit_exceeded? - registration_limit > 0 && registrations.count + program.speakers.confirmed.unregistered(program.conference).count >= registration_limit + registration_limit.positive? && registrations.count + program.speakers.confirmed.unregistered(program.conference).count >= registration_limit end # Returns an hexadecimal color given a collection. The returned color changed @@ -757,7 +756,7 @@ class Conference < ApplicationRecord # * +True+ -> if accepted booths are equal to the booth limit # * +False+ -> Accepted booths have not reached the booth limit def maximum_accepted_booths? - booth_limit > 0 && booths.accepted.count + booths.confirmed.count >= booth_limit + booth_limit.positive? && booths.accepted.count + booths.confirmed.count >= booth_limit end ## @@ -829,7 +828,7 @@ class Conference < ApplicationRecord # Reports an error when such a condition is found def valid_times_range? if start_hour && end_hour - errors.add(:start_hour, 'is lower than 0') if start_hour < 0 + errors.add(:start_hour, 'is lower than 0') if start_hour.negative? errors.add(:end_hour, 'is lower or equal than start hour') if end_hour <= start_hour errors.add(:end_hour, 'is greater than 24') if end_hour > 24 end @@ -843,7 +842,7 @@ class Conference < ApplicationRecord def weeks(start_week, end_week) weeks = end_week - start_week + 1 weeks_of_year = Date.new(start_date.year, 12, 31).strftime('%W').to_i - weeks < 0 ? weeks + weeks_of_year : weeks + weeks.negative? ? weeks + weeks_of_year : weeks end ## @@ -961,7 +960,7 @@ class Conference < ApplicationRecord # * +True+ -> One difficulty level or more # * +False+ -> No diffculty level def difficulty_levels_set? - program.difficulty_levels.count > 0 + program.difficulty_levels.count.positive? end ## @@ -971,7 +970,7 @@ class Conference < ApplicationRecord # * +True+ -> One difficulty level or more # * +False+ -> No diffculty level def event_types_set? - program.event_types.count > 0 + program.event_types.count.positive? end ## @@ -981,7 +980,7 @@ class Conference < ApplicationRecord # * +True+ -> One track or more # * +False+ -> No track def tracks_set? - program.tracks.count > 0 + program.tracks.count.positive? end ## @@ -991,7 +990,7 @@ class Conference < ApplicationRecord # * +True+ -> One room or more # * +False+ -> No room def rooms_set? - venue.present? && venue.rooms.count > 0 + venue.present? && venue.rooms.count.positive? end # Checks if the conference has a venue object. @@ -1097,19 +1096,19 @@ class Conference < ApplicationRecord # * +hash+ -> hash def self.calculate_user_distribution_hash(active_user, unconfirmed_user, dead_user) result = {} - if active_user > 0 + if active_user.positive? result['Active'] = { 'color' => 'green', 'value' => active_user } end - if unconfirmed_user > 0 + if unconfirmed_user.positive? result['Unconfirmed'] = { 'color' => 'red', 'value' => unconfirmed_user } end - if dead_user > 0 + if dead_user.positive? result['Dead'] = { 'color' => 'black', 'value' => dead_user diff --git a/app/models/event.rb b/app/models/event.rb index 943572eb..c0361868 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -120,7 +120,15 @@ class Event < ApplicationRecord @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 + if @total_rating.positive? + number_with_precision( + @total_rating / @total.to_f, + precision: 2, + strip_insignificant_zeros: true + ) + else + 0 + end end # get event speakers with the event sumbmitter at the first position @@ -310,7 +318,7 @@ class Event < ApplicationRecord def before_end_of_conference errors - .add(:created_at, "can't be after the conference end date!") if program.conference && program.conference.end_date && + .add(:created_at, "can't be after the conference end date!") if program.conference&.end_date && (Date.today > program.conference.end_date) end @@ -322,7 +330,7 @@ class Event < ApplicationRecord # Allow only confirmed tracks that belong to the same program as the event # def valid_track - return unless track && track.program && program + return unless track&.program && program errors.add(:track, 'is invalid') unless track.confirmed? && track.program == program end diff --git a/app/models/program.rb b/app/models/program.rb index 14554786..a6c7aff3 100644 --- a/app/models/program.rb +++ b/app/models/program.rb @@ -80,7 +80,7 @@ class Program < ApplicationRecord tracks.self_organized.confirmed.order(start_date: :asc).each do |track| event_schedules += track.selected_schedule.event_schedules.order(start_time: :asc) if track.selected_schedule end - event_schedules.sort_by(&:start_time) if event_schedules + event_schedules&.sort_by(&:start_time) end ## @@ -134,7 +134,7 @@ class Program < ApplicationRecord # * +false+ -> If rating is not enabled # * +true+ -> If rating is enabled def rating_enabled? - rating && rating > 0 + rating&.positive? end ## @@ -240,7 +240,9 @@ class Program < ApplicationRecord # Check if schedule_interval is a divisor of 60 minutes # def schedule_interval_divisor_60 - errors.add(:schedule_interval, 'must be a divisor of 60') if schedule_interval > 0 && 60 % schedule_interval > 0 + if schedule_interval.positive? && 60 % schedule_interval.positive? + errors.add(:schedule_interval, 'must be a divisor of 60') + end end ## @@ -248,7 +250,7 @@ class Program < ApplicationRecord # def unschedule_unfit_events unfit_schedules = event_schedules.select do |event_schedule| - event_schedule.start_time.min % schedule_interval > 0 + event_schedule.start_time.min % schedule_interval.positive? end EventSchedule.where(id: unfit_schedules.map(&:id)).destroy_all end diff --git a/app/models/registration.rb b/app/models/registration.rb index 45d2d55b..536392dc 100644 --- a/app/models/registration.rb +++ b/app/models/registration.rb @@ -77,7 +77,8 @@ class Registration < ApplicationRecord end def registration_limit_not_exceed - if conference.registration_limit > 0 && conference.registrations(:reload).count >= conference.registration_limit + if conference.registration_limit.positive? && + conference.registrations(:reload).count >= conference.registration_limit errors.add(:base, 'Registration limit exceeded') end end diff --git a/app/models/registration_period.rb b/app/models/registration_period.rb index 9aa82444..dadb1ae1 100644 --- a/app/models/registration_period.rb +++ b/app/models/registration_period.rb @@ -11,10 +11,10 @@ class RegistrationPeriod < ApplicationRecord def before_end_of_conference errors - .add(:start_date, "can't be after the conference end date (#{conference.end_date})") if conference && conference.end_date && start_date && (start_date > conference.end_date) + .add(:start_date, "can't be after the conference end date (#{conference.end_date})") if conference&.end_date && start_date && (start_date > conference.end_date) errors - .add(:end_date, "can't be after the conference end date (#{conference.end_date})") if conference && conference.end_date && end_date && (end_date > conference.end_date) + .add(:end_date, "can't be after the conference end date (#{conference.end_date})") if conference&.end_date && end_date && (end_date > conference.end_date) end def start_date_before_end_date diff --git a/app/models/ticket_purchase.rb b/app/models/ticket_purchase.rb index 15de3785..2c63fc85 100644 --- a/app/models/ticket_purchase.rb +++ b/app/models/ticket_purchase.rb @@ -48,7 +48,7 @@ class TicketPurchase < ApplicationRecord end def self.purchase_ticket(conference, quantity, ticket, user) - if quantity > 0 + if quantity.positive? purchase = new(ticket_id: ticket.id, conference_id: conference.id, user_id: user.id, @@ -65,7 +65,7 @@ class TicketPurchase < ApplicationRecord user_id: user.id, paid: false).first - purchase.quantity = quantity if quantity > 0 + purchase.quantity = quantity if quantity.positive? purchase end diff --git a/app/models/track.rb b/app/models/track.rb index 01755c0e..b53d7944 100644 --- a/app/models/track.rb +++ b/app/models/track.rb @@ -113,10 +113,8 @@ class Track < ApplicationRecord def revoke_role_and_cleanup role = Role.find_by(name: 'track_organizer', resource: self) - if role - role.users.each do |user| - user.remove_role 'track_organizer', self - end + role&.users&.each do |user| + user.remove_role 'track_organizer', self end self.selected_schedule_id = nil diff --git a/app/models/user.rb b/app/models/user.rb index d062529a..a7a279c5 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -126,7 +126,7 @@ class User < ApplicationRecord def self.for_ichain_username(username, attributes) user = find_by(username: username) - raise UserDisabled if user && user.is_disabled + raise UserDisabled if user&.is_disabled if user user.update_attributes(email: attributes[:email], diff --git a/app/models/venue.rb b/app/models/venue.rb index 6a758242..9ef8b109 100644 --- a/app/models/venue.rb +++ b/app/models/venue.rb @@ -19,7 +19,7 @@ class Venue < ApplicationRecord def country_name name = ISO3166::Country[country] - name.name if name + name&.name end def location? diff --git a/app/views/admin/events/_datatable_row.haml b/app/views/admin/events/_datatable_row.haml index 17cf7560..4f361338 100644 --- a/app/views/admin/events/_datatable_row.haml +++ b/app/views/admin/events/_datatable_row.haml @@ -1,5 +1,6 @@ -- cache ['admin/events/index', conference_id, event, event.submitter, event.speakers, event_types, tracks, difficulty_levels] do - %tr{id: "event-#{event.id}"} +- cache ['admin/events/index', conference_id, event, event.submitter, + event.speakers, event_types, tracks, difficulty_levels] do + %tr{ id: "event-#{event.id}" } %td = event.id @@ -38,7 +39,8 @@ = event_switch_checkbox(event, :require_registration, conference_id) - if event.require_registration = link_to registered_text(event), - registrations_admin_conference_program_event_path(conference_id, event), + registrations_admin_conference_program_event_path(conference_id, + event), class: 'btn btn-xs btn-default' %td.text-center{ data: { order: event.is_highlight.to_s } } diff --git a/app/views/admin/events/_datatable_row_rating.haml b/app/views/admin/events/_datatable_row_rating.haml index 99ff49ec..8e3e9c29 100644 --- a/app/views/admin/events/_datatable_row_rating.haml +++ b/app/views/admin/events/_datatable_row_rating.haml @@ -2,11 +2,11 @@ %div{ data: { toggle: 'tooltip' }, title: rating_tooltip(event, max_rating) }< = rating_stars(event.average_rating, max_rating, avgrate: true) -%div.clearfix +.clearfix - if event.voted?(current_user) %span.label.label-success You voted: - = "#{event.user_rating(current_user)}/#{max_rating}" + = rating_fraction(event.user_rating(current_user), max_rating) - else %span.label.label-danger Not rated diff --git a/app/views/admin/events/_export_menu.haml b/app/views/admin/events/_export_menu.haml index 233986df..e7743d23 100644 --- a/app/views/admin/events/_export_menu.haml +++ b/app/views/admin/events/_export_menu.haml @@ -1,4 +1,4 @@ -%div.btn-group +.btn-group %button.btn.btn-success.dropdown-toggle{ data: { toggle: 'dropdown' } } Export = export_format.upcase diff --git a/db/migrate/20141128073306_migrate_data_remove_column_include_cfp_in_splash_add_column_include_cfp.rb b/db/migrate/20141128073306_migrate_data_remove_column_include_cfp_in_splash_add_column_include_cfp.rb index 2f794f75..169bcfb3 100644 --- a/db/migrate/20141128073306_migrate_data_remove_column_include_cfp_in_splash_add_column_include_cfp.rb +++ b/db/migrate/20141128073306_migrate_data_remove_column_include_cfp_in_splash_add_column_include_cfp.rb @@ -17,7 +17,7 @@ class MigrateDataRemoveColumnIncludeCfpInSplashAddColumnIncludeCfp < ActiveRecor TempConference.all.each do |conference| cfp = TempCallForPaper.find_by(conference_id: conference.id) - if cfp && cfp.include_cfp_in_splash + if cfp&.include_cfp_in_splash splashpage = TempSplashpage.find_or_initialize_by(conference_id: conference.id) splashpage.include_cfp = cfp.include_cfp_in_splash # true splashpage.save! diff --git a/lib/tasks/events_registrations.rake b/lib/tasks/events_registrations.rake index 726b739b..ea0254dd 100644 --- a/lib/tasks/events_registrations.rake +++ b/lib/tasks/events_registrations.rake @@ -5,7 +5,7 @@ namespace :events_registrations do duplicates = EventsRegistration.all.map { |er| er.id if er.valid? == false}.compact puts "Duplicates found: #{duplicates.count}" - if duplicates.count > 0 + if duplicates.count.positive? puts "With IDs: #{duplicates}" end diff --git a/spec/helpers/events_helper_spec.rb b/spec/helpers/events_helper_spec.rb index 93c75272..61768afc 100644 --- a/spec/helpers/events_helper_spec.rb +++ b/spec/helpers/events_helper_spec.rb @@ -19,16 +19,16 @@ describe EventsHelper, type: :helper do end end - describe "#rating_tooltip" do + describe '#rating_tooltip' do let(:max_rating) { 5 } let(:event) { create(:event) } let(:average_rating) { "#{event.average_rating}/#{max_rating}" } let(:vote_count) { pluralize(event.voters.length, 'vote') } - it "includes the average rating" do + it 'includes the average rating' do expect(rating_tooltip(event, max_rating)).to match(average_rating) end - it "includes the vote count" do + it 'includes the vote count' do expect(rating_tooltip(event, max_rating)).to match(vote_count) end end