diff --git a/app/controllers/admin/conferences_controller.rb b/app/controllers/admin/conferences_controller.rb index cafc2b32..1def3c5b 100644 --- a/app/controllers/admin/conferences_controller.rb +++ b/app/controllers/admin/conferences_controller.rb @@ -8,7 +8,7 @@ module Admin def index # Redirect to new form if there is no conference - if Conference.count == 0 + if Conference.none? redirect_to new_admin_conference_path return end diff --git a/app/controllers/admin/roles_controller.rb b/app/controllers/admin/roles_controller.rb index ca455dce..d69e1ab1 100644 --- a/app/controllers/admin/roles_controller.rb +++ b/app/controllers/admin/roles_controller.rb @@ -69,7 +69,7 @@ module Admin end # The conference must have at least 1 organizer - if @role.name == 'organizer' && state == 'false' && @role.users.count == 1 + if @role.name == 'organizer' && state == 'false' && @role.users.one? redirect_to admin_conference_role_path(@conference.short_title, @role.name), error: 'The conference must have at least 1 organizer!' return diff --git a/app/controllers/admin/volunteers_controller.rb b/app/controllers/admin/volunteers_controller.rb index b0b85ffc..35629345 100644 --- a/app/controllers/admin/volunteers_controller.rb +++ b/app/controllers/admin/volunteers_controller.rb @@ -3,6 +3,7 @@ module Admin class VolunteersController < Admin::BaseController include VolunteersHelper + load_and_authorize_resource :conference, find_by: :short_title def index diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 93105587..315c4e30 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -3,6 +3,7 @@ class ApplicationController < ActionController::Base before_action :set_paper_trail_whodunnit include ApplicationHelper + add_flash_types :error protect_from_forgery with: :exception, prepend: true before_action :store_location diff --git a/app/controllers/conferences_controller.rb b/app/controllers/conferences_controller.rb index 8a4262f1..43809551 100644 --- a/app/controllers/conferences_controller.rb +++ b/app/controllers/conferences_controller.rb @@ -50,14 +50,14 @@ class ConferencesController < ApplicationController ).order('tracks.name') end if splashpage.include_booths - @booths = @conference.confirmed_booths.order('title') + @booths = @conference.confirmed_booths.order(:title) end end if splashpage.include_registrations || splashpage.include_tickets - @tickets = @conference.tickets.order('price_cents') + @tickets = @conference.tickets.order(:price_cents) end if splashpage.include_lodgings - @lodgings = @conference.lodgings.order('name') + @lodgings = @conference.lodgings.order(:name) end if splashpage.include_sponsors @sponsorship_levels = @conference.sponsorship_levels.eager_load( diff --git a/app/helpers/events_helper.rb b/app/helpers/events_helper.rb index ddb16e4c..b2768317 100644 --- a/app/helpers/events_helper.rb +++ b/app/helpers/events_helper.rb @@ -39,7 +39,7 @@ module EventsHelper end def canceled_replacement_event_label(event, event_schedule, *label_classes) - if event.state == 'canceled' || event.state == 'withdrawn' + if ['canceled', 'withdrawn'].include?(event.state) content_tag :span, 'CANCELED', class: (['label', 'label-danger'] + label_classes) elsif event_schedule.present? && event_schedule.replacement?(@withdrawn_event_schedules) content_tag :span, 'REPLACEMENT', class: (['label', 'label-info'] + label_classes) diff --git a/app/models/admin_ability.rb b/app/models/admin_ability.rb index 67bf6cfe..34bccc51 100644 --- a/app/models/admin_ability.rb +++ b/app/models/admin_ability.rb @@ -141,7 +141,7 @@ class AdminAbility # Abilities for Role (Conference resource) can [:index, :show], Role do |role| - role.resource_type == 'Conference' || role.resource_type == 'Track' + ['Conference', 'Track'].include?(role.resource_type) end can [:edit, :update, :toggle_user], Role do |role| @@ -180,7 +180,7 @@ class AdminAbility # Abilities for Role (Conference resource) can [:index, :show], Role do |role| - role.resource_type == 'Conference' || role.resource_type == 'Track' + ['Conference', 'Track'].include?(role.resource_type) end # Can add or remove users from role, when user has that same role for the conference # Eg. If you are member of the CfP team, you can add more CfP team members (add users to the role 'CfP') @@ -218,7 +218,7 @@ class AdminAbility # Abilities for Role (Conference resource) can [:index, :show], Role do |role| - role.resource_type == 'Conference' || role.resource_type == 'Track' + ['Conference', 'Track'].include?(role.resource_type) end # Can add or remove users from role, when user has that same role for the conference # Eg. If you are member of the CfP team, you can add more CfP team members (add users to the role 'CfP') @@ -242,7 +242,7 @@ class AdminAbility # Abilities for Role (Conference resource) can [:index, :show], Role do |role| - role.resource_type == 'Conference' || role.resource_type == 'Track' + ['Conference', 'Track'].include?(role.resource_type) end # Can add or remove users from role, when user has that same role for the conference # Eg. If you are member of the CfP team, you can add more CfP team members (add users to the role 'CfP') @@ -278,7 +278,7 @@ class AdminAbility # Show Roles in the admin sidebar and allow authorization of the index action can [:index, :show], Role do |role| - role.resource_type == 'Conference' || role.resource_type == 'Track' + ['Conference', 'Track'].include?(role.resource_type) end can :toggle_user, Role do |role| diff --git a/app/models/booth.rb b/app/models/booth.rb index 85c6c7a8..06357f0e 100644 --- a/app/models/booth.rb +++ b/app/models/booth.rb @@ -2,6 +2,7 @@ class Booth < ApplicationRecord include ActiveRecord::Transitions + has_paper_trail ignore: [:updated_at], meta: { conference_id: :conference_id } belongs_to :conference diff --git a/app/models/comment.rb b/app/models/comment.rb index ee1a07a8..0b4e6800 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -35,13 +35,13 @@ class Comment < ApplicationRecord # Helper class method to lookup all comments assigned # to all commentable types for a given user. scope :find_comments_by_user, lambda { |user| - where(user_id: user.id).order('created_at DESC') + where(user_id: user.id).order(created_at: :desc) } # Helper class method to look up all comments for # commentable class name and commentable id. scope :find_comments_for_commentable, lambda { |commentable_str, commentable_id| - where(commentable_type: commentable_str.to_s, commentable_id: commentable_id).order('created_at DESC') + where(commentable_type: commentable_str.to_s, commentable_id: commentable_id).order(created_at: :desc) } scope :find_since_last_login, lambda { |user| diff --git a/app/models/conference.rb b/app/models/conference.rb index 2e63e4ee..46505572 100644 --- a/app/models/conference.rb +++ b/app/models/conference.rb @@ -2,13 +2,14 @@ class Conference < ApplicationRecord include RevisionCount + require 'uri' serialize :events_per_week, Hash # Needed to call 'Conference.with_role' in /models/ability.rb # Dependent destroy will fail as roles#destroy will be cancelled,hence delete_all resourcify :roles, dependent: :delete_all - default_scope { order('start_date DESC') } + default_scope { order(start_date: :desc) } scope :upcoming, (-> { where(end_date: Date.current..) }) scope :past, (-> { where(end_date: ...Date.current) }) @@ -39,7 +40,7 @@ class Conference < ApplicationRecord has_many :participants, through: :registrations, source: :user has_many :vdays, dependent: :destroy has_many :vpositions, dependent: :destroy - has_many :sponsorship_levels, -> { order('position ASC') }, dependent: :destroy + has_many :sponsorship_levels, -> { order(:position) }, dependent: :destroy has_many :sponsors, dependent: :destroy has_many :commercials, as: :commercialable, dependent: :destroy has_many :subscriptions, dependent: :destroy @@ -107,7 +108,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).any? end ## @@ -350,7 +351,7 @@ class Conference < ApplicationRecord # * +hash+ -> user: submissions def self.get_top_submitter(limit = 5) submitter = EventUser.select(:user_id).where('event_role = ?', 'submitter').limit(limit).group(:user_id) - counter = submitter.order('count_all desc').count(:all) + counter = submitter.order(count_all: :desc).count(:all) calculate_user_submission_hash(submitter, counter) end @@ -363,7 +364,7 @@ class Conference < ApplicationRecord submitter = EventUser.joins(:event).select(:user_id) .where('event_role = ? and program_id = ?', 'submitter', Conference.find(id).program.id) .limit(limit).group(:user_id) - counter = submitter.order('count_all desc').count(:all) + counter = submitter.order(count_all: :desc).count(:all) Conference.calculate_user_submission_hash(submitter, counter) end @@ -921,7 +922,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.any? end ## @@ -931,7 +932,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.any? end ## @@ -941,7 +942,7 @@ class Conference < ApplicationRecord # * +True+ -> One track or more # * +False+ -> No track def tracks_set? - program.tracks.count > 0 + program.tracks.any? end ## @@ -951,7 +952,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.any? end # Checks if the conference has a venue object. diff --git a/app/models/event.rb b/app/models/event.rb index dc7202c2..82f8204b 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -4,6 +4,7 @@ class Event < ApplicationRecord include ActionView::Helpers::NumberHelper # for number_with_precision include ActiveRecord::Transitions include RevisionCount + has_paper_trail on: [:create, :update], ignore: [:updated_at, :guid, :week], meta: { conference_id: :conference_id } acts_as_commentable diff --git a/app/models/event_schedule.rb b/app/models/event_schedule.rb index 8c5e9753..32ec8b4f 100644 --- a/app/models/event_schedule.rb +++ b/app/models/event_schedule.rb @@ -82,13 +82,13 @@ class EventSchedule < ApplicationRecord end def start_after_end_hour - return unless event && start_time && event.program&.conference && event.program.conference.end_hour + return unless event && start_time && event.program&.conference&.end_hour errors.add(:start_time, "can't be after the conference end hour (#{event.program.conference.end_hour})") if start_time.hour >= event.program.conference.end_hour end def start_before_start_hour - return unless event && start_time && event.program&.conference && event.program.conference.start_hour + return unless event && start_time && event.program&.conference&.start_hour errors.add(:start_time, "can't be before the conference start hour (#{event.program.conference.start_hour})") if start_time.hour < event.program.conference.start_hour end diff --git a/app/models/room.rb b/app/models/room.rb index 4bd88834..af180fb6 100644 --- a/app/models/room.rb +++ b/app/models/room.rb @@ -2,6 +2,7 @@ class Room < ApplicationRecord include RevisionCount + belongs_to :venue has_many :event_schedules, dependent: :destroy has_many :tracks diff --git a/app/models/survey.rb b/app/models/survey.rb index 933fa9f6..62ce553e 100644 --- a/app/models/survey.rb +++ b/app/models/survey.rb @@ -25,7 +25,7 @@ class Survey < ActiveRecord::Base now = Time.current.in_time_zone(timezone) if start_date && end_date - now >= start_date && now <= end_date + now.between?(start_date, end_date) elsif start_date && !end_date now >= start_date elsif !start_date && end_date diff --git a/app/models/ticket.rb b/app/models/ticket.rb index 677ef90f..abc014cd 100644 --- a/app/models/ticket.rb +++ b/app/models/ticket.rb @@ -81,7 +81,7 @@ class Ticket < ApplicationRecord def tickets_of_conference_have_same_currency tickets = Ticket.where(conference_id: conference_id) - return if tickets.count.zero? || (tickets.count == 1 && self == tickets.first) + return if tickets.none? || (tickets.one? && self == tickets.first) unless tickets.all?{|t| t.price_currency == price_currency } errors.add(:price_currency, 'is different from the existing tickets of this conference.') diff --git a/app/models/ticket_purchase.rb b/app/models/ticket_purchase.rb index f8e6c210..6d373d3f 100644 --- a/app/models/ticket_purchase.rb +++ b/app/models/ticket_purchase.rb @@ -97,8 +97,6 @@ class TicketPurchase < ApplicationRecord end end -private - def set_week self.week = created_at.strftime('%W') save! diff --git a/app/models/track.rb b/app/models/track.rb index d34bfbef..b249bef3 100644 --- a/app/models/track.rb +++ b/app/models/track.rb @@ -233,8 +233,8 @@ class Track < ApplicationRecord (program.tracks.accepted + program.tracks.confirmed - [self]).each do |existing_track| next unless existing_track.room == room && existing_track.start_date && existing_track.end_date - if start_date >= existing_track.start_date && start_date <= existing_track.end_date || - end_date >= existing_track.start_date && end_date <= existing_track.end_date || + if start_date.between?(existing_track.start_date, existing_track.end_date) || + end_date.between?(existing_track.start_date, existing_track.end_date) || start_date <= existing_track.start_date && end_date >= existing_track.end_date errors.add(:track, 'has overlapping dates with a confirmed or accepted track in the same room') break diff --git a/app/models/user.rb b/app/models/user.rb index 564384f9..4cbac281 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -30,6 +30,7 @@ class User < ApplicationRecord :avatar_content_type, :avatar_file_size, :avatar_updated_at, :updated_at, :confirmation_sent_at, :confirmation_token, :reset_password_token] include Gravtastic + gravtastic size: 32 before_create :setup_role @@ -238,7 +239,7 @@ class User < ApplicationRecord end def registered - if registrations.count == 0 + if registrations.none? 'None' else registrations.map { |r| r.conference.title }.join ', ' @@ -247,7 +248,7 @@ class User < ApplicationRecord def attended registrations_attended = registrations.where(attended: true) - if registrations_attended.count == 0 + if registrations_attended.none? 'None' else registrations_attended.map { |r| r.conference.title }.join ', ' @@ -271,7 +272,7 @@ class User < ApplicationRecord end def self.empty? - User.count == 1 && User.first.email == 'deleted@localhost.osem' + User.one? && User.first.email == 'deleted@localhost.osem' end private diff --git a/db/migrate/20140730104658_migrate_roles_for_cancancan.rb b/db/migrate/20140730104658_migrate_roles_for_cancancan.rb index ed76b7c0..adbe9d05 100644 --- a/db/migrate/20140730104658_migrate_roles_for_cancancan.rb +++ b/db/migrate/20140730104658_migrate_roles_for_cancancan.rb @@ -8,7 +8,7 @@ class MigrateRolesForCancancan < ActiveRecord::Migration Role.all.each do |role| role.users.each do |user| Conference.all.each do |conference| - if role.name == 'Admin' || role.name == 'Organizer' + if ['Admin', 'Organizer'].include?(role.name) user.add_role :organizer, conference user.update_columns(is_admin: true) else diff --git a/db/migrate/20170531094819_move_conferences_to_organizations.rb b/db/migrate/20170531094819_move_conferences_to_organizations.rb index 7f7e776c..01cfa87f 100644 --- a/db/migrate/20170531094819_move_conferences_to_organizations.rb +++ b/db/migrate/20170531094819_move_conferences_to_organizations.rb @@ -13,7 +13,7 @@ class MoveConferencesToOrganizations < ActiveRecord::Migration add_reference :conferences, :organization, index: true TempConference.reset_column_information - if TempConference.count != 0 + if TempConference.any? organization = TempOrganization.create(name: 'organization', description: 'Default organization') TempConference.all.each do |conference| conference.organization_id = organization.id diff --git a/lib/tasks/data_demo.rake b/lib/tasks/data_demo.rake index ae57b717..0e5450b4 100644 --- a/lib/tasks/data_demo.rake +++ b/lib/tasks/data_demo.rake @@ -5,6 +5,7 @@ namespace :data do task demo: :environment do include FactoryBot::Syntax::Methods + conference = create(:full_conference, title: 'Open Source Event Manager Demo', short_title: 'osemdemo' ,description: "This is a [Open Source Event Manager](http://osem.io/) demo instance. You can log in as **admin** with the password **password123** or just you just [sign up](/accounts/sign_up) with your own user. We hope you enjoy checking out all the functionality, if you have questions don't hesitate to [contact us](http://osem.io/#contact)!\r\n\r\n## Data will be destroyed every thirty minutes or whenever someone updates the [OSEM source code on github](https://github.com/openSUSE/osem/commits/master).") conference.contact.update(email: 'osemdemo@osem.io', sponsor_email: 'osemdemo@osem.io') create(:admin, email: 'admin@osem.io', username: 'admin', password: 'password123', password_confirmation: 'password123') diff --git a/lib/tasks/events_registrations.rake b/lib/tasks/events_registrations.rake index 171f4b25..ccab6e0c 100644 --- a/lib/tasks/events_registrations.rake +++ b/lib/tasks/events_registrations.rake @@ -7,13 +7,13 @@ 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.any? puts "With IDs: #{duplicates}" end EventsRegistration.all.each do |er| records = EventsRegistration.where(registration_id: er.registration_id, event_id: er.event_id) - if records.count > 1 + if records.many? # Iterate through duplicates (excluding 1st record) (1..(records.count - 1)).each do |i| puts "Deleting EventsRegistration record with ID #{records[i].id} ..." diff --git a/spec/mailers/mailbot_spec.rb b/spec/mailers/mailbot_spec.rb index 39aaf3cf..afcdb0b0 100644 --- a/spec/mailers/mailbot_spec.rb +++ b/spec/mailers/mailbot_spec.rb @@ -32,7 +32,7 @@ describe Mailbot do end describe '.registration_mail' do - include_examples 'mailer actions' do + it_behaves_like 'mailer actions' do let(:mail) { Mailbot.registration_mail(conference, user).deliver_now } end end @@ -44,7 +44,7 @@ describe Mailbot do accepted_body: 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit') end - include_examples 'mailer actions' do + it_behaves_like 'mailer actions' do let(:mail) { Mailbot.acceptance_mail(event).deliver_now } end end @@ -56,7 +56,7 @@ describe Mailbot do rejected_body: 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit') end - include_examples 'mailer actions' do + it_behaves_like 'mailer actions' do let(:mail) { Mailbot.rejection_mail(event).deliver_now } end end @@ -68,7 +68,7 @@ describe Mailbot do confirmed_without_registration_body: 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit') end - include_examples 'mailer actions' do + it_behaves_like 'mailer actions' do let(:mail) { Mailbot.confirm_reminder_mail(event).deliver_now } end end diff --git a/spec/support/login_macros.rb b/spec/support/login_macros.rb index 7557c14d..96237512 100644 --- a/spec/support/login_macros.rb +++ b/spec/support/login_macros.rb @@ -2,6 +2,7 @@ module LoginMacros include Warden::Test::Helpers + Warden.test_mode! def sign_in(user)