mongoid_grid 0.0.8 → 0.0.10

Sign up to get free protection for your applications and to get access to all the features.
data/README.txt CHANGED
@@ -1,61 +1,39 @@
1
- Mongoid::Grid / Rack::Grid
1
+ Mongoid::Grid
2
2
 
3
- Mongoid::Grid is a plugin for mongoid that uses GridFS. Heavily inspired
4
- by grip (http://github.com/jnunemaker/grip)
5
-
6
- Rack::Grid is used to serve a GridFS file from rack. Mostly copied
7
- from http://github.com/skinandbones/rack-gridfs
8
-
9
- Download the source at
10
- http://github.com/dusty/mongoid_grid
3
+ mongoid_grid is a GridFS plugin for Mongoid::Document
11
4
 
5
+ ***NOTE: The rack helper is no longer included in this project. If you
6
+ need it, please switch to rack_grid: https://github.com/dusty/rack_grid
12
7
 
13
8
  Installation
14
9
 
15
- Put the libraries in your project however you want.
16
-
17
- # gem install mongoid_grid
18
-
19
- Or, make your own gem.
20
-
21
- # git clone http://github.com/dusty/mongoid_grid
22
- # cd mongoid_grid
10
+ # git clone https://github.com/dusty/mongoid_grid.git
23
11
  # gem build mongoid_grid.gemspec
24
-
25
- Then require the libraries you want to use.
26
-
27
- require 'mongoid/grid'
28
- require 'rack/grid'
29
-
12
+ # gem install mongoid_grid-0.0.x.gem
30
13
 
31
14
  Usage
32
15
 
16
+ require 'mongoid_grid'
33
17
  class Monkey
34
18
  include Mongoid::Document
35
19
  include Mongoid::Grid
36
20
  field :name
37
21
  attachment :image
38
22
  end
39
-
23
+
40
24
  m = Monkey.create(:name => 'name')
41
-
25
+
42
26
  # To add an attachment
43
27
  m.image = File.open('/tmp/me.jpg')
44
28
  m.save
45
-
29
+
46
30
  # To remove an attachment
47
31
  m.image = nil
48
32
  m.save
49
-
33
+
50
34
  # To get the attachment
51
35
  m.image.read
52
-
53
- # To use Rack::Grid with Sinatra
54
-
55
- configure do
56
- use Rack::Grid, :database => 'my_db'
57
- end
58
-
59
- <img src="<%= m.image_url %>" alt="<%= m.image_name %>" />
60
-
61
-
36
+
37
+ Inspired By
38
+ - http://github.com/jnunemaker/grip
39
+
@@ -1,5 +1,4 @@
1
1
  require 'mime/types'
2
- require 'mongoid'
3
2
  module Mongoid
4
3
  module Grid
5
4
 
@@ -19,7 +18,7 @@ module Mongoid
19
18
  # Callbacks to handle the attachment saving and deleting
20
19
  after_save :create_attachments
21
20
  after_save :delete_attachments
22
- after_destroy :destroy_attachments
21
+ after_destroy :destroy_attachments, :delete_attachments
23
22
 
24
23
  ##
25
24
  # Fields for the attachment.
@@ -114,13 +113,18 @@ module Mongoid
114
113
  # Attachments we need to add after save.
115
114
  def create_attachment(name,file)
116
115
  if file.respond_to?(:read)
117
- filename = file.respond_to?(:original_filename) ?
118
- file.original_filename : File.basename(file.path)
116
+ filename = if file.respond_to?(:original_filename) && file.original_filename
117
+ file.original_filename
118
+ elsif file.respond_to?(:tempfile)
119
+ File.basename(file.tempfile.path)
120
+ else
121
+ File.basename(file.path)
122
+ end
119
123
  type = MIME::Types.type_for(filename).first
120
124
  mime = type ? type.content_type : "application/octet-stream"
121
125
  send("#{name}_id=", BSON::ObjectId.new)
122
126
  send("#{name}_name=", filename)
123
- send("#{name}_size=", File.size(file))
127
+ send("#{name}_size=", File.size(file.respond_to?(:tempfile) ? file.tempfile : file))
124
128
  send("#{name}_type=", mime)
125
129
  create_attachment_queue[name] = file
126
130
  end
@@ -156,10 +160,12 @@ module Mongoid
156
160
  # Attachments we need to remove after save
157
161
  def delete_attachment(name,id)
158
162
  delete_attachment_queue[name] = id if id.is_a?(BSON::ObjectId)
159
- send("#{name}_id=", nil)
160
- send("#{name}_name=", nil)
161
- send("#{name}_size=", nil)
162
- send("#{name}_type=", nil)
163
+ unless destroyed?
164
+ send("#{name}_id=", nil)
165
+ send("#{name}_name=", nil)
166
+ send("#{name}_size=", nil)
167
+ send("#{name}_type=", nil)
168
+ end
163
169
  end
164
170
 
165
171
  ##
metadata CHANGED
@@ -1,12 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid_grid
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 0
8
- - 8
9
- version: 0.0.8
4
+ prerelease:
5
+ version: 0.0.10
10
6
  platform: ruby
11
7
  authors:
12
8
  - Dusty Doris
@@ -14,7 +10,7 @@ autorequire:
14
10
  bindir: bin
15
11
  cert_chain: []
16
12
 
17
- date: 2010-12-16 00:00:00 -05:00
13
+ date: 2011-05-10 00:00:00 -04:00
18
14
  default_executable:
19
15
  dependencies:
20
16
  - !ruby/object:Gem::Dependency
@@ -25,29 +21,10 @@ dependencies:
25
21
  requirements:
26
22
  - - ">="
27
23
  - !ruby/object:Gem::Version
28
- segments:
29
- - 0
30
24
  version: "0"
31
25
  type: :runtime
32
26
  version_requirements: *id001
33
- - !ruby/object:Gem::Dependency
34
- name: mongoid
35
- prerelease: false
36
- requirement: &id002 !ruby/object:Gem::Requirement
37
- none: false
38
- requirements:
39
- - - ">="
40
- - !ruby/object:Gem::Version
41
- segments:
42
- - 2
43
- - 0
44
- - 0
45
- - beta
46
- - 19
47
- version: 2.0.0.beta.19
48
- type: :runtime
49
- version_requirements: *id002
50
- description: Plugin for Mongoid to use GridFS and a Rack helper
27
+ description: Plugin for Mongoid::Document to attach files via GridFS
51
28
  email: github@dusty.name
52
29
  executables: []
53
30
 
@@ -57,12 +34,10 @@ extra_rdoc_files:
57
34
  - README.txt
58
35
  files:
59
36
  - README.txt
60
- - lib/mongoid/grid.rb
61
- - lib/rack/grid.rb
37
+ - lib/mongoid_grid.rb
62
38
  - test/test_mongoid_grid.rb
63
- - test/test_rack_grid.rb
64
39
  has_rdoc: true
65
- homepage: http://github.com/dusty/mongoid_grid
40
+ homepage: http://github.com/dusty/mongo_grid
66
41
  licenses: []
67
42
 
68
43
  post_install_message:
@@ -75,23 +50,19 @@ required_ruby_version: !ruby/object:Gem::Requirement
75
50
  requirements:
76
51
  - - ">="
77
52
  - !ruby/object:Gem::Version
78
- segments:
79
- - 0
80
53
  version: "0"
81
54
  required_rubygems_version: !ruby/object:Gem::Requirement
82
55
  none: false
83
56
  requirements:
84
57
  - - ">="
85
58
  - !ruby/object:Gem::Version
86
- segments:
87
- - 0
88
59
  version: "0"
89
60
  requirements: []
90
61
 
91
62
  rubyforge_project: none
92
- rubygems_version: 1.3.7
63
+ rubygems_version: 1.6.2
93
64
  signing_key:
94
65
  specification_version: 3
95
- summary: Plugin for Mongoid to use GridFS and a Rack helper
66
+ summary: Plugin for Mongoid::Document to attach files via GridFS
96
67
  test_files: []
97
68
 
data/lib/rack/grid.rb DELETED
@@ -1,115 +0,0 @@
1
- require 'timeout'
2
- require 'mongo'
3
-
4
- module Rack
5
- class Grid
6
- class ConnectionError < StandardError ; end
7
-
8
- attr_reader :host, :port, :database, :prefix, :username, :password
9
-
10
- def initialize(app, options = {})
11
- opts = {}
12
- options.each { |k,v| opts[k.to_s] = v }
13
- options = {
14
- 'host' => 'localhost',
15
- 'prefix' => 'grid',
16
- 'port' => Mongo::Connection::DEFAULT_PORT
17
- }.merge(opts)
18
-
19
- @app = app
20
- @host = options['host']
21
- @port = options['port']
22
- @database = options['database']
23
- @prefix = options['prefix']
24
- @username = options['username']
25
- @password = options['password']
26
- @db = options['db']
27
-
28
- @cache_control = options['cache_control']
29
- end
30
-
31
- def db
32
- @db = @db.call() if Proc === @db
33
- connect! unless @db
34
- @db
35
- end
36
-
37
- ##
38
- # Strip the _id out of the path. This allows the user to send something
39
- # like /grid/4ba69fde8c8f369a6e000003/filename.jpg to find the file
40
- # with an id of 4ba69fde8c8f369a6e000003.
41
- def call(env)
42
- @env = env
43
- request = Rack::Request.new(@env)
44
- if request.path_info =~ /^\/#{prefix}\/(\w+).*$/
45
- grid_request($1)
46
- else
47
- @app.call(env)
48
- end
49
- end
50
-
51
- ##
52
- # Get file from GridFS or return a 404
53
- def grid_request(id)
54
- file = Mongo::Grid.new(db).get(BSON::ObjectId.from_string(id))
55
-
56
- etag, last_modified = file.instance_variable_get(:@md5), Time.at( file.upload_date.to_i )
57
- headers = {
58
- 'ETag' => "\"#{etag}\"",
59
- 'Last-Modified' => last_modified.httpdate,
60
- 'Cache-Control' => cache_control_header,
61
- }
62
- if not_modified?( etag, last_modified )
63
- [304, headers, 'Not Modified']
64
- else
65
- [200, headers.update('Content-Type' => file.content_type), [file.read]]
66
- end
67
- rescue Mongo::GridError, BSON::InvalidObjectId
68
- [404, {'Content-Type' => 'text/plain'}, ['File not found.']]
69
- end
70
-
71
- private
72
- def connect!
73
- Timeout::timeout(5) do
74
- @db = Mongo::Connection.new(host,port).db(database)
75
- db.authenticate(username, password) if (username || password)
76
- end
77
- rescue StandardError => e
78
- raise ConnectionError, "Timeout connecting to GridFS (#{e.to_s})"
79
- end
80
-
81
- DEFAULT_CACHE_CONTROL = "max-age=0, private, must-revalidate"
82
- def cache_control_header
83
- if @cache_control.blank?
84
- DEFAULT_CACHE_CONTROL
85
-
86
- elsif @cache_control[:no_cache]
87
- 'no-cache'
88
-
89
- else
90
- extras = @cache_control[:extras]
91
- max_age = @cache_control[:max_age]
92
-
93
- options = []
94
- options << "max-age=#{max_age.to_i}" if max_age
95
- options << (@cache_control[:public] ? 'public' : 'private')
96
- options << 'must-revalidate' if @cache_control[:must_revalidate]
97
- options.concat(extras) if extras
98
-
99
- options.join(', ')
100
- end
101
- end
102
-
103
- def not_modified?( etag, last_modified )
104
- if_none_match = @env['HTTP_IF_NONE_MATCH']
105
- if if_modified_since = @env['HTTP_IF_MODIFIED_SINCE']
106
- if_modified_since = Time.rfc2822( if_modified_since ) rescue nil
107
- end
108
-
109
- not_modified = if_none_match.present? || if_modified_since.present?
110
- not_modified &&= (if_none_match == "\"#{etag}\"") if if_none_match && etag
111
- not_modified &&= (if_modified_since >= last_modified) if if_modified_since && last_modified
112
- not_modified
113
- end
114
- end
115
- end
File without changes