Skip to content
This repository has been archived by the owner on Mar 6, 2024. It is now read-only.

Commit

Permalink
Merge remote-tracking branch 'origin' into dcc/lh-decideix-202304
Browse files Browse the repository at this point in the history
  • Loading branch information
darccio committed Apr 21, 2023
2 parents 982448b + 6794a4b commit 058ac3f
Show file tree
Hide file tree
Showing 30 changed files with 111 additions and 75 deletions.
2 changes: 1 addition & 1 deletion app/controllers/consultations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def create_params
end

def update_params
params.require(:consultation).permit(:title, :description, :status, config: %i[mode ballot])
params.require(:consultation).permit(:title, :description, :status, config: %i[mode ballot distribution])
end

def event
Expand Down
6 changes: 1 addition & 5 deletions app/interactors/cast_votes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,9 @@ def cast_vote(vote_params)
question = vote_params[:question]

envelope = Envelope.new(question, current_user, **vote_params.to_h.symbolize_keys)
envelope.save(async: async?)
envelope.save_later

envelope.receipt.save!
self.receipts << envelope.receipt
end

def async?
Rails.configuration.x.asembleo.async_vote
end
end
2 changes: 1 addition & 1 deletion app/interactors/create_magic_link.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@ def user
end

def deliver
SessionsMailer.magic_link_email(email, token).deliver_later
SessionsMailer.magic_link_email(email, token.class.name, token.id, token.to_hash, scope: :global).deliver_later
end
end
3 changes: 2 additions & 1 deletion app/interactors/create_token.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ def issue_receipt
end

def deliver
SessionsMailer.magic_link_email(identifier, token.reload).deliver_later
token.reload
SessionsMailer.magic_link_email(identifier, token.class.name, token.id, token.to_hash, scope:).deliver_later
end
end
3 changes: 2 additions & 1 deletion app/interactors/redirect_by_session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ def active_question

join_condition = [
receipts[:question_id].eq(questions[:question_id]),
receipts[:token_id].eq(identity.id)
receipts[:voter_type].eq(identity.class.name),
receipts[:voter_id].eq(identity.id)
]
where = {
status: :opened,
Expand Down
8 changes: 5 additions & 3 deletions app/jobs/vote_cast_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ class VoteCastJob < ApplicationJob
queue_as :default

# TODO: Vote hash chaining
def perform(question_id, token_id, value, created_at)
def perform(question_id, token_class, token_id, value, created_at)
question = Question.find(question_id)
token = Token.find(token_id)

token_class = token_class.constantize
token = token_class.find(token_id)

envelope = Envelope.new(question, token, value:, created_at:)
envelope.save
envelope.save!
end
end
15 changes: 9 additions & 6 deletions app/mailers/sessions_mailer.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# frozen_string_literal: true

class SessionsMailer < ApplicationMailer
def magic_link_email(to, token)
@token = token
def magic_link_email(to, voter_type, voter_id, voter_hash, scope: :consultation)
@scope = scope.to_sym
@voter_hash = voter_hash
raise Errors::InvalidEmail unless EmailValidator.valid?(to, mode: :strict)

tag = TokenTag.sent
Expand All @@ -14,20 +15,22 @@ def magic_link_email(to, token)

raise e
ensure
token.tags << tag
token.save!
return unless voter_type == 'Token'

tag.token_id = voter_id
tag.save!
end

private

def template_name
return 'user_magic_link_email' if @token.scope == 'global'
return 'user_magic_link_email' if @scope == :global

'magic_link_email'
end

def subject
return I18n.t('sessions_mailer.user_magic_link_email.subject') if @token.scope == 'global'
return I18n.t('sessions_mailer.user_magic_link_email.subject') if @scope == :global

I18n.t('sessions_mailer.magic_link_email.subject')
end
Expand Down
15 changes: 8 additions & 7 deletions app/models/envelope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,25 @@ def receipt
return @receipt if defined?(@receipt)

@receipt ||= Receipt.new.tap do |r|
r.token_id = token.id
r.voter = token
r.question = question
r.created_at = created_at
r.fingerprint = FingerprintService.generate(r, token.to_hash, values)
end
end

def save(async: false)
if async
VoteCastJob.perform_later(question.id, token.id, values, receipt.created_at)
else
votes.map(&:save!)
end
def save_later
VoteCastJob.perform_later(question.id, token.class.name, token.id, values, receipt.created_at)
end

def save!
votes.each(&:save!)
end

private

def vote_alias
return unless token.is_a?(Token)
return unless consultation.ballot == 'open'

token.on_behalf_of || token.alias || token.to_hash
Expand Down
4 changes: 2 additions & 2 deletions app/models/question.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ def valid_option?(value)
options.exists?(value:)
end

def voted?(token)
receipts.exists?(token:)
def voted?(voter)
receipts.exists?(voter:)
end

def short_description
Expand Down
2 changes: 1 addition & 1 deletion app/models/receipt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
class Receipt < ApplicationRecord
has_paper_trail

belongs_to :token
belongs_to :voter, polymorphic: true
belongs_to :question
end
2 changes: 1 addition & 1 deletion app/models/token.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Token < ApplicationRecord
belongs_to :consultation, optional: true
belongs_to :event, optional: true

has_many :receipts, dependent: :destroy
has_many :receipts, dependent: :destroy, as: :voter
has_many :tags, foreign_key: 'token_id', class_name: 'TokenTag', dependent: :destroy, inverse_of: :token

enum role: { voter: 0, manager: 1, admin: 2 }
Expand Down
2 changes: 1 addition & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
class User < ApplicationRecord
has_paper_trail

has_many :receipts, foreign_key: 'token_id', dependent: :destroy
has_many :receipts, dependent: :destroy, as: :voter

enum :role, { voter: 0, manager: 1, admin: 2 }, default: :voter
enum :status, { enabled: 1, disabled: 0 }, default: :disabled
Expand Down
7 changes: 5 additions & 2 deletions app/services/markdown_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ def self.render(text, plain_text: false)
ActionController::Base.helpers.sanitize(content)
end

def header(text, _header_level)
%(<strong>#{text}</strong>)
def header(text, header_level)
new_header_level = header_level + 2
new_header_level = 6 if new_header_level > 6

%(<h#{new_header_level}>#{text}</h#{new_header_level}>)
end
end
4 changes: 2 additions & 2 deletions app/views/consultations/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<h2><%= @consultation.title %></h2>
<%= render @consultation.status %>
<div class="mb-3">
<%= markdown @consultation.description %>
</div>
<%= render @consultation.status %>
</div>
4 changes: 2 additions & 2 deletions app/views/sessions_mailer/magic_link_email.text.erb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<%= t('application_mailer.greeting') %>,

<%= t('sessions_mailer.magic_link_email.body',
magic_link_url: magic_link_url(@token.to_hash),
magic_link_url: magic_link_url(@voter_hash),
root_url: root_url,
token: @token).strip %>
token: @voter_hash).strip %>
<%= t('application_mailer.complimentary_close', recipient: Rails.configuration.x.asembleo.title).strip %>
1 change: 0 additions & 1 deletion config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ class Application < Rails::Application
config.x.asembleo.private_instance = ENV['ASEMBLEO_PRIVATE_INSTANCE'].present?
config.x.asembleo.open_registration = ENV['ASEMBLEO_OPEN_REGISTRATION'].present?
config.x.asembleo.token_alias = ENV['ASEMBLEO_TOKEN_ALIAS'].presence
config.x.asembleo.async_vote = ActiveModel::Type::Boolean.new.cast(ENV['ASEMBLEO_ASYNC_VOTE'].presence) || false
config.x.asembleo.hide_receipt = ActiveModel::Type::Boolean.new.cast(ENV['ASEMBLEO_HIDE_RECEIPT'].presence) || false

# Branding
Expand Down
8 changes: 5 additions & 3 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ en:
confirm: Are you sure?
create: Create
edit: Edit
email: Email
email: &email Email
delete: Delete
footer: Asembleo is a free software project developed by Coopanio
log_in: Log in
Expand All @@ -105,8 +105,7 @@ en:
consultation_created: Consultation created.
consultation_draft: The consultation is still in draft mode.
consultation_finished: You have answered all the questions.
consultation_open: The consultation is open, but there are no active questions.
Try it again later.
consultation_open: The consultation is open. Please proceed to vote by clicking next.
consultation_archived: The consultation has been archived.
consultation_closed: The consultation is closed.
consultation_deleted: Consultation deleted.
Expand Down Expand Up @@ -316,6 +315,9 @@ en:
session:
identifier: Identifier
password: Password
user:
email: *email
nid: Identity document
events_helper:
open: Open
close: Close
Expand Down
7 changes: 7 additions & 0 deletions db/migrate/20230421070610_add_voter_to_receipts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class AddVoterToReceipts < ActiveRecord::Migration[7.0]
def change
add_reference :receipts, :voter, polymorphic: true, null: true
end
end
11 changes: 11 additions & 0 deletions db/migrate/20230421070925_backfill_existing_receipts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

class BackfillExistingReceipts < ActiveRecord::Migration[7.0]
disable_ddl_transaction!

def change
Receipt.unscoped.in_batches do |batch|
batch.update_all(voter_type: 'Token', voter_id: Arel.sql('token_id'))
end
end
end
7 changes: 7 additions & 0 deletions db/migrate/20230421071603_remove_token_from_receipts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class RemoveTokenFromReceipts < ActiveRecord::Migration[7.0]
def change
remove_reference :receipts, :token, null: false, foreign_key: true
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class RemoveTokenAndQuestionIndexFromReceipts < ActiveRecord::Migration[7.0]
def change
remove_index :receipts, name: 'index_receipts_on_token_id_and_question_id'
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class AddVoterAndQuestionIndexToReceipts < ActiveRecord::Migration[7.0]
def change
add_index :receipts, %i[voter_type voter_id question_id], unique: true
end
end
10 changes: 5 additions & 5 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.0].define(version: 2023_03_14_232746) do
ActiveRecord::Schema[7.0].define(version: 2023_04_21_151747) do
create_table "active_storage_attachments", force: :cascade do |t|
t.string "name", null: false
t.string "record_type", null: false
Expand Down Expand Up @@ -116,13 +116,14 @@

create_table "receipts", force: :cascade do |t|
t.text "fingerprint"
t.integer "token_id", null: false
t.integer "question_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "voter_type"
t.integer "voter_id"
t.index ["question_id"], name: "index_receipts_on_question_id"
t.index ["token_id", "question_id"], name: "index_receipts_on_token_id_and_question_id", unique: true
t.index ["token_id"], name: "index_receipts_on_token_id"
t.index ["voter_type", "voter_id", "question_id"], name: "index_receipts_on_voter_type_and_voter_id_and_question_id", unique: true
t.index ["voter_type", "voter_id"], name: "index_receipts_on_voter"
end

create_table "token_receipts", id: false, force: :cascade do |t|
Expand Down Expand Up @@ -204,7 +205,6 @@
add_foreign_key "question_links", "questions"
add_foreign_key "questions", "consultations"
add_foreign_key "receipts", "questions"
add_foreign_key "receipts", "tokens"
add_foreign_key "token_receipts", "consultations"
add_foreign_key "token_tags", "tokens"
add_foreign_key "tokens", "consultations"
Expand Down
2 changes: 2 additions & 0 deletions test/controllers/questions_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class QuestionsControllerTest < ActionDispatch::IntegrationTest

post sessions_url, params: { session: { identifier: token.to_hash } }
post votes_url, params: { vote: { question.id => { value: [value] } }, question_id: question.id }.stringify_keys
perform_enqueued_jobs
end
end

Expand Down Expand Up @@ -50,6 +51,7 @@ class QuestionsControllerTest < ActionDispatch::IntegrationTest

post sessions_url, params: { session: { identifier: token.to_hash } }
post votes_url, params: { vote: { question.id => { value: [value] } }, question_id: question.id }.stringify_keys
perform_enqueued_jobs
end

results = subject
Expand Down
Loading

0 comments on commit 058ac3f

Please sign in to comment.