diff --git a/.rubocop.yml b/.rubocop.yml index bf158032..0ad83c9d 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -160,6 +160,10 @@ Lint/Void: #################### Rails ############################### -# Enforce Rails specific style +# Enforce Rails specific style Rails: Enabled: true + +# Avoid use of old-style attribute validation +Rails/Validation: + Enabled: true diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index a0487443..2ada8e94 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -141,23 +141,6 @@ Rails/TimeZone: - 'spec/models/campaign_spec.rb' - 'spec/models/conference_spec.rb' -# Offense count: 16 -# Configuration parameters: Include. -# Include: app/models/**/*.rb -Rails/Validation: - Exclude: - - 'app/models/call_for_paper.rb' - - 'app/models/comment.rb' - - 'app/models/conference.rb' - - 'app/models/photo.rb' - - 'app/models/registration.rb' - - 'app/models/sponsor.rb' - - 'app/models/sponsorship_level.rb' - - 'app/models/subscription.rb' - - 'app/models/ticket.rb' - - 'app/models/ticket_purchase.rb' - - 'app/models/vposition.rb' - # Offense count: 18 Style/AccessorMethodName: Exclude: diff --git a/app/models/comment.rb b/app/models/comment.rb index 00751bb8..947b2f54 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -1,7 +1,7 @@ class Comment < ActiveRecord::Base - acts_as_nested_set scope: [:commentable_id, :commentable_type] - validates_presence_of :body - validates_presence_of :user + acts_as_nested_set scope: %i(commentable_id commentable_type) + validates :body, presence: true + validates :user, presence: true after_create :send_notification # NOTE: install the acts_as_votable plugin if you @@ -13,7 +13,7 @@ class Comment < ActiveRecord::Base # NOTE: Comments belong to a user belongs_to :user - has_paper_trail on: [:create, :destroy], meta: { conference_id: :conference_id } + has_paper_trail on: %i(create destroy), meta: { conference_id: :conference_id } # Helper class method that allows you to build a comment # by passing a commentable object, a user_id, and comment text diff --git a/app/models/conference.rb b/app/models/conference.rb index 1d549ec7..e3b1d3dd 100644 --- a/app/models/conference.rb +++ b/app/models/conference.rb @@ -7,7 +7,7 @@ class Conference < ActiveRecord::Base default_scope { order('start_date DESC') } - has_paper_trail ignore: [:updated_at, :guid, :revision, :events_per_week], meta: { conference_id: :id } + has_paper_trail ignore: %i(updated_at guid revision events_per_week), meta: { conference_id: :id } has_and_belongs_to_many :questions @@ -48,15 +48,15 @@ class Conference < ActiveRecord::Base mount_uploader :picture, PictureUploader, mount_on: :logo_file_name - validates_presence_of :title, - :short_title, - :start_date, - :end_date, - :start_hour, - :end_hour + validates :title, + :short_title, + :start_date, + :end_date, + :start_hour, + :end_hour, presence: true - validates_uniqueness_of :short_title - validates_format_of :short_title, with: /\A[a-zA-Z0-9_-]*\z/ + validates :short_title, uniqueness: true + validates :short_title, format: { with: /\A[a-zA-Z0-9_-]*\z/ } validates :registration_limit, numericality: { only_integer: true, greater_than_or_equal_to: 0 } # This validation is needed since a conference with a start date greater than the end date is not possible @@ -559,7 +559,7 @@ class Conference < ActiveRecord::Base # do not notify unless one of the dates changed return false unless start_date_changed? || end_date_changed? # do not notify unless the mail content is set up - (!email_settings.conference_dates_updated_subject.blank? && !email_settings.conference_dates_updated_body.blank?) + (email_settings.conference_dates_updated_subject.present? && email_settings.conference_dates_updated_body.present?) end ## @@ -575,7 +575,7 @@ class Conference < ActiveRecord::Base # do not notify unless one of the dates changed return false unless registration_period.start_date_changed? || registration_period.end_date_changed? # do not notify unless the mail content is set up - (!email_settings.conference_registration_dates_updated_subject.blank? && !email_settings.conference_registration_dates_updated_body.blank?) + (email_settings.conference_registration_dates_updated_subject.present? && email_settings.conference_registration_dates_updated_body.present?) end def registration_limit_exceeded? @@ -699,7 +699,7 @@ class Conference < ActiveRecord::Base # Completed weeks events_per_week.each do |week, values| values.each do |state, value| - if [:confirmed, :unconfirmed].include?(state) + if %i(confirmed unconfirmed).include?(state) unless result[state.to_s.capitalize] result[state.to_s.capitalize] = {} end diff --git a/app/models/registration.rb b/app/models/registration.rb index 33507ec1..5d2555f2 100644 --- a/app/models/registration.rb +++ b/app/models/registration.rb @@ -9,7 +9,7 @@ class Registration < ActiveRecord::Base has_many :events_registrations has_many :events, through: :events_registrations, dependent: :destroy - has_paper_trail ignore: [:updated_at, :week], meta: { conference_id: :conference_id } + has_paper_trail ignore: %i(updated_at week), meta: { conference_id: :conference_id } accepts_nested_attributes_for :user accepts_nested_attributes_for :qanswers @@ -24,7 +24,7 @@ class Registration < ActiveRecord::Base validates :user, presence: true - validates_uniqueness_of :user_id, scope: :conference_id, message: 'already Registered!' + validates :user_id, uniqueness: { scope: :conference_id, message: 'already Registered!' } validate :registration_limit_not_exceed, on: :create validate :registration_to_events_only_if_present diff --git a/app/models/sponsor.rb b/app/models/sponsor.rb index 985cef44..df687f20 100644 --- a/app/models/sponsor.rb +++ b/app/models/sponsor.rb @@ -6,5 +6,5 @@ class Sponsor < ActiveRecord::Base mount_uploader :picture, PictureUploader, mount_on: :logo_file_name - validates_presence_of :name, :website_url, :sponsorship_level + validates :name, :website_url, :sponsorship_level, presence: true end diff --git a/app/models/sponsorship_level.rb b/app/models/sponsorship_level.rb index d4630971..da411859 100644 --- a/app/models/sponsorship_level.rb +++ b/app/models/sponsorship_level.rb @@ -1,5 +1,5 @@ class SponsorshipLevel < ActiveRecord::Base - validates_presence_of :title + validates :title, presence: true belongs_to :conference acts_as_list scope: :conference has_many :sponsors diff --git a/app/models/subscription.rb b/app/models/subscription.rb index 1ee97ca6..aa688ae8 100644 --- a/app/models/subscription.rb +++ b/app/models/subscription.rb @@ -1,9 +1,9 @@ class Subscription < ActiveRecord::Base - validates_uniqueness_of :user_id, scope: [:conference_id] + validates :user_id, uniqueness: { scope: [:conference_id] } belongs_to :conference belongs_to :user - has_paper_trail on: [:create, :destroy], ignore: [:updated_at], meta: { conference_id: :conference_id } + has_paper_trail on: %i(create destroy), ignore: [:updated_at], meta: { conference_id: :conference_id } - validates_uniqueness_of :user_id, scope: :conference_id, message: 'already subscribed!' + validates :user_id, uniqueness: { scope: :conference_id, message: 'already subscribed!' } end diff --git a/app/models/ticket.rb b/app/models/ticket.rb index e7af9809..7bac9462 100644 --- a/app/models/ticket.rb +++ b/app/models/ticket.rb @@ -13,7 +13,7 @@ class Ticket < ActiveRecord::Base validates :price_cents, :price_currency, :title, presence: true - validates_numericality_of :price_cents, greater_than_or_equal_to: 0 + validates :price_cents, numericality: { greater_than_or_equal_to: 0 } def bought?(user) buyers.include?(user) diff --git a/app/models/ticket_purchase.rb b/app/models/ticket_purchase.rb index 793f6926..07fb7e16 100644 --- a/app/models/ticket_purchase.rb +++ b/app/models/ticket_purchase.rb @@ -5,7 +5,7 @@ class TicketPurchase < ActiveRecord::Base validates :ticket_id, :user_id, :conference_id, :quantity, presence: true - validates_numericality_of :quantity, greater_than: 0 + validates :quantity, numericality: { greater_than: 0 } delegate :title, to: :ticket delegate :description, to: :ticket @@ -15,8 +15,8 @@ class TicketPurchase < ActiveRecord::Base scope :paid, -> { where(paid: true) } scope :unpaid, -> { where(paid: false) } - scope :by_conference, -> (conference) { where(conference_id: conference.id) } - scope :by_user, -> (user) { where(user_id: user.id) } + scope :by_conference, ->(conference) { where(conference_id: conference.id) } + scope :by_user, ->(user) { where(user_id: user.id) } def self.purchase(conference, user, purchases) errors = [] diff --git a/app/models/vposition.rb b/app/models/vposition.rb index 3220aaa7..8a7dbdf9 100644 --- a/app/models/vposition.rb +++ b/app/models/vposition.rb @@ -4,5 +4,5 @@ class Vposition < ActiveRecord::Base has_many :vchoices has_many :vdays, through: :vchoices - validates_presence_of :title, :vdays + validates :title, :vdays, presence: true end