Skip to content

Commit 2cddc7a

Browse files
authored
Fix model promotions (#815)
* set story attributes if story idea present * update form and controller * handle promoting assest in workshop idea * add tailwind styles to form * add asset promotion to story ideas
1 parent 322bdcf commit 2cddc7a

9 files changed

Lines changed: 127 additions & 34 deletions

File tree

app/controllers/stories_controller.rb

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,13 @@ def show
3333
end
3434

3535
def new
36-
@story = Story.new.decorate
37-
@story = @story.decorate
36+
if params[:story_idea_id].present?
37+
@story_idea = StoryIdea.find(params[:story_idea_id])
38+
@story = Story.new(set_story_attributes_from(@story_idea))
39+
else
40+
@story = Story.new
41+
end
42+
@story.decorate
3843
set_form_variables
3944
end
4045

@@ -52,7 +57,9 @@ def create
5257
@story = Story.new(story_params)
5358

5459
if @story.save
55-
if params.dig(:library_asset, :new_assets).present?
60+
if params[:promote_idea_assets] == "true"
61+
@story.attach_assets_from_idea!
62+
elsif params.dig(:library_asset, :new_assets).present?
5663
update_asset_owner(@story)
5764
end
5865

@@ -108,4 +115,15 @@ def story_params
108115
:created_by_id, :updated_by_id, :story_idea_id, :spotlighted_facilitator_id
109116
)
110117
end
118+
119+
def set_story_attributes_from(idea)
120+
{
121+
rhino_body: idea.body,
122+
project_id: idea.project.id,
123+
workshop_id: idea.workshop_id,
124+
external_workshop_title: idea.external_workshop_title,
125+
windows_type_id: idea.windows_type_id,
126+
youtube_url: idea.youtube_url
127+
}
128+
end
111129
end

app/controllers/workshop_ideas_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def create
2929
notification_type: 0)
3030

3131
if params.dig(:library_asset, :new_assets).present?
32-
update_asset_owner(@workshop)
32+
update_asset_owner(@workshop_idea)
3333
end
3434

3535
redirect_to workshop_ideas_path, notice: "Workshop idea was successfully created."

app/controllers/workshops_controller.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ def create
8383
Workshop.transaction do
8484
if @workshop.save
8585
assign_associations(@workshop)
86-
if params.dig(:library_asset, :new_assets).present?
86+
if params[:promote_idea_assets] == "true"
87+
@workshop.attach_assets_from_idea!
88+
elsif params.dig(:library_asset, :new_assets).present?
8789
update_asset_owner(@workshop)
8890
end
8991
success = true
@@ -298,7 +300,7 @@ def workshop_params
298300
sector_ids: [],
299301
workshop_series_children_attributes: [ :id, :workshop_child_id, :workshop_parent_id, :theme_name,
300302
:series_description, :series_description_spanish,
301-
:position, :_destroy ],
303+
:position, :_destroy ]
302304
)
303305
end
304306

app/models/story.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,14 @@ def organization_locality
8383
def organization_description
8484
project&.organization_description
8585
end
86+
87+
def attach_assets_from_idea!
88+
return unless story_idea
89+
story_idea.assets.find_each do |asset|
90+
new_asset = assets.build(type: asset.type)
91+
new_asset.file.attach(asset.file.blob)
92+
end
93+
94+
save!
95+
end
8696
end

app/models/workshop.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,16 @@ def featured_or_visitor_featured_changed?
291291
featured_changed? || visitor_featured_changed? || inactive_changed?
292292
end
293293

294+
def attach_assets_from_idea!
295+
return unless workshop_idea
296+
297+
workshop_idea.assets.find_each do |asset|
298+
new_asset = assets.build(type: asset.type)
299+
new_asset.file.attach(asset.file.blob)
300+
end
301+
302+
save!
303+
end
294304

295305
private
296306

app/services/workshop_from_idea_service.rb

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ def initialize(workshop_idea, user:)
99
def call
1010
Workshop.new(attributes_from_idea).tap do |workshop|
1111
duplicate_series_children(workshop)
12-
duplicate_assets(workshop)
1312
end
1413
end
1514

@@ -19,26 +18,44 @@ def call
1918

2019
def attributes_from_idea
2120
workshop_idea.attributes.slice(
22-
"title", "objective", "objective_spanish",
23-
"materials", "materials_spanish",
24-
"optional_materials", "optional_materials_spanish",
25-
"setup", "setup_spanish",
26-
"introduction", "introduction_spanish",
27-
"demonstration", "demonstration_spanish",
28-
"warm_up", "warm_up_spanish",
29-
"creation", "creation_spanish",
30-
"closing", "closing_spanish",
31-
"opening_circle", "opening_circle_spanish",
32-
"notes", "notes_spanish",
33-
"tips", "tips_spanish",
34-
"windows_type_id", "age_range", "age_range_spanish",
35-
"visualization", "visualization_spanish",
36-
"extra_field", "extra_field_spanish",
37-
"misc1", "misc1_spanish", "misc2", "misc2_spanish",
21+
"title", "windows_type_id", "age_range",
3822
"time_intro", "time_closing", "time_creation",
3923
"time_demonstration", "time_warm_up",
4024
"time_opening", "time_opening_circle"
4125
).merge(
26+
rhino_objective: workshop_idea.rhino_objective,
27+
rhino_materials: workshop_idea.rhino_materials,
28+
rhino_optional_materials: workshop_idea.rhino_optional_materials,
29+
rhino_setup: workshop_idea.rhino_setup,
30+
rhino_introduction: workshop_idea.rhino_introduction,
31+
rhino_demonstration: workshop_idea.rhino_demonstration,
32+
rhino_warm_up: workshop_idea.rhino_warm_up,
33+
rhino_creation: workshop_idea.rhino_creation,
34+
rhino_closing: workshop_idea.rhino_closing,
35+
rhino_opening_circle: workshop_idea.rhino_opening_circle,
36+
rhino_notes: workshop_idea.rhino_notes,
37+
rhino_tips: workshop_idea.rhino_tips,
38+
rhino_visualization: workshop_idea.rhino_visualization,
39+
rhino_extra_field: workshop_idea.rhino_extra_field,
40+
rhino_misc1: workshop_idea.rhino_misc1,
41+
rhino_misc2: workshop_idea.rhino_misc2,
42+
rhino_objective_spanish: workshop_idea.rhino_objective_spanish,
43+
rhino_materials_spanish: workshop_idea.rhino_materials_spanish,
44+
rhino_optional_materials_spanish: workshop_idea.rhino_optional_materials_spanish,
45+
rhino_age_range_spanish: workshop_idea.rhino_age_range_spanish,
46+
rhino_setup_spanish: workshop_idea.rhino_setup_spanish,
47+
rhino_introduction_spanish: workshop_idea.rhino_introduction_spanish,
48+
rhino_opening_circle_spanish: workshop_idea.rhino_opening_circle_spanish,
49+
rhino_demonstration_spanish: workshop_idea.rhino_demonstration_spanish,
50+
rhino_warm_up_spanish: workshop_idea.rhino_warm_up_spanish,
51+
rhino_visualization_spanish: workshop_idea.rhino_visualization_spanish,
52+
rhino_creation_spanish: workshop_idea.rhino_creation_spanish,
53+
rhino_closing_spanish: workshop_idea.rhino_closing_spanish,
54+
rhino_notes_spanish: workshop_idea.rhino_notes_spanish,
55+
rhino_tips_spanish: workshop_idea.rhino_tips_spanish,
56+
rhino_misc1_spanish: workshop_idea.rhino_misc1_spanish,
57+
rhino_misc2_spanish: workshop_idea.rhino_misc2_spanish,
58+
rhino_extra_field_spanish: workshop_idea.rhino_extra_field_spanish,
4259
user_id: user.id,
4360
workshop_idea_id: workshop_idea.id,
4461
month: workshop_idea.created_at.month,
@@ -61,10 +78,4 @@ def duplicate_series_children(workshop)
6178
end
6279
)
6380
end
64-
65-
def duplicate_assets(workshop)
66-
workshop_idea.assets.each do |image|
67-
workshop.assets.build(file: image.file.blob)
68-
end
69-
end
7081
end

app/views/stories/_form.html.erb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,24 @@
125125
<%= f.button :submit, class: "btn btn-primary" %>
126126
</div>
127127
</div>
128+
129+
<% if story_idea %>
130+
<div class="mt-6">
131+
<%= label_tag :promote_idea_assets, class: "flex items-start space-x-3 cursor-pointer" do %>
132+
<%= check_box_tag :promote_idea_assets, true, false, class: "mt-1 h-5 w-5 text-blue-600 rounded border-gray-300 focus:ring-blue-500" %>
133+
134+
<div class="ml-2 flex flex-col">
135+
<span class="text-gray-900 font-medium">
136+
If the story idea was submitted with attachments, check this box to transfer them to this new story.
137+
</span>
138+
139+
<p class="text-gray-500 text-sm mt-1">
140+
(This will override any attachments uploaded in the form below once you click submit.)
141+
</p>
142+
</div>
143+
<% end %>
144+
</div>
145+
<% end %>
128146
<% end %>
129147

130148
<div class="my-4"><%= render "assets/form", owner: @story %></div>

app/views/workshop_ideas/_form.html.erb

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77

88
<%= tag.div class: "unpersisted_resource_asset_params" %>
99

10-
<% if current_user.super_user && f.object.workshops.any? %>
10+
<%= f.hidden_field :created_by_id, value: current_user&.id unless f.object.persisted? %>
11+
<%= f.hidden_field :updated_by_id, value: current_user&.id %>
12+
13+
<% promoted_to_workshop = f.object.workshops.any? %>
14+
<% if promoted_to_workshop %>
1115
<div class="admin-only bg-yellow-100 m-3 p-3">
1216
This workshops idea has been promoted to a
1317
<% f.object.workshops.each do |workshop| %>
@@ -16,11 +20,13 @@
1620
<% end %>
1721
and should not be edited here.
1822
</div>
23+
<% elsif current_user.super_user? && f.object.persisted? %>
24+
<div class="admin-only bg-blue-100 m-3 p-3">
25+
<%= link_to "Promote to Workshop", new_workshop_path(workshop_idea_id: @workshop_idea.id),
26+
class: "btn btn-secondary-outline",
27+
data: { confirm: "All edits must be saved before promoting. Are you sure you wish to promote?All edits must be saved before promoting." } %>
28+
</div>
1929
<% end %>
20-
21-
<%= f.hidden_field :created_by_id, value: current_user&.id unless f.object.persisted? %>
22-
<%= f.hidden_field :updated_by_id, value: current_user&.id %>
23-
2430
<!-- Row 1 -->
2531
<div class="grid grid-cols-1 md:grid-cols-3 gap-6 mb-6">
2632
<div>

app/views/workshops/_form.html.erb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,24 @@
431431

432432
<%= f.submit "Submit", class: "btn btn-primary" %>
433433
</div>
434+
435+
<% if @workshop_idea %>
436+
<div class="mt-6">
437+
<%= label_tag :promote_idea_assets, class: "flex items-start space-x-3 cursor-pointer" do %>
438+
<%= check_box_tag :promote_idea_assets, true, false, class: "mt-1 h-5 w-5 text-blue-600 rounded border-gray-300 focus:ring-blue-500" %>
439+
440+
<div class="ml-2 flex flex-col">
441+
<span class="text-gray-900 font-medium">
442+
If the workshop idea was submitted with attachments, check this box to transfer them to this new workshop.
443+
</span>
444+
445+
<p class="text-gray-500 text-sm mt-1">
446+
(This will override any attachments uploaded in the form below once you click submit.)
447+
</p>
448+
</div>
449+
<% end %>
450+
</div>
451+
<% end %>
434452
<% end %>
435453
</div>
436454

0 commit comments

Comments
 (0)