1
0
Fork 0

Move from users to accounts

This commit is contained in:
Alex Kotov 2018-12-02 07:28:34 +05:00
parent 0c724db429
commit 400b0d109c
No known key found for this signature in database
GPG Key ID: 4E831250F47DE154
19 changed files with 81 additions and 58 deletions

View File

@ -13,8 +13,16 @@ class ApplicationController < ActionController::Base
rescue_from ActiveRecord::RecordNotFound, with: :not_found
rescue_from NotAuthorizedError, with: :unauthorized
helper_method :current_account
private
def current_account
@current_account ||= current_user&.account
end
alias pundit_user current_account
def set_raven_context
Raven.user_context id: current_user.id if user_signed_in?
Raven.extra_context params: params.to_unsafe_h, url: request.url

View File

@ -9,7 +9,7 @@ class PassportConfirmationsController < ApplicationController
def create
ActiveRecord::Base.transaction do
ConfirmPassport.call(passport: @passport,
user: current_user).tap do |context|
account: current_account).tap do |context|
authorize_if_present context.passport_confirmation
end
end

View File

@ -10,7 +10,7 @@ class ConfirmPassport
def create_passport_confirmation
passport_confirmation =
context.passport.passport_confirmations.build user: context.user
context.passport.passport_confirmations.build account: context.account
context.fail! passport_confirmation: nil unless passport_confirmation.save

View File

@ -1,4 +1,7 @@
# frozen_string_literal: true
class Account < ApplicationRecord
rolify role_join_table_name: 'account_roles'
has_many :passport_confirmations, dependent: :restrict_with_exception
end

View File

@ -0,0 +1,6 @@
# frozen_string_literal: true
class AccountRole < ApplicationRecord
belongs_to :account
belongs_to :role
end

View File

@ -2,9 +2,9 @@
class PassportConfirmation < ApplicationRecord
belongs_to :passport
belongs_to :user
belongs_to :account
validates :user_id, uniqueness: { scope: :passport_id }
validates :account_id, uniqueness: { scope: :passport_id }
validate :passport_has_image

View File

@ -1,9 +1,9 @@
# frozen_string_literal: true
class Role < ApplicationRecord
has_many :user_roles, dependent: :destroy
has_many :account_roles, dependent: :destroy
has_many :users, through: :user_roles, source: :user
has_many :accounts, through: :account_roles, source: :account
belongs_to :resource, polymorphic: true, optional: true

View File

@ -13,8 +13,4 @@ class User < ApplicationRecord
:trackable,
:validatable,
)
rolify role_join_table_name: 'user_roles'
has_many :passport_confirmations, dependent: :restrict_with_exception
end

View File

@ -1,6 +0,0 @@
# frozen_string_literal: true
class UserRole < ApplicationRecord
belongs_to :user
belongs_to :role
end

View File

@ -1,10 +1,10 @@
# frozen_string_literal: true
class ApplicationPolicy
attr_reader :user, :record
attr_reader :account, :record
def initialize(user, record)
@user = user
def initialize(account, record)
@account = account
@record = record
end
@ -41,14 +41,14 @@ class ApplicationPolicy
# :nocov:
def policy(record)
Pundit.policy user, record
Pundit.policy account, record
end
class Scope
attr_reader :user, :scope
attr_reader :account, :scope
def initialize(user, scope)
@user = user
def initialize(account, scope)
@account = account
@scope = scope
end

View File

@ -3,7 +3,7 @@
class PassportConfirmationPolicy < ApplicationPolicy
def create?
return false if record.passport.nil?
return false if record.user != user
return false if record.account != account
policy(record.passport).show?
end

View File

@ -7,8 +7,8 @@
<hr/>
<% if @passport.image && user_signed_in? %>
<% if @passport.passport_confirmations.where(user: current_user).exists? %>
<% if @passport.image && current_account %>
<% if @passport.passport_confirmations.where(account: current_account).exists? %>
<div role="alert" class="alert alert-success">
<%= translate '.instruction_confirmed' %>
</div>

View File

@ -0,0 +1,16 @@
# frozen_string_literal: true
class MoveFromUsersToAccounts < ActiveRecord::Migration[5.2]
def change
remove_foreign_key :passport_confirmations, :users
remove_foreign_key :user_roles, :users
rename_table :user_roles, :account_roles
rename_column :passport_confirmations, :user_id, :account_id
rename_column :account_roles, :user_id, :account_id
add_foreign_key :passport_confirmations, :accounts
add_foreign_key :account_roles, :accounts
end
end

View File

@ -10,11 +10,21 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2018_12_02_020031) do
ActiveRecord::Schema.define(version: 2018_12_02_021854) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "account_roles", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.bigint "account_id", null: false
t.bigint "role_id", null: false
t.index ["account_id", "role_id"], name: "index_account_roles_on_account_id_and_role_id", unique: true
t.index ["account_id"], name: "index_account_roles_on_account_id"
t.index ["role_id"], name: "index_account_roles_on_role_id"
end
create_table "accounts", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
@ -69,10 +79,10 @@ ActiveRecord::Schema.define(version: 2018_12_02_020031) do
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.bigint "passport_id", null: false
t.bigint "user_id", null: false
t.index ["passport_id", "user_id"], name: "index_passport_confirmations_on_passport_id_and_user_id", unique: true
t.bigint "account_id", null: false
t.index ["account_id"], name: "index_passport_confirmations_on_account_id"
t.index ["passport_id", "account_id"], name: "index_passport_confirmations_on_passport_id_and_account_id", unique: true
t.index ["passport_id"], name: "index_passport_confirmations_on_passport_id"
t.index ["user_id"], name: "index_passport_confirmations_on_user_id"
end
create_table "passports", force: :cascade do |t|
@ -109,16 +119,6 @@ ActiveRecord::Schema.define(version: 2018_12_02_020031) do
t.string "api_token", null: false
end
create_table "user_roles", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.bigint "user_id", null: false
t.bigint "role_id", null: false
t.index ["role_id"], name: "index_user_roles_on_role_id"
t.index ["user_id", "role_id"], name: "index_user_roles_on_user_id_and_role_id", unique: true
t.index ["user_id"], name: "index_user_roles_on_user_id"
end
create_table "users", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
@ -145,9 +145,9 @@ ActiveRecord::Schema.define(version: 2018_12_02_020031) do
t.index ["unlock_token"], name: "index_users_on_unlock_token", unique: true
end
add_foreign_key "account_roles", "accounts"
add_foreign_key "account_roles", "roles"
add_foreign_key "membership_applications", "country_states"
add_foreign_key "passport_confirmations", "accounts"
add_foreign_key "passport_confirmations", "passports"
add_foreign_key "passport_confirmations", "users"
add_foreign_key "user_roles", "roles"
add_foreign_key "user_roles", "users"
end

View File

@ -3,6 +3,6 @@
FactoryBot.define do
factory :passport_confirmation do
association :passport, factory: :passport_with_image
association :user
association :account
end
end

View File

@ -3,17 +3,17 @@
require 'rails_helper'
RSpec.describe ConfirmPassport do
subject { described_class.call passport: passport, user: user }
subject { described_class.call passport: passport, account: account }
let!(:passport) { create :passport_with_image }
let!(:user) { create :user }
let!(:account) { create :account }
specify do
expect(subject).to be_success
end
specify do
expect(subject).to have_attributes passport: passport, user: user
expect(subject).to have_attributes passport: passport, account: account
end
specify do
@ -22,7 +22,7 @@ RSpec.describe ConfirmPassport do
specify do
expect(subject.passport_confirmation).to \
have_attributes passport: passport, user: user
have_attributes passport: passport, account: account
end
specify do
@ -44,7 +44,7 @@ RSpec.describe ConfirmPassport do
specify do
expect(subject).to have_attributes(
passport: passport,
user: user,
account: account,
passport_confirmation: nil,
)
end

View File

@ -5,5 +5,9 @@ require 'rails_helper'
RSpec.describe Account do
subject { create :account }
pending "add some examples to (or delete) #{__FILE__}"
it do
is_expected.to \
have_many(:passport_confirmations)
.dependent(:restrict_with_exception)
end
end

View File

@ -6,13 +6,13 @@ RSpec.describe PassportConfirmation do
subject { create :passport_confirmation }
it { is_expected.to belong_to(:passport).required }
it { is_expected.to belong_to(:user).required }
it { is_expected.to belong_to(:account).required }
it { is_expected.to validate_presence_of(:passport).with_message(:required) }
it { is_expected.to validate_presence_of(:user).with_message(:required) }
it { is_expected.to validate_presence_of(:account).with_message(:required) }
it do
is_expected.to validate_uniqueness_of(:user_id).scoped_to(:passport_id)
is_expected.to validate_uniqueness_of(:account_id).scoped_to(:passport_id)
end
it do

View File

@ -5,9 +5,5 @@ require 'rails_helper'
RSpec.describe User do
subject { create :user }
it do
is_expected.to \
have_many(:passport_confirmations)
.dependent(:restrict_with_exception)
end
pending "add some examples to (or delete) #{__FILE__}"
end