From c4a0dd62ffa6d524f069897bef27fb9214c1896d Mon Sep 17 00:00:00 2001 From: Prommah Date: Thu, 24 Sep 2015 10:19:00 +0100 Subject: [PATCH] Fix styling and tidy validations --- app/models/ban.rb | 11 +++---- app/models/user.rb | 33 +++++++++----------- spec/features/users/user_signs_up_spec.rb | 38 +++++++++++------------ 3 files changed, 39 insertions(+), 43 deletions(-) mode change 100644 => 100755 app/models/ban.rb diff --git a/app/models/ban.rb b/app/models/ban.rb old mode 100644 new mode 100755 index ef68856..2d07a1f --- a/app/models/ban.rb +++ b/app/models/ban.rb @@ -33,14 +33,13 @@ class Ban < ActiveRecord::Base scope :effective, conditions: "expiry > UTC_TIMESTAMP()" scope :ineffective, conditions: "expiry < UTC_TIMESTAMP()" + before_validation :check_user + validate :validate_type validate :validate_ventban - validates_length_of :steamid, maximum: 14, allow_blank: true - validates_format_of :steamid, with: /\A0:[01]:[0-9]{1,10}\Z/, allow_blank: true - validates_format_of :addr, with: /\A([0-9]{1,3}\.){3}[0-9]{1,3}:?[0-9]{0,5}\z/, allow_blank: true - validates_length_of :reason, maximum: 255, allow_nil: true, allow_blank: true - - before_validation :check_user + validates :steamid, length: {maximum: 14}, format: /\A0:[01]:[0-9]{1,10}\Z/, allow_blank: true + validates :addr, format: /\A([0-9]{1,3}\.){3}[0-9]{1,3}:?[0-9]{0,5}\z/, allow_blank: true + validates :reason, length: {maximum: 255}, allow_blank: true belongs_to :user belongs_to :server diff --git a/app/models/user.rb b/app/models/user.rb index 4d50141..13d0407 100755 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -100,24 +100,20 @@ class User < ActiveRecord::Base scope :idle, :conditions => ["lastvisit < ?", 30.minutes.ago.utc] - validates_uniqueness_of :username, :email - validates_uniqueness_of :steamid, :allow_nil => true - validates_length_of :firstname, :in => 1..15, :allow_blank => true - validates_length_of :lastname, :in => 1..25, :allow_blank => true - validates_length_of :username, :in => 2..20 - validates_format_of :username, :with => /\A[A-Za-z0-9_\-\+]{2,20}\Z/ - validates_presence_of :raw_password, :on => :create - validates_length_of :email, :maximum => 50 - validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i - validates_length_of :steamid, :maximum => 14 - validates :steamid, presence: true, on: :create + before_validation :update_password + + validates :username, uniqueness: true, length: {in: 2..20}, format: /\A[A-Za-z0-9_\-\+]{2,20}\Z/ + validates :firstname, length: {in: 1..15}, allow_blank: true + validates :lastname, length: {in: 1..25}, allow_blank: true + validates :raw_password, presence: {on: :create} + validates :email, uniqueness: true, length: {maximum: 50}, format: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i + validates :steamid, uniqueness: {allow_nil: true}, length: {maximum: 14}, presence: {on: :create} validate :validate_steamid - validates_length_of :time_zone, :maximum => 100, :allow_blank => true, :allow_nil => true - validates_inclusion_of [:public_email], :in => [true, false], :allow_nil => true + validates :time_zone, length: {maximum: 100}, allow_blank: true + validates :public_email, inclusion: [true, false], allow_nil: true validate :validate_team before_create :init_variables - before_validation :update_password before_save :correct_steamid_universe @@ -243,15 +239,16 @@ class User < ActiveRecord::Base end def validate_steamid - errors.add :steamid unless self.steamid.nil? || - (m = self.steamid.match(/\A([01]):([01]):(\d{1,10})\Z/)) && + errors.add :steamid unless + steamid.nil? || + (m = steamid.match(/\A([01]):([01]):(\d{1,10})\Z/)) && (id = m[3].to_i) && id >= 1 && id <= 2147483647 end def correct_steamid_universe - if self.steamid.present? - self.steamid[0] = "0" + if steamid.present? + steamid[0] = "0" end end diff --git a/spec/features/users/user_signs_up_spec.rb b/spec/features/users/user_signs_up_spec.rb index ff098f6..2d5b2e7 100755 --- a/spec/features/users/user_signs_up_spec.rb +++ b/spec/features/users/user_signs_up_spec.rb @@ -1,64 +1,64 @@ -require 'spec_helper' +require "spec_helper" -feature 'Visitor signs up', js: :true do +feature "Visitor signs up", js: :true do let(:user) { attributes_for(:user) } before do visit new_user_path end - scenario 'with valid Username, Email, Password and Steam ID' do + scenario "with valid Username, Email, Password and Steam ID" do within registration_form do fill_form(:user, user.slice(*sign_up_attributes)) click_button submit(:user, :create) end - expect(user_status).to have_content('ACCOUNT') + expect(user_status).to have_content("ACCOUNT") end - scenario 'with invalid Email' do + scenario "with invalid Email" do within registration_form do - fill_form(:user, user.slice(*sign_up_attributes).merge({ email: "invalid" })) + fill_form(:user, user.slice(*sign_up_attributes).merge(email: "invalid")) click_button submit(:user, :create) end - expect(page).to have_content(error_message('email.invalid')) + expect(page).to have_content(error_message("email.invalid")) end - scenario 'with blank Password' do + scenario "with blank Password" do within registration_form do - fill_form(:user, user.slice(*sign_up_attributes).merge({ raw_password: "" })) + fill_form(:user, user.slice(*sign_up_attributes).merge(raw_password: "")) click_button submit(:user, :create) end - expect(page).to have_content(error_message('raw_password.blank')) + expect(page).to have_content(error_message("raw_password.blank")) end - scenario 'with invalid Steam ID' do + scenario "with invalid Steam ID" do within registration_form do - fill_form(:user, user.slice(*sign_up_attributes).merge({ steamid: "invalid" })) + fill_form(:user, user.slice(*sign_up_attributes).merge(steamid: "invalid")) click_button submit(:user, :create) end - expect(page).to have_content(error_message('steamid.invalid')) + expect(page).to have_content(error_message("steamid.invalid")) end - scenario 'with out of range Steam ID' do + scenario "with out of range Steam ID" do within registration_form do - fill_form(:user, user.slice(*sign_up_attributes).merge({ steamid: "0:0:2147483648" })) + fill_form(:user, user.slice(*sign_up_attributes).merge(steamid: "0:0:2147483648")) click_button submit(:user, :create) end - expect(page).to have_content(error_message('steamid.invalid')) + expect(page).to have_content(error_message("steamid.invalid")) end - scenario 'with nil Steam ID' do + scenario "with nil Steam ID" do within registration_form do - fill_form(:user, user.slice(*sign_up_attributes).merge({ steamid: nil })) + fill_form(:user, user.slice(*sign_up_attributes).merge(steamid: nil)) click_button submit(:user, :create) end - expect(page).to have_content(error_message('steamid.invalid')) + expect(page).to have_content(error_message("steamid.invalid")) end def sign_up_attributes