Skip to content

Commit

Permalink
Split date_loc_invites permission
Browse files Browse the repository at this point in the history
  • Loading branch information
smoyte committed Sep 18, 2023
1 parent 554e915 commit 0f91f59
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 14 deletions.
5 changes: 3 additions & 2 deletions app/models/meals/import.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Import < ApplicationRecord

BASIC_HEADERS = %i[served_at calendars formula communities action id].freeze
REQUIRED_HEADERS = %i[served_at calendars].freeze
DB_ID_REGEX = /\A\d+\z/.freeze
DB_ID_REGEX = /\A\d+\z/

acts_as_tenant :cluster

Expand Down Expand Up @@ -114,7 +114,8 @@ def assign_defaults_to_new_meal

def copy_attribs_to_target_meal
attribs = []
attribs.concat(%i[served_at calendars communities]) if row_policy.change_date_loc_invites?
attribs.concat(%i[served_at calendars]) if row_policy.change_date_loc?
attribs.concat(%i[communities]) if row_policy.change_invites?
attribs << :formula if row_policy.change_formula?
attribs << :assignments if row_policy.change_workers?
attribs.each { |attrib| target_meal.send("#{attrib}=", new_meal.send(attrib)) }
Expand Down
23 changes: 16 additions & 7 deletions app/policies/meals/meal_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Meals
class MealPolicy < ApplicationPolicy
alias meal record
alias_method :meal, :record

class Scope < Scope
ASSIGNED = "EXISTS (SELECT id FROM meal_assignments
Expand All @@ -21,7 +21,7 @@ def resolve
scope
elsif user.active?
scope.where("#{ASSIGNED} OR #{INVITED} OR #{SIGNED_UP}",
user.id, user.community_id, user.household_id)
user.id, user.community_id, user.household_id)
else
scope.where(SIGNED_UP, user.household_id)
end
Expand Down Expand Up @@ -53,7 +53,7 @@ def import?
end

def update?
change_date_loc_invites? || change_formula? || change_menu? || change_workers?
change_date_loc? || change_invites? || change_formula? || change_menu? || change_workers?
end

def destroy?
Expand Down Expand Up @@ -88,7 +88,11 @@ def show_reimbursement_details?
active_admin_or?(:meals_coordinator, :biller) || head_cook?
end

def change_date_loc_invites?
def change_date_loc?
active_admin_or?(:meals_coordinator)
end

def change_invites?
active_admin_or?(:meals_coordinator)
end

Expand Down Expand Up @@ -129,7 +133,8 @@ def permitted_attributes
permitted = []
permitted.concat(worker_attribs) if change_workers?
permitted.concat(menu_attribs) if change_menu?
permitted.concat(date_loc_invite_attribs) if change_date_loc_invites?
permitted.concat(date_loc_attribs) if change_date_loc?
permitted.concat(invite_attribs) if change_invites?
permitted.concat(signup_attribs) if change_signups?
permitted.concat(expense_attribs) if change_expenses?
permitted << :formula_id if change_formula?
Expand Down Expand Up @@ -176,8 +181,12 @@ def head_cook?
active? && user == meal.head_cook
end

def date_loc_invite_attribs
[:served_at, {community_boxes: [Community.all.map(&:id).map(&:to_s)]}, {calendar_ids: []}]
def date_loc_attribs
[:served_at, {calendar_ids: []}]
end

def invite_attribs
[{community_boxes: [Community.all.map(&:id).map(&:to_s)]}]
end

def menu_attribs
Expand Down
4 changes: 2 additions & 2 deletions app/views/meals/meals/form/_general.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<%= meal.form_section(:general, expanded: local_assigns[:expanded]) do %>
<div class="fields">
<% if policy(meal).change_date_loc_invites? %>
<% if policy(meal).change_date_loc? %>
<%= f.input :served_at, as: :datetime_picker, include_day_of_week: true %>
<%= f.input :calendar_ids, collection: @calendar_options, label_method: :name_with_prefix,
input_html: {multiple: "multiple"} %>
Expand All @@ -21,7 +21,7 @@
<% end %>
<% if multi_community? %>
<% if policy(meal).change_date_loc_invites? %>
<% if policy(meal).change_invites? %>
<div class="form-group text optional
<%= meal.errors[:invitations].present? ? "has-error" : "" %>">
<label class="text optional col-sm-3 control-label" for="meal_invitations">Communities</label>
Expand Down
6 changes: 3 additions & 3 deletions spec/policies/meals/meal_policy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@
end
end

permissions :new?, :create?, :import?, :change_date_loc_invites?,
:change_workers_without_notification? do
permissions :new?, :create?, :import?, :change_date_loc?, :change_invites?,
:change_workers_without_notification? do
it_behaves_like "permits admins or special role but not regular users", :meals_coordinator
end

Expand All @@ -68,7 +68,7 @@
end

permissions :change_formula?, :change_menu?, :change_signups?, :change_capacity_close_time?, :change_expenses?,
:close?, :cancel? do
:close?, :cancel? do
it_behaves_like "permits admins or special role but not regular users", :meals_coordinator
it_behaves_like "forbids if finalized"

Expand Down

0 comments on commit 0f91f59

Please sign in to comment.