Skip to content

Commit

Permalink
Merge pull request #20 from OmarMWarraich/19-manage-image-deletion
Browse files Browse the repository at this point in the history
feat: Image Deletion
  • Loading branch information
OmarMWarraich authored Jul 23, 2024
2 parents 21b9d83 + 2028e0a commit 616c95b
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ gem "tzinfo-data", platforms: %i[ windows jruby ]
gem "bootsnap", require: false

# Use Active Storage variants [https://guides.rubyonrails.org/active_storage_overview.html#transforming-images]
# gem "image_processing", "~> 1.2"
gem "image_processing", "~> 1.2"

group :development, :test do
# See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem
Expand Down
5 changes: 5 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ GEM
activesupport (>= 6.1)
i18n (1.14.5)
concurrent-ruby (~> 1.0)
image_processing (1.12.2)
mini_magick (>= 4.9.5, < 5)
ruby-vips (>= 2.0.17, < 3)
importmap-rails (2.0.1)
actionpack (>= 6.0.0)
activesupport (>= 6.0.0)
Expand Down Expand Up @@ -165,6 +168,7 @@ GEM
net-smtp
marcel (1.0.4)
matrix (0.4.2)
mini_magick (4.12.0)
mini_mime (1.1.5)
minitest (5.24.0)
msgpack (1.7.2)
Expand Down Expand Up @@ -324,6 +328,7 @@ DEPENDENCIES
debug
dotenv-rails (~> 2.8)
font-awesome-sass (~> 5.15.1)
image_processing (~> 1.2)
importmap-rails
jbuilder
letter_opener
Expand Down
8 changes: 7 additions & 1 deletion app/assets/stylesheets/application.scss
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,10 @@

@import "bootstrap";
@import "font-awesome-sprockets";
@import "font-awesome";
@import "font-awesome";

.btn-transparent {
background-color: rgba(0, 0, 0, 0.3);
border: none;
color: whitesmoke;
}
9 changes: 9 additions & 0 deletions app/controllers/generated_images_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,13 @@ def show
format.html { head :ok }
end
end

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

respond_to do |format|
format.turbo_stream { render turbo_stream: turbo_stream.remove(@generated_image), notice: "Generated image was successfully destroyed." }
end
end
end
15 changes: 10 additions & 5 deletions app/controllers/txt2_imgs_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,19 @@ def index
config = RStableDiffusionAI::Configuration.new
config.host = ENV['SD_API_HOST']
config.scheme = ENV['SD_API_SCHEME']
config.debugging = true
config.timeout = 15
# config.debugging = true

client = RStableDiffusionAI::ApiClient.new(config)

api_instance = RStableDiffusionAI::DefaultApi.new(client)

result = api_instance.get_sd_models_sdapi_v1_sd_models_get
@models = result.map { |model| model.model_name }
begin
api_instance = RStableDiffusionAI::DefaultApi.new(client)
result = api_instance.get_sd_models_sdapi_v1_sd_models_get
@models = result.map { |model| model.model_name }
rescue StandardError
Rails.logger.error "failed to get models"
@models = []
end

@gallery = current_user.generated_images.order(created_at: :desc).limit(30)
end
Expand Down
4 changes: 3 additions & 1 deletion app/models/generated_image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ class GeneratedImage < ApplicationRecord
include ActionView::RecordIdentifier

belongs_to :user
has_one_attached :sketch
has_one_attached :sketch do |attachable|
attachable.variant :thumb, resize_to_limit: [100, 100]
end

after_create_commit { broadcast_created }

Expand Down
2 changes: 2 additions & 0 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,7 @@
<div class="alert alert-danger"><%= alert %></div>
<% end %>
<%= yield %>

<script src="https://kit.fontawesome.com/90fa2dbe9e.js" crossorigin="anonymous"></script>
</body>
</html>
6 changes: 5 additions & 1 deletion app/views/txt2_imgs/_gallery.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@
<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 %>
<%= image_tag gallery.sketch.variant(:thumb), class: 'img-thumbnail', alt: gallery.info, title: gallery.prompt %>
<% end %>
</div>
</div>
<%= button_to gallery, method: :delete, data: { turbo_confirm: "Do you really want to delete this image?" },
class: 'position-absolute bottom-0 end-0 m-0 btn-transparent', style: 'padding: 10px' do %>
<i class="fa-solid fa-trash-can"></i>
<% end %>
</div>
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +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
resources :generated_images, only: [:show, :destroy] # 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

0 comments on commit 616c95b

Please sign in to comment.