Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Setup Document model and pdf attachment.

* Added MongoMapper & Carrierwave
  • Loading branch information...
commit b4ccb5348913869db00eef50380b521dc3ae6842 1 parent 50d7741
@jonmagic authored
View
4 Gemfile
@@ -1,6 +1,10 @@
source :rubygems
gem 'sinatra'
gem 'rake'
+gem 'bson_ext'
+gem 'mongo_mapper'
+gem 'carrierwave', :git => 'git://github.com/jnicklas/carrierwave.git' # fixes issue with sinatra 1.3+
+gem 'mm-carrierwave'
group :test do
gem 'rspec'
View
33 Gemfile.lock
@@ -1,6 +1,22 @@
+GIT
+ remote: git://github.com/jnicklas/carrierwave.git
+ revision: 7e5913b0fd627b8b91a95df6466748e4276ed21a
+ specs:
+ carrierwave (0.5.7)
+ activesupport (~> 3.0)
+
GEM
remote: http://rubygems.org/
specs:
+ activemodel (3.1.1)
+ activesupport (= 3.1.1)
+ builder (~> 3.0.0)
+ i18n (~> 0.6)
+ activesupport (3.1.1)
+ multi_json (~> 1.0)
+ bson (1.4.0)
+ bson_ext (1.4.0)
+ builder (3.0.0)
capybara (1.1.1)
mime-types (>= 1.16)
nokogiri (>= 1.3.3)
@@ -12,9 +28,22 @@ GEM
ffi (~> 1.0.6)
diff-lcs (1.1.3)
ffi (1.0.9)
+ i18n (0.6.0)
json_pure (1.6.1)
mime-types (1.16)
+ mm-carrierwave (0.0.2)
+ carrierwave (~> 0.5)
+ mongo_mapper (~> 0.9)
+ mongo (1.3.1)
+ bson (>= 1.3.1)
+ mongo_mapper (0.9.2)
+ activemodel (~> 3.0)
+ activesupport (~> 3.0)
+ plucky (~> 0.3.8)
+ multi_json (1.0.3)
nokogiri (1.5.0)
+ plucky (0.3.8)
+ mongo (~> 1.3)
rack (1.3.4)
rack-protection (1.1.4)
rack
@@ -47,7 +76,11 @@ PLATFORMS
ruby
DEPENDENCIES
+ bson_ext
capybara
+ carrierwave!
+ mm-carrierwave
+ mongo_mapper
rake
rspec
sinatra
View
6 lib/document.rb
@@ -0,0 +1,6 @@
+class Document
+ include MongoMapper::Document
+
+ key :page_contents, Array
+ mount_uploader :pdf, PdfUploader
+end
View
26 lib/pdf_archive.rb
@@ -3,6 +3,32 @@
require 'bundler'
Bundler.require
+# Application module
+module PdfArchive
+ def self.environment
+ ENV['RACK_ENV'] || 'development'
+ end
+
+ def self.root
+ @root ||= Pathname(File.expand_path('../..', __FILE__))
+ end
+end
+
+# MongoMapper setup
+mongo_url = ENV['MONGOHQ_URL'] || "mongodb://localhost:27017/pdf_archive-#{PdfArchive.environment}"
+uri = URI.parse(mongo_url)
+database = uri.path.gsub('/', '')
+MongoMapper.connection = Mongo::Connection.new(uri.host, uri.port, {})
+MongoMapper.database = database
+
+# CarrierWave setup
+require 'carrierwave/orm/mongomapper'
+
+# require pdf uploader and document model
+require 'pdf_uploader'
+require 'document'
+
+# Routes
get '/' do
erb :home
end
View
15 lib/pdf_uploader.rb
@@ -0,0 +1,15 @@
+class PdfUploader < CarrierWave::Uploader::Base
+ storage :file
+
+ def cache_dir
+ "#{PdfArchive.root}/tmp/cache/#{model.id}"
+ end
+
+ def store_dir
+ if PdfArchive.environment == 'test'
+ "#{PdfArchive.root}/tmp/documents/#{model.id}"
+ else
+ "#{PdfArchive.root}/public/documents/#{model.id}"
+ end
+ end
+end
View
19 spec/document_spec.rb
@@ -0,0 +1,19 @@
+require 'spec_helper'
+
+describe Document do
+ describe "properties" do
+ let(:document) { Document.create(:pdf => pdf_fixture('onepage.pdf')) }
+
+ it "has an id" do
+ document.id.should be_present
+ end
+
+ it "has a pdf_filename" do
+ document.pdf_filename.should == "onepage.pdf"
+ end
+
+ it "has an array for page_contents" do
+ document.page_contents.should == []
+ end
+ end
+end
View
BIN  spec/fixtures/onepage.pdf
Binary file not shown
View
8 spec/pdf_archive_spec.rb
@@ -1,5 +1,13 @@
require 'spec_helper'
+describe PdfArchive do
+ describe ".environment" do
+ it "should return the environment" do
+ PdfArchive.environment.should == "test"
+ end
+ end
+end
+
describe 'PDF Archive', :type => :request do
describe "GET /" do
before(:each) do
View
29 spec/spec_helper.rb
@@ -1,6 +1,9 @@
+ENV['RACK_ENV'] = 'test'
+
require 'bundler'
Bundler.require 'test'
require 'rack/test'
+require 'fileutils'
require File.expand_path(File.dirname(__FILE__) + "/../lib/pdf_archive")
@@ -8,8 +11,28 @@
RSpec.configure do |config|
config.include Rack::Test::Methods
-end
-def app
- Sinatra::Application
+ config.before do
+ MongoMapper.database.collections.each(&:remove)
+ end
+
+ def pdf_fixture(filename)
+ fixture_path = File.expand_path("./spec/fixtures/")
+ file_path = File.join(fixture_path, filename)
+ File.open(file_path)
+ end
+
+ def tmp_dir
+ path = File.expand_path("./tmp")
+ Dir.mkdir(path) unless File.directory?(path)
+ path
+ end
+
+ def app
+ Sinatra::Application
+ end
+
+ config.after(:all) do
+ FileUtils.rm_rf(tmp_dir)
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.