Commit f825ebe1 authored by Kaspar Vollenweider's avatar Kaspar Vollenweider 👻 Committed by Kaspar
Browse files

some mangling with the relation and the forms

parent 3b28f6bc
class FeedbacksController < ApplicationController
before_action :set_feedback, only: [:show, :edit, :update, :destroy]
before_action :set_feedback_about
before_action :set_feedbackable, only: [:new, :create]
def index
authorize Feedback
@feedbacks = policy_scope(Feedback).where(feedbackable: @assignment)
@feedbacks = policy_scope(Feedback)
end
def show; end
def new
@feedback = Feedback.new(feedbackable: @feedback_about, volunteer: current_user.volunteer)
@feedback = Feedback.new(feedbackable: @feedbackable, volunteer: @volunteer, author: current_user)
authorize @feedback
end
......@@ -18,10 +18,10 @@ class FeedbacksController < ApplicationController
def create
@feedback = Feedback.new(feedback_params.merge(author_id: current_user.id,
volunteer_id: current_user.volunteer.id))
volunteer_id: @volunteer.id))
authorize @feedback
if @feedback.save
redirect_to url_for([@feedback_about, @feedback]), make_notice
redirect_to url_for([@feedback.feedbackable, @feedback]), make_notice
else
render :new
end
......@@ -29,7 +29,7 @@ class FeedbacksController < ApplicationController
def update
if @feedback.update(feedback_params)
redirect_to url_for([@feedback_about, @feedback]), make_notice
redirect_to url_for([@feedbackable, @feedback]), make_notice
else
render :edit
end
......@@ -37,24 +37,27 @@ class FeedbacksController < ApplicationController
def destroy
@feedback.destroy
redirect_back(fallback_location: url_for(@feedback_about))
redirect_back(fallback_location: url_for(@feedbackable))
end
private
def set_feedbackable
@feedbackable = Assignment.find(params[:assignment_id]) if params[:assignment_id]
@feedbackable = GroupOffer.find(params[:group_offer_id]) if params[:group_offer_id]
return @volunteer = @feedbackable.volunteer if @feedbackable.class == Assignment
@volunteer = current_user.volunteer if current_user.volunteer?
end
def set_feedback
@feedback = Feedback.find(params[:id])
@feedbackable = @feedback.feedbackable
@volunteer = @feedback.volunteer
authorize @feedback
end
def set_feedback_about
return @feedback_about = Assignment.find(params[:assignment_id]) if params[:assignment_id]
return @feedback_about = GroupOffer.find(params[:group_offer_id]) if params[:group_offer_id]
@feedback_about = nil
end
def feedback_params
params.require(:feedback).permit(:goals, :achievements, :future, :comments, :conversation,
:feedbackable_id, :feedbackable_type, :volunteer_id)
:feedbackable_id, :feedbackable_type, :volunteer_id, :feedbackable_id_and_type, :author_id)
end
end
class Feedback < ApplicationRecord
attr_reader :feedbackable_id_and_type
belongs_to :volunteer
belongs_to :author, class_name: 'User'
belongs_to :feedbackable, polymorphic: true, optional: true
......@@ -10,4 +12,12 @@ class Feedback < ApplicationRecord
def group_offer?
feedbackable_type == 'GroupOffer'
end
def feedbackable_id_and_type=(id_and_type)
self.feedbackable_id, self.feedbackable_type = id_and_type.split(',', 2)
end
def feedbackable_id_and_type
"#{feedbackable_id},#{feedbackable_type}"
end
end
class FeedbackPolicy < ApplicationPolicy
class Scope < ApplicationScope
def resolve
return resolve_assignment if superadmin?
resolve_author_and_assignment if volunteer?
return all if superadmin?
scope.where(volunteer: user.volunteer, author: user) if volunteer?
end
end
alias_method :index?, :superadmin_or_volunteer?
def superadmin_or_volunteers_feedback?
binding.pry
superadmin? || volunteer? && user.volunteer.id == record.volunteer.id
end
......
= simple_form_for [@feedback_about, @feedback] do |f|
= simple_error_notice f
- if @feedbackable.present?
= simple_form_for [@feedbackable, @feedback] do |f|
= simple_error_notice f
= f.hidden_field :feedbackable_id, value: params["#{controller_name.singularize}_id".to_sym]
= f.hidden_field :feedbackable_type, value: @feedback_about
= f.hidden_field :feedbackable_id, value: @feedbackable
= f.hidden_field :feedbackable_type, value: @feedbackable
.row
.col-xs-12
= f.input :goals
= render 'main_fields', f: f
- else
= simple_form_for @feedback do |f|
= simple_error_notice f
.row
.col-xs-12
= f.input :achievements
= f.input :feedbackable_id_and_type, as: :select, collection: @volunteer.assignment_group_offer_collection
.row
.col-xs-12
= f.input :future
= render 'main_fields', f: f
.row
.col-xs-12
= f.input :comments
.row
.col-xs-12
= f.input :conversation
.row
.col-xs-12
= f.button :submit
= f.hidden_field :volunteer_id, value: @feedback.volunteer_id
= f.hidden_field :author_id, value: @feedback.author_id
- [:goals, :achievements, :future, :comments, :conversation].each do |field|
.row
.col-xs-12
= f.input field
.row
.col-xs-12
= f.button :submit
- @feedbacks ||= @assignment.feedbacks
h1= t_title
.row
.col-xs-12
h1= t_title
.row
.col-xs-12
table.table.table-striped
thead
tr
th= t_model(Volunteer)
th= t_model(Client)
th= t_attr(:goals, Feedback)
th= t_attr(:achievements, Feedback)
th= t_attr(:future, Feedback)
th= t_attr(:comments, Feedback)
th= t_attr(:conversation, Feedback)
th colspan='3'
table.table.table-striped
thead
tr
th= t_model(Volunteer)
th= t_model(Client)
th= t_attr(:goals, Feedback)
th= t_attr(:achievements, Feedback)
th= t_attr(:future, Feedback)
th= t_attr(:comments, Feedback)
th= t_attr(:conversation, Feedback)
th colspan='3'
tbody
- @feedbacks.each do |record|
tr
td= link_to record.volunteer.contact.full_name, volunteer_path(record.volunteer)
td
- if record.assignment?
= link_to record.feedbackable.client.contact.full_name, client_path(record.feedbackable.client)
- else
= link_to record.feedbackable.group_offer.to_label, group_offer_path(record.feedbackable)
td= record.goals
td= record.achievements
td= record.future
td= record.comments
td= t(record.conversation)
td= link_to t_action(:show), url_for([@feedback_about, record])
td= link_to t_action(:edit), url_for([@feedback_about, record]) + '/edit'
td= link_to t_action(:destroy), url_for([@feedback_about, record]), confirm_deleting(record)
tbody
- @feedbacks.each do |record|
tr
td= link_to record.volunteer.contact.full_name, volunteer_path(record.volunteer)
td
- if record.assignment?
= link_to record.feedbackable.client.contact.full_name, client_path(record.feedbackable.client)
- else
= link_to record.feedbackable.group_offer.to_label, group_offer_path(record.feedbackable)
td= record.goals
td= record.achievements
td= record.future
td= record.comments
td= t(record.conversation)
td= link_to t_action(:show), url_for([@feedback_about, record])
td= link_to t_action(:edit), url_for([@feedback_about, record]) + '/edit'
td= link_to t_action(:destroy), url_for([@feedback_about, record]), confirm_deleting(record)
.row
.col-xs-12
= form_navigation_btn :new
.row
.col-xs-12
......
= simple_form_for [@volunteer, @hour] do |f|
= simple_error_notice f
= f.hidden_field :volunteer_id, value: params[:volunteer_id] || @hour.volunteer.id
.row
.col-xs-12
= f.input :hourable_id_and_type, as: :select, collection: @volunteer.assignment_group_offer_collection
.row
.col-xs-12
= f.hidden_field :volunteer_id, value: params[:volunteer_id] || @hour.volunteer.id
.row
.col-xs-12
......
......@@ -5,17 +5,17 @@
nav.navbar.section-navigation
ul.list-inline
- if policy(Volunteer).can_manage?
li = render 'journals/journal_block', journaled_for: @volunteer
li= render 'journals/journal_block', journaled_for: @volunteer
ul.list-inline
- if @volunteer.seeking_clients?
li = button_link t_title(:new, Assignment), new_assignment_path(volunteer_id: @volunteer)
li= button_link t_title(:new, Assignment), new_assignment_path(volunteer_id: @volunteer)
- if @volunteer.assignments.any?
li
= simple_form_for [@volunteer, BillingExpense.new] do |f|
= f.hidden_field :volunteer_id, value: @volunteer.id
= f.button :submit
- if @volunteer.billing_expenses.any?
li = button_link t_title(:index, BillingExpense), volunteer_billing_expenses_path(@volunteer)
li= button_link t_title(:index, BillingExpense), volunteer_billing_expenses_path(@volunteer)
- if @volunteer.assignments?
li= button_link t('.new_certificate'), new_volunteer_certificate_path(@volunteer)
- if @volunteer.certificates.size == 1
......
......@@ -147,6 +147,9 @@ de:
resigned: Abgemeldet
updated_at: Geändert um
volunteer_applications: &id-volunteers-by-actions
feedback:
new: Neues Feedback
index: Feedback Liste
acceptance: &id-volunteer-acceptance
management: Aufnahme Verwaltung
accepted: Akzeptiert
......
......@@ -145,6 +145,9 @@ en:
reserved: Reserved
resigned: Terminated
volunteer_applications: &id-volunteers-by-actions
feedback: &id-volunteer-feedbacks
new: New feedback
index: Feedback index
acceptance: &id-volunteer-aceptance
management: Acceptance Management
accepted: Accepted
......
......@@ -3,7 +3,6 @@ Rails.application.routes.draw do
get :thanks, on: :collection
end
# Authenticated routes start here (Watch out!)
devise_for :users
resources :clients do
......@@ -13,8 +12,13 @@ Rails.application.routes.draw do
end
resources :departments
resources :performance_reports
resources :profiles, except: [:destroy, :index]
resources :volunteer_emails
resources :users
resources :profiles, except: [:destroy, :index]
resources :reminders, only: [:index, :update, :destroy]
resources :group_offer_categories, except: [:destroy]
resources :feedbacks, only: [:new, :create]
resources :volunteers do
get :seeking_clients, on: :collection
get :find_client, on: :member, to: 'assignments#find_client'
......@@ -22,19 +26,16 @@ Rails.application.routes.draw do
resources :hours
resources :billing_expenses, except: [:edit, :update]
resources :certificates
resources :feedbacks
end
resources :volunteer_emails
resources :profiles, except: [:destroy, :index]
resources :assignments do
resources :feedbacks
end
resources :reminders, only: [:index, :update, :destroy]
resources :group_offers do
get :archived, on: :collection
put :change_active_state, on: :member
resources :feedbacks
end
resources :group_offer_categories, except: [:destroy]
root 'application#home'
end
......@@ -14,11 +14,11 @@ class FeedbackPolicyTest < PolicyAssertions::Test
end
test 'volunteer has limited access' do
volunteer = create(:user_volunteer)
feedback = create :feedback
feedback_volunteer = create :feedback, author: volunteer
refute_permit(volunteer, feedback, 'show?', 'edit?', 'update?', 'destroy?')
assert_permit(volunteer, feedback_volunteer, 'index?', 'show?', 'edit?', 'update?',
volunteer = create(:volunteer, user: create(:user_volunteer))
foreign_feedback = create :feedback, volunteer: volunteer, author: create(:user)
feedback_volunteer = create :feedback, author: volunteer.user
refute_permit(volunteer.user, foreign_feedback, 'show?', 'edit?', 'update?', 'destroy?')
assert_permit(volunteer.user, feedback_volunteer, 'index?', 'show?', 'edit?', 'update?',
'destroy?')
end
end
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