diff --git a/app/models/account.rb b/app/models/account.rb index fe3866e..c1b7282 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -3,6 +3,8 @@ class Account < ApplicationRecord rolify role_join_table_name: :account_roles + belongs_to :person, optional: true + has_one :user, dependent: :restrict_with_exception has_many :own_membership_apps, @@ -17,6 +19,8 @@ class Account < ApplicationRecord self.guest_token = SecureRandom.hex end + validates :person_id, allow_nil: true, uniqueness: true + def guest? user.nil? end diff --git a/app/models/person.rb b/app/models/person.rb index 5bb341a..d9986e4 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true class Person < ApplicationRecord + has_one :account, dependent: :restrict_with_exception end diff --git a/db/migrate/20181210033307_add_person_to_accounts.rb b/db/migrate/20181210033307_add_person_to_accounts.rb new file mode 100644 index 0000000..34b4dab --- /dev/null +++ b/db/migrate/20181210033307_add_person_to_accounts.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class AddPersonToAccounts < ActiveRecord::Migration[5.2] + def change + add_reference :accounts, :person, index: { unique: true }, foreign_key: true + end +end diff --git a/db/schema.rb b/db/schema.rb index 3804d35..d414af8 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2018_12_10_033105) do +ActiveRecord::Schema.define(version: 2018_12_10_033307) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -29,6 +29,8 @@ ActiveRecord::Schema.define(version: 2018_12_10_033105) do t.datetime "created_at", null: false t.datetime "updated_at", null: false t.string "guest_token", null: false + t.bigint "person_id" + t.index ["person_id"], name: "index_accounts_on_person_id", unique: true end create_table "active_storage_attachments", force: :cascade do |t| @@ -195,6 +197,7 @@ ActiveRecord::Schema.define(version: 2018_12_10_033105) do add_foreign_key "account_roles", "accounts" add_foreign_key "account_roles", "roles" + add_foreign_key "accounts", "people" add_foreign_key "membership_apps", "accounts" add_foreign_key "membership_apps", "country_states" add_foreign_key "passport_confirmations", "accounts" diff --git a/factories/accounts.rb b/factories/accounts.rb index 52e1b80..69faf96 100644 --- a/factories/accounts.rb +++ b/factories/accounts.rb @@ -7,7 +7,11 @@ FactoryBot.define do association :user end - factory :superuser_account, parent: :usual_account do + factory :personal_account, parent: :usual_account do + association :person + end + + factory :superuser_account, parent: :personal_account do after :create do |account, _evaluator| account.add_role :superuser end diff --git a/spec/models/account_spec.rb b/spec/models/account_spec.rb index 25ceb82..ab042f7 100644 --- a/spec/models/account_spec.rb +++ b/spec/models/account_spec.rb @@ -3,7 +3,9 @@ require 'rails_helper' RSpec.describe Account do - subject { create :usual_account } + subject { create :personal_account } + + it { is_expected.to belong_to(:person).optional } it do is_expected.to \ @@ -24,6 +26,7 @@ RSpec.describe Account do .dependent(:restrict_with_exception) end + it { is_expected.not_to validate_presence_of :person } it { is_expected.not_to validate_presence_of :user } pending '.guests' diff --git a/spec/models/person_spec.rb b/spec/models/person_spec.rb index de4c7aa..5c88d38 100644 --- a/spec/models/person_spec.rb +++ b/spec/models/person_spec.rb @@ -5,5 +5,11 @@ require 'rails_helper' RSpec.describe Person do subject { create :person } + it do + is_expected.to \ + have_one(:account) + .dependent(:restrict_with_exception) + end + pending "add some examples to (or delete) #{__FILE__}" end