diff --git a/.gitignore b/.gitignore index ba1d63c..dd94029 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,5 @@ /tmp /Gemfile.lock + +/coverage diff --git a/app/models/upload.rb b/app/models/upload.rb new file mode 100644 index 0000000..648bd26 --- /dev/null +++ b/app/models/upload.rb @@ -0,0 +1,17 @@ +class Upload < ActiveRecord::Base + attr_accessor :delete_password + attr_accessor :download_password + + before_save :encrypt + + validates :filename, presence: true + validates :ext, presence: true + validates :content_type, presence: true + validates :data, presence: true + + private + + def encrypt + true + end +end diff --git a/bin/rspec b/bin/rspec new file mode 100755 index 0000000..bba8a9e --- /dev/null +++ b/bin/rspec @@ -0,0 +1,2 @@ +#!/usr/bin/env bash +exec $(dirname $0)/spring rspec "$@" diff --git a/bin/spring b/bin/spring new file mode 100755 index 0000000..ce54348 --- /dev/null +++ b/bin/spring @@ -0,0 +1,18 @@ +#!/usr/bin/env ruby + +# This is a special way of invoking the spring gem in order to +# work around the performance issue discussed in +# https://github.com/rubygems/rubygems/pull/435 + +glob = "{#{Gem::Specification.dirs.join(",")}}/spring-*.gemspec" +candidates = Dir[glob].to_a.sort_by { |c| Gem::Version.new(File.basename(c).split(/[-\.]/)[1...-1].join(".")) } + +spec = Gem::Specification.load(candidates.last) + +if spec + spec.activate + load spec.bin_file("spring") +else + $stderr.puts "Could not find spring gem in #{Gem::Specification.dirs.join(", ")}." + exit 1 +end diff --git a/db/migrate/20131011162152_create_uploads.rb b/db/migrate/20131011162152_create_uploads.rb new file mode 100644 index 0000000..6b6e252 --- /dev/null +++ b/db/migrate/20131011162152_create_uploads.rb @@ -0,0 +1,14 @@ +class CreateUploads < ActiveRecord::Migration + def change + create_table :uploads do |t| + t.string :filename, null: false + t.string :ext, null: false + t.string :content_type, null: false + t.binary :data, null: false + t.string :download_password_encrypt + t.string :delete_password_encrypt + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 0000000..a915763 --- /dev/null +++ b/db/schema.rb @@ -0,0 +1,27 @@ +# encoding: UTF-8 +# This file is auto-generated from the current state of the database. Instead +# of editing this file, please use the migrations feature of Active Record to +# incrementally modify your database, and then regenerate this schema definition. +# +# Note that this schema.rb definition is the authoritative source for your +# database schema. If you need to create the application database on another +# system, you should be using db:schema:load, not running all the migrations +# from scratch. The latter is a flawed and unsustainable approach (the more migrations +# you'll amass, the slower it'll run and the greater likelihood for issues). +# +# It's strongly recommended that you check this file into your version control system. + +ActiveRecord::Schema.define(version: 20131011162152) do + + create_table "uploads", force: true do |t| + t.string "filename", null: false + t.string "ext", null: false + t.string "content_type", null: false + t.binary "data", null: false + t.string "download_password_encrypt" + t.string "delete_password_encrypt" + t.datetime "created_at" + t.datetime "updated_at" + end + +end diff --git a/spec/factories/uploads.rb b/spec/factories/uploads.rb new file mode 100644 index 0000000..9e55b60 --- /dev/null +++ b/spec/factories/uploads.rb @@ -0,0 +1,10 @@ +# Read about factories at https://github.com/thoughtbot/factory_girl + +FactoryGirl.define do + factory :upload do + filename "xx.png" + ext ".png" + content_type "image/png" + data "asdf" + end +end diff --git a/spec/models/upload_spec.rb b/spec/models/upload_spec.rb new file mode 100644 index 0000000..69beeae --- /dev/null +++ b/spec/models/upload_spec.rb @@ -0,0 +1,21 @@ +require 'spec_helper' + +describe Upload do + let(:upload) { FactoryGirl.build :upload } + describe "validations" do + it { should validate_presence_of :filename } + it { should validate_presence_of :ext } + it { should validate_presence_of :content_type } + it { should validate_presence_of :data } + context "when parameters is #{FactoryGirl.attributes_for(:upload).to_s}" do + it { expect(upload).to be_valid } + end + end + + describe "#encrypt" do + it "should accept encrypt before save" do + expect(upload).to receive(:encrypt).and_return(true) + upload.save + end + end +end