Skip to content

Instantly share code, notes, and snippets.

@jcreed
Last active November 16, 2023 17:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcreed/b6b657b244cf204d50cee882e9a2f8e8 to your computer and use it in GitHub Desktop.
Save jcreed/b6b657b244cf204d50cee882e9a2f8e8 to your computer and use it in GitHub Desktop.
assoc controller update
class Admin::AssociationsController < Admin::BaseController
def index
Association.transaction do
@search_manager = SearchManagers::CasSearchManager.new(current_user_identity, params)
@associations = @search_manager.search
@counts = {
institutions: institutions_for_acceptable_associations_count,
organizations: organizations_for_acceptable_associations_count,
programs: programs_for_acceptable_associations_count,
users: users_for_acceptable_associations_count,
}
end
end
def show
redirect_to edit_admin_association_path(find_association)
end
def edit
@association = find_association
@features = Permissions::Feature.order(:name)
@organizations = Organization.order(:name)
@min_allowable_deadline = Program.
where(association_id: @association.id).
minimum("hard_deadline")
@roles = Permissions::Role.all
@subpanel_presenter = WorkGroups::SubpanelPresenter.new
end
def update
@association = find_association
new_params = prepare_subpanels
if @association.update(new_params)
logo = params[:association][:logo]
if logo.present?
@association.logo.attach(
io: logo,
key: "#{@association.s3_alias}/logo/#{logo.original_filename}",
filename: logo.original_filename,
)
end
if @association.invalid?
redirect_to(edit_admin_association_path(@association),
notice: "Unable to update Association: #{@association.errors.full_messages.join(', ')}")
else
redirect_to(admin_associations_path, notice: "Updated Association.")
end
else
redirect_to(edit_admin_association_path(@association),
notice: "Unable to update Association: #{@association.errors.full_messages.join(', ')}")
end
end
def advance_interview_feature_and_use_sso
@association = find_association
render json: { has_feature: @association&.has_feature?('advanced_interview_management'), has_use_sso: @association.use_sso }
end
protected
def find_association
@_association ||= Association.find(params[:id])
end
def association_params
params.require(:association).permit(
:name,
:help_manual_url,
:document_name,
:cas_token,
:profession_name,
:profession_token,
:matriculated_elsewhere_feature_name,
:exclude_from_summary_count,
:exclude_from_identity_switcher,
:max_allowable_deadline,
:use_sso,
:logo,
:logo_border_color,
feature_ids: [],
subpanel_ids: [],
)
end
# Get the associations it is acceptable to count for.
def acceptable_associations
@acceptable_associations ||= Association.included_in_summary_count
end
def institutions_for_acceptable_associations_count
Institution.joins(:cases).
merge(acceptable_associations).
count
end
def organizations_for_acceptable_associations_count
Organization.joins(:cas).
merge(acceptable_associations).
count
end
def programs_for_acceptable_associations_count
Program.joins(:cas).
merge(acceptable_associations).
count
end
def users_for_acceptable_associations_count
User.joins(user_identities: { user_identity_cas: :cas }).
merge(Permissions::UserIdentity.active).
merge(acceptable_associations).
count
end
def dup_association_params
dup_association_params = association_params.except(:logo)
dup_association_params[:max_allowable_deadline] = Date.strptime(dup_association_params[:max_allowable_deadline], '%m/%d/%Y') rescue nil
dup_association_params
end
private
def prepare_subpanels
new_params = if current_feature_ids.include?(specific_cas_custom_question_feature_id) && panel_on?('CAS Custom Questions')
swapout_custom_question_panel('CAS Custom Questions')
elsif current_feature_ids.exclude?(specific_cas_custom_question_feature_id) && panel_on?('All CAS Custom Question Subpanels')
swapout_custom_question_panel('All CAS Custom Question Subpanels')
end
end
def swapout_custom_question_panel(panel)
current_params = dup_association_params
ccq_id = Subpanel.find_by_name('CAS Custom Questions')&.id.to_s
accqs_id = Subpanel.find_by_name('All CAS Custom Question Subpanels')&.id.to_s
if panel == 'CAS Custom Questions'
remove_panel_id = ccq_id
add_panel_id = accqs_id
else
remove_panel_id = accqs_id
add_panel_id = ccq_id
end
subpanels = current_params[:subpanel_ids].reject { |id| id == remove_panel_id }
subpanels << add_panel_id
current_params[:subpanel_ids] = subpanels
current_params
end
def specific_cas_custom_question_feature_id
Permissions::Feature.find_by_name('question_block_specific_cas_custom_question_subpanels')&.id.to_s
end
def current_feature_ids
association_params[:feature_ids]
end
def panel_on?(panel)
@association.subpanels.find_by_name(panel).present?
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment