griddle 0.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.rdoc +17 -0
- data/Rakefile +52 -0
- data/VERSION +1 -0
- data/griddle.gemspec +73 -0
- data/lib/griddle/attachment.rb +72 -0
- data/lib/griddle/has_grid_attachment.rb +53 -0
- data/lib/griddle/style.rb +39 -0
- data/lib/griddle/upfile.rb +48 -0
- data/lib/griddle.rb +18 -0
- data/rails/init.rb +1 -0
- data/test/attachment_test.rb +42 -0
- data/test/fixtures/baboon.jpg +0 -0
- data/test/fixtures/fox.jpg +0 -0
- data/test/fixtures/sample.pdf +0 -0
- data/test/has_attachment_test.rb +72 -0
- data/test/models.rb +19 -0
- data/test/style_test.rb +51 -0
- data/test/test_helper.rb +27 -0
- data/test/upfile_test.rb +25 -0
- metadata +101 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Matt Mongeau
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
= griddle
|
2
|
+
|
3
|
+
Description goes here.
|
4
|
+
|
5
|
+
== Note on Patches/Pull Requests
|
6
|
+
|
7
|
+
* Fork the project.
|
8
|
+
* Make your feature addition or bug fix.
|
9
|
+
* Add tests for it. This is important so I don't break it in a
|
10
|
+
future version unintentionally.
|
11
|
+
* Commit, do not mess with rakefile, version, or history.
|
12
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
13
|
+
* Send me a pull request. Bonus points for topic branches.
|
14
|
+
|
15
|
+
== Copyright
|
16
|
+
|
17
|
+
Copyright (c) 2010 Matt Matt san Mongeau. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "griddle"
|
8
|
+
gem.summary = %Q{GridFS made simple.}
|
9
|
+
gem.description = %Q{GridFS made simple...}
|
10
|
+
gem.email = "matt@toastyapps.com"
|
11
|
+
gem.homepage = "http://github.com/toastyapps/griddle"
|
12
|
+
gem.authors = ["Matt Mongeau"]
|
13
|
+
gem.add_development_dependency "shoulda", ">= 0"
|
14
|
+
gem.add_dependency "mongo_mapper", ">= 0"
|
15
|
+
end
|
16
|
+
Jeweler::GemcutterTasks.new
|
17
|
+
rescue LoadError
|
18
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
19
|
+
end
|
20
|
+
|
21
|
+
require 'rake/testtask'
|
22
|
+
Rake::TestTask.new(:test) do |test|
|
23
|
+
test.libs << 'lib' << 'test'
|
24
|
+
test.pattern = 'test/**/*_test.rb'
|
25
|
+
test.verbose = true
|
26
|
+
end
|
27
|
+
|
28
|
+
begin
|
29
|
+
require 'rcov/rcovtask'
|
30
|
+
Rcov::RcovTask.new do |test|
|
31
|
+
test.libs << 'test'
|
32
|
+
test.pattern = 'test/**/*_test.rb'
|
33
|
+
test.verbose = true
|
34
|
+
end
|
35
|
+
rescue LoadError
|
36
|
+
task :rcov do
|
37
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
task :test => :check_dependencies
|
42
|
+
task :default => :test
|
43
|
+
|
44
|
+
require 'rake/rdoctask'
|
45
|
+
Rake::RDocTask.new do |rdoc|
|
46
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
47
|
+
|
48
|
+
rdoc.rdoc_dir = 'rdoc'
|
49
|
+
rdoc.title = "griddle #{version}"
|
50
|
+
rdoc.rdoc_files.include('README*')
|
51
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
52
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.0
|
data/griddle.gemspec
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{griddle}
|
8
|
+
s.version = "0.0.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Matt Mongeau"]
|
12
|
+
s.date = %q{2010-03-03}
|
13
|
+
s.description = %q{GridFS made simple...}
|
14
|
+
s.email = %q{matt@toastyapps.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
22
|
+
"LICENSE",
|
23
|
+
"README.rdoc",
|
24
|
+
"Rakefile",
|
25
|
+
"VERSION",
|
26
|
+
"griddle.gemspec",
|
27
|
+
"lib/griddle.rb",
|
28
|
+
"lib/griddle/attachment.rb",
|
29
|
+
"lib/griddle/has_grid_attachment.rb",
|
30
|
+
"lib/griddle/style.rb",
|
31
|
+
"lib/griddle/upfile.rb",
|
32
|
+
"rails/init.rb",
|
33
|
+
"test/attachment_test.rb",
|
34
|
+
"test/fixtures/baboon.jpg",
|
35
|
+
"test/fixtures/fox.jpg",
|
36
|
+
"test/fixtures/sample.pdf",
|
37
|
+
"test/has_attachment_test.rb",
|
38
|
+
"test/models.rb",
|
39
|
+
"test/style_test.rb",
|
40
|
+
"test/test_helper.rb",
|
41
|
+
"test/upfile_test.rb"
|
42
|
+
]
|
43
|
+
s.homepage = %q{http://github.com/toastyapps/griddle}
|
44
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
45
|
+
s.require_paths = ["lib"]
|
46
|
+
s.rubygems_version = %q{1.3.5}
|
47
|
+
s.summary = %q{GridFS made simple.}
|
48
|
+
s.test_files = [
|
49
|
+
"test/attachment_test.rb",
|
50
|
+
"test/has_attachment_test.rb",
|
51
|
+
"test/models.rb",
|
52
|
+
"test/style_test.rb",
|
53
|
+
"test/test_helper.rb",
|
54
|
+
"test/upfile_test.rb"
|
55
|
+
]
|
56
|
+
|
57
|
+
if s.respond_to? :specification_version then
|
58
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
59
|
+
s.specification_version = 3
|
60
|
+
|
61
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
62
|
+
s.add_development_dependency(%q<shoulda>, [">= 0"])
|
63
|
+
s.add_runtime_dependency(%q<mongo_mapper>, [">= 0"])
|
64
|
+
else
|
65
|
+
s.add_dependency(%q<shoulda>, [">= 0"])
|
66
|
+
s.add_dependency(%q<mongo_mapper>, [">= 0"])
|
67
|
+
end
|
68
|
+
else
|
69
|
+
s.add_dependency(%q<shoulda>, [">= 0"])
|
70
|
+
s.add_dependency(%q<mongo_mapper>, [">= 0"])
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
@@ -0,0 +1,72 @@
|
|
1
|
+
module Griddle
|
2
|
+
class Attachment
|
3
|
+
include MongoMapper::Document
|
4
|
+
|
5
|
+
belongs_to :owner, :polymorphic => true
|
6
|
+
key :name, String
|
7
|
+
key :owner_id, ObjectId, :required => true
|
8
|
+
key :owner_type, String, :required => true
|
9
|
+
key :file_name, String
|
10
|
+
key :file_size, Integer
|
11
|
+
key :content_type, String
|
12
|
+
key :styles, Hash
|
13
|
+
key :options, Hash
|
14
|
+
|
15
|
+
before_destroy :destroy_file
|
16
|
+
before_save :save_file
|
17
|
+
|
18
|
+
def self.for(name, owner, options = {})
|
19
|
+
a = Attachment.find_or_create_by_name_and_owner_type_and_owner_id(name, owner.class.to_s, owner.id)
|
20
|
+
if options.has_key?(:styles)
|
21
|
+
a.styles = (options[:styles] || {}).inject({}) do |h, value|
|
22
|
+
h[value.first] = Style.new value.first, value.last, a
|
23
|
+
h
|
24
|
+
end
|
25
|
+
end
|
26
|
+
a
|
27
|
+
end
|
28
|
+
|
29
|
+
def grid_key
|
30
|
+
@grid_key ||= "#{owner_type.tableize}/#{owner_id}/#{name}/#{file_name}".downcase
|
31
|
+
end
|
32
|
+
|
33
|
+
def assign(uploaded_file)
|
34
|
+
return nil unless valid_assignment?(uploaded_file)
|
35
|
+
@tmp_file = uploaded_file
|
36
|
+
end
|
37
|
+
|
38
|
+
def file=(new_file)
|
39
|
+
file_name = new_file.respond_to?(:original_filename) ? new_file.original_filename : File.basename(new_file.path)
|
40
|
+
self.file_name = file_name
|
41
|
+
self.file_size = File.size(new_file)
|
42
|
+
self.content_type = new_file.content_type
|
43
|
+
|
44
|
+
GridFS::GridStore.open(self.class.database, grid_key, 'w', :content_type => self.content_type) do |f|
|
45
|
+
f.write new_file.read
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def file
|
50
|
+
GridFS::GridStore.new(self.class.database, grid_key, 'r') unless file_name.blank?
|
51
|
+
end
|
52
|
+
|
53
|
+
def destroy_file
|
54
|
+
GridFS::GridStore.unlink(self.class.database, grid_key)
|
55
|
+
end
|
56
|
+
|
57
|
+
def exists?
|
58
|
+
!file_name.nil?
|
59
|
+
end
|
60
|
+
|
61
|
+
private
|
62
|
+
|
63
|
+
def save_file
|
64
|
+
self.file = @tmp_file if @tmp_file
|
65
|
+
end
|
66
|
+
|
67
|
+
def valid_assignment?(file)
|
68
|
+
file.nil? || (file.respond_to?(:original_filename) && file.respond_to?(:content_type))
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'mongo/gridfs'
|
2
|
+
module Griddle
|
3
|
+
module HasGridAttachment
|
4
|
+
def self.included(base)
|
5
|
+
base.class_eval do
|
6
|
+
extend ClassMethods
|
7
|
+
include InstanceMethods
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
module ClassMethods
|
12
|
+
def has_grid_attachment name, options = {}
|
13
|
+
write_inheritable_attribute(:attachment_definitions, {}) if attachment_definitions.nil?
|
14
|
+
attachment_definitions[name] = options
|
15
|
+
|
16
|
+
after_save :save_attached_files
|
17
|
+
|
18
|
+
define_method(name) do |*args|
|
19
|
+
attachment_for(name)
|
20
|
+
end
|
21
|
+
|
22
|
+
define_method("#{name}=") do |file|
|
23
|
+
attachment_for(name).assign(file)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def attachment_definitions
|
28
|
+
read_inheritable_attribute(:attachment_definitions)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
module InstanceMethods
|
33
|
+
|
34
|
+
def attachment_for name
|
35
|
+
@_gripster_attachments ||= {}
|
36
|
+
@_gripster_attachments[name] ||= Attachment.for(name, self)
|
37
|
+
end
|
38
|
+
|
39
|
+
def each_attachment
|
40
|
+
self.class.attachment_definitions.each do |name, definition|
|
41
|
+
yield(name, attachment_for(name))
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def save_attached_files
|
46
|
+
each_attachment do |name, attachment|
|
47
|
+
attachment.owner_id = self.id
|
48
|
+
attachment.send(:save) unless attachment.nil?
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module Griddle
|
2
|
+
class Style
|
3
|
+
|
4
|
+
attr_accessor :name, :definition, :attachment
|
5
|
+
|
6
|
+
def initialize(name, definition, attachment)
|
7
|
+
@name = name
|
8
|
+
@attachment = attachment
|
9
|
+
@definition = case
|
10
|
+
when definition.is_a?(String)
|
11
|
+
{
|
12
|
+
:geometry => definition
|
13
|
+
}
|
14
|
+
when definition.is_a?(Array)
|
15
|
+
raise "Don't send an array to Style"
|
16
|
+
else
|
17
|
+
raise "Definition must be a type of String, Array, or Hash" unless definition.is_a?(Hash)
|
18
|
+
{
|
19
|
+
:geometry => definition[:geometry]
|
20
|
+
}
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
def [](key)
|
26
|
+
return nil unless respond_to? key
|
27
|
+
send(key)
|
28
|
+
end
|
29
|
+
|
30
|
+
def attachment
|
31
|
+
@attachment
|
32
|
+
end
|
33
|
+
|
34
|
+
def geometry
|
35
|
+
@definition[:geometry]
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
module Griddle
|
2
|
+
# The Upfile module is a convenience module for adding uploaded-file-type methods
|
3
|
+
# to the +File+ class. Useful for testing.
|
4
|
+
# user.avatar = File.new("test/test_avatar.jpg")
|
5
|
+
module Upfile
|
6
|
+
|
7
|
+
# Infer the MIME-type of the file from the extension.
|
8
|
+
def content_type
|
9
|
+
type = (self.path.match(/\.(\w+)$/)[1] rescue "octet-stream").downcase
|
10
|
+
case type
|
11
|
+
when %r"jp(e|g|eg)" then "image/jpeg"
|
12
|
+
when %r"tiff?" then "image/tiff"
|
13
|
+
when %r"png", "gif", "bmp" then "image/#{type}"
|
14
|
+
when "txt" then "text/plain"
|
15
|
+
when %r"html?" then "text/html"
|
16
|
+
when "js" then "application/js"
|
17
|
+
when "csv", "xml", "css" then "text/#{type}"
|
18
|
+
else "application/x-#{type}"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
# Returns the file's normal name.
|
23
|
+
def original_filename
|
24
|
+
File.basename(self.path)
|
25
|
+
end
|
26
|
+
|
27
|
+
# Returns the size of the file.
|
28
|
+
def size
|
29
|
+
File.size(self)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
if defined? StringIO
|
35
|
+
class StringIO
|
36
|
+
attr_accessor :original_filename, :content_type
|
37
|
+
def original_filename
|
38
|
+
@original_filename ||= "stringio.txt"
|
39
|
+
end
|
40
|
+
def content_type
|
41
|
+
@content_type ||= "text/plain"
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
class File #:nodoc:
|
47
|
+
include Griddle::Upfile
|
48
|
+
end
|
data/lib/griddle.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
if RUBY_VERSION =~ /1.9.[0-9]/
|
2
|
+
require 'fileutils'
|
3
|
+
else
|
4
|
+
require 'ftools'
|
5
|
+
end
|
6
|
+
require 'mime/types'
|
7
|
+
require 'griddle/has_grid_attachment'
|
8
|
+
require 'griddle/attachment'
|
9
|
+
require 'griddle/upfile'
|
10
|
+
require 'griddle/style'
|
11
|
+
|
12
|
+
module Griddle
|
13
|
+
def self.version
|
14
|
+
"0.0.1"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
File.send(:include, Griddle::Upfile)
|
data/rails/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'griddle'
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
require "models"
|
3
|
+
|
4
|
+
class AttachmentTest < Test::Unit::TestCase
|
5
|
+
|
6
|
+
context "An Attachment" do
|
7
|
+
|
8
|
+
setup do
|
9
|
+
@dir = File.dirname(__FILE__) + '/fixtures'
|
10
|
+
@image = File.open("#{@dir}/baboon.jpg", 'r')
|
11
|
+
@doc = DocNoAttachment.create
|
12
|
+
@attachment = Griddle::Attachment.for(:image, @doc)
|
13
|
+
end
|
14
|
+
|
15
|
+
should "have a #grid_key" do
|
16
|
+
assert_equal @attachment.grid_key, "#{@doc.class.to_s.tableize}/#{@doc.id}/image/"
|
17
|
+
end
|
18
|
+
|
19
|
+
should "#assign a valid assignment" do
|
20
|
+
@attachment.assign(@image)
|
21
|
+
@attachment.save
|
22
|
+
assert @attachment.file.is_a? GridFS::GridStore
|
23
|
+
assert GridFS::GridStore.exist?(DocNoAttachment.database, @attachment.grid_key)
|
24
|
+
end
|
25
|
+
|
26
|
+
context "with a file" do
|
27
|
+
|
28
|
+
setup do
|
29
|
+
@attachment.assign(@image)
|
30
|
+
end
|
31
|
+
|
32
|
+
should "#destroy_file" do
|
33
|
+
@attachment.destroy_file
|
34
|
+
assert !GridFS::GridStore.exist?(DocNoAttachment.database, @attachment.grid_key)
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
|
42
|
+
end
|
Binary file
|
Binary file
|
Binary file
|
@@ -0,0 +1,72 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
require "models"
|
3
|
+
|
4
|
+
class HasAttachmentTest < Test::Unit::TestCase
|
5
|
+
context "A Doc that has_grid_attachment :image" do
|
6
|
+
setup do
|
7
|
+
@document = Doc.new
|
8
|
+
@dir = File.dirname(__FILE__) + '/fixtures'
|
9
|
+
@image = File.open("#{@dir}/baboon.jpg", 'r')
|
10
|
+
@file_system = MongoMapper.database['fs.files']
|
11
|
+
end
|
12
|
+
|
13
|
+
teardown do
|
14
|
+
#@file_system.drop
|
15
|
+
@image.close
|
16
|
+
end
|
17
|
+
|
18
|
+
should "have :after_save callback" do
|
19
|
+
assert_equal(1, Doc.after_save.collect(&:method).count)
|
20
|
+
end
|
21
|
+
|
22
|
+
context "when assigned a file" do
|
23
|
+
setup do
|
24
|
+
@document.image = @image
|
25
|
+
@document.save!
|
26
|
+
end
|
27
|
+
|
28
|
+
should "should return an Attachment" do
|
29
|
+
assert_equal(Griddle::Attachment, @document.image.class)
|
30
|
+
end
|
31
|
+
|
32
|
+
should "read file from grid store" do
|
33
|
+
assert_equal "image/jpeg", @file_system.find_one(:filename => @document.image.grid_key)['contentType']
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
context "with styles" do
|
39
|
+
|
40
|
+
setup do
|
41
|
+
@document = DocWithStyles.new
|
42
|
+
end
|
43
|
+
|
44
|
+
context "when assigned a file" do
|
45
|
+
|
46
|
+
setup do
|
47
|
+
@document.image = @image
|
48
|
+
@document.save!
|
49
|
+
end
|
50
|
+
|
51
|
+
should "have a styles" do
|
52
|
+
assert_kind_of Hash, @document.image.styles
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
|
59
|
+
context "when multiple instances" do
|
60
|
+
setup do
|
61
|
+
@document2 = Doc.new
|
62
|
+
@image2 = File.open("#{@dir}/fox.jpg", 'r')
|
63
|
+
@document3 = Doc.new
|
64
|
+
@image3 = File.open("#{@dir}/baboon.jpg", 'r')
|
65
|
+
@document2.image = @image2
|
66
|
+
@document3.image = @image3
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
data/test/models.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
class Doc
|
2
|
+
include MongoMapper::Document
|
3
|
+
include Griddle::HasGridAttachment
|
4
|
+
has_grid_attachment :image
|
5
|
+
end
|
6
|
+
|
7
|
+
class DocNoAttachment
|
8
|
+
include MongoMapper::Document
|
9
|
+
end
|
10
|
+
|
11
|
+
class DocWithStyles
|
12
|
+
include MongoMapper::Document
|
13
|
+
include Griddle::HasGridAttachment
|
14
|
+
|
15
|
+
has_grid_attachment :image, :styles => {
|
16
|
+
:thumb => '50x50#'
|
17
|
+
}
|
18
|
+
|
19
|
+
end
|
data/test/style_test.rb
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
require "models"
|
3
|
+
|
4
|
+
class StyleTest < Test::Unit::TestCase
|
5
|
+
|
6
|
+
context "An Attachment with style rules" do
|
7
|
+
|
8
|
+
setup do
|
9
|
+
@dir = File.dirname(__FILE__) + '/fixtures'
|
10
|
+
@image = File.open("#{@dir}/baboon.jpg", 'r')
|
11
|
+
@options = {
|
12
|
+
:styles=>{
|
13
|
+
:thumb => "50x50#"
|
14
|
+
}
|
15
|
+
}
|
16
|
+
|
17
|
+
@doc = DocNoAttachment.create
|
18
|
+
@attachment = Griddle::Attachment.for(:image, @doc, @options)
|
19
|
+
end
|
20
|
+
|
21
|
+
context "and a file" do
|
22
|
+
|
23
|
+
setup do
|
24
|
+
@attachment.assign(@image)
|
25
|
+
end
|
26
|
+
|
27
|
+
should "have a styles hash" do
|
28
|
+
assert_kind_of Hash, @attachment.styles
|
29
|
+
end
|
30
|
+
|
31
|
+
should ":thumb be a type of Griddle::Style" do
|
32
|
+
assert_kind_of Griddle::Style, @attachment.styles[:thumb]
|
33
|
+
end
|
34
|
+
|
35
|
+
should "have an #attachment for a style" do
|
36
|
+
assert_equal @attachment, @attachment.styles[:thumb].attachment
|
37
|
+
end
|
38
|
+
|
39
|
+
should "have a geometry for a style" do
|
40
|
+
assert_equal @options[:styles][:thumb], @attachment.styles[:thumb][:geometry]
|
41
|
+
end
|
42
|
+
|
43
|
+
should "have a #geometry for a style" do
|
44
|
+
assert_equal @options[:styles][:thumb], @attachment.styles[:thumb].geometry
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'mongo_mapper'
|
5
|
+
require 'griddle'
|
6
|
+
require 'test/unit'
|
7
|
+
require 'shoulda'
|
8
|
+
|
9
|
+
TEST_DB = 'griddle-test' unless Object.const_defined?("TEST_DB")
|
10
|
+
|
11
|
+
MongoMapper.database = TEST_DB
|
12
|
+
|
13
|
+
class Test::Unit::TestCase
|
14
|
+
def teardown
|
15
|
+
MongoMapper.database.collections.each do |coll|
|
16
|
+
coll.remove
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
# Make sure that each test case has a teardown
|
21
|
+
# method to clear the db after each test.
|
22
|
+
def inherited(base)
|
23
|
+
base.define_method teardown do
|
24
|
+
super
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/test/upfile_test.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class UpfileTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
context "A File" do
|
6
|
+
setup do
|
7
|
+
@dir = File.dirname(__FILE__) + '/fixtures'
|
8
|
+
@image = File.open("#{@dir}/baboon.jpg", 'r')
|
9
|
+
end
|
10
|
+
|
11
|
+
should "have a #content_type" do
|
12
|
+
assert_equal @image.content_type, "image/jpeg"
|
13
|
+
end
|
14
|
+
|
15
|
+
should "have an #original_filename" do
|
16
|
+
assert_equal @image.original_filename, "baboon.jpg"
|
17
|
+
end
|
18
|
+
|
19
|
+
should "have a #size" do
|
20
|
+
assert_equal @image.size, File.size(@image.path)
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: griddle
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Matt Mongeau
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-03-03 00:00:00 -05:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: shoulda
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: mongo_mapper
|
27
|
+
type: :runtime
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: "0"
|
34
|
+
version:
|
35
|
+
description: GridFS made simple...
|
36
|
+
email: matt@toastyapps.com
|
37
|
+
executables: []
|
38
|
+
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files:
|
42
|
+
- LICENSE
|
43
|
+
- README.rdoc
|
44
|
+
files:
|
45
|
+
- .document
|
46
|
+
- .gitignore
|
47
|
+
- LICENSE
|
48
|
+
- README.rdoc
|
49
|
+
- Rakefile
|
50
|
+
- VERSION
|
51
|
+
- griddle.gemspec
|
52
|
+
- lib/griddle.rb
|
53
|
+
- lib/griddle/attachment.rb
|
54
|
+
- lib/griddle/has_grid_attachment.rb
|
55
|
+
- lib/griddle/style.rb
|
56
|
+
- lib/griddle/upfile.rb
|
57
|
+
- rails/init.rb
|
58
|
+
- test/attachment_test.rb
|
59
|
+
- test/fixtures/baboon.jpg
|
60
|
+
- test/fixtures/fox.jpg
|
61
|
+
- test/fixtures/sample.pdf
|
62
|
+
- test/has_attachment_test.rb
|
63
|
+
- test/models.rb
|
64
|
+
- test/style_test.rb
|
65
|
+
- test/test_helper.rb
|
66
|
+
- test/upfile_test.rb
|
67
|
+
has_rdoc: true
|
68
|
+
homepage: http://github.com/toastyapps/griddle
|
69
|
+
licenses: []
|
70
|
+
|
71
|
+
post_install_message:
|
72
|
+
rdoc_options:
|
73
|
+
- --charset=UTF-8
|
74
|
+
require_paths:
|
75
|
+
- lib
|
76
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: "0"
|
81
|
+
version:
|
82
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: "0"
|
87
|
+
version:
|
88
|
+
requirements: []
|
89
|
+
|
90
|
+
rubyforge_project:
|
91
|
+
rubygems_version: 1.3.5
|
92
|
+
signing_key:
|
93
|
+
specification_version: 3
|
94
|
+
summary: GridFS made simple.
|
95
|
+
test_files:
|
96
|
+
- test/attachment_test.rb
|
97
|
+
- test/has_attachment_test.rb
|
98
|
+
- test/models.rb
|
99
|
+
- test/style_test.rb
|
100
|
+
- test/test_helper.rb
|
101
|
+
- test/upfile_test.rb
|