From 05e97a5b0c49bb0b43f478e0553b6e6ab8281f96 Mon Sep 17 00:00:00 2001 From: OmarMWarraich Date: Tue, 23 Jul 2024 18:45:53 +0500 Subject: [PATCH] feat: Image Gallery --- app/controllers/generated_images_controller.rb | 13 +++++++++++++ app/controllers/txt2_imgs_controller.rb | 2 ++ app/helpers/generated_images_helper.rb | 2 ++ app/models/generated_image.rb | 18 ++++++++++++++++++ app/views/generated_images/show.html.erb | 2 ++ app/views/txt2_imgs/_gallery.html.erb | 9 +++++++++ app/views/txt2_imgs/index.html.erb | 5 ++++- config/routes.rb | 2 ++ .../generated_images_controller_test.rb | 8 ++++++++ 9 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 app/controllers/generated_images_controller.rb create mode 100644 app/helpers/generated_images_helper.rb create mode 100644 app/views/generated_images/show.html.erb create mode 100644 app/views/txt2_imgs/_gallery.html.erb create mode 100644 test/controllers/generated_images_controller_test.rb diff --git a/app/controllers/generated_images_controller.rb b/app/controllers/generated_images_controller.rb new file mode 100644 index 0000000..5921639 --- /dev/null +++ b/app/controllers/generated_images_controller.rb @@ -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 diff --git a/app/controllers/txt2_imgs_controller.rb b/app/controllers/txt2_imgs_controller.rb index 965ac7d..d82153d 100644 --- a/app/controllers/txt2_imgs_controller.rb +++ b/app/controllers/txt2_imgs_controller.rb @@ -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 diff --git a/app/helpers/generated_images_helper.rb b/app/helpers/generated_images_helper.rb new file mode 100644 index 0000000..aeeb478 --- /dev/null +++ b/app/helpers/generated_images_helper.rb @@ -0,0 +1,2 @@ +module GeneratedImagesHelper +end diff --git a/app/models/generated_image.rb b/app/models/generated_image.rb index b7aa451..90bfbfd 100644 --- a/app/models/generated_image.rb +++ b/app/models/generated_image.rb @@ -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", @@ -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| diff --git a/app/views/generated_images/show.html.erb b/app/views/generated_images/show.html.erb new file mode 100644 index 0000000..83a8c3a --- /dev/null +++ b/app/views/generated_images/show.html.erb @@ -0,0 +1,2 @@ +

GeneratedImages#show

+

Find me in app/views/generated_images/show.html.erb

diff --git a/app/views/txt2_imgs/_gallery.html.erb b/app/views/txt2_imgs/_gallery.html.erb new file mode 100644 index 0000000..cd24f73 --- /dev/null +++ b/app/views/txt2_imgs/_gallery.html.erb @@ -0,0 +1,9 @@ +
+
+
+ <%= 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 %> +
+
+
diff --git a/app/views/txt2_imgs/index.html.erb b/app/views/txt2_imgs/index.html.erb index c247554..947de16 100644 --- a/app/views/txt2_imgs/index.html.erb +++ b/app/views/txt2_imgs/index.html.erb @@ -17,7 +17,10 @@
Gallery
- + <%= turbo_stream_from "#{dom_id(current_user)}_gallery" %> +
\ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 0dbbdfb..05a47c5 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,5 @@ Rails.application.routes.draw do + # get 'generated_images/show' # get 'txt2_imgs/index' # get 'sessions/new' # get 'registrations/new' @@ -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. diff --git a/test/controllers/generated_images_controller_test.rb b/test/controllers/generated_images_controller_test.rb new file mode 100644 index 0000000..5907442 --- /dev/null +++ b/test/controllers/generated_images_controller_test.rb @@ -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