From 02059ad2a8070dbc95e408547297e267bc41627c Mon Sep 17 00:00:00 2001 From: Petko Bordjukov Date: Thu, 7 Aug 2014 14:05:13 +0300 Subject: [PATCH] Introduce SpeakerProfile All the personal data of the user that is not used for system tasks (such as login) is held here. --- app/models/speaker_profile.rb | 9 ++++++++ app/models/user.rb | 1 + .../20140807103632_create_speaker_profiles.rb | 17 ++++++++++++++ ...move_speaker_profile_columns_from_users.rb | 13 +++++++++++ ..._add_user_reference_to_speaker_profiles.rb | 5 ++++ spec/factories/speaker_profiles.rb | 16 +++++++++++++ spec/factories/users.rb | 3 --- spec/models/speaker_profile_spec.rb | 23 +++++++++++++++++++ 8 files changed, 84 insertions(+), 3 deletions(-) create mode 100644 app/models/speaker_profile.rb create mode 100644 db/migrate/20140807103632_create_speaker_profiles.rb create mode 100644 db/migrate/20140807103805_remove_speaker_profile_columns_from_users.rb create mode 100644 db/migrate/20140807104059_add_user_reference_to_speaker_profiles.rb create mode 100644 spec/factories/speaker_profiles.rb create mode 100644 spec/models/speaker_profile_spec.rb diff --git a/app/models/speaker_profile.rb b/app/models/speaker_profile.rb new file mode 100644 index 0000000..d7f3414 --- /dev/null +++ b/app/models/speaker_profile.rb @@ -0,0 +1,9 @@ +class SpeakerProfile < ActiveRecord::Base + belongs_to :user + + validates :first_name, presence: true + validates :last_name, presence: true + validates :photo_url, presence: true + validates :mobile_phone, presence: true + validates :biography, presence: true +end diff --git a/app/models/user.rb b/app/models/user.rb index 99b38f5..58df7b8 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -3,4 +3,5 @@ class User < ActiveRecord::Base # :lockable, :timeoutable and :omniauthable devise :database_authenticatable, :registerable, :confirmable, :recoverable, :rememberable, :trackable, :validatable + has_one :speaker_profile end diff --git a/db/migrate/20140807103632_create_speaker_profiles.rb b/db/migrate/20140807103632_create_speaker_profiles.rb new file mode 100644 index 0000000..286e036 --- /dev/null +++ b/db/migrate/20140807103632_create_speaker_profiles.rb @@ -0,0 +1,17 @@ +class CreateSpeakerProfiles < ActiveRecord::Migration + def change + create_table :speaker_profiles do |t| + t.string :first_name, null: false + t.string :last_name, null: false + t.string :organisation + t.string :public_email + t.string :photo_url, null: false + t.string :mobile_phone, null: false + t.text :biography, null: false + t.string :github, null: false + t.string :twitter, null: false + + t.timestamps + end + end +end diff --git a/db/migrate/20140807103805_remove_speaker_profile_columns_from_users.rb b/db/migrate/20140807103805_remove_speaker_profile_columns_from_users.rb new file mode 100644 index 0000000..0845879 --- /dev/null +++ b/db/migrate/20140807103805_remove_speaker_profile_columns_from_users.rb @@ -0,0 +1,13 @@ +class RemoveSpeakerProfileColumnsFromUsers < ActiveRecord::Migration + def change + remove_column :users, :first_name, :string + remove_column :users, :last_name, :string + remove_column :users, :organisation, :string + remove_column :users, :hide_email, :boolean + remove_column :users, :photo_url, :string + remove_column :users, :mobile_phone, :string + remove_column :users, :biography, :text + remove_column :users, :github, :string + remove_column :users, :twitter, :string + end +end diff --git a/db/migrate/20140807104059_add_user_reference_to_speaker_profiles.rb b/db/migrate/20140807104059_add_user_reference_to_speaker_profiles.rb new file mode 100644 index 0000000..fa4ed9c --- /dev/null +++ b/db/migrate/20140807104059_add_user_reference_to_speaker_profiles.rb @@ -0,0 +1,5 @@ +class AddUserReferenceToSpeakerProfiles < ActiveRecord::Migration + def change + add_reference :speaker_profiles, :user, index: true + end +end diff --git a/spec/factories/speaker_profiles.rb b/spec/factories/speaker_profiles.rb new file mode 100644 index 0000000..2e64802 --- /dev/null +++ b/spec/factories/speaker_profiles.rb @@ -0,0 +1,16 @@ +# Read about factories at https://github.com/thoughtbot/factory_girl + +FactoryGirl.define do + factory :speaker_profile do + first_name "John" + last_name "Doe" + organisation "Example Org" + public_email "a@b.com" + photo_url "http://placehold.it/50x50" + mobile_phone "0883444555" + biography "Lorem ipsum" + github "octocat" + twitter "handle" + user + end +end diff --git a/spec/factories/users.rb b/spec/factories/users.rb index 0fd5854..4c3963e 100644 --- a/spec/factories/users.rb +++ b/spec/factories/users.rb @@ -4,11 +4,8 @@ FactoryGirl.define do sequence(:email) { |n| "user-#{n}@example.org" } factory :user do - first_name 'John' - last_name 'Doe' password 'password' password_confirmation 'password' - mobile_phone '0883444555' email end end diff --git a/spec/models/speaker_profile_spec.rb b/spec/models/speaker_profile_spec.rb new file mode 100644 index 0000000..4272afd --- /dev/null +++ b/spec/models/speaker_profile_spec.rb @@ -0,0 +1,23 @@ +require 'rails_helper' + +RSpec.describe SpeakerProfile, :type => :model do + it 'is invalid without a first name' do + expect(build(:speaker_profile, first_name: nil)).to have_error_on :first_name + end + + it 'is invalid without a last name' do + expect(build(:speaker_profile, last_name: nil)).to have_error_on :last_name + end + + it 'is invalid without a photo' do + expect(build(:speaker_profile, photo_url: nil)).to have_error_on :photo_url + end + + it 'is invalid without a mobile phone' do + expect(build(:speaker_profile, mobile_phone: nil)).to have_error_on :mobile_phone + end + + it 'is invalid without a bio' do + expect(build(:speaker_profile, biography: nil)).to have_error_on :biography + end +end