Skip to content

Commit

Permalink
add string presence type for optional unique columns
Browse files Browse the repository at this point in the history
  • Loading branch information
senid231 committed Nov 17, 2019
1 parent 4dbabb9 commit 428ce8e
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 0 deletions.
3 changes: 3 additions & 0 deletions app/models/employee.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
class Employee < ApplicationRecord
include ActsAsLoginable

attribute :inn, :string_presence
attribute :passport_number, :string_presence

validates :first_name, :last_name, presence: true
validates :inn, :passport_number, uniqueness: true, allow_blank: true

Expand Down
4 changes: 4 additions & 0 deletions app/models/student.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
class Student < ApplicationRecord
include ActsAsLoginable

attribute :inn, :string_presence
attribute :passport_number, :string_presence
attribute :ticket_number, :string_presence

validates :first_name, :last_name, presence: true
validates :inn, :passport_number, :ticket_number, uniqueness: true, allow_blank: true

Expand Down
1 change: 1 addition & 0 deletions config/initializers/active_record.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require 'custom_types/string_presence_type'
10 changes: 10 additions & 0 deletions lib/custom_types/string_presence_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

class StringPresenceType < ActiveRecord::Type::String
def cast_value(value)
value = value.presence if value.is_a?(String)
super(value)
end
end

ActiveRecord::Type.register :string_presence, StringPresenceType
14 changes: 14 additions & 0 deletions spec/models/employee_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,20 @@
include_examples :changes_records_count_of, described_class, by: 1
include_examples :changes_records_count_of, LoginRecord, by: 1

context 'with inn and passport_number as empty strings' do
let(:create_params) do
super().merge inn: '', passport_number: ''
end

include_examples :creates_record do
let(:expected_record_attrs) do
create_params.except(:login_record).merge(inn: nil, passport_number: '', allowed_services: match_array([1, 2]))
end
end
include_examples :changes_records_count_of, described_class, by: 1
include_examples :changes_records_count_of, LoginRecord, by: 1
end

context 'with optional attributes' do
let(:create_params) do
super().merge middle_name: 'Jamesovich',
Expand Down
19 changes: 19 additions & 0 deletions spec/models/student_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,25 @@
include_examples :changes_records_count_of, described_class, by: 1
include_examples :changes_records_count_of, LoginRecord, by: 1

context 'with inn, ticket_number and passport_number as empty strings' do
let(:create_params) do
super().merge inn: '', passport_number: '', ticket_number: ''
end

include_examples :creates_record do
let(:expected_record_attrs) do
create_params.except(:login_record).merge(
inn: nil,
passport_number: '',
ticket_number: '',
allowed_services: match_array([1, 2])
)
end
end
include_examples :changes_records_count_of, described_class, by: 1
include_examples :changes_records_count_of, LoginRecord, by: 1
end

context 'with optional attributes' do
let(:create_params) do
super().merge middle_name: 'Jamesovich',
Expand Down

0 comments on commit 428ce8e

Please sign in to comment.