Skip to content

Commit

Permalink
Section: Member may upload a HeroImage (#2101)
Browse files Browse the repository at this point in the history
  • Loading branch information
rosschapman authored Jan 25, 2024
1 parent f852ab3 commit 91a1cf5
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 3 deletions.
12 changes: 11 additions & 1 deletion app/controllers/rooms_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,17 @@ def create

def update
respond_to do |format|
if room.update(room_params)
# TODO: Move logic for Media resource management to a dedicated controller (see: https://github.com/zinc-collective/convene/pull/2101/files#r1464115624)
new_media = Media.create
new_media.upload.attach(room_params[:hero_image_upload])
room_params_for_update = {}.merge(
room_params.except(:hero_image_upload),
{
hero_image: new_media
}
)

if room.update(room_params_for_update)
format.html do
redirect_to [:edit, room.space], notice: t(".success", room_name: room.name)
end
Expand Down
2 changes: 2 additions & 0 deletions app/models/room.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# A Room in Convene acts as a gathering place.
class Room < ApplicationRecord
belongs_to :hero_image, class_name: "Media", optional: true

# The space whose settings govern the default publicity and access controls for the Room.
belongs_to :space, inverse_of: :rooms
location(parent: :space)
Expand Down
2 changes: 1 addition & 1 deletion app/policies/room_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def create?
alias_method :new?, :create?

def permitted_attributes(params)
[:access_level, :name, :description, :slug, gizmos_attributes:
[:access_level, :name, :description, :slug, :hero_image_upload, gizmos_attributes:
policy(Furniture).permitted_attributes(params)]
end

Expand Down
7 changes: 6 additions & 1 deletion app/views/application/_file_field.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
<% required ||= required | false %>
<div>
<%= form.label attribute %>
<%= form.label local_assigns[:label] || attribute %>
<% if local_assigns[:label_hint] %>
<div>
<small><%= label_hint %></small>
</div>
<% end %>
<%= form.file_field attribute, required: required %>
<%= render partial: "error", locals: { model: form.object, attribute: attribute } %>
</div>
12 changes: 12 additions & 0 deletions app/views/rooms/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@
<%= render "text_field", attribute: :name, form: room_form %>
<%= render "text_field", attribute: :slug, form: room_form %>
<%= render "text_area", attribute: :description, form: room_form, label_hint: 'Add brief summary of your section for search engines to display as snippets in results' %>
<%- if room.hero_image&.upload&.attached? %>
<div class="relative overflow-hidden bg-gray-100 dark:bg-gray-600">
<%= image_tag room.hero_image&.upload %>
</div>
<% end %>
<%= render "file_field",
attribute: :hero_image_upload,
form: room_form,
label: "Upload a header image",
label_hint: "Images are great for marketing!",
required: false
%>
<h3>Privacy and Security</h3>
<%= render "radio_group", attribute: :access_level, options: Room::access_levels.values, form: room_form %>
<footer>
Expand Down
8 changes: 8 additions & 0 deletions db/migrate/20240120034325_add_hero_image_to_rooms.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class AddHeroImageToRooms < ActiveRecord::Migration[7.1]
def change
safety_assured {
add_column :rooms, :hero_image_id, :uuid, null: true, default: nil
add_foreign_key :rooms, :media, column: "hero_image_id"
}
end
end
2 changes: 2 additions & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"expired",
"ignored",
"revoked",
"sent",
], force: :cascade

create_enum :membership_status, [
Expand Down Expand Up @@ -322,6 +323,7 @@
add_foreign_key "marketplace_tax_rates", "furnitures", column: "marketplace_id"
add_foreign_key "marketplace_tax_rates", "spaces", column: "bazaar_id"
add_foreign_key "memberships", "invitations"
add_foreign_key "rooms", "media", column: "hero_image_id"
add_foreign_key "space_agreements", "spaces"
add_foreign_key "spaces", "rooms", column: "entrance_id"
end
4 changes: 4 additions & 0 deletions spec/factories/room.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,9 @@
create_list(:furniture, evaluator.furniture_count, room: room)
end
end

trait :with_hero_image do
hero_image factory: :media
end
end
end

0 comments on commit 91a1cf5

Please sign in to comment.