-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Center on account form * Fix CI and reorder overrides in engine.rb * Account spec * Registration spec * Disable cov on monkey-patched unhappy paths
- Loading branch information
Showing
18 changed files
with
355 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
app/commands/concerns/decidim/centers/create_omniauth_registration_override.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# frozen_string_literal: true | ||
|
||
require "active_support/concern" | ||
|
||
module Decidim | ||
module Centers | ||
module CreateOmniauthRegistrationOverride | ||
extend ActiveSupport::Concern | ||
|
||
include PublishCenterUpdateEvent | ||
|
||
def call | ||
verify_oauth_signature! | ||
|
||
begin | ||
if existing_identity | ||
# :nocov: | ||
user = existing_identity.user | ||
verify_user_confirmed(user) | ||
|
||
return broadcast(:ok, user) | ||
# :nocov: | ||
end | ||
return broadcast(:invalid) if form.invalid? | ||
|
||
transaction do | ||
create_or_find_user | ||
@identity = create_identity | ||
end | ||
trigger_omniauth_registration | ||
publish_center_update_event | ||
|
||
broadcast(:ok, @user) | ||
rescue ActiveRecord::RecordInvalid => e | ||
# :nocov: | ||
broadcast(:error, e.record) | ||
# :nocov: | ||
end | ||
end | ||
end | ||
end | ||
end |
32 changes: 32 additions & 0 deletions
32
app/commands/concerns/decidim/centers/create_registration_override.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# frozen_string_literal: true | ||
|
||
require "active_support/concern" | ||
|
||
module Decidim | ||
module Centers | ||
module CreateRegistrationOverride | ||
extend ActiveSupport::Concern | ||
|
||
include PublishCenterUpdateEvent | ||
|
||
def call | ||
if form.invalid? | ||
# :nocov: | ||
user = User.has_pending_invitations?(form.current_organization.id, form.email) | ||
user.invite!(user.invited_by) if user | ||
return broadcast(:invalid) | ||
# :nocov: | ||
end | ||
|
||
create_user | ||
publish_center_update_event | ||
|
||
broadcast(:ok, @user) | ||
rescue ActiveRecord::RecordInvalid | ||
# :nocov: | ||
broadcast(:invalid) | ||
# :nocov: | ||
end | ||
end | ||
end | ||
end |
19 changes: 19 additions & 0 deletions
19
app/commands/concerns/decidim/centers/publish_center_update_event.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# frozen_string_literal: true | ||
|
||
require "active_support/concern" | ||
|
||
module Decidim | ||
module Centers | ||
module PublishCenterUpdateEvent | ||
extend ActiveSupport::Concern | ||
|
||
def publish_center_update_event | ||
ActiveSupport::Notifications.publish( | ||
"decidim.centers.user.updated", | ||
user_id: @user.id, | ||
center_id: @form.center_id | ||
) | ||
end | ||
end | ||
end | ||
end |
35 changes: 35 additions & 0 deletions
35
app/commands/concerns/decidim/centers/update_account_override.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# frozen_string_literal: true | ||
|
||
require "active_support/concern" | ||
|
||
module Decidim | ||
module Centers | ||
module UpdateAccountOverride | ||
extend ActiveSupport::Concern | ||
|
||
include PublishCenterUpdateEvent | ||
|
||
def call | ||
return broadcast(:invalid) unless @form.valid? | ||
|
||
update_personal_data | ||
update_avatar | ||
update_password | ||
|
||
if @user.valid? | ||
@user.save! | ||
notify_followers | ||
publish_center_update_event | ||
broadcast(:ok, @user.unconfirmed_email.present?) | ||
else | ||
# :nocov: | ||
[:avatar, :password, :password_confirmation].each do |key| | ||
@form.errors.add key, @user.errors[key] if @user.errors.has_key? key | ||
end | ||
broadcast(:invalid) | ||
# :nocov: | ||
end | ||
end | ||
end | ||
end | ||
end |
27 changes: 27 additions & 0 deletions
27
app/forms/concerns/decidim/centers/account_form_override.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# frozen_string_literal: true | ||
|
||
require "active_support/concern" | ||
|
||
module Decidim | ||
module Centers | ||
module AccountFormOverride | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
alias_method :original_map_model, :map_model | ||
|
||
include Decidim::Centers::ApplicationHelper | ||
|
||
attribute :center_id, Integer | ||
|
||
validates :center_id, presence: true | ||
|
||
def map_model(model) | ||
original_map_model(model) | ||
|
||
self.center_id = model.center.try(:id) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# frozen_string_literal: true | ||
|
||
module Decidim | ||
module Centers | ||
class SyncCenterUserJob < ApplicationJob | ||
queue_as :default | ||
|
||
def perform(data) | ||
@user = Decidim::User.find(data[:user_id]) | ||
@center = Decidim::Centers::Center.find(data[:center_id]) | ||
create_or_update_center_user | ||
end | ||
|
||
private | ||
|
||
def create_or_update_center_user | ||
Decidim::Centers::CreateOrUpdateCenterUser.call(@center, @user) do | ||
on(:ok) do | ||
Rails.logger.info "SyncCenterUserJob: Success: updated for user #{@user.id}" | ||
end | ||
|
||
on(:invalid) do | ||
Rails.logger.error "SyncCenterUserJob: ERROR: not updated for user #{@user.id}" | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<!-- insert_after "erb[loud]:contains('email_field')" original "d629550ba83d41a6d1c41553b7adbed311549633" --> | ||
|
||
<%= render partial: "/decidim/centers/profile_form", locals: { f: f } %> |
3 changes: 3 additions & 0 deletions
3
app/overrides/decidim/devise/omniauth_registrations/new/centers.html.erb.deface
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<!-- insert_after "erb[loud]:contains('email_field')" --> | ||
|
||
<%= render partial: "/decidim/centers/registration_form", locals: { f: f } %> |
3 changes: 3 additions & 0 deletions
3
app/overrides/decidim/devise/registrations/new/centers.html.erb.deface
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<!-- insert_before "#card__tos" --> | ||
|
||
<%= render partial: "/decidim/centers/registration_form", locals: { f: f } %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<%= f.collection_select :center_id, f.object.center_options_for_select, :first, :last %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<div class="card"> | ||
<div class="card__content card__centers"> | ||
<div class="field"> | ||
<%= f.collection_select :center_id, f.object.center_options_for_select, :first, :last %> | ||
</div> | ||
</div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# frozen_string_literal: true | ||
|
||
require "spec_helper" | ||
|
||
describe "Account", type: :system do | ||
let(:organization) { create :organization } | ||
let(:user) { create :user, :confirmed, organization: organization } | ||
let!(:center) { create :center, organization: organization } | ||
let!(:other_center) { create :center, organization: organization } | ||
|
||
before do | ||
switch_to_host(organization.host) | ||
login_as user, scope: :user | ||
end | ||
|
||
shared_examples_for "user changes the center" do | ||
it "can update the center" do | ||
within "form.edit_user" do | ||
within "#user_center_id" do | ||
find("option[value='#{other_center.id}']").click | ||
end | ||
|
||
find("*[type=submit]").click | ||
end | ||
|
||
within_flash_messages do | ||
expect(page).to have_content("successfully") | ||
end | ||
|
||
expect(find("#user_center_id").value).to eq(other_center.id.to_s) | ||
end | ||
end | ||
|
||
context "when the user doesn't have center" do | ||
before do | ||
visit decidim.account_path | ||
end | ||
|
||
it "shows an empty value on the center input" do | ||
expect(find("#user_center_id").value).to eq("") | ||
end | ||
|
||
include_examples "user changes the center" | ||
end | ||
|
||
context "when the user has center" do | ||
let!(:center_user) { create :center_user, center: center, user: user } | ||
|
||
before do | ||
visit decidim.account_path | ||
end | ||
|
||
it "shows the current center on the center input" do | ||
expect(find("#user_center_id").value).to eq(center.id.to_s) | ||
end | ||
|
||
include_examples "user changes the center" | ||
end | ||
end |
Oops, something went wrong.