Skip to content
This repository was archived by the owner on Jun 6, 2020. It is now read-only.

Commit 33366a9

Browse files
committed
Upload model and controller
1 parent 2d021bf commit 33366a9

File tree

17 files changed

+243
-3
lines changed

17 files changed

+243
-3
lines changed

Gemfile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,14 @@ group :development, :test do
4646
gem 'hirb'
4747
gem 'hirb-unicode'
4848
gem 'awesome_print'
49+
50+
gem 'rspec-rails'
51+
gem 'factory_girl_rails'
4952
end
5053

5154
group :test do
52-
gem 'rspec-rails'
5355
gem 'shoulda-matchers'
5456
gem 'database_rewinder'
55-
gem 'factory_girl_rails'
5657
gem 'forgery'
5758
gem 'capybara'
5859

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Place all the behaviors and hooks related to the matching controller here.
2+
# All this logic will automatically be available in application.js.
3+
# You can use CoffeeScript in this file: http://coffeescript.org/
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Place all the styles related to the upload controller here.
2+
// They will automatically be included in application.css.
3+
// You can use Sass (SCSS) here: http://sass-lang.com/
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class UploadsController < ApplicationController
2+
before_action :set_upload, only: [:destroy]
3+
4+
def new
5+
@upload = Upload.new
6+
end
7+
8+
def create
9+
@upload = Upload.new(upload_params)
10+
11+
if @upload.save
12+
redirect_to root_url, notice: 'Upload was successfully created.'
13+
else
14+
render action: 'new'
15+
end
16+
end
17+
18+
# DELETE /uploads/1
19+
# DELETE /uploads/1.json
20+
def destroy
21+
@upload.destroy
22+
redirect_to root_url
23+
end
24+
25+
private
26+
27+
def set_upload
28+
@upload = Upload.find(params[:id])
29+
end
30+
31+
def upload_params
32+
params.require(:upload).permit(:file).merge(ip: request.remote_ip)
33+
end
34+
end

app/helpers/uploads_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module UploadsHelper
2+
end

app/models/upload.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Upload < ActiveRecord::Base
2+
attr_accessor :file
3+
4+
before_validation :set_attributes_from_file, :if => Proc.new { |instance| instance.new_record? }
5+
6+
validates :filename, presence: true, format: { with: /\.[A-Za-z]+\Z/ }
7+
validates :ext, presence: true
8+
validates :ip, presence: true
9+
10+
private
11+
12+
def set_attributes_from_file
13+
set_filename
14+
set_ext
15+
set_data
16+
end
17+
18+
def set_data
19+
self.data = @file.read
20+
end
21+
22+
def set_filename
23+
self.filename = @file.original_filename
24+
end
25+
26+
def set_ext
27+
self.ext = File.extname(@file.original_filename)
28+
end
29+
end

app/views/uploads/_form.html.haml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
= form_for @upload do |f|
2+
- if @upload.errors.any?
3+
#error_explanation
4+
%h2= "#{pluralize(@upload.errors.count, "error")} prohibited this upload from being saved:"
5+
%ul
6+
- @upload.errors.full_messages.each do |msg|
7+
%li= msg
8+
9+
.actions
10+
= f.submit 'Save'

app/views/uploads/new.html.haml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
%h1 New upload
2+
3+
= render 'form'
4+
5+
= link_to 'Back', uploads_path

config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
PecaUploader::Application.routes.draw do
22
root "welcome#index"
3+
resources :upload, only: [:new, :create, :destroy]
34
end
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class CreateUploads < ActiveRecord::Migration
2+
def change
3+
create_table :uploads do |t|
4+
t.string :filename, presence: true
5+
t.string :ext, presence: true
6+
t.string :comment
7+
t.string :ip, presence: true
8+
t.integer :view_count
9+
t.binary :data, :limit => 5.megabytes
10+
11+
t.timestamps
12+
end
13+
end
14+
end

0 commit comments

Comments
 (0)