1
0
Fork 0

Add association Account#person

This commit is contained in:
Alex Kotov 2018-12-10 08:36:09 +05:00
parent 7252a6d021
commit fda65f1f98
No known key found for this signature in database
GPG key ID: 4E831250F47DE154
7 changed files with 31 additions and 3 deletions

View file

@ -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

View file

@ -1,4 +1,5 @@
# frozen_string_literal: true
class Person < ApplicationRecord
has_one :account, dependent: :restrict_with_exception
end

View 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

View file

@ -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"

View file

@ -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

View file

@ -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'

View file

@ -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