From 771a881c910a8fb0c9dbface321bd39582b9c1bb Mon Sep 17 00:00:00 2001 From: Jesse Morgan Date: Sun, 29 Mar 2015 14:23:06 -0700 Subject: Cleaning up routes and the Downloads controller. --- app/controllers/downloads_controller.rb | 69 +++------------------------------ app/views/downloads/_form.html.erb | 37 ------------------ app/views/downloads/edit.html.erb | 6 --- app/views/downloads/new.html.erb | 5 --- app/views/downloads/show.html.erb | 29 -------------- app/views/downloads/show.json.jbuilder | 1 - config/routes.rb | 58 +-------------------------- 7 files changed, 6 insertions(+), 199 deletions(-) delete mode 100644 app/views/downloads/_form.html.erb delete mode 100644 app/views/downloads/edit.html.erb delete mode 100644 app/views/downloads/new.html.erb delete mode 100644 app/views/downloads/show.html.erb delete mode 100644 app/views/downloads/show.json.jbuilder diff --git a/app/controllers/downloads_controller.rb b/app/controllers/downloads_controller.rb index 2681b0a..14e3b04 100644 --- a/app/controllers/downloads_controller.rb +++ b/app/controllers/downloads_controller.rb @@ -11,16 +11,16 @@ class DownloadsController < ApplicationController end # GET /downloads/:filename - def download - download = Download.find_by(filename: params[:filename]) or not_found + def show + not_found if @download.nil? s3client = Aws::S3::Client.new() - s3obj = Aws::S3::Object.new(ENV['S3_DOWNLOADS_BUCKET'], download.filename, client: s3client) + s3obj = Aws::S3::Object.new(ENV['S3_DOWNLOADS_BUCKET'], @download.filename, client: s3client) resp = s3obj.get FileUtils.mkdir_p(Rails.root.join('tmp', 'downloads')) - cached_filename = Rails.root.join('tmp', 'downloads', download.filename) + cached_filename = Rails.root.join('tmp', 'downloads', @download.filename) File.open(cached_filename, 'wb') do |file| file.write resp.body.read @@ -29,68 +29,9 @@ class DownloadsController < ApplicationController send_file cached_filename end - # GET /downloads/1 - # GET /downloads/1.json - def show - end - - # GET /downloads/new - def new - @download = Download.new - end - - # GET /downloads/1/edit - def edit - end - - # POST /downloads - # POST /downloads.json - def create - @download = Download.new(download_params) - - respond_to do |format| - if @download.save - format.html { redirect_to @download, notice: 'Download was successfully created.' } - format.json { render :show, status: :created, location: @download } - else - format.html { render :new } - format.json { render json: @download.errors, status: :unprocessable_entity } - end - end - end - - # PATCH/PUT /downloads/1 - # PATCH/PUT /downloads/1.json - def update - respond_to do |format| - if @download.update(download_params) - format.html { redirect_to @download, notice: 'Download was successfully updated.' } - format.json { render :show, status: :ok, location: @download } - else - format.html { render :edit } - format.json { render json: @download.errors, status: :unprocessable_entity } - end - end - end - - # DELETE /downloads/1 - # DELETE /downloads/1.json - def destroy - @download.destroy - respond_to do |format| - format.html { redirect_to downloads_url, notice: 'Download was successfully destroyed.' } - format.json { head :no_content } - end - end - private # Use callbacks to share common setup or constraints between actions. def set_download - @download = Download.find(params[:id]) - end - - # Never trust parameters from the scary internet, only allow the white list through. - def download_params - params.require(:download).permit(:name, :filename, :type, :description, :hits) + @download = Download.find_by(filename: params[:id]) end end diff --git a/app/views/downloads/_form.html.erb b/app/views/downloads/_form.html.erb deleted file mode 100644 index 1ab1879..0000000 --- a/app/views/downloads/_form.html.erb +++ /dev/null @@ -1,37 +0,0 @@ -<%= form_for(@download) do |f| %> - <% if @download.errors.any? %> -
-

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

- - -
- <% end %> - -
- <%= f.label :name %>
- <%= f.text_field :name %> -
-
- <%= f.label :filename %>
- <%= f.text_field :filename %> -
-
- <%= f.label :type %>
- <%= f.text_field :type %> -
-
- <%= f.label :description %>
- <%= f.text_area :description %> -
-
- <%= f.label :hits %>
- <%= f.number_field :hits %> -
-
- <%= f.submit %> -
-<% end %> diff --git a/app/views/downloads/edit.html.erb b/app/views/downloads/edit.html.erb deleted file mode 100644 index 3b57a65..0000000 --- a/app/views/downloads/edit.html.erb +++ /dev/null @@ -1,6 +0,0 @@ -

Editing Download

- -<%= render 'form' %> - -<%= link_to 'Show', @download %> | -<%= link_to 'Back', downloads_path %> diff --git a/app/views/downloads/new.html.erb b/app/views/downloads/new.html.erb deleted file mode 100644 index ac31df5..0000000 --- a/app/views/downloads/new.html.erb +++ /dev/null @@ -1,5 +0,0 @@ -

New Download

- -<%= render 'form' %> - -<%= link_to 'Back', downloads_path %> diff --git a/app/views/downloads/show.html.erb b/app/views/downloads/show.html.erb deleted file mode 100644 index 276888a..0000000 --- a/app/views/downloads/show.html.erb +++ /dev/null @@ -1,29 +0,0 @@ -

<%= notice %>

- -

- Name: - <%= @download.name %> -

- -

- Filename: - <%= @download.filename %> -

- -

- Type: - <%= @download.type %> -

- -

- Description: - <%= @download.description %> -

- -

- Hits: - <%= @download.hits %> -

- -<%= link_to 'Edit', edit_download_path(@download) %> | -<%= link_to 'Back', downloads_path %> diff --git a/app/views/downloads/show.json.jbuilder b/app/views/downloads/show.json.jbuilder deleted file mode 100644 index 6bab62c..0000000 --- a/app/views/downloads/show.json.jbuilder +++ /dev/null @@ -1 +0,0 @@ -json.extract! @download, :id, :name, :filename, :type, :description, :hits, :created_at, :updated_at diff --git a/config/routes.rb b/config/routes.rb index 33b0274..6487e82 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,59 +1,3 @@ Rails.application.routes.draw do - get 'downloads' => 'downloads#index' - get 'downloads/:filename' => 'downloads#download', :filename => /.+/, :as => 'download' - - # The priority is based upon order of creation: first created -> highest priority. - # See how all your routes lay out with "rake routes". - - # You can have the root of your site routed with "root" - # root 'welcome#index' - - # Example of regular route: - # get 'products/:id' => 'catalog#view' - - # Example of named route that can be invoked with purchase_url(id: product.id) - # get 'products/:id/purchase' => 'catalog#purchase', as: :purchase - - # Example resource route (maps HTTP verbs to controller actions automatically): - # resources :products - - # Example resource route with options: - # resources :products do - # member do - # get 'short' - # post 'toggle' - # end - # - # collection do - # get 'sold' - # end - # end - - # Example resource route with sub-resources: - # resources :products do - # resources :comments, :sales - # resource :seller - # end - - # Example resource route with more complex sub-resources: - # resources :products do - # resources :comments - # resources :sales do - # get 'recent', on: :collection - # end - # end - - # Example resource route with concerns: - # concern :toggleable do - # post 'toggle' - # end - # resources :posts, concerns: :toggleable - # resources :photos, concerns: :toggleable - - # Example resource route within a namespace: - # namespace :admin do - # # Directs /admin/products/* to Admin::ProductsController - # # (app/controllers/admin/products_controller.rb) - # resources :products - # end + resources :downloads, :only => [:index, :show], :id => /.+/ end -- cgit v1.2.3