Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1031 added copy feature on impact card create form #1058

Merged
merged 1 commit into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/controllers/concerns/initiative_child_records.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ module InitiativeChildRecords
extend ActiveSupport::Concern

def update_stakeholders!(initiative, params)
return if initiative.blank?

update_child_records!(
initiative,
params[:initiatives_organisations_attributes],
Expand All @@ -15,6 +17,8 @@ def update_stakeholders!(initiative, params)
end

def update_subsystem_tags!(initiative, params)
return if initiative.blank?

update_child_records!(
initiative,
params[:initiatives_subsystem_tags_attributes],
Expand Down
19 changes: 18 additions & 1 deletion app/controllers/impact_cards_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,19 @@ def edit
# @linked_initiatives = build_linked_intiatives(source_scorecard, target_scorecard)
end

def create # rubocop:disable Metrics/AbcSize
def create # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
@impact_card = current_account.scorecards.build(impact_card_params)
authorize(@impact_card, policy_class: ScorecardPolicy)

if @impact_card.save
update_stakeholders!(@impact_card.initiatives.first, initiatives_organisations_params)
update_subsystem_tags!(@impact_card.initiatives.first, initiatives_subsystem_tags_params)

if params[:impact_card_source_id].present?
@source_impact_card = current_account.scorecards.find(params[:impact_card_source_id])
copy_initiatives(@impact_card, @source_impact_card) if @source_impact_card.present?
end

redirect_to(impact_card_path(@impact_card), notice: "#{@impact_card.model_name.human} was successfully created.")
else
render(:new)
Expand Down Expand Up @@ -278,6 +283,18 @@ def merge
# 'Copy from other card and link'
# end

def copy_initiatives(target_impact_card, source_impact_card)
existing_initiative_names = target_impact_card.initiatives.pluck(:name)

source_impact_card.initiatives.where.not(name: existing_initiative_names).find_each do |source_initiative|
target_initiative = source_initiative.dup
target_initiative.scorecard = target_impact_card
target_initiative.organisations = source_initiative.organisations
target_initiative.subsystem_tags = source_initiative.subsystem_tags
target_initiative.save!
end
end

def initiatives_organisations_params
params.fetch(:impact_card, {}).fetch(:initiatives_attributes, {}).fetch('0', {}).permit(
{
Expand Down
2 changes: 1 addition & 1 deletion app/views/application/_section.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<div class="grid grid-cols-1 gap-x-8 gap-y-10 border-b border-gray-900/10 pb-12 md:grid-cols-4">
<div class="grid grid-cols-1 gap-x-8 gap-y-10 border-b border-gray-900/10 pb-12 md:grid-cols-3">
<%= yield %>
</div>
3 changes: 3 additions & 0 deletions app/views/application/_section_body.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
<div class="<%= merge_tailwind_class('grid max-w-2xl grid-cols-1 gap-x-6 gap-y-8 sm:grid-cols-6 md:col-span-2', local_assigns[:class]) %>">
<%= yield %>
</div>



4 changes: 0 additions & 4 deletions app/views/icons/_old_settings.html.erb

This file was deleted.

90 changes: 51 additions & 39 deletions app/views/impact_cards/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
<div class="mx-auto max-w-6xl">
<%= form_with model: @impact_card, url: impact_cards_path, scope: :impact_card, class: 'mx-auto max-w-4xl' do |form| %>
<h1 class="text-2xl/8 font-semibold text-zinc-950 sm:text-xl/8 dark:text-white">New Impact Card - Initial Setup</h1>
<hr role="presentation" class="my-10 mt-6 w-full border-t border-zinc-950/10 dark:border-white/10">
<div class="space-y-12">
<div class="grid grid-cols-1 gap-x-8 gap-y-10 border-b border-gray-900/10 pb-12 md:grid-cols-3">
<div>
<h2 class="text-base/7 font-semibold text-zinc-950 dark:text-white">Overview</h2>
<p class="mt-1 text-sm/6 text-gray-600">General settings for your card.</p>
</div>

<div class="grid max-w-2xl grid-cols-1 gap-x-6 gap-y-8 sm:grid-cols-6 md:col-span-2">
<%= render 'form_header', title: 'New Impact Card - Initial Setup' %>
<%= render 'form_body' do %>
<%= render 'section' do %>
<%= render 'section_header', title: 'Overview', subtitle: 'General settings this impact card.' %>
<%= render 'section_body' do %>
<div class="sm:col-span-4">
<%= form.label :name, 'Title' %>
<%= form.label :name %>
<div class="mt-2">
<%= form.text_field(:name, placeholder: 'Impact card title', autofocus: true, required: true) %>
<%= form.text_field(:name, placeholder: 'Card name', autofocus: true, required: true) %>
</div>
</div>

Expand All @@ -22,35 +17,56 @@
<div class="mt-2">
<%= form.text_area :description, rows: 3, placeholder: '' %>
</div>
<p class="text-sm/6 text-gray-600">Write a brief description of your new card (optional).</p>
<p class="text-sm/6 text-gray-600">A brief description of the impact card (optional).</p>
</div>
</div>
</div>

<div class="grid grid-cols-1 gap-x-8 gap-y-10 border-b border-gray-900/10 dark:border-white/10 pb-12 md:grid-cols-3">
<div>
<h2 class="text-base/7 font-semibold text-zinc-950 dark:text-white">Data Collection</h2>
<p class="mt-1 text-sm/6 text-gray-600">Choose a data model to track activity.</p>
</div>
<% end %>
<% end %>

<div class="grid max-w-2xl grid-cols-1 gap-x-6 gap-y-8 sm:grid-cols-6 md:col-span-2">
<%= render 'section' do %>
<%= render 'section_header', title: 'Data Collection', subtitle: 'Choose a data model to track activity.' %>
<%= render 'section_body' do %>
<div class="sm:col-span-4">
<%= form.label :type, 'Data model' %>
<div class="mt-2">
<%= form.select :type, [TransitionCard, SustainableDevelopmentGoalAlignmentCard].map { |type| [type.name.titleize, type.name] } %>
</div>
</div>
</div>
</div>
<% end %>
<% end %>

<%= form.fields_for :initiatives do |initiatives_form| %>
<div class="grid grid-cols-1 gap-x-8 gap-y-10 border-b border-gray-900/10 dark:border-white/10 pb-12 md:grid-cols-3">
<div>
<h2 class="text-base/7 font-semibold text-zinc-950 dark:text-white">First Initiative</h2>
<p class="mt-1 text-sm/6 text-gray-600">Create your first initiative. You can add more later.</p>
</div>
<% if current_account.scorecards.count.positive? %>
<%= render 'section' do %>
<%= render 'section_header', title: 'Import Initiatives', subtitle: 'Copy initiatives from another impact card.' do %>
<p class="mt-1 text-sm/6 text-gray-600">
Note: this is a completely optional step, allowing you to quickly set up an impact card by copying the structure of an existing one.
</p>
<% end %>
<%= render 'section_body' do %>
<div class="sm:col-span-4">
<%= form.label :impact_card_source, 'Impact Card Source' %>
<div class="mt-2">
<%=
custom_select_tag(
:impact_card_source_id,
options_from_collection_for_select(policy_scope(Scorecard), :id, :name),
include_blank: 'Select an impact card'
)
%>
</div>
<p class="text-sm/6 text-gray-600 mt-1">Impact card to use as a template for initiatives (optional).</p>
</div>
<% end %>
<% end %>
<% end %>

<div class="grid max-w-2xl grid-cols-1 gap-x-6 gap-y-8 sm:grid-cols-6 md:col-span-2">
<%= form.fields_for :initiatives do |initiatives_form| %>
<%= render 'section' do %>
<%= render 'section_header', title: 'First Initiative', subtitle: 'Create your first initiative.' do %>
<p class="mt-1 text-sm/6 text-gray-600">
Note: you can add more initiatives later, after creating this impact card.
</p>
<% end %>
<%= render 'section_body' do %>
<div class="sm:col-span-4">
<%= initiatives_form.label :name %>
<div class="mt-2">
Expand All @@ -64,8 +80,8 @@
<%= initiatives_form.text_area :description, rows: 3, placeholder: 'Initiative description' %>
</div>
</div>
</div>
</div>
<% end %>
<% end %>

<div class="grid grid-cols-1 gap-x-8 gap-y-10 border-b border-gray-900/10 dark:border-white/10 pb-12 md:grid-cols-3">
<div>
Expand Down Expand Up @@ -172,11 +188,7 @@
</div>
</div>
<% end %>
</div>

<div class="mt-6 flex items-center justify-end gap-x-6">
<button type="button" class="text-sm/6 font-semibold text-gray-900 dark:text-white">Cancel</button>
<%= form.submit 'Save' %>
</div>
<% end %>
<%= render 'form_buttons', form:, cancel_path: impact_cards_path %>
<%- end %>
</div>
Loading