Add association Account#person
This commit is contained in:
parent
7252a6d021
commit
fda65f1f98
7 changed files with 31 additions and 3 deletions
|
@ -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
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class Person < ApplicationRecord
|
||||
has_one :account, dependent: :restrict_with_exception
|
||||
end
|
||||
|
|
7
db/migrate/20181210033307_add_person_to_accounts.rb
Normal file
7
db/migrate/20181210033307_add_person_to_accounts.rb
Normal file
|
@ -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
|
|
@ -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"
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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'
|
||||
|
|
|
@ -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
|
||||
|
|
Reference in a new issue