pg_rails 7.5.5 → 7.5.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6147cb183d30183dd5d8a80431d86acf783df0d94b0521a3cd68d25a71e75feb
4
- data.tar.gz: 2a25529a08aa2883675013101c688ef2d2afd9e84294438e67aac9393fcfe3c9
3
+ metadata.gz: b62210d098125764e407b10038d44a3e3fe91a52f6eff700fd83555dded8d194
4
+ data.tar.gz: 4e9ac223dd652c00045af466a82af01ea07062f8968947286c304014ea3cb5ac
5
5
  SHA512:
6
- metadata.gz: 4f3e802e2f8f75c265744ee30602c192a4e7ebbee6551295ef140e5d0af978ce5729bb9520cc98557518961bb0c2805252797de899fadafbdec183623822c8c1
7
- data.tar.gz: 15ccca5fb52f689ce26402d75fcbf3b7794f8bf2b374c15e5d4ca385b012c17c4228bb45c475a5dc43ee449403eedaab4c95f17ebb1f9c1b8326124870b37af4
6
+ metadata.gz: b4cb69cca012bf23c8e1229353cd8b346150bc3029b9e45e995ce2f8ccb74d375c4019ce50ca894aaa9ef7cb5036781dcec219fa8ce108b64264e786d7467cda
7
+ data.tar.gz: 074b884d3867fffc9a1835aa59ca9c69dc9b64c60042d49151bbc9b60bad72d12488964be429e4eb21626dcb251b6ae8ed4b61d58a40f62cc7d845a12118e72b
@@ -9,6 +9,12 @@ module Users
9
9
 
10
10
  layout 'pg_layout/centered'
11
11
 
12
+ around_action :set_without_tenant
13
+
14
+ def set_without_tenant(&)
15
+ ActsAsTenant.without_tenant(&)
16
+ end
17
+
12
18
  def list
13
19
  @user_accounts = Current.user.user_accounts.kept
14
20
  end
@@ -7,15 +7,19 @@ module Users
7
7
  def create
8
8
  build_resource(sign_up_params)
9
9
 
10
- resource.save
11
- yield resource if block_given?
12
- if resource.persisted?
13
- expire_data_after_sign_in!
14
- render_message
15
- else
16
- clean_up_passwords resource
17
- set_minimum_password_length
18
- respond_with resource
10
+ ActiveRecord::Base.transaction do
11
+ resource.save
12
+ yield resource if block_given?
13
+ if resource.persisted?
14
+ create_account_for(resource)
15
+
16
+ expire_data_after_sign_in!
17
+ render_message
18
+ else
19
+ clean_up_passwords resource
20
+ set_minimum_password_length
21
+ respond_with resource
22
+ end
19
23
  end
20
24
  end
21
25
 
@@ -27,5 +31,12 @@ module Users
27
31
  HTML
28
32
  render turbo_stream: turbo_stream.update('form-signup', msg)
29
33
  end
34
+
35
+ private
36
+
37
+ def create_account_for(user)
38
+ account = Account.create!(nombre: user.email)
39
+ user.user_accounts.create!(account:)
40
+ end
30
41
  end
31
42
  end
@@ -38,6 +38,10 @@ class Account < ApplicationRecord
38
38
  parent.table[:nombre]
39
39
  end
40
40
 
41
+ before_validation do
42
+ self.plan = 0 if plan.blank?
43
+ end
44
+
41
45
  def to_s
42
46
  nombre
43
47
  end
@@ -41,17 +41,7 @@ class User < ApplicationRecord
41
41
  # Hace falta?
42
42
  has_many :accounts, through: :user_accounts
43
43
 
44
- # default_scope lambda {
45
- # if ActsAsTenant.current_tenant.present?
46
- # joins(:user_accounts).where('user_accounts.account_id': ActsAsTenant.current_tenant.id)
47
- # else
48
- # raise ActsAsTenant::Errors::NoTenantSet unless Current.user.blank? || ActsAsTenant.unscoped?
49
-
50
- # # Aún no autenticó devise o es el admin
51
- # all
52
-
53
- # end
54
- # }
44
+ acts_as_tenant :account, through: :user_accounts
55
45
 
56
46
  has_many :notifications, as: :recipient, class_name: 'Noticed::Notification'
57
47
 
@@ -80,26 +70,6 @@ class User < ApplicationRecord
80
70
  kept? ? super : :locked
81
71
  end
82
72
 
83
- after_create do
84
- create_account unless orphan
85
- end
86
-
87
- def create_account
88
- account =
89
- if ActsAsTenant.current_tenant.present?
90
- # :nocov:
91
- raise PgEngine::Error, 'user not invited' unless Rails.env.test?
92
- # :nocov:
93
-
94
- ActsAsTenant.current_tenant
95
- else
96
- Account.create(nombre: email, plan: 0)
97
- end
98
- ua = user_accounts.create(account:)
99
-
100
- raise(Error, 'no se pudo crear la cuenta') unless ua.persisted?
101
- end
102
-
103
73
  def password_required?
104
74
  !persisted? || !password.nil? || !password_confirmation.nil?
105
75
  end
@@ -26,13 +26,14 @@ class UserAccount < ApplicationRecord
26
26
  audited
27
27
  include Hashid::Rails
28
28
 
29
- belongs_to :user
30
- belongs_to :account
29
+ belongs_to :user, inverse_of: :user_accounts
30
+ acts_as_tenant :account
31
31
 
32
32
  belongs_to :creado_por, optional: true, class_name: 'User'
33
33
  belongs_to :actualizado_por, optional: true, class_name: 'User'
34
34
 
35
35
  # scope :kept, -> { undiscarded.joins(:account).merge(Account.kept) }
36
+ # FIXME: merge with User.kept
36
37
  scope :kept, -> { joins(:account).merge(Account.kept) }
37
38
 
38
39
  enumerize :profiles, in: {
@@ -22,7 +22,7 @@ class UserPolicy < ApplicationPolicy
22
22
  user.developer? || user == record
23
23
  end
24
24
 
25
- # def base_access_to_collection?
26
- # user&.present?
27
- # end
25
+ def base_access_to_collection?
26
+ user.present?
27
+ end
28
28
  end
@@ -8,7 +8,7 @@ div style="max-width: 30em"
8
8
  th Nombre
9
9
  th Email
10
10
  th Roles
11
- - @account.user_accounts.each do |user_account|
11
+ - @account.user_accounts.kept.each do |user_account|
12
12
  tr
13
13
  td = user_account.user
14
14
  td = user_account.user.email
@@ -1,6 +1,6 @@
1
1
  ActsAsTenant.configure do |config|
2
- config.require_tenant = lambda do
3
- Current.namespace == :users
2
+ config.require_tenant = lambda do |options|
3
+ Current.namespace.present? || !options[:model].in?([User, UserAccount, Email, EmailLog])
4
4
  end
5
5
 
6
6
  # Customize the query for loading the tenant in background jobs
@@ -92,9 +92,9 @@ es:
92
92
  new:
93
93
  sign_up: Crear una cuenta
94
94
  passwords:
95
- send_instructions: Vas a recibir un correo con instrucciones sobre cómo resetear tu contraseña en unos pocos minutos.
95
+ send_instructions: Vas a recibir un correo con instrucciones para restablecer tu contraseña en unos pocos minutos.
96
96
  new:
97
- send_me_reset_password_instructions: Enviar instrucciones para restablecer contraseña
97
+ send_me_reset_password_instructions: Restablecer contraseña
98
98
  forgot_your_password: ¿Olvidaste tu contraseña?
99
99
  shared:
100
100
  links:
@@ -38,7 +38,7 @@ RSpec.describe Admin::AccountsController do
38
38
 
39
39
  let(:invalid_attributes) do
40
40
  {
41
- plan: nil
41
+ nombre: nil
42
42
  }
43
43
  end
44
44
 
@@ -96,20 +96,24 @@ RSpec.describe Admin::UsersController do
96
96
 
97
97
  describe 'POST #create' do
98
98
  context 'with valid params' do
99
+ subject do
100
+ post :create, params: { user: valid_attributes }
101
+ end
102
+
99
103
  it 'creates a new User' do
100
- expect do
101
- post :create, params: { user: valid_attributes }
102
- end.to change(User.unscoped, :count).by(1)
104
+ expect { subject }.to change(User.unscoped, :count).by(1)
103
105
  end
104
106
 
105
107
  it 'dont creates a new account' do
106
- expect do
107
- post :create, params: { user: valid_attributes }
108
- end.not_to change(Account.unscoped, :count)
108
+ expect { subject }.not_to change(Account.unscoped, :count)
109
+ end
110
+
111
+ it 'dont creates a new user account' do
112
+ expect { subject }.not_to change(UserAccount.unscoped, :count)
109
113
  end
110
114
 
111
115
  it 'redirects to the created user' do
112
- post :create, params: { user: valid_attributes }
116
+ subject
113
117
  expect(response).to redirect_to([:admin, User.unscoped.last])
114
118
  end
115
119
  end
Binary file
@@ -15,52 +15,34 @@ RSpec.describe User do
15
15
  expect(user.default_account).to be_present
16
16
  end
17
17
 
18
- context 'si es orphan' do
19
- let(:user) { create(:user, orphan: true) }
20
-
21
- it do
22
- expect(user.accounts).to be_empty
23
- end
24
-
25
- it do
26
- expect { user.default_account }.to raise_error(User::Error)
27
- end
28
- end
29
-
30
- context 'Si falla la creación de cuenta, que rollbackee la transaction de create user' do
31
- # rubocop:disable Lint/SuppressedException
32
- subject do
33
- user.save
34
- rescue User::Error
35
- end
36
- # rubocop:enable Lint/SuppressedException
37
-
38
- let(:user) do
39
- build(:user)
40
- end
41
-
42
- before do
43
- # rubocop:disable RSpec/MessageChain
44
- allow(user).to receive_message_chain(:user_accounts, :create) {
45
- instance_double(UserAccount, persisted?: false)
46
- }
47
- # rubocop:enable RSpec/MessageChain
48
- end
49
-
50
- it do
51
- expect { subject }.not_to change(described_class, :count)
52
- end
53
-
54
- it do
55
- subject
56
- expect(user).not_to be_persisted
57
- end
58
- end
59
-
60
18
  describe 'search ransacker' do
61
19
  it 'searchs' do
62
20
  results = described_class.ransack(search_cont: user.nombre).result.to_a
63
21
  expect(results).to eq [user]
64
22
  end
65
23
  end
24
+
25
+ describe 'default scope' do
26
+ before do
27
+ ActsAsTenant.current_tenant = nil
28
+ ActsAsTenant.test_tenant = nil
29
+ Current.reset
30
+ end
31
+
32
+ it 'scopes according to tenant' do
33
+ account = create(:account)
34
+ other_account = create(:account)
35
+ usr1 = usr2 = usr3 = usr4 = nil
36
+ ActsAsTenant.with_tenant(other_account) do
37
+ usr3 = create(:user)
38
+ usr4 = create(:user)
39
+ end
40
+ ActsAsTenant.with_tenant(account) do
41
+ usr1 = create(:user)
42
+ usr2 = create(:user)
43
+ expect(described_class.all).to contain_exactly(usr1, usr2)
44
+ end
45
+ expect(described_class.all).to contain_exactly(usr1, usr2, usr3, usr4)
46
+ end
47
+ end
66
48
  end
@@ -1,16 +1,8 @@
1
1
  require 'rails_helper'
2
2
 
3
- describe Users::RegistrationsController do
4
- render_views
5
-
6
- before do
7
- # rubocop:disable RSpec/InstanceVariable
8
- @request.env['devise.mapping'] = Devise.mappings[:user]
9
- # rubocop:enable RSpec/InstanceVariable
10
- end
11
-
3
+ describe 'registrations controller' do
12
4
  describe '#new' do
13
- subject { get :new }
5
+ subject { get '/users/sign_up' }
14
6
 
15
7
  it do
16
8
  subject
@@ -20,7 +12,12 @@ describe Users::RegistrationsController do
20
12
 
21
13
  describe '#create' do
22
14
  subject do
23
- post :create, params: { user: { nombre:, apellido:, email:, password:, password_confirmation: } }
15
+ post '/users', params: { user: { nombre:, apellido:, email:, password:, password_confirmation: } }
16
+ end
17
+
18
+ before do
19
+ ActsAsTenant.current_tenant = nil
20
+ ActsAsTenant.test_tenant = nil
24
21
  end
25
22
 
26
23
  let(:nombre) { Faker::Name.first_name }
@@ -33,6 +30,14 @@ describe Users::RegistrationsController do
33
30
  expect { subject }.to change(User, :count).by(1)
34
31
  end
35
32
 
33
+ it do
34
+ expect { subject }.to change(UserAccount, :count).by(1)
35
+ end
36
+
37
+ it do
38
+ expect { subject }.to change(Account, :count).by(1)
39
+ end
40
+
36
41
  it do
37
42
  subject
38
43
  expect(response.body).to include I18n.t('devise.registrations.signed_up_but_unconfirmed')
@@ -46,10 +51,28 @@ describe Users::RegistrationsController do
46
51
  expect(response).not_to be_successful
47
52
  end
48
53
  end
54
+
55
+ context 'cuando falla la creación de la UserAccount' do
56
+ before do
57
+ allow_any_instance_of(Users::RegistrationsController).to receive(:create_account_for).and_raise(StandardError)
58
+ end
59
+
60
+ it do
61
+ expect { subject }.not_to change(User, :count)
62
+ end
63
+
64
+ it do
65
+ expect { subject }.not_to change(UserAccount, :count)
66
+ end
67
+
68
+ it do
69
+ expect { subject }.not_to change(Account, :count)
70
+ end
71
+ end
49
72
  end
50
73
 
51
74
  describe '#edit' do
52
- subject { get :edit }
75
+ subject { get '/users/edit' }
53
76
 
54
77
  let(:logger_user) { create :user, :developer }
55
78
 
@@ -38,11 +38,17 @@ describe 'redirection' do
38
38
 
39
39
  context 'when has been removed from account' do
40
40
  let!(:other_account) { create :account }
41
- let!(:other_user_account) { logged_user.user_accounts.create(account: other_account) }
41
+ let!(:other_user_account) do
42
+ ActsAsTenant.with_tenant(other_account) do
43
+ logged_user.user_accounts.create!
44
+ end
45
+ end
42
46
 
43
47
  before do
44
48
  third_account = create :account
45
- logged_user.user_accounts.create(account: third_account)
49
+ ActsAsTenant.with_tenant(third_account) do
50
+ logged_user.user_accounts.create!
51
+ end
46
52
  end
47
53
 
48
54
  it 'redirects to switcher' do
@@ -29,7 +29,9 @@ describe 'Tenants' do
29
29
  let(:other_account) { create :account }
30
30
 
31
31
  before do
32
- logged_user.user_accounts.create!(account: other_account)
32
+ ActsAsTenant.with_tenant(other_account) do
33
+ logged_user.user_accounts.create!
34
+ end
33
35
  end
34
36
 
35
37
  it 'shows the switcher' do
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module PgRails
4
- VERSION = '7.5.5'
4
+ VERSION = '7.5.6'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pg_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 7.5.5
4
+ version: 7.5.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Martín Rosso
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-09-14 00:00:00.000000000 Z
11
+ date: 2024-09-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -849,7 +849,6 @@ files:
849
849
  - pg_engine/spec/controllers/public/mensaje_contactos_controller_spec.rb
850
850
  - pg_engine/spec/controllers/public/webhooks_controller_spec.rb
851
851
  - pg_engine/spec/controllers/users/confirmations_controller_spec.rb
852
- - pg_engine/spec/controllers/users/registrations_controller_spec.rb
853
852
  - pg_engine/spec/decorators/email_decorator_spec.rb
854
853
  - pg_engine/spec/factories/accounts.rb
855
854
  - pg_engine/spec/factories/email_logs.rb
@@ -883,6 +882,7 @@ files:
883
882
  - pg_engine/spec/requests/users/accounts_spec.rb
884
883
  - pg_engine/spec/requests/users/base_controller_spec.rb
885
884
  - pg_engine/spec/requests/users/date_jumper_spec.rb
885
+ - pg_engine/spec/requests/users/registrations_spec.rb
886
886
  - pg_engine/spec/requests/users/switcher_spec.rb
887
887
  - pg_engine/spec/system/alerts_spec.rb
888
888
  - pg_engine/spec/system/breadcrumbs_spec.rb