Skip to content

Commit

Permalink
Merge pull request #1 from sorah/downloading
Browse files Browse the repository at this point in the history
Add StuffsController#download
  • Loading branch information
sorah committed May 24, 2014
2 parents a513ae7 + 6f1523f commit 5450f1c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
6 changes: 5 additions & 1 deletion app/controllers/stuffs_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class StuffsController < ApplicationController
before_action :set_stuff, only: [:show, :edit, :update, :destroy]
before_action :set_stuff, only: [:show, :edit, :update, :destroy, :download]

# GET /stuffs
# GET /stuffs.json
Expand Down Expand Up @@ -39,6 +39,10 @@ def destroy
end
end

def download
send_file @stuff.filepath
end

private
# Use callbacks to share common setup or constraints between actions.
def set_stuff
Expand Down
6 changes: 5 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
Rails.application.routes.draw do
resources :stuffs, only: %i(index create show destroy)
resources :stuffs, only: %i(index create show destroy) do
member do
get :download
end
end

# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
Expand Down
20 changes: 19 additions & 1 deletion spec/controllers/stuffs_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@
# This should return the minimal set of attributes required to create a valid
# Stuff. As you add validations to Stuff, be sure to
# adjust the attributes here as well.
let(:file) do
fixture_file_upload('a.png')
end

let(:valid_attributes) do
{
title: "MyString",
file: fixture_file_upload('a.png'),
file: file,
}
end

Expand Down Expand Up @@ -103,4 +107,18 @@
end
end

describe "GET download" do
let!(:stuff) do
Stuff.create!(valid_attributes)
end

context "when stuff exists" do
it "sends the file" do
get :download, id: stuff.id

expect(response.code).to eq '200'
expect(response.body).to eq File.binread(file.path)
end
end
end
end

0 comments on commit 5450f1c

Please sign in to comment.