Skip to content

Commit

Permalink
Merge pull request #18 from OmarMWarraich/17-develop-an-image-gallery
Browse files Browse the repository at this point in the history
feat: Image Gallery
  • Loading branch information
OmarMWarraich authored Jul 23, 2024
2 parents d56e15b + 05e97a5 commit 21b9d83
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 1 deletion.
13 changes: 13 additions & 0 deletions app/controllers/generated_images_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class GeneratedImagesController < ApplicationController
before_action :authenticate_user!

def show
@generated_image = current_user.generated_images.find(params[:id])

@generated_image.broadcast_to_main_image

respond_to do |format|
format.html { head :ok }
end
end
end
2 changes: 2 additions & 0 deletions app/controllers/txt2_imgs_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ def index

result = api_instance.get_sd_models_sdapi_v1_sd_models_get
@models = result.map { |model| model.model_name }

@gallery = current_user.generated_images.order(created_at: :desc).limit(30)
end

def create
Expand Down
2 changes: 2 additions & 0 deletions app/helpers/generated_images_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module GeneratedImagesHelper
end
18 changes: 18 additions & 0 deletions app/models/generated_image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ class GeneratedImage < ApplicationRecord
after_create_commit { broadcast_created }

def broadcast_created
broadcast_to_main_image

broadcast_to_gallery
end

def broadcast_to_main_image
broadcast_update_to(
"#{dom_id(user)}_main_image",
partial: "generated_images/display_main_image",
Expand All @@ -40,6 +46,18 @@ def broadcast_created
)
end

def broadcast_to_gallery
broadcast_prepend_to(
"#{dom_id(user)}_gallery",
partial: "/txt2_imgs/gallery",
locals: {
scroll_to: true,
gallery: self
},
target: "main_gallery"
)
end

def self.create_from_sd_render_job(render_settings, user_id, result)
user = User.find(user_id)
result.images.each do |src_image|
Expand Down
2 changes: 2 additions & 0 deletions app/views/generated_images/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1>GeneratedImages#show</h1>
<p>Find me in app/views/generated_images/show.html.erb</p>
9 changes: 9 additions & 0 deletions app/views/txt2_imgs/_gallery.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<div id="<%= dom_id(gallery)%>" class="col-4 mb-1 p-0 position-relative">
<div class='row'>
<div class='col'>
<%= link_to generated_image_path(gallery), method: :get, data: { turbo_frame: 'image_maker' } do %>
<%= image_tag gallery.sketch, class: 'img-thumbnail', alt: gallery.info, title: gallery.prompt %>
<% end %>
</div>
</div>
</div>
5 changes: 4 additions & 1 deletion app/views/txt2_imgs/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
</div>
<div class='col-md-2'>
<h5 class='border-bottom pb-2 mb-2'><i class="fas fa-duotone fa-images"></i> Gallery</h5>

<%= turbo_stream_from "#{dom_id(current_user)}_gallery" %>
<div id="main_gallery" class="row">
<%= render partial: '/txt2_imgs/gallery', collection: @gallery %>
</div>
</div>
</div>
</div>
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Rails.application.routes.draw do
# get 'generated_images/show'
# get 'txt2_imgs/index'
# get 'sessions/new'
# get 'registrations/new'
Expand All @@ -10,6 +11,7 @@
resource :password_reset # where password reset routes are defined
resource :password_change # where password change routes are defined
resource :txt2_imgs, only: [:index, :create] # where the main app routes are defined
resources :generated_images, only: [:show] # where the generated images routes are defined

# Reveal health status on /up that returns 200 if the app boots with no exceptions, otherwise 500.
# Can be used by load balancers and uptime monitors to verify that the app is live.
Expand Down
8 changes: 8 additions & 0 deletions test/controllers/generated_images_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require "test_helper"

class GeneratedImagesControllerTest < ActionDispatch::IntegrationTest
test "should get show" do
get generated_images_show_url
assert_response :success
end
end

0 comments on commit 21b9d83

Please sign in to comment.