From 04afbfa1f5018a72192c3c9653e534fd1945ce6a Mon Sep 17 00:00:00 2001 From: Jennifer Wills Date: Sat, 30 May 2020 23:59:10 -0400 Subject: [PATCH] multi upload working --- Gemfile | 1 + Gemfile.lock | 4 ++ app/controllers/images_controller.rb | 44 +++++++++---- app/controllers/users_controller.rb | 74 ---------------------- app/helpers/users_helper.rb | 2 - app/views/images/edit.html.erb | 15 ++--- app/views/images/index.html.erb | 53 ++++++++-------- app/views/images/show.html.erb | 6 +- app/views/{site => images}/upload.html.erb | 6 +- app/views/layouts/application.html.erb | 7 +- app/views/site/home.html.erb | 26 -------- app/views/site/private.html.erb | 26 -------- app/views/users/_form.html.erb | 17 ----- app/views/users/_user.json.jbuilder | 2 - app/views/users/edit.html.erb | 6 -- app/views/users/index.html.erb | 25 -------- app/views/users/index.json.jbuilder | 1 - app/views/users/new.html.erb | 5 -- app/views/users/show.html.erb | 4 -- app/views/users/show.json.jbuilder | 1 - config/routes.rb | 12 ++-- test/controllers/users_controller_test.rb | 48 -------------- test/system/users_test.rb | 41 ------------ 23 files changed, 85 insertions(+), 341 deletions(-) delete mode 100644 app/controllers/users_controller.rb delete mode 100644 app/helpers/users_helper.rb rename app/views/{site => images}/upload.html.erb (79%) delete mode 100644 app/views/site/home.html.erb delete mode 100644 app/views/site/private.html.erb delete mode 100644 app/views/users/_form.html.erb delete mode 100644 app/views/users/_user.json.jbuilder delete mode 100644 app/views/users/edit.html.erb delete mode 100644 app/views/users/index.html.erb delete mode 100644 app/views/users/index.json.jbuilder delete mode 100644 app/views/users/new.html.erb delete mode 100644 app/views/users/show.html.erb delete mode 100644 app/views/users/show.json.jbuilder delete mode 100644 test/controllers/users_controller_test.rb delete mode 100644 test/system/users_test.rb diff --git a/Gemfile b/Gemfile index 9d1fcfa..7f0eec6 100644 --- a/Gemfile +++ b/Gemfile @@ -28,6 +28,7 @@ gem 'jbuilder', '~> 2.7' gem 'bootstrap', '~> 4.3.1' gem 'jquery-rails' gem 'image_processing' +gem 'active_storage-send_zip' # Reduces boot times through caching; required in config/boot.rb gem 'bootsnap', '>= 1.4.2', require: false diff --git a/Gemfile.lock b/Gemfile.lock index 493c989..266e132 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -37,6 +37,9 @@ GEM erubi (~> 1.4) rails-dom-testing (~> 2.0) rails-html-sanitizer (~> 1.1, >= 1.2.0) + active_storage-send_zip (0.3.3) + rails (> 5.2) + rubyzip (< 3.0) activejob (6.0.3.1) activesupport (= 6.0.3.1) globalid (>= 0.3.6) @@ -210,6 +213,7 @@ PLATFORMS ruby DEPENDENCIES + active_storage-send_zip bootsnap (>= 1.4.2) bootstrap (~> 4.3.1) byebug diff --git a/app/controllers/images_controller.rb b/app/controllers/images_controller.rb index eeb2187..4999e23 100644 --- a/app/controllers/images_controller.rb +++ b/app/controllers/images_controller.rb @@ -20,17 +20,19 @@ def edit # POST /images # POST /images.json def create - @image = Image.new(create_params) + parameters =create_params + parameters[:files].each do |f| + tmp = {:title => parameters[:title], :private => parameters[:private], :tags => parameters[:tags], :file => f} - respond_to do |format| - if @image.save - format.html { redirect_to root_path, notice: 'Image was successfully created.' } - format.json { render :show, status: :created, location: @image } - else + @image = Image.create!(tmp) + if !(@image.save) format.html { render :new } format.json { render json: @image.errors, status: :unprocessable_entity } end + + end + redirect_to root_path, notice: 'Image was successfully created.' end # PATCH/PUT /images/1 @@ -50,13 +52,33 @@ def update # DELETE /images/1 # DELETE /images/1.json def destroy + @image.file.purge @image.destroy respond_to do |format| - format.html { redirect_to images_url, notice: 'Image was successfully destroyed.' } + format.html { redirect_to root_path, notice: 'Image was successfully destroyed.' } format.json { head :no_content } end end + def destroy_all + @image.file.purge + @image.destroy + respond_to do |format| + format.html { redirect_to root_path, notice: 'Image was successfully destroyed.' } + format.json { head :no_content } + end + end + + + def download + send_data @resume, type: "application/pdf", disposition: "attachment" + end + + def download_all + send_data @resume, type: "application/pdf", disposition: "attachment" + end + + private # Use callbacks to share common setup or constraints between actions. def set_image @@ -65,16 +87,14 @@ def set_image # Only allow a list of trusted parameters through. def create_params - params.require(:title) - params.require(:file) + params.require(:files) + # byebug params.require(:private) - params.permit(:title, :tags, :file, :private) + params.permit(:title, :tags, :private, files: []) end # Only allow a list of trusted parameters through. def update_params - params.require(:title) - params.require(:private) params.permit(:title, :tags, :private) end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb deleted file mode 100644 index 3485a4d..0000000 --- a/app/controllers/users_controller.rb +++ /dev/null @@ -1,74 +0,0 @@ -class UsersController < ApplicationController - before_action :set_user, only: [:show, :edit, :update, :destroy] - - # GET /users - # GET /users.json - def index - @users = User.all - end - - # GET /users/1 - # GET /users/1.json - def show - end - - # GET /users/new - def new - @user = User.new - end - - # GET /users/1/edit - def edit - end - - # POST /users - # POST /users.json - def create - @user = User.new(user_params) - - respond_to do |format| - if @user.save - format.html { redirect_to @user, notice: 'User was successfully created.' } - format.json { render :show, status: :created, location: @user } - else - format.html { render :new } - format.json { render json: @user.errors, status: :unprocessable_entity } - end - end - end - - # PATCH/PUT /users/1 - # PATCH/PUT /users/1.json - def update - respond_to do |format| - if @user.update(user_params) - format.html { redirect_to @user, notice: 'User was successfully updated.' } - format.json { render :show, status: :ok, location: @user } - else - format.html { render :edit } - format.json { render json: @user.errors, status: :unprocessable_entity } - end - end - end - - # DELETE /users/1 - # DELETE /users/1.json - def destroy - @user.destroy - respond_to do |format| - format.html { redirect_to users_url, notice: 'User was successfully destroyed.' } - format.json { head :no_content } - end - end - - private - # Use callbacks to share common setup or constraints between actions. - def set_user - @user = User.find(params[:id]) - end - - # Only allow a list of trusted parameters through. - def user_params - params.fetch(:user, {}) - end -end diff --git a/app/helpers/users_helper.rb b/app/helpers/users_helper.rb deleted file mode 100644 index 2310a24..0000000 --- a/app/helpers/users_helper.rb +++ /dev/null @@ -1,2 +0,0 @@ -module UsersHelper -end diff --git a/app/views/images/edit.html.erb b/app/views/images/edit.html.erb index 62eeadd..9ee2fab 100644 --- a/app/views/images/edit.html.erb +++ b/app/views/images/edit.html.erb @@ -1,25 +1,22 @@

Editing Image

-<%= form_with(url: image_path, method: "post", :html => {:class => "form-horizontal center"}) do |form| %> +<%= form_with(url: image_path(@image), method: "put", :html => {:class => "form-horizontal center"}) do |form| %>
<%= form.label(:title, "Image Title:") %> - <%= form.text_field :title, :required => true %> + <%= form.text_field :title, :value => @image.title %>
<%= form.label(:tags, "Choose Tags:") %> - <%= form.text_field :tags%> + <%= form.text_field :tags, :value => @image.tags%>
<%= form.label(:private, "Make Picture Public:") %> <%= form.check_box :private %>
- <%= submit_tag("Upload") %> - + <%= submit_tag("Save", :class => "btn btn-light") %> + <%= link_to 'Back', @image , :class => "btn btn-light"%> + <%= button_to "Delete", { controller: :images, action: :destroy, id: @image.id }, method: :delete, data: { confirm: 'Are you sure?' }, :class => "btn btn-light" %> <% end %> - - -<%= link_to 'Show', @image %> | -<%= link_to 'Back', images_path %> diff --git a/app/views/images/index.html.erb b/app/views/images/index.html.erb index b600cb9..88f1c28 100644 --- a/app/views/images/index.html.erb +++ b/app/views/images/index.html.erb @@ -1,25 +1,28 @@ -

<%= notice %>

- -

Images

- - - - - - - - - - <% @images.each do |image| %> - - - - - - <% end %> - -
<%= link_to 'Show', image %><%= link_to 'Edit', edit_image_path(image) %><%= link_to 'Destroy', image, method: :delete, data: { confirm: 'Are you sure?' } %>
- -
- -<%= link_to 'New Image', new_image_path %> +

Public Images

+ +
+ <% @images.each do |img| %> +
+
+ <%= image_tag img.file , :width => "100%" %> +
+

<%= img.title %>

+ + <% if img.tags %> +

Tags: <%= img.tags %>

+ <% end %> + + <%= link_to "View", image_path(img), :class => "btn btn-light" %> + +
+ +
+ +
+ + + <% end %> + +
+ + diff --git a/app/views/images/show.html.erb b/app/views/images/show.html.erb index 76e553f..f78cc3e 100644 --- a/app/views/images/show.html.erb +++ b/app/views/images/show.html.erb @@ -11,8 +11,10 @@

Tags: <%= @image.tags %>

<% end %> - <%= link_to 'Edit', edit_image_path(@image) , :class => "btn btn-light"%> - <%= link_to 'Back', images_path , :class => "btn btn-light"%> + <%= link_to 'Edit', edit_image_path(@image), :class => "btn btn-light"%> + <%= link_to "Download", download_image_path(@image), "data-turbolinks" => false, :class => "btn btn-light" %> + + <%= link_to 'Back', root_path , :class => "btn btn-light"%> diff --git a/app/views/site/upload.html.erb b/app/views/images/upload.html.erb similarity index 79% rename from app/views/site/upload.html.erb rename to app/views/images/upload.html.erb index 3389ae3..23caae5 100644 --- a/app/views/site/upload.html.erb +++ b/app/views/images/upload.html.erb @@ -2,12 +2,12 @@ <%= form_with(url: images_create_path, method: "post", :html => {:class => "form-horizontal center"}) do |form| %>
- <%= form.label(:file, "Choose File:") %> - <%= form.file_field :file, :required => true%> + <%= form.label(:files, "Choose Files:") %> + <%= form.file_field :files, multiple: true, :required => true%>
<%= form.label(:title, "Image Title:") %> - <%= form.text_field :title, :required => true %> + <%= form.text_field :title %>
<%= form.label(:tags, "Choose Tags:") %> diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 3f5b16a..38c0c4f 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -7,6 +7,7 @@ <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %> <%= javascript_tag 'application', 'data-turbolinks-track': 'reload' %> + @@ -24,11 +25,7 @@ - - diff --git a/app/views/site/home.html.erb b/app/views/site/home.html.erb deleted file mode 100644 index 1847db8..0000000 --- a/app/views/site/home.html.erb +++ /dev/null @@ -1,26 +0,0 @@ -

Public Images

- -
- <% @images.each do |img| %> -
-
- <%= image_tag img.file , :width => "100%" %> -
-

<%= img.title %>

- - <% if img.tags %> -

Tags: <%= img.tags %>

- <% end %> - -
- -
- -
- - - <% end %> - -
- -
diff --git a/app/views/site/private.html.erb b/app/views/site/private.html.erb deleted file mode 100644 index b6048a0..0000000 --- a/app/views/site/private.html.erb +++ /dev/null @@ -1,26 +0,0 @@ -

My Private Images

- -
- <% @images.each do |img| %> -
-
- <%= image_tag img.file , :width => "100%" %> -
-

<%= img.title %>

- - <% if img.tags %> -

Tags: <%= img.tags %>

- <% end %> - -
- -
- -
- - - <% end %> - -
- - diff --git a/app/views/users/_form.html.erb b/app/views/users/_form.html.erb deleted file mode 100644 index 636e3e0..0000000 --- a/app/views/users/_form.html.erb +++ /dev/null @@ -1,17 +0,0 @@ -<%= form_with(model: user, local: true) do |form| %> - <% if user.errors.any? %> -
-

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

- - -
- <% end %> - -
- <%= form.submit %> -
-<% end %> diff --git a/app/views/users/_user.json.jbuilder b/app/views/users/_user.json.jbuilder deleted file mode 100644 index 2f081bb..0000000 --- a/app/views/users/_user.json.jbuilder +++ /dev/null @@ -1,2 +0,0 @@ -json.extract! user, :id, :created_at, :updated_at -json.url user_url(user, format: :json) diff --git a/app/views/users/edit.html.erb b/app/views/users/edit.html.erb deleted file mode 100644 index 1a5c2a6..0000000 --- a/app/views/users/edit.html.erb +++ /dev/null @@ -1,6 +0,0 @@ -

Editing User

- -<%= render 'form', user: @user %> - -<%= link_to 'Show', @user %> | -<%= link_to 'Back', users_path %> diff --git a/app/views/users/index.html.erb b/app/views/users/index.html.erb deleted file mode 100644 index f2aea3c..0000000 --- a/app/views/users/index.html.erb +++ /dev/null @@ -1,25 +0,0 @@ -

<%= notice %>

- -

Users

- - - - - - - - - - <% @users.each do |user| %> - - - - - - <% end %> - -
<%= link_to 'Show', user %><%= link_to 'Edit', edit_user_path(user) %><%= link_to 'Destroy', user, method: :delete, data: { confirm: 'Are you sure?' } %>
- -
- -<%= link_to 'New User', new_user_path %> diff --git a/app/views/users/index.json.jbuilder b/app/views/users/index.json.jbuilder deleted file mode 100644 index 98788da..0000000 --- a/app/views/users/index.json.jbuilder +++ /dev/null @@ -1 +0,0 @@ -json.array! @users, partial: "users/user", as: :user diff --git a/app/views/users/new.html.erb b/app/views/users/new.html.erb deleted file mode 100644 index 844c39b..0000000 --- a/app/views/users/new.html.erb +++ /dev/null @@ -1,5 +0,0 @@ -

New User

- -<%= render 'form', user: @user %> - -<%= link_to 'Back', users_path %> diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb deleted file mode 100644 index 32abeb0..0000000 --- a/app/views/users/show.html.erb +++ /dev/null @@ -1,4 +0,0 @@ -

<%= notice %>

- -<%= link_to 'Edit', edit_user_path(@user) %> | -<%= link_to 'Back', users_path %> diff --git a/app/views/users/show.json.jbuilder b/app/views/users/show.json.jbuilder deleted file mode 100644 index ff40bb9..0000000 --- a/app/views/users/show.json.jbuilder +++ /dev/null @@ -1 +0,0 @@ -json.partial! "users/user", user: @user diff --git a/config/routes.rb b/config/routes.rb index 90e7014..050aa5e 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,14 +1,12 @@ Rails.application.routes.draw do - get 'site/home' get 'site/upload' - get 'site/private' + get 'images/upload' post 'images/create' - - root to: 'site#home' - resources :users - resources :images - # get 'site/home' + root to: 'images#index' + resources :images do + get :download, on: :member + end # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html end diff --git a/test/controllers/users_controller_test.rb b/test/controllers/users_controller_test.rb deleted file mode 100644 index 2d142e6..0000000 --- a/test/controllers/users_controller_test.rb +++ /dev/null @@ -1,48 +0,0 @@ -require 'test_helper' - -class UsersControllerTest < ActionDispatch::IntegrationTest - setup do - @user = users(:one) - end - - test "should get index" do - get users_url - assert_response :success - end - - test "should get new" do - get new_user_url - assert_response :success - end - - test "should create user" do - assert_difference('User.count') do - post users_url, params: { user: { } } - end - - assert_redirected_to user_url(User.last) - end - - test "should show user" do - get user_url(@user) - assert_response :success - end - - test "should get edit" do - get edit_user_url(@user) - assert_response :success - end - - test "should update user" do - patch user_url(@user), params: { user: { } } - assert_redirected_to user_url(@user) - end - - test "should destroy user" do - assert_difference('User.count', -1) do - delete user_url(@user) - end - - assert_redirected_to users_url - end -end diff --git a/test/system/users_test.rb b/test/system/users_test.rb deleted file mode 100644 index 9782a5c..0000000 --- a/test/system/users_test.rb +++ /dev/null @@ -1,41 +0,0 @@ -require "application_system_test_case" - -class UsersTest < ApplicationSystemTestCase - setup do - @user = users(:one) - end - - test "visiting the index" do - visit users_url - assert_selector "h1", text: "Users" - end - - test "creating a User" do - visit users_url - click_on "New User" - - click_on "Create User" - - assert_text "User was successfully created" - click_on "Back" - end - - test "updating a User" do - visit users_url - click_on "Edit", match: :first - - click_on "Update User" - - assert_text "User was successfully updated" - click_on "Back" - end - - test "destroying a User" do - visit users_url - page.accept_confirm do - click_on "Destroy", match: :first - end - - assert_text "User was successfully destroyed" - end -end