group_offer_policy.rb 2.12 KB
Newer Older
1
class GroupOfferPolicy < ApplicationPolicy
2 3
  include GroupAssignmentsAttributes

4 5
  class Scope < ApplicationScope
    def resolve
6
      superadmin? || department_manager? ? all : none
7 8 9
    end
  end

10 11 12 13 14
  def permitted_attributes
    attributes = [
      :title, :offer_type, :offer_state, :necessary_volunteers, :description,
      :women, :men, :children, :teenagers, :unaccompanied, :all, :long_term, :regular,
      :short_term, :workday, :weekend, :morning, :afternoon, :evening, :flexible, :schedule_details,
15
      :creator_id, :organization, :location, :period_end, :group_offer_category_id, :comments,
16 17
      group_assignments_attributes
    ]
18
    attributes << :department_id if edit?
19 20 21
    attributes
  end

22 23
  def superadmin_or_department_manager_is_responsible?
    superadmin? || department_manager? && (user.department.any? || user.group_offers.any?)
24 25
  end

26
  # actions on collection
27 28
  alias_method :index?,  :superadmin_or_department_manager?
  alias_method :search?, :superadmin_or_department_manager?
29

30 31 32
  # actions related to creating a member
  alias_method :new?,              :superadmin_or_deparment_manager_has_department?
  alias_method :create?,           :superadmin_or_deparment_manager_has_department?
33

34
  # actions related to editing a member
35 36 37 38 39
  alias_method :edit?,                        :superadmin_or_department_manager_offer?
  alias_method :update?,                      :superadmin_or_department_manager_offer?
  alias_method :change_active_state?,         :superadmin_or_department_manager_offer?
  alias_method :initiate_termination?,        :superadmin_or_department_manager_offer?
  alias_method :submit_initiate_termination?, :superadmin_or_department_manager_offer?
40
  alias_method :end_all_assignments?,         :superadmin_or_department_manager_offer?
41
  alias_method :search_volunteer?,            :superadmin_or_departments_offer_or_volunteer_included?
42

43 44 45 46 47
  # action related to showing a member
  alias_method :show?, :superadmin_or_department_manager_or_volunteer_included?

  # supplemental privileges
  alias_method :show_comments?,         :superadmin_or_department_manager_offer?
48
  alias_method :supervisor_privileges?, :superadmin?
49
end