Skip to content

Commit

Permalink
Merge pull request #5 from Caroline-tamu/improve_code_quality
Browse files Browse the repository at this point in the history
Edit feature added
  • Loading branch information
caroline-li97 authored Oct 19, 2024
2 parents 1c6b2d6 + 5a4a16f commit 8151718
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 113 deletions.
14 changes: 13 additions & 1 deletion app/assets/stylesheets/application.css
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,16 @@
margin-bottom: 10px;
background-color: #555555;
padding: 10px;
} */
}
.flash {
padding: 10px;
margin: 10px 0;
border-radius: 5px;
}
.flash-notice {
background-color: #d4edda;
color: #155724;
border: 1px solid #c3e6cb;
}*/
112 changes: 34 additions & 78 deletions app/controllers/galleries_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,81 +8,47 @@ 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)
Expand All @@ -91,43 +57,33 @@ def destroy
@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'
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


Expand Down
24 changes: 13 additions & 11 deletions app/views/galleries/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
</div>
</div>

<h1 class="d-flex justify-content-center">Add images (No more than 5 images pre gallery!)</h1>
<h1 class="d-flex justify-content-center">Edit Gallery Details</h1>

<% if flash[:notice] %>
<div class="alert alert-success">
<%= flash[:notice] %>
Expand All @@ -48,7 +49,6 @@
<% if @gallery.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@gallery.errors.count, "error") %> prohibited this gallery from being saved:</h2>

<ul>
<% @gallery.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
Expand Down Expand Up @@ -77,6 +77,12 @@
<div class="col-sm-4 custom-padding mb-2">
<div class="card mb-4">
<img src="<%= picture.url %>" class="card-img-top">
<div class="card-body text-center">
<% idx = @gallery.gallery_picture.index(picture)%>
<% if @gallery.gallery_picture.length > 1 %>
<%= link_to 'Delete', gallery_delete_path(@gallery, idx), method: :get%>
<% end %>
</div>
</div>
</div>
<% end %>
Expand All @@ -85,24 +91,20 @@

<div class="d-flex justify-content-center">
<div class="custom-file">
<%= f.label :upload_images , multipart: true %>
<%= f.file_field :test_picture, multiple: true, class: 'custom-file-input', id: 'test_picture' , style: "margin-bottom: 10px;"%>

<div class="actions" style="margin-bottom: 10px;" >
<%= button_tag "Add images", type: "submit" %>
<%= f.label :upload_images, 'Choose Files', class: 'custom-file-label' %>
<%= f.file_field :test_picture, multiple: true, class: 'custom-file-input', id: 'test_picture' %>
<div class="actions" style="margin-top: 10px;">
<%= submit_tag 'Update Project', class: 'btn btn-primary' %>
</div>

<a href="add_images" class="btn btn-success" style="margin-bottom: 10px;" >Update gallery</a>
</div>
</div>



<% end %>
<a class="btn full color-1 size-1 hover-1" href = show method="get"><i class="fa fa-chevron-left"></i>Back</a>




<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
<!-- SCRIPT -->
<script src="<%= asset_path "jquery.min.js"%>"></script>
Expand Down
34 changes: 13 additions & 21 deletions app/views/galleries/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,22 @@
</div>
</div>

<% if flash[:notice] %>
<div class="alert alert-success">
<%= flash[:notice] %>
</div>
<% end %>

<!-- Main Content -->
<div class="container float-left">
<div class = "container be-detail-container float-left">
<h2> <%= @gallery.gallery_title %> </h2>
</div>
<%if GeneralInfo.find(@gallery.GeneralInfo_id).userKey == session[:current_user_key]%>
<%= link_to 'Edit This Project', edit_gallery_path(@gallery.id)%>
<br>
<%= link_to 'Delete This Project', show_profile_destroy_path(@gallery.id), :method => :delete, data: { confirm: "Are you sure to delete?" } %>
<%end%>
</div>
<div class = "container be-detail-container float-left">
<h3> Description: </h3>
<p> <%= @gallery.gallery_description %> </p>
Expand Down Expand Up @@ -69,27 +79,13 @@
<a class="be-img-block profile-img img-thumbnail" href="#">
<%= 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] %>
<a href="delete/idx=<%= idx %>">Delete<%=which%></a>
<%end%>
<%end%>

</a>
</div>


</div>
</div>
<% end %>

<%# <div class="col-md-3">
</div> %>

</div> %>
</div>
<%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%>
</div>
</div>
</div>
Expand Down Expand Up @@ -127,10 +123,6 @@

<br>

<% 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' %>
Expand Down
1 change: 0 additions & 1 deletion app/views/show_profile/show_profile.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@
</div>
</header>


<!-- MAIN CONTENT -->
<div id="content-block"
<% if @general_info && @general_info.cover_picture.url %>
Expand Down
41 changes: 41 additions & 0 deletions features/edit_gallery.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
Feature: Edit project information

As a creator
So that I can update my project information
Want to edit and save changes to published projects

Background: project in database

Given the following project exist:
| gallery_title | gallery_description | gallery_picture |
| BigProject | Here is a project | "http://example.com/img1.png" |

Scenario: Enter the page to edit a project
Given I am on the details page for "BigProject"
When I follow "Edit This Project"
And I should see "Edit Gallery Details"

Scenario: Edit title of a project
Given I am on the edit page for "BigProject"
When I fill in "Gallery title" with "ArtConnect"
When I click on "Updated Project"
And I should see "ArtConnect"
And I should not see "BigProject"

Scenario: Edit description of a project
Given I am on the edit page for "BigProject"
When I fill in "Gallery description" with "an online art platform designed to create an interactive space for artists"
When I click on "Updated Project"
And I should see "an online art platform designed to create an interactive space for artists"
And I should not see "Here is a project"

Scenario: Edit image of a project
Given I am on the edit page for "BigProject"
When I upload a file "sample.png"
And I click on "Updated Project"
And I should see exactly 2 images
When I follow "Edit This Project"
And I click on "Delete"
And I click on "Updated Project"
And I should see exactly 1 images

10 changes: 10 additions & 0 deletions features/step_definitions/edit_gallery_steps.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Given(/^the following project exist:$/) do |table|
table.hashes.each do |gallery|
gallery['gallery_picture'] = gallery['gallery_picture'].gsub('"', '') if gallery['gallery_picture']

Gallery.create!(gallery_title: gallery['gallery_title'],
gallery_description: gallery['gallery_description'],
gallery_picture: gallery['gallery_picture'])
end
end

2 changes: 1 addition & 1 deletion install/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,4 @@ rails server -b 0.0.0.0 -p 3000

# if every works, that's great!
# if not, try using docker-compose
# GOOD LUCK!
# GOOD LUCK!

0 comments on commit 8151718

Please sign in to comment.