Commit b9fc8bc8 authored by Kaspar Vollenweider's avatar Kaspar Vollenweider 👻 Committed by Ales Rosina
Browse files

adding journals feature to volunteers and clients

parent 1f8f8df3
class JournalsController < ApplicationController
before_action :set_journal, only: [:show, :edit, :update, :destroy]
before_action :set_journaled
def index
@journals = Journal.where(journal_relations.except(:user_id))
end
def show; end
def new
@journal = Journal.new(journal_relations)
authorize(@journal)
end
def edit; end
def create
@journal = Journal.new(journal_params.merge(journal_relations))
authorize(@journal)
if @journal.save
redirect_to @journaled
else
render :new
end
end
def update
if @journal.update(journal_params)
redirect_to @journaled
else
render :edit
end
end
def destroy
@journal.destroy
redirect_to @journaled
end
def journal_relations
{
journalable_type: @journaled.class.model_name.name,
journalable_id: @journaled.id,
user_id: current_user.id
}
end
private
def set_journal
@journal = Journal.find(params[:id])
authorize(@journal)
end
def set_journaled
return @journaled = Client.find(params[:client_id]) if params[:client_id]
@journaled = Volunteer.find(params[:volunteer_id])
end
def journal_params
params.require(:journal).permit(
:subject, :user_id, :body, :client_id, :volunteer_id, :client_id
)
end
end
......@@ -69,4 +69,8 @@ module ApplicationHelper
model_name_from_record_or_class(f.object).element,
label_html: { class: 'conditional-group' }
end
def confirm_deleting(record)
{ method: :delete, data: { confirm: t_confirm_delete(record) } }
end
end
......@@ -21,8 +21,8 @@ class Client < ApplicationRecord
has_many :relatives, as: :relativeable, dependent: :destroy
accepts_nested_attributes_for :relatives, allow_destroy: true
has_many :journal, as: :journalable
accepts_nested_attributes_for :journal
has_many :journals, as: :journalable, dependent: :destroy
accepts_nested_attributes_for :journals, allow_destroy: true
validates :state, inclusion: { in: STATES }
......
......@@ -8,8 +8,8 @@ class Volunteer < ApplicationRecord
has_one :contact, as: :contactable
accepts_nested_attributes_for :contact
has_many :journal, as: :journalable
accepts_nested_attributes_for :journal
has_many :journals, as: :journalable, dependent: :destroy
accepts_nested_attributes_for :journals, allow_destroy: true
belongs_to :user, optional: true
......
......@@ -52,4 +52,8 @@ class ClientPolicy < ApplicationPolicy
def need_accompanying?
user.superadmin?
end
def supervisor?
user.superadmin?
end
end
class JournalPolicy < ApplicationPolicy
attr_reader :user, :journal
def initialize(user, journal)
@user = user
@journal = journal
end
def new?
user.superadmin?
end
def create?
user.superadmin?
end
def index?
user.superadmin?
end
def show?
user.superadmin?
end
def edit?
user.superadmin?
end
def update?
user.superadmin?
end
def destroy?
user.superadmin?
end
end
......@@ -38,6 +38,10 @@ class VolunteerPolicy < ApplicationPolicy
user && user.superadmin?
end
def supervisor?
user.superadmin?
end
private
def volunteers_owns_profile
......
......@@ -18,9 +18,10 @@ tr
td = client.created_at.strftime("%d-%m-%Y")
- if params[:action] == 'index'
td = link_to t_action(:show), client
td = link_to t_model(Journal), { controller: 'journals', action: 'index', client_id: client.id }
td = link_to t_action(:edit), edit_client_path(client)
td
- if policy(Client).destroy?
= link_to t_action(:destroy), client, data: { confirm: t('are_you_sure')}, method: :delete
= link_to t_action(:destroy), client, confirm_deleting(client)
- else
td = button_link t('find_volunteer'), find_volunteer_client_path(id: client)
......@@ -13,7 +13,7 @@
th= t_attr(:goals)
th= t_attr(:competent_authority)
th= t_attr(:created_at)
th colspan='3'
th colspan='4'
tbody
= render clients
......@@ -2,6 +2,4 @@
.col-xs-12
h1= t_title(:edit)
== render 'form'
= form_navigation_btn :back
= render 'form'
......@@ -2,69 +2,70 @@
.col-xs-12
h1= @client.contact.full_name
.row
.col-xs-12
.table-responsive
table.table.table-no-border-top
tbody
= render 'contacts/show', contact: @client.contact
tr
td= t_attr(:date_of_birth)
td= @client.date_of_birth.try(:year)
tr
td= t_attr(:gender)
td
- if @client.gender.present?
= t("gender.#{@client.gender}")
tr
td= t_attr(:nationality)
td= country(@client.nationality)
tr
td= t_attr(:permit)
td= @client.permit
= render 'language_skills/show', speaker: @client
= render 'relatives/show', relative: @client
tr
td= t_attr(:goals)
td= @client.goals
= render 'request', client: @client
tr
td= t_attr(:education)
td= @client.education
tr
td= t_attr(:actual_activities)
td= @client.actual_activities
tr
td= t_attr(:interests)
td= @client.interests
tr
td= t_attr(:state)
td= t("state.#{@client.state}")
tr
td= t_attr(:involved_authority)
td= @client.involved_authority
tr
td= t_attr(:competent_authority)
td= @client.competent_authority
tr
td= t('registered_by')
td= mail_to @client.user.email
- if policy(Client).destroy?
tr
td= t_attr(:comments)
td= @client.comments
- if policy(Client).supervisor?
= render 'journals/journal_block', journaled_for: @client
.table-responsive
table.table.table-no-border-top
tbody
= render 'contacts/show', contact: @client.contact
tr
td= t_attr(:date_of_birth)
td= @client.date_of_birth.try(:year)
tr
td= t_attr(:gender)
td
- if @client.gender.present?
= t("gender.#{@client.gender}")
tr
td= t_attr(:nationality)
td= country(@client.nationality)
tr
td= t_attr(:permit)
td= @client.permit
= render 'language_skills/show', speaker: @client
= render 'relatives/show', relative: @client
tr
td= t_attr(:goals)
td= @client.goals
= render 'request', client: @client
tr
td= t_attr(:education)
td= @client.education
tr
td= t_attr(:actual_activities)
td= @client.actual_activities
tr
td= t_attr(:interests)
td= @client.interests
tr
td= t_attr(:state)
td= t("state.#{@client.state}")
tr
td= t_attr(:involved_authority)
td= @client.involved_authority
tr
td= t_attr(:competent_authority)
td= @client.competent_authority
tr
td= t('registered_by')
td= mail_to @client.user.email
- if policy(Client).destroy?
tr
td= t_attr(:comments)
td= @client.comments
= render 'schedules/show', participant: @client
......
= simple_form_for [@journaled, @journal] do |f|
= simple_error_notice f
.row
.col-xs-12
= f.input :subject
.col-xs-12
= f.input :body
.row
.col-xs-12
= f.button :submit
.row
.col-xs-12
button.btn.btn-primary(data-toggle='collapse' data-target='#journalBlock' type='button' aria-expanded='false' aria-controls='journalBlock')
= 'Journal'
#journalBlock.collapse
.row
.col-xs-12
= link_to t('add_journal_entry'), { controller: 'journals', action: 'new', "#{controller_name.singularize}_id": journaled_for.id }, class: 'btn btn-success'
- if journaled_for.journals.any?
.table-responsive
table.table
thead
tr
th Datum
th Titel
th Text
th Author
th colspan=3
tbody
- journaled_for.journals.each do |record|
tr
td= l(record.created_at)
td= record.subject
td= record.body.truncate(40) if record.body
td= link_to record.user.to_label, record.user.profile
td= link_to t_action(:show), client_journal_path(journaled_for, record)
td= link_to t_action(:edit), edit_client_journal_path(journaled_for, record)
td= link_to t_action(:destroy), client_journal_path(journaled_for, record), confirm_deleting(record)
hr
h1= t_title(:edit, Journal)
= render 'form'
.row
.col-xs-12
= button_link t('back'), @journaled
h1= t_title(:index, Journal)
.table-responsive
table.table
thead
tr
th Datum
th Titel
th Text
th Author
th colspan=3
tbody
- @journaled.journals.each do |record|
tr
td= l(record.created_at)
td= record.subject
td= record.body.truncate(40) if record.body
td= link_to record.user.to_label, record.user.profile
td= link_to t_action(:show), client_journal_path(@journaled, record)
td= link_to t_action(:edit), edit_client_journal_path(@journaled, record)
td= link_to t_action(:destroy), client_journal_path(@journaled, record), confirm_deleting(record)
.row
.col-xs-12
= button_link t('back'), @journaled
h1= t_title(:new, Journal)
= render 'form'
.row
.col-xs-12
= button_link t('back'), @journaled
h1= t_title(:show, Journal)
.row
.col-xs-12
p
strong= "#{t_attr(:subject, Journal)}: "
= @journal.subject
p
strong= "#{t_attr(:subject, Journal)}: "
= link_to @journal.user.to_label, @journal.user.profile
p
strong= "#{t_attr(:created_at, Journal)}: "
= l(@journal.created_at)
.row
.col-xs-12
= @journal.body
.row
.col-xs-12
= button_link t('back'), @journaled
......@@ -19,5 +19,6 @@ tr
- else
= t('volunteer_self_applicant')
td = link_to t_action(:show), volunteer
td = link_to t_model(Journal), { controller: 'journals', action: 'index', volunteer_id: volunteer.id }
td = link_to t_action(:edit), edit_volunteer_path(volunteer)
td = link_to t_action(:destroy), volunteer, data: { confirm: t_confirm_delete(volunteer) }, method: :delete
td = link_to t_action(:destroy), volunteer, confirm_deleting(volunteer)
h1 Email Text editieren
= form_navigation_btn :back
......@@ -23,7 +23,7 @@
th= sort_link @q, :expectations
th= sort_link @q, :interests
th= sort_link @q, :state
th colspan='3'
th colspan='4'
tbody
= render @volunteers
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment