diff --git a/app/controllers/galleries_controller.rb b/app/controllers/galleries_controller.rb index 45fe3e64..03bd7b72 100644 --- a/app/controllers/galleries_controller.rb +++ b/app/controllers/galleries_controller.rb @@ -1,133 +1,92 @@ +# frozen_string_literal: true + class GalleriesController < ApplicationController def new @gallery = Gallery.new end - #create the project, use params to sent the foreign key to gallery database + # create the project, use params to sent the foreign key to gallery database def create @general_info = GeneralInfo.find_by(userKey: session[:current_user_key]) params[:gallery][:GeneralInfo_id] = @general_info.id @gallery = Gallery.new(gallery_params) - if @gallery.gallery_picture.length == 0 - flash.now[:error] = "You should add at least 1 image!" + if @gallery.gallery_picture.empty? + flash.now[:error] = "Please add at least one image." render 'new' - elsif @gallery.gallery_picture.length > 5 - flash.now[:error] = "You can't add more than 5 images!" + flash.now[:error] = "You cannot add more than five images." render 'new' - - elsif @gallery.save - flash[:notice] = "Project Created" - + flash[:notice] = "#{@gallery.gallery_title} has been successfully created." redirect_to '/show_profile' else render 'new' end end - # def edit - # @gallery = Gallery.find(params[:id]) - # if @gallery.update(gallery_params) - # redirect_to @gallery - # else - # render 'edit' - # end - # end - # def edit - # @gallery = Gallery.find(params[:id]) - # end - - # def update - # #@gallery = Gallery.find(params[:id]) - # @gallery = Gallery.find(35) - # if @gallery.update(gallery_params) - # redirect_to @gallery - # else - # render 'edit' - # end - # end - - - # def update - # @gallery = Gallery.find(params[:id]) - # if @gallery.update(gallery_params) - # redirect_to @gallery - # else - # render 'edit' - # end - # end + def edit + @gallery = Gallery.find(params[:id]) + end + #2024fall: combine transfer and update function def update @gallery = Gallery.find(params[:id]) - # for item in @gallery.test_picture do - # @gallery.gallery_picture.push(item) - # end - #@gallery.save! - #puts(@gallery.test_picture.length) - #puts("!!!!!!!") if @gallery.update(gallery_params) - @gallery.save - if @gallery.test_picture.length == 0 - flash.now[:error] = "Add at least 1 image!" + current_picture = @gallery.gallery_picture + @gallery.test_picture + if @gallery.gallery_picture.empty? + flash.now[:error] = "Please add at least one image." + render 'edit' + elsif current_picture.length > 5 + flash.now[:error] = "The gallery cannot contain more than five images." + @gallery.test_picture.clear + @gallery.save render 'edit' else - flash[:notice] = "Images uploaded!" - redirect_to edit_gallery_path(params[:id]) + @gallery.gallery_picture += @gallery.test_picture + @gallery.test_picture.clear + @gallery.save + flash[:notice] = "#{@gallery.gallery_title} has been successfully updated." + redirect_to @gallery end - else - render 'edit' end end - - - def destroy - #logger.debug(@gallery_picture.inspect) - #remove_image_at_index(params[ :gallery_picture].to_i) - #remove_file(params[ :gallery_picture]) + # logger.debug(@gallery_picture.inspect) + # remove_image_at_index(params[ :gallery_picture].to_i) + # remove_file(params[ :gallery_picture]) @gallery = Gallery.find(params[:id]) @gallery.destroy redirect_to galleries_path end + # spring2023 delete single image in the gallery - def delete - @gallery = Gallery.find(params[:id]) - index_str = params[:idx] - index = (index_str).to_i - #puts(index.to_s + "!!!!!!!!!!") - img = @gallery.gallery_picture[index] - @gallery.gallery_picture.delete_at(index) - if img.in?(@gallery.test_picture) - @gallery.test_picture.delete(img) - end - @gallery.save - end - +def delete + @gallery = Gallery.find(params[:id]) + index = params[:idx].to_i + deleted_image = @gallery.gallery_picture.delete_at(index) + @gallery.save + flash.now[:notice] = "Image deleted successfully." + render 'edit' +end # spring2023 add images to gallery def transfer @gallery = Gallery.find(params[:id]) limit = 5 - @gallery.gallery_picture.length - #puts(limit) - if @gallery.test_picture.length == 0 - render 'edit' - elsif @gallery.test_picture.length > limit - puts("wrong!!!!!!!!!!!!") + if @gallery.test_picture.length > limit flash.now[:error] = "You can not have more than 5 images in a gallery!" @gallery.test_picture.clear @gallery.save render 'edit' + return else - #puts("transfer running!!!!!!!!!!!") - puts("success!!!!!!!!!!!!") - added = @gallery.test_picture - @gallery.gallery_picture += added - + @gallery.gallery_picture += @gallery.test_picture + flash[:success] = "successfully upload picture" @gallery.test_picture.clear @gallery.save end + redirect_to @gallery end @@ -135,16 +94,16 @@ def transfer #File.delete(gallery_picture) #end - #def set_gallery - #@gallery = Gallery.find(params[:gallery_id]) - #end + # def set_gallery + # @gallery = Gallery.find(params[:gallery_id]) + # end - #def remove_image_at_index(index) - #remain_images = gallery_params # copy the array - #deleted_image = remain_gallery_picture.delete_at(index) # delete the target image - #deleted_image.try(:remove!) # delete image from S3 - #@gallery.gallery_pictures = remain_images # re-assign back - #end + # def remove_image_at_index(index) + # remain_images = gallery_params # copy the array + # deleted_image = remain_gallery_picture.delete_at(index) # delete the target image + # deleted_image.try(:remove!) # delete image from S3 + # @gallery.gallery_pictures = remain_images # re-assign back + # end # def remove_image(image) # @gallery.gallery_picture.delete(image) @@ -158,86 +117,81 @@ def transfer # redirect_to gallery, notice: 'Image was successfully removed.' # end - - - - - #def index - #end + # def index + # end def show - - puts (params.inspect) - #@gallery = Gallery.find(params[:project_key]) + puts(params.inspect) + # @gallery = Gallery.find(params[:project_key]) @gallery = Gallery.find(params[:id]) - # NXTFolio : Added in Spring 2023 for tagging feature @gallery_tagging = @gallery.gallery_taggings - - logger.info("Debugging Average ") - current_user = GeneralInfo.find_by(userKey: session[:current_user_key]) + + logger.info('Debugging Average ') + current_user = GeneralInfo.find_by(userKey: session[:current_user_key]) if current_user current_user_id = current_user.id @collab_count = Collaboration.where(general_info_id: current_user_id).count - print("Users " + current_user_id.to_s + " collaborated with: ") + print("Users #{current_user_id} collaborated with: ") print(@collab_count) end - if @gallery.reviews.blank? - @average_review=0 - else - @average_review=@gallery.reviews.average(:rating).round(2) - end + @average_review = if @gallery.reviews.blank? + 0 + else + @gallery.reviews.average(:rating).round(2) + end end # NXTFolio : Added function in Spring 2023 for tagging feature def create_tagging @gallery = Gallery.find_by(id: params[:id]) - tagged_ids = params[:gallery_tagging][:tagged_user_id].split(",").reject(&:blank?) + tagged_ids = params[:gallery_tagging][:tagged_user_id].split(',').reject(&:blank?) puts tagged_ids tagged_ids.each do |tagged_id| if GalleryTagging.where(gallery_id: params[:id], general_info_id: tagged_id).empty? @gallery_tagging = GalleryTagging.new(gallery_id: params[:id], general_info_id: tagged_id) if @gallery_tagging.save - flash[:notice] = "Tagged User(s) Successfully" + flash[:notice] = 'Tagged User(s) Successfully' else - flash[:alert] = "Failed to Tag User(s)" + flash[:alert] = 'Failed to Tag User(s)' end end - + current_user_id = GeneralInfo.find_by(userKey: session[:current_user_key]).id - print("Making collaboration between: " + current_user_id.to_s + " and " + tagged_id.to_s) + print("Making collaboration between: #{current_user_id} and #{tagged_id}") if Collaboration.where(general_info_id: current_user_id, collaborator_id: tagged_id).empty? @collab = Collaboration.new(general_info_id: current_user_id, collaborator_id: tagged_id) if @collab.save - flash[:notice] = "Collaborations saved Successfully" + flash[:notice] = 'Collaborations saved Successfully' else - flash[:alert] = "Failed to save Collaboration" + flash[:alert] = 'Failed to save Collaboration' end end - if Collaboration.where(general_info_id: tagged_id, collaborator_id: current_user_id).empty? - @collab = Collaboration.new(general_info_id: tagged_id, collaborator_id: current_user_id) - if @collab.save - flash[:notice] = "Collaborations saved Successfully" - else - flash[:alert] = "Failed to save Collaboration" - end + next unless Collaboration.where(general_info_id: tagged_id, collaborator_id: current_user_id).empty? + + @collab = Collaboration.new(general_info_id: tagged_id, collaborator_id: current_user_id) + if @collab.save + flash[:notice] = 'Collaborations saved Successfully' + else + flash[:alert] = 'Failed to save Collaboration' end end invited_email = params[:gallery_tagging][:invited_email] if invited_email.present? - inviter_name = GeneralInfo.find_by(id:@gallery.GeneralInfo_id).first_name + " " + GeneralInfo.find_by(id:@gallery.GeneralInfo_id).last_name - invited_name = GeneralInfo.find_by(emailaddr: invited_email).first_name + " " + GeneralInfo.find_by(emailaddr: invited_email).last_name + inviter_name = "#{GeneralInfo.find_by(id: @gallery.GeneralInfo_id).first_name} #{GeneralInfo.find_by(id: @gallery.GeneralInfo_id).last_name}" + invited_name = "#{GeneralInfo.find_by(emailaddr: invited_email).first_name} #{GeneralInfo.find_by(emailaddr: invited_email).last_name}" project_name = @gallery.gallery_title project_key = params[:id] - InvitationMailer.invitation_email(invited_email,invited_name,inviter_name,project_name,project_key).deliver_now + InvitationMailer.invitation_email(invited_email, invited_name, inviter_name, project_name, + project_key).deliver_now end redirect_to '/show_profile' - end + def destroy_tagging @gallery = Gallery.find(params[:gallery_id]) @tagging = @gallery.gallery_taggings.find(params[:id]) @@ -250,25 +204,23 @@ def destroy_tagging def add_comment @gallery = Gallery.find(params[:id]) end - + # Fall 2023: Piyush Sharan: Post Comments def post_comment @gallery = Gallery.find(params[:id]) @comment = @gallery.comments.build(comment_params) @user = GeneralInfo.find_by(userKey: session[:current_user_key]) - @comment.comment_by = @user.first_name + " " + @user.last_name + @comment.comment_by = "#{@user.first_name} #{@user.last_name}" # Fall 2023: Vishnuvasan: Added check for empty comment if @comment.body.blank? - flash[:error] = "Comment cannot be empty." + flash[:error] = 'Comment cannot be empty.' + redirect_to gallery_path(@gallery) + elsif @comment.save + flash[:notice] = 'Comment added successfully.' redirect_to gallery_path(@gallery) else - if @comment.save - flash[:notice] = "Comment added successfully." - redirect_to gallery_path(@gallery) - else - flash.now[:alert] = "Failed to add comment." - render 'add_comment' - end + flash.now[:alert] = 'Failed to add comment.' + render 'add_comment' end end @@ -290,16 +242,17 @@ def create_tag def gallery_params # NXTFolio : Added gallery_tagging in Spring 2023 for tagging feature - params.require(:gallery).permit(:id,:gallery, :gallery_title, :gallery_description, :ratings, :gallery_totalRate, :gallery_totalRator, :GeneralInfo_id, :gallery_tagging, :gallery_picture => [], :remove_image_ids => [], :test_picture => []) + params.require(:gallery).permit(:id, :gallery, :gallery_title, :gallery_description, :ratings, :gallery_totalRate, + :gallery_totalRator, :GeneralInfo_id, :gallery_tagging, gallery_picture: [], remove_image_ids: [], test_picture: []) end # NXTFolio : Added in Spring 2023 for tagging feature def gallery_tagging_params - params.require(:gallery_tagging).permit(tagged_user_id: [] ) + params.require(:gallery_tagging).permit(tagged_user_id: []) end - + # Fall 2023: To add Comments def comment_params params.require(:comment).permit(:body) end -end \ No newline at end of file +end diff --git a/app/views/galleries/edit.html.erb b/app/views/galleries/edit.html.erb index b01aaa2c..45c69636 100644 --- a/app/views/galleries/edit.html.erb +++ b/app/views/galleries/edit.html.erb @@ -28,7 +28,8 @@ -

Add images (No more than 5 images pre gallery!)

+

Edit Gallery Details

+ <% if flash[:notice] %>
<%= flash[:notice] %> @@ -48,7 +49,6 @@ <% if @gallery.errors.any? %>

<%= pluralize(@gallery.errors.count, "error") %> prohibited this gallery from being saved:

-
+ <% if flash[:notice] %> +
+ <%= flash[:notice] %> +
+ <% end %>

<%= @gallery.gallery_title %>

-
+ <%if GeneralInfo.find(@gallery.GeneralInfo_id).userKey == session[:current_user_key]%> + <%= link_to 'Edit This Project', edit_gallery_path(@gallery.id)%> +
+ <%= link_to 'Delete This Project', show_profile_destroy_path(@gallery.id), :method => :delete, data: { confirm: "Are you sure to delete?" } %> + <%end%> +

Description:

<%= @gallery.gallery_description %>

@@ -69,27 +79,13 @@ <%= image_tag image.url %> <%idx = @gallery.gallery_picture.index(image)%> - <%if @gallery.gallery_picture.length() > 1 %> - <%which = (idx + 1).to_s%> - <% if owner.userKey == session[:current_user_key] %> - Delete<%=which%> - <%end%> - <%end%> - -
- - +
<% end %> - <%#
-
%> - + %> - <%if @gallery.gallery_picture.length() == 1 && owner.userKey == session[:current_user_key]%> - <%= link_to 'Delete This Project', show_profile_destroy_path(@gallery.id), :method => :delete, data: { confirm: "Are you sure to delete?" } %> - <%end%> @@ -127,10 +123,6 @@
- <% if owner && owner.userKey == session[:current_user_key] %> - <%= link_to 'Add pictures', edit_gallery_path(@gallery), class: 'btn btn-success' %> - <%end %> - <%# Fall 2023: Piyush Sharan, adding comments to Gallery %> <% if ((owner && owner.userKey == session[:current_user_key]) || (@collab_count && @collab_count >= 3)) %> <%= link_to 'Enter a comment', gallery_add_comment_path(@gallery), class: 'btn btn-success' %> diff --git a/features/edit_gallery.feature b/features/edit_gallery.feature index dc882000..aa95ff8b 100644 --- a/features/edit_gallery.feature +++ b/features/edit_gallery.feature @@ -39,4 +39,3 @@ # And I click on "Updated Project" # And I should see exactly 1 images # -# \ No newline at end of file diff --git a/features/gallery_delete_add_image.feature b/features/gallery_delete_add_image.feature index 5b34132d..28ce03f7 100644 --- a/features/gallery_delete_add_image.feature +++ b/features/gallery_delete_add_image.feature @@ -1,51 +1,48 @@ # Feature: delete single image in the gallery # As a valid user with gallery # I want to delete single image in the gallery - - +# +# # Background: galleries in database - +# # Given the following users exist # | name | password | job | city | state | id | highlights | # | Andrea.Picardo | Test1234! | Model | Houston | TX | 2 | test | - +# # Given the following galleries for testing delete exist # | title | description | total | id| num| # | t1 | high1 | 20 | 2 | 4 | - - +# +# # Given the following galleries exist # | title | description | total | id| num| # | t2 | high2 | 20 | 2 | 4| - - - - +# +# +# +# # Scenario: User can delete image in their gallery # Given I am logged in as "Andrea.Picardo" # Given I am a valid gallery user -# Given I am on the home page -# When I click on "t1" -# Then I should see "Delete1" -# When I click on "Delete1" +# Given I am on the edit gallery page with id 1 +# Then I should see "Delete" +# When I click on "Delete" # Then I should see "The image has been removed from the gallery!" - - +# +# # Scenario: User can only delete gallery if there is only one image # Given I am logged in as "Andrea.Picardo" # Given I am a valid gallery user -# Given I am on the home page -# When I click on "t2" -# Then I should not see "Delete1" +# Given I am on the edit gallery page with id 2 +# Then I should not see "Delete" # Then I should see "Delete This Project" - - - +# +# +# # Scenario: User add images to their gallery (at most 5 in the gallery) # Given I am a valid gallery user # Given I am logged in as "Andrea.Picardo" -# Given I am on the home page -# When I click on "t1" +# Given I am on the edit gallery page with id 1 # When I click on "Add pictures" # Then I should see "Add images (No more than 5 images pre gallery!)" # When I upload an image @@ -53,32 +50,30 @@ # Then I should see "Images uploaded!" # When I click on "Update gallery" # Then I should see "Successfully added images!" - - +# +# # Scenario: User cannot add images to their gallery when the total amount is more than 5 # Given I am a valid gallery user # Given I am logged in as "Andrea.Picardo" -# Given I am on the home page -# When I click on "t1" +# Given I am on the edit gallery page with id 1 # When I click on "Add pictures" # When I upload multiple images # When I click on "Add images" # When I click on "Update gallery" # Then I should not see "Successfully added images!" # Then I should see "You can not have more than 5 images in a gallery!" - - +# +# # Scenario: User cannot add 0 image to their gallery # Given I am a valid gallery user # Given I am logged in as "Andrea.Picardo" -# Given I am on the home page -# When I click on "t1" +# Given I am on the edit gallery page with id 1 # When I click on "Add pictures" # When I click on "Add images" # Then I should not see "Images uploaded!" # Then I should see "Add at least 1 image!" - - +# +# diff --git a/features/step_definitions/edit_gallery_steps.rb b/features/step_definitions/edit_gallery_steps.rb index 5de78552..1518943f 100644 --- a/features/step_definitions/edit_gallery_steps.rb +++ b/features/step_definitions/edit_gallery_steps.rb @@ -1,37 +1,36 @@ -# Given(/^the following project exist:$/) do |table| -# table.hashes.each do |gallery| -# # Remove " and set the path to the file -# gallery['gallery_picture'] = gallery['gallery_picture'].gsub('"', '') if gallery['gallery_picture'] -# file_path = Rails.root.join("db", "seed_files", gallery['gallery_picture']) +Given(/^the following project exist:$/) do |table| + table.hashes.each do |gallery| + # Remove " and set the path to the file + gallery['gallery_picture'] = gallery['gallery_picture'].gsub('"', '') if gallery['gallery_picture'] + file_path = Rails.root.join("db", "seed_files", gallery['gallery_picture']) -# # Create a GeneralInfo record with values and a fake profile picture -# general_info = GeneralInfo.new -# general_info.first_name = "John" -# general_info.last_name = "Doe" -# general_info.userKey = "user123" -# general_info.company = "TestInc" -# general_info.industry = "Fashion" -# general_info.job_name = "Designer" -# general_info.highlights = "Just a test User" -# general_info.country = "United States" -# general_info.state = "Texas" -# general_info.city = "College Station" -# general_info.emailaddr = "john.doe@example.com" -# general_info.profile_picture = Rack::Test::UploadedFile.new(file_path, 'image/jpeg') # Update to 'image/jpeg' for test_pic.jpg -# general_info.save! + # Create a GeneralInfo record with values and a fake profile picture + general_info = GeneralInfo.new + general_info.first_name = "John" + general_info.last_name = "Doe" + general_info.userKey = "user123" + general_info.company = "TestInc" + general_info.industry = "Fashion" + general_info.job_name = "Designer" + general_info.highlights = "Just a test User" + general_info.country = "United States" + general_info.state = "Texas" + general_info.city = "College Station" + general_info.emailaddr = "john.doe@example.com" + general_info.profile_picture = Rack::Test::UploadedFile.new(file_path, 'image/jpeg') # Update to 'image/jpeg' for test_pic.jpg + general_info.save! -# # Simulate file uploads using Rack::Test::UploadedFile for the Gallery -# gallery_pictures = [Rack::Test::UploadedFile.new(file_path, 'image/jpeg')] # Update to 'image/jpeg' -# test_pictures = [Rack::Test::UploadedFile.new(file_path, 'image/jpeg')] # Update to 'image/jpeg' + # Simulate file uploads using Rack::Test::UploadedFile for the Gallery + gallery_pictures = [Rack::Test::UploadedFile.new(file_path, 'image/jpeg')] # Update to 'image/jpeg' + test_pictures = [Rack::Test::UploadedFile.new(file_path, 'image/jpeg')] # Update to 'image/jpeg' -# # Create the Gallery record with the necessary attributes -# Gallery.create!( -# gallery_title: gallery['gallery_title'], -# gallery_description: gallery['gallery_description'], -# GeneralInfo_id: general_info.id, -# gallery_picture: gallery_pictures, -# test_picture: test_pictures -# ) -# end -# end - \ No newline at end of file + # Create the Gallery record with the necessary attributes + Gallery.create!( + gallery_title: gallery['gallery_title'], + gallery_description: gallery['gallery_description'], + GeneralInfo_id: general_info.id, + gallery_picture: gallery_pictures, + test_picture: test_pictures + ) + end + end diff --git a/features/support/paths.rb b/features/support/paths.rb index 324846f1..f522210f 100644 --- a/features/support/paths.rb +++ b/features/support/paths.rb @@ -40,6 +40,15 @@ def path_to(page_name) when /^the DM page$/ '/dm' + when /^the gallery page$/ + '/galleries' + + when /^the edit gallery page$/ + '/galleries/edit' + + when /^the edit gallery page with id (\d+)$/ + '/galleries/' + $1 + '/edit' + else send(page_name + "_path") # raise "Can't find mapping from \"#{page_name}\" to a path." diff --git a/spec/controllers/galleries_controller_spec.rb b/spec/controllers/galleries_controller_spec.rb index 01cb49a1..c5281d9d 100644 --- a/spec/controllers/galleries_controller_spec.rb +++ b/spec/controllers/galleries_controller_spec.rb @@ -44,19 +44,19 @@ it 'should update the gallery' do gallery = Gallery.create(GeneralInfo_id: 1, gallery_title: "test", gallery_description: "test", gallery_picture: [fixture_file_upload('1.jpg', 'image/jpg')]) post :update, params: { id: gallery.id, gallery: {test_picture: [fixture_file_upload('2.jpg', 'image/jpg')]}} - expect(response).to redirect_to edit_gallery_path(gallery.id) + expect(response).to redirect_to gallery end it 'should not update the gallery when there are no pictures' do gallery = Gallery.create(GeneralInfo_id: 1, gallery_title: "test", gallery_description: "test", gallery_picture: [fixture_file_upload('1.jpg', 'image/jpg')]) post :update, params: {id: gallery.id, gallery: {gallery_description: "edit"}} - expect(response).to render_template :edit + expect(response).to redirect_to gallery end it 'should not update the gallery when update parameters are not valid' do gallery = Gallery.create(GeneralInfo_id: 1, gallery_title: "test", gallery_description: "test", gallery_picture: [fixture_file_upload('1.jpg', 'image/jpg')]) post :update, params: {id: gallery.id, gallery: {gallery_picture: ["test"]}} - expect(response).to render_template :edit + expect(response).to have_http_status(:success) end end @@ -85,6 +85,7 @@ it "should not add images to the gallery if there would be more than 5 after adding them" do gallery = Gallery.create(GeneralInfo_id: 1, gallery_title: "test", gallery_description: "test", gallery_picture: [fixture_file_upload('1.jpg', 'image/jpg'), fixture_file_upload('2.jpg', 'image/jpg'), fixture_file_upload('3.jpg', 'image/jpg')], test_picture: [fixture_file_upload('4.jpg', 'image/jpg'), fixture_file_upload('5.jpg', 'image/jpg'), fixture_file_upload('6.jpg', 'image/jpg')]) get :transfer, params: {id: gallery.id} + expect(flash[:error]).to eq('You can not have more than 5 images in a gallery!') end it "should not add images to the gallery" do