Skip to content

Commit

Permalink
Refactor based on PR review
Browse files Browse the repository at this point in the history
  • Loading branch information
stage-rl committed Dec 5, 2023
1 parent 19072ea commit a78bac7
Show file tree
Hide file tree
Showing 12 changed files with 63 additions and 63 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,8 @@
],
"[erb]": {
"editor.defaultFormatter": "Jota0222.multi-formatter"
},
"[markdown]": {
"editor.defaultFormatter": null
}
}
2 changes: 1 addition & 1 deletion app/components/message_draft_body_component.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
<%= text_field_tag message_title_id, @message.title, placeholder: "Predmet", "data-action": "change->message-drafts#update", class: "mb-3 px-3 py-4 placeholder-slate-300 text-slate-900 relative bg-white bg-white rounded-lg text-base border-0 shadow outline-none focus:outline-none focus:ring w-full" %>
<%= text_area_tag message_text_id, @message.metadata["message_body"], autofocus: @is_last, placeholder: "Text", "data-action": "change->message-drafts#update", rows: 10, class: "px-3 py-4 placeholder-slate-300 text-slate-900 relative bg-white bg-white rounded-lg text-base border-0 shadow outline-none focus:outline-none focus:ring w-full h-full" %>
<% else %>
<%= link_to locked_message_draft_path(@message), data: { turbo_frame: "modal" } do %>
<%= button_to confirm_unlock_message_draft_path(@message), class: "w-full", data: { turbo_frame: "modal" } do %>
<%= text_field_tag message_title_id, @message.title, placeholder: "Predmet", readonly: true, class: "mb-3 px-3 py-4 placeholder-slate-300 text-slate-900 relative bg-white bg-white rounded-lg text-base border-0 shadow outline-none focus:outline-none focus:ring w-full" %>
<%= text_area_tag message_text_id, @message.metadata["message_body"], autofocus: @is_last, readonly: true, placeholder: "Text", rows: 10, class: "px-3 py-4 placeholder-slate-300 text-slate-900 relative bg-white bg-white rounded-lg text-base border-0 shadow outline-none focus:outline-none focus:ring w-full h-full" %>
<% end %>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Draft nie je možné upravovať
<% end %>
<% modal.with_modal_content do %>
<div class="mt-2">
<div class="mt-2 pb-4">
<p class="text-sm text-gray-500">
<% if @message.reason_for_readonly == :form_signed %>
<%= t "message_draft.form_signed" %>
Expand All @@ -17,7 +17,9 @@
<%= render Common::ModalActionsComponent.new do |actions| %>
<% if @message.reason_for_readonly == :form_signed %>
<% actions.with_submit_button do %>
<%= link_to 'Odstrániť podpisy', enable_edit_message_draft_path(@message), class: 'flex justify-center items-center px-2.5 py-1.5 rounded-md bg-blue-600 text-sm font-medium text-white', data: { turbo_frame: "_top" } %>
<%= button_to unlock_message_draft_path(@message), class: "inline-flex w-full justify-center rounded-md bg-red-600 px-3 py-2 text-sm font-semibold text-white shadow-sm hover:bg-red-500 sm:ml-3 sm:w-auto", data: { turbo_frame: "_top" } do %>
Odstrániť podpisy
<% end %>
<% end %>
<% end %>
<% end %>
Expand Down
5 changes: 5 additions & 0 deletions app/components/message_draft_confirm_unlock_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class MessageDraftConfirmUnlockComponent < ViewComponent::Base
def initialize(message)
@message = message
end
end
5 changes: 0 additions & 5 deletions app/components/message_draft_locked_component.rb

This file was deleted.

14 changes: 6 additions & 8 deletions app/controllers/message_drafts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,16 @@ def destroy
redirect_to redirect_path, notice: "Draft bol zahodený"
end

def enable_edit
def unlock
authorize @message
@message.transaction do
if @message.remove_form_signature
redirect_to message_thread_path(@message.thread), notice: "Podpisy boli úspešne odstránené, správu je možné upravovať"
else
redirect_to message_thread_path(@message.thread), alert: "Nastala neočakávaná chyba, nepodarilo sa odstrániť podpisy"
end
if @message.remove_form_signature
redirect_to message_thread_path(@message.thread), notice: "Podpisy boli úspešne odstránené, správu je možné upravovať"
else
redirect_to message_thread_path(@message.thread), alert: "Nastala neočakávaná chyba, nepodarilo sa odstrániť podpisy"
end
end

def locked
def confirm_unlock
authorize @message
end

Expand Down
57 changes: 33 additions & 24 deletions app/models/message_draft.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,7 @@ def self.create_message_reply(original_message: , author:)
"status": "created"
}
)

# TODO clean the domain (no UPVS stuff)
message_draft.objects.create!(
name: "form.xml",
mimetype: "application/x-eform-xml",
object_type: "FORM",
is_signed: false
)
create_form_object

message_draft
end
Expand All @@ -86,22 +79,10 @@ def update_content(title:, body:)
self.title = title
metadata["message_body"] = body
save!

return unless title.present? && body.present?

# TODO clean the domain (no UPVS stuff)
if form.message_object_datum
form.message_object_datum.update(
blob: Upvs::FormBuilder.build_general_agenda_xml(subject: title, body: body)
)
else
form.message_object_datum = MessageObjectDatum.create(
message_object: form,
blob: Upvs::FormBuilder.build_general_agenda_xml(subject: title, body: body)
)
end

self.reload
update_form_object
reload
end

def draft?
Expand Down Expand Up @@ -153,7 +134,7 @@ def submitted!
save!
EventBus.publish(:message_draft_submitted, self)
end

def invalid?
metadata["status"] == "invalid"
end
Expand All @@ -164,8 +145,12 @@ def original_message

def remove_form_signature
return false unless form
return false unless form.is_signed?

form.remove_signature
form.destroy
create_form_object
reload
update_form_object
end

private
Expand Down Expand Up @@ -195,4 +180,28 @@ def validate_objects
errors.merge!(object.errors)
end
end

def create_form_object
# TODO: clean the domain (no UPVS stuff)
objects.create!(
name: "form.xml",
mimetype: "application/x-eform-xml",
object_type: "FORM",
is_signed: false
)
end

def update_form_object
# TODO: clean the domain (no UPVS stuff)
if form.message_object_datum
form.message_object_datum.update(
blob: Upvs::FormBuilder.build_general_agenda_xml(subject: title, body: metadata["message_body"])
)
else
form.message_object_datum = MessageObjectDatum.create(
message_object: form,
blob: Upvs::FormBuilder.build_general_agenda_xml(subject: title, body: metadata["message_body"])
)
end
end
end
14 changes: 1 addition & 13 deletions app/models/message_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
class MessageObject < ApplicationRecord
belongs_to :message, inverse_of: :objects
has_one :message_object_datum, dependent: :destroy
has_many :nested_message_objects, inverse_of: :message_object
has_many :nested_message_objects, inverse_of: :message_object, dependent: :destroy

scope :unsigned, -> { where(is_signed: false) }
scope :to_be_signed, -> { where(to_be_signed: true) }
Expand Down Expand Up @@ -68,18 +68,6 @@ def destroyable?
message.draft? && message.not_yet_submitted? && !form?
end

def remove_signature
return false unless form?
return false unless is_signed

unsigned_object = nested_message_objects&.first
return false unless unsigned_object

update(name: unsigned_object.name, mimetype: unsigned_object.mimetype, is_signed: false)
message_object_datum.update(blob: unsigned_object.content)
unsigned_object.destroy
end

private

def allowed_mime_type?
Expand Down
8 changes: 4 additions & 4 deletions app/policies/message_draft_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def index?
end

def create?
true # TODO can everyone create new messages?
true # TODO: can everyone create new messages?
end

def show?
Expand All @@ -52,11 +52,11 @@ def destroy?
create?
end

def locked?
enable_edit?
def confirm_unlock?
unlock?
end

def enable_edit?
def unlock?
create?
end
end
3 changes: 3 additions & 0 deletions app/views/message_drafts/confirm_unlock.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<%= tag.turbo_frame id: "modal" do %>
<%= render MessageDraftConfirmUnlockComponent.new(@message) %>
<% end %>
3 changes: 0 additions & 3 deletions app/views/message_drafts/locked.html.erb

This file was deleted.

6 changes: 3 additions & 3 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@

resources :message_drafts do
member do
get 'locked'
get 'enable_edit'
post 'submit'
post :confirm_unlock
post :unlock
post :submit
end

post 'submit_all', on: :collection
Expand Down

0 comments on commit a78bac7

Please sign in to comment.