sequel_paperclip 0.0.1
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 +19 -0
- data/README.rdoc +158 -0
- data/Rakefile +46 -0
- data/VERSION +1 -0
- data/lib/sequel_paperclip/attachment.rb +60 -0
- data/lib/sequel_paperclip/interpolations.rb +68 -0
- data/lib/sequel_paperclip/processors/dummy.rb +29 -0
- data/lib/sequel_paperclip/processors/image.rb +131 -0
- data/lib/sequel_paperclip/validators/activemodel.rb +39 -0
- data/lib/sequel_paperclip.rb +133 -0
- data/sequel_paperclip.gemspec +60 -0
- data/spec/sequel_paperclip_spec.rb +7 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +9 -0
- metadata +95 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (c) 2010-2010 Corin Langosch
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to
|
5
|
+
deal in the Software without restriction, including without limitation the
|
6
|
+
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
7
|
+
sell copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
16
|
+
THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
17
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
18
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
19
|
+
|
data/README.rdoc
ADDED
@@ -0,0 +1,158 @@
|
|
1
|
+
=About
|
2
|
+
|
3
|
+
Paperclip is intended as an easy file attachment library for Sequel. It was heavily
|
4
|
+
inspired by {Paperclip for Activerecord}[http://github.com/thoughtbot/paperclip]. The
|
5
|
+
intent behind it was to keep setup as easy as possible and to treat files as
|
6
|
+
much like other attributes as possible. This means they aren't saved to their
|
7
|
+
final locations on disk, nor are they deleted if set to nil, until #save is called.
|
8
|
+
It has support for interpolations and postprocessors, including standart ones like
|
9
|
+
a thumbnail generator by default.
|
10
|
+
|
11
|
+
==Install
|
12
|
+
|
13
|
+
Simply install it as any other gem:
|
14
|
+
|
15
|
+
gem install sequel_paperclip
|
16
|
+
|
17
|
+
Or when using bundler, add it got your Gemfile:
|
18
|
+
|
19
|
+
gem sequel_paperclip
|
20
|
+
|
21
|
+
Some postprocessors depend on external libraries or programs. Please see the processors
|
22
|
+
sections for details.
|
23
|
+
|
24
|
+
==Quick Start
|
25
|
+
|
26
|
+
In your model:
|
27
|
+
|
28
|
+
class User < Sequel::Model
|
29
|
+
plugin :paperclip
|
30
|
+
|
31
|
+
attachment :photo,
|
32
|
+
:url => ":host:/:model:/:id:_:basename:_:style:.:format:",
|
33
|
+
:path => ":path:/:model:/:id:_:basename:_:style:.:format:",
|
34
|
+
:styles => {
|
35
|
+
:small => { :geometry => "60x60#", :format => :jpg },
|
36
|
+
:medium => { :geometry => "250x200>", :format => :jpg },
|
37
|
+
:huge => { :geometry => "600x500>", :format => :jpg },
|
38
|
+
},
|
39
|
+
:processors => [
|
40
|
+
{
|
41
|
+
:type => :image,
|
42
|
+
:convert_arguments => %w(-auto-orient -quality 75 -strip), # optional
|
43
|
+
},
|
44
|
+
]
|
45
|
+
end
|
46
|
+
|
47
|
+
In your migrations:
|
48
|
+
|
49
|
+
class AddPhotoToUser < Sequel::Migration
|
50
|
+
def up
|
51
|
+
alter_table :users do
|
52
|
+
add_column :photo_basename, String
|
53
|
+
add_column :avatar_file_size, Integer # optional
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def down
|
58
|
+
alter_table :users do
|
59
|
+
drop_column :photo_file_name
|
60
|
+
drop_column :avatar_file_size
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
In your edit and new views:
|
66
|
+
|
67
|
+
<% form_for @user, :html => { :multipart => true } do |form| %>
|
68
|
+
<%= form.file_field :photo %>
|
69
|
+
<% end %>
|
70
|
+
|
71
|
+
In your controller:
|
72
|
+
|
73
|
+
def create
|
74
|
+
# nothing need's to be changed
|
75
|
+
end
|
76
|
+
|
77
|
+
def update
|
78
|
+
# nothing need's to be changed
|
79
|
+
end
|
80
|
+
|
81
|
+
def destroy
|
82
|
+
# nothing need's to be changed
|
83
|
+
end
|
84
|
+
|
85
|
+
In your show view:
|
86
|
+
|
87
|
+
<%= image_tag @user.photo_url(:small) %>
|
88
|
+
<%= image_tag @user.photo_url(:medium) %>
|
89
|
+
<%= image_tag @user.photo_url(:huge) %>
|
90
|
+
|
91
|
+
==Interpolations
|
92
|
+
|
93
|
+
To support flexible urls and paths Paperclip supports variable interploations.
|
94
|
+
Strings to be interpolated look like :xxx:. The following predefined interpolations
|
95
|
+
exist:
|
96
|
+
|
97
|
+
id: record id
|
98
|
+
model: underscored, pluralized class name
|
99
|
+
host: "/system"
|
100
|
+
path: Rails public system folder
|
101
|
+
style: style (user.photo_url(:thumb) -> :thumb)
|
102
|
+
format: format defined for the passed style
|
103
|
+
filename: filename
|
104
|
+
filesize: only available if a database column photo_file_size exists
|
105
|
+
basename: basename of the filename
|
106
|
+
extname: extname of the filename
|
107
|
+
rails_root: Rails.root
|
108
|
+
rails_ev: Rails.env
|
109
|
+
|
110
|
+
To add your own interploation, put the following code in some initializer. If
|
111
|
+
using rails a good location is initializers/paperclip.rb:
|
112
|
+
|
113
|
+
Sequel::Plugins::Paperclip::Interpolations.set(:host) do |attachment, model, style|
|
114
|
+
Rails.env.production? ? "http://some.fancy.mirror.com" : "/system"
|
115
|
+
end
|
116
|
+
|
117
|
+
This would add/ replace the :host: interpolation.
|
118
|
+
|
119
|
+
==Processors
|
120
|
+
|
121
|
+
Processors are run before the attachment is saved. Multiple processors can be
|
122
|
+
specified, and they will be invoked in the order they are defined in the
|
123
|
+
:processors array.
|
124
|
+
|
125
|
+
The following processors are included by default:
|
126
|
+
|
127
|
+
===image
|
128
|
+
Adds the ability to generate thumbnails. Requires imagemagick to installed and the
|
129
|
+
convert and identify commands being in the path and executable. Expects a geometry
|
130
|
+
definition for each defined style. If a format is defined for a style, the image
|
131
|
+
is converted to that format.
|
132
|
+
|
133
|
+
===dummy
|
134
|
+
This processor is only used internally and should never be used.
|
135
|
+
|
136
|
+
==Storage
|
137
|
+
|
138
|
+
Attachments are stored as files in the file system. To specify the location,
|
139
|
+
please have a look at the example above and interpolations.
|
140
|
+
|
141
|
+
==Todo
|
142
|
+
|
143
|
+
* Source documentation (rdoc)
|
144
|
+
* Tests
|
145
|
+
|
146
|
+
==Contributing
|
147
|
+
|
148
|
+
If you'd like to contribute a feature or bugfix: Thanks! To make sure your
|
149
|
+
fix/feature has a high chance of being included, please read the following
|
150
|
+
guidelines:
|
151
|
+
|
152
|
+
1. Fork the project.
|
153
|
+
2. Make your feature addition or bug fix.
|
154
|
+
3. Add tests for it. This is important so we don’t break anything in a future version unintentionally.
|
155
|
+
4. Commit, do not mess with rakefile, version, or history. (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)
|
156
|
+
5. Send me a pull request. Bonus points for topic branches.
|
157
|
+
|
158
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = 'sequel_paperclip'
|
8
|
+
gem.authors = ['Corin Langosch']
|
9
|
+
gem.date = Date.today.to_s
|
10
|
+
gem.email = 'info@netskin.com'
|
11
|
+
gem.homepage = 'http://github.com/gucki/sequel_paperclip'
|
12
|
+
gem.summary = 'Easy file attachment management for Sequel'
|
13
|
+
gem.description = 'Sequel plugin which provides Paperclip (attachments, thumbnail resizing, etc.) functionality for model.'
|
14
|
+
gem.add_development_dependency "rspec", ">= 1.2.9"
|
15
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
16
|
+
end
|
17
|
+
Jeweler::GemcutterTasks.new
|
18
|
+
rescue LoadError
|
19
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
20
|
+
end
|
21
|
+
|
22
|
+
require 'spec/rake/spectask'
|
23
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
24
|
+
spec.libs << 'lib' << 'spec'
|
25
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
26
|
+
end
|
27
|
+
|
28
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
29
|
+
spec.libs << 'lib' << 'spec'
|
30
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
31
|
+
spec.rcov = true
|
32
|
+
end
|
33
|
+
|
34
|
+
task :spec => :check_dependencies
|
35
|
+
|
36
|
+
task :default => :spec
|
37
|
+
|
38
|
+
require 'rake/rdoctask'
|
39
|
+
Rake::RDocTask.new do |rdoc|
|
40
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
41
|
+
|
42
|
+
rdoc.rdoc_dir = 'rdoc'
|
43
|
+
rdoc.title = "sequel_paperclip #{version}"
|
44
|
+
rdoc.rdoc_files.include('README*')
|
45
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
46
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
@@ -0,0 +1,60 @@
|
|
1
|
+
module Sequel
|
2
|
+
module Plugins
|
3
|
+
module Paperclip
|
4
|
+
class Attachment
|
5
|
+
attr_reader :name
|
6
|
+
attr_reader :options
|
7
|
+
attr_accessor :processors
|
8
|
+
|
9
|
+
def initialize(name, options = {})
|
10
|
+
unless options[:styles]
|
11
|
+
options[:styles] = {
|
12
|
+
:original => {}
|
13
|
+
}
|
14
|
+
end
|
15
|
+
|
16
|
+
unless options[:processors]
|
17
|
+
options[:processors] = [
|
18
|
+
{
|
19
|
+
:type => :dummy,
|
20
|
+
}
|
21
|
+
]
|
22
|
+
end
|
23
|
+
|
24
|
+
@name = name
|
25
|
+
@options = options
|
26
|
+
self.processors = []
|
27
|
+
options[:processors].each do |processor|
|
28
|
+
self.processors << "Sequel::Plugins::Paperclip::Processors::#{processor[:type].to_s.capitalize}".constantize.new(self, processor)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def process(model, src_path)
|
33
|
+
files_to_store = {}
|
34
|
+
processors.each do |processor|
|
35
|
+
processor.pre_runs(model, src_path)
|
36
|
+
options[:styles].each_pair do |style, style_options|
|
37
|
+
files_to_store[style] ||= Tempfile.new("paperclip")
|
38
|
+
puts "processing #{name} for style #{style} with processor #{processor.name}"
|
39
|
+
processor.run(style, style_options, files_to_store[style])
|
40
|
+
end
|
41
|
+
processor.post_runs
|
42
|
+
end
|
43
|
+
files_to_store
|
44
|
+
end
|
45
|
+
|
46
|
+
def exists?(model)
|
47
|
+
!!model.send("#{name}_basename")
|
48
|
+
end
|
49
|
+
|
50
|
+
def path(model, style)
|
51
|
+
Interpolations.interpolate(options[:path], self, model, style)
|
52
|
+
end
|
53
|
+
|
54
|
+
def url(model, style)
|
55
|
+
Interpolations.interpolate(options[:url], self, model, style)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
module Sequel
|
2
|
+
module Plugins
|
3
|
+
module Paperclip
|
4
|
+
class Interpolations
|
5
|
+
def self.set(name, &block)
|
6
|
+
(class << self; self; end).instance_eval do
|
7
|
+
define_method(name, &block)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.interpolate(string, attachment, model, style)
|
12
|
+
string.gsub(/:\w+:/i) do |tag|
|
13
|
+
send(tag[1..-2], attachment, model, style)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.id(attachment, model, style)
|
18
|
+
model.id
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.model(attachment, model, style)
|
22
|
+
model.class.to_s.underscore.pluralize
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.host(attachment, model, style)
|
26
|
+
"/system"
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.path(attachment, model, style)
|
30
|
+
"#{Rails.root}/public/system"
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.style(attachment, model, style)
|
34
|
+
style
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.format(attachment, model, style)
|
38
|
+
attachment.options[:styles][style][:format]
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.filename(attachment, model, style)
|
42
|
+
model.send("#{attachment.name}_filename")
|
43
|
+
end
|
44
|
+
|
45
|
+
def self.filesize(attachment, model, style)
|
46
|
+
model.send("#{attachment.name}_filesize")
|
47
|
+
end
|
48
|
+
|
49
|
+
def self.basename(attachment, model, style)
|
50
|
+
model.send("#{attachment.name}_basename")
|
51
|
+
end
|
52
|
+
|
53
|
+
def self.extname(attachment, model, style)
|
54
|
+
File.extname(filename(attachment, model, style))
|
55
|
+
end
|
56
|
+
|
57
|
+
def self.rails_root(attachment, model, style)
|
58
|
+
Rails.root
|
59
|
+
end
|
60
|
+
|
61
|
+
def self.rails_env(attachment, model, style)
|
62
|
+
Rails.env
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module Sequel
|
2
|
+
module Plugins
|
3
|
+
module Paperclip
|
4
|
+
module Processors
|
5
|
+
class Dummy
|
6
|
+
attr_reader :name
|
7
|
+
attr_reader :attachment
|
8
|
+
attr_reader :options
|
9
|
+
|
10
|
+
def initialize(attachment, options)
|
11
|
+
@name = self.class.name
|
12
|
+
end
|
13
|
+
|
14
|
+
def pre_runs(model, src_path)
|
15
|
+
@src_path = src_path
|
16
|
+
end
|
17
|
+
|
18
|
+
def run(style, style_options, dst_file)
|
19
|
+
FileUtils.cp(@src_path, dst_file.path)
|
20
|
+
end
|
21
|
+
|
22
|
+
def post_runs
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
@@ -0,0 +1,131 @@
|
|
1
|
+
module Sequel
|
2
|
+
module Plugins
|
3
|
+
module Paperclip
|
4
|
+
module Processors
|
5
|
+
class Image
|
6
|
+
attr_reader :name
|
7
|
+
attr_reader :attachment
|
8
|
+
attr_reader :options
|
9
|
+
|
10
|
+
def initialize(attachment, options)
|
11
|
+
@name = self.class.name
|
12
|
+
@attachment = attachment
|
13
|
+
@options = options
|
14
|
+
end
|
15
|
+
|
16
|
+
def pre_runs(model, src_path)
|
17
|
+
@model = model
|
18
|
+
@src_path = src_path
|
19
|
+
@src_geometry = Geometry.from_file(@src_path)
|
20
|
+
end
|
21
|
+
|
22
|
+
def run(style, style_options, dst_file)
|
23
|
+
dst_geometry = Geometry.from_s(style_options[:geometry])
|
24
|
+
dst_crop = style_options[:geometry][-1,1] == "#"
|
25
|
+
resize_str, crop_str = @src_geometry.transform(dst_geometry, dst_crop)
|
26
|
+
|
27
|
+
cmd = []
|
28
|
+
cmd << "convert"
|
29
|
+
cmd << "-resize"
|
30
|
+
cmd << "'#{resize_str}'"
|
31
|
+
if crop_str
|
32
|
+
cmd << "-crop"
|
33
|
+
cmd << "'#{crop_str}'"
|
34
|
+
end
|
35
|
+
if options[:convert_arguments]
|
36
|
+
cmd << options[:convert_arguments]
|
37
|
+
end
|
38
|
+
cmd << @src_path
|
39
|
+
cmd << "#{style_options[:format]}:#{dst_file.path}"
|
40
|
+
`#{cmd*" "}`
|
41
|
+
end
|
42
|
+
|
43
|
+
def post_runs
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
class Image
|
48
|
+
class Geometry
|
49
|
+
attr_accessor :height, :width, :modifiers
|
50
|
+
|
51
|
+
def initialize(width = nil, height = nil, modifiers = nil)
|
52
|
+
self.width = width.to_f
|
53
|
+
self.height = height.to_f
|
54
|
+
self.modifiers = modifiers
|
55
|
+
end
|
56
|
+
|
57
|
+
def self.from_file(file)
|
58
|
+
path = file.respond_to?(:path) ? file.path : file
|
59
|
+
geometry = `identify -format %wx%h #{path}`
|
60
|
+
from_s(geometry)
|
61
|
+
end
|
62
|
+
|
63
|
+
def self.from_s(string)
|
64
|
+
match = string.match(/(\d+)x(\d+)(.*)/)
|
65
|
+
if match
|
66
|
+
Geometry.new(match[1], match[2], match[3])
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def to_s
|
71
|
+
"%dx%d"%[self.width, self.height]+self.modifiers
|
72
|
+
end
|
73
|
+
|
74
|
+
def square?
|
75
|
+
height == width
|
76
|
+
end
|
77
|
+
|
78
|
+
def horizontal?
|
79
|
+
height < width
|
80
|
+
end
|
81
|
+
|
82
|
+
def vertical?
|
83
|
+
height > width
|
84
|
+
end
|
85
|
+
|
86
|
+
def aspect
|
87
|
+
width/height
|
88
|
+
end
|
89
|
+
|
90
|
+
def larger
|
91
|
+
[height, width].max
|
92
|
+
end
|
93
|
+
|
94
|
+
def smaller
|
95
|
+
[height, width].min
|
96
|
+
end
|
97
|
+
|
98
|
+
def transform(dst, crop = false)
|
99
|
+
if crop
|
100
|
+
ratio = Geometry.new(dst.width/self.width, dst.height/self.height)
|
101
|
+
scale_geometry, scale = scaling(dst, ratio)
|
102
|
+
crop_geometry = cropping(dst, ratio, scale)
|
103
|
+
else
|
104
|
+
scale_geometry = dst.to_s
|
105
|
+
crop_geometry = nil
|
106
|
+
end
|
107
|
+
[scale_geometry, crop_geometry]
|
108
|
+
end
|
109
|
+
|
110
|
+
def scaling(dst, ratio)
|
111
|
+
if ratio.horizontal? || ratio.square?
|
112
|
+
["%dx"%dst.width, ratio.width]
|
113
|
+
else
|
114
|
+
["x%d"%dst.height, ratio.height]
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
def cropping(dst, ratio, scale)
|
119
|
+
if ratio.horizontal? || ratio.square?
|
120
|
+
"%dx%d+%d+%d"%[dst.width, dst.height, 0, (self.height*scale-dst.height)/2]
|
121
|
+
else
|
122
|
+
"%dx%d+%d+%d"%[dst.width, dst.height, (self.width*scale-dst.width)/2, 0]
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
@@ -0,0 +1,39 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
class PaperclipValidator < ActiveModel::EachValidator
|
3
|
+
def humanized_size(num)
|
4
|
+
for x in ['Byte','KB','MB','GB','TB']
|
5
|
+
return "%d %s"%[num, x] if num < 1024.0
|
6
|
+
num /= 1024.0
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
def validate_each(model, attribute, value)
|
11
|
+
return unless value
|
12
|
+
|
13
|
+
if options[:size]
|
14
|
+
min = options[:size].min
|
15
|
+
max = options[:size].max
|
16
|
+
if value.size < min
|
17
|
+
model.errors.add(attribute, "zu klein (mindestes #{humanized_size(min)})")
|
18
|
+
end
|
19
|
+
if value.size > max
|
20
|
+
model.errors.add(attribute, "zu groß (maximal #{humanized_size(max)})")
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
if options[:geometry]
|
25
|
+
geo1 = Sequel::Plugins::Paperclip::Processors::Image::Geometry.from_s(options[:geometry])
|
26
|
+
geo2 = Sequel::Plugins::Paperclip::Processors::Image::Geometry.from_file(value)
|
27
|
+
if geo2
|
28
|
+
if geo2.width < geo1.width
|
29
|
+
model.errors.add(attribute, "zu klein (weniger als %d Pixel breit)"%[geo1.width])
|
30
|
+
end
|
31
|
+
if geo2.height < geo1.height
|
32
|
+
model.errors.add(attribute, "zu klein (weniger als %d Pixel hoch)"%[geo1.height])
|
33
|
+
end
|
34
|
+
else
|
35
|
+
model.errors.add(attribute, "unbekanntes Bildformat oder Datei beschäftigt")
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,133 @@
|
|
1
|
+
require 'tempfile'
|
2
|
+
require 'sequel_paperclip/interpolations'
|
3
|
+
require 'sequel_paperclip/attachment'
|
4
|
+
require 'sequel_paperclip/processors/dummy'
|
5
|
+
require 'sequel_paperclip/processors/image'
|
6
|
+
require 'sequel_paperclip/validators/activemodel'
|
7
|
+
|
8
|
+
module Sequel
|
9
|
+
module Plugins
|
10
|
+
module Paperclip
|
11
|
+
def self.apply(model, opts={}, &block)
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.configure(model, opts={}, &block)
|
15
|
+
model.class_inheritable_hash :attachments
|
16
|
+
model.attachments = {}
|
17
|
+
end
|
18
|
+
|
19
|
+
module ClassMethods
|
20
|
+
def attachment(name, options)
|
21
|
+
attr_accessor name
|
22
|
+
|
23
|
+
attachment = Attachment.new(name, options)
|
24
|
+
attachments[name] = attachment
|
25
|
+
|
26
|
+
columns = db_schema.keys
|
27
|
+
unless columns.include?(:"#{name}_filename") || columns.include?(:"#{name}_basename")
|
28
|
+
raise ArgumentError, "a column named #{name}_filename or #{name}_basename has to exist"
|
29
|
+
end
|
30
|
+
|
31
|
+
if columns.include?(:"#{name}_filename")
|
32
|
+
if columns.include?(:"#{name}_basename")
|
33
|
+
raise ArgumentError, "it does not make sense to have a column named #{name}_filename and #{name}_basename"
|
34
|
+
end
|
35
|
+
|
36
|
+
define_method("#{name}_basename") do
|
37
|
+
filename = send("#{name}_filename")
|
38
|
+
filename ? File.basename(filename) : nil
|
39
|
+
end
|
40
|
+
|
41
|
+
define_method("#{name}_basename=") do |basename|
|
42
|
+
old_filename = send("#{name}_filename")
|
43
|
+
extname = old_filename ? File.extname(old_filename) : ""
|
44
|
+
send("#{name}_filename=", basename+extname)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
define_method("#{name}?") do
|
49
|
+
attachment.exists?(self)
|
50
|
+
end
|
51
|
+
|
52
|
+
define_method("#{name}_url") do |style|
|
53
|
+
attachment.url(self, style)
|
54
|
+
end
|
55
|
+
|
56
|
+
define_method("#{name}_path") do |style|
|
57
|
+
attachment.path(self, style)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
module InstanceMethods
|
63
|
+
def before_save
|
64
|
+
self.class.attachments.each_value do |attachment|
|
65
|
+
file = send(attachment.name)
|
66
|
+
next unless file
|
67
|
+
|
68
|
+
unless file.is_a?(File) || file.is_a?(Tempfile)
|
69
|
+
raise ArgumentError, "#{attachment.name} is not a File"
|
70
|
+
end
|
71
|
+
|
72
|
+
basename = send("#{attachment.name}_basename")
|
73
|
+
if basename.blank?
|
74
|
+
basename = ActiveSupport::SecureRandom.hex(4).to_s
|
75
|
+
send("#{attachment.name}_basename=", basename)
|
76
|
+
end
|
77
|
+
|
78
|
+
if respond_to?("#{attachment.name}_filename")
|
79
|
+
send("#{attachment.name}_filename=", basename+File.extname(file.original_filename).downcase)
|
80
|
+
end
|
81
|
+
|
82
|
+
if respond_to?("#{attachment.name}_filesize")
|
83
|
+
send("#{attachment.name}_filesize=", file.size)
|
84
|
+
end
|
85
|
+
|
86
|
+
if respond_to?("#{attachment.name}_originalname")
|
87
|
+
send("#{attachment.name}_originalname=", file.original_filename)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
super
|
91
|
+
end
|
92
|
+
|
93
|
+
def after_save
|
94
|
+
self.class.attachments.each_value do |attachment|
|
95
|
+
file = send(attachment.name)
|
96
|
+
next unless file
|
97
|
+
|
98
|
+
files_to_store = attachment.process(self, file.path)
|
99
|
+
attachment.options[:styles].each_key do |style|
|
100
|
+
src_file = files_to_store[style]
|
101
|
+
dst_path = attachment.path(self, style)
|
102
|
+
puts "saving #{dst_path} (#{src_file.size} bytes)"
|
103
|
+
FileUtils.mkdir_p(File.dirname(dst_path))
|
104
|
+
FileUtils.cp(src_file.path, dst_path)
|
105
|
+
src_file.close!
|
106
|
+
end
|
107
|
+
end
|
108
|
+
super
|
109
|
+
end
|
110
|
+
|
111
|
+
def after_destroy
|
112
|
+
self.class.attachments.each_value do |attachment|
|
113
|
+
attachment.options[:styles].each_key do |style|
|
114
|
+
next unless attachment.exists?(self)
|
115
|
+
dst_path = attachment.path(self, style)
|
116
|
+
|
117
|
+
puts "deleting #{dst_path}"
|
118
|
+
begin
|
119
|
+
FileUtils.rm(dst_path)
|
120
|
+
rescue Errno::ENOENT => error
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
124
|
+
super
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
module DatasetMethods
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
@@ -0,0 +1,60 @@
|
|
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{sequel_paperclip}
|
8
|
+
s.version = "0.0.1"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Corin Langosch"]
|
12
|
+
s.date = %q{2010-08-31}
|
13
|
+
s.description = %q{Sequel plugin which provides Paperclip (attachments, thumbnail resizing, etc.) functionality for model.}
|
14
|
+
s.email = %q{info@netskin.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
|
+
"lib/sequel_paperclip.rb",
|
27
|
+
"lib/sequel_paperclip/attachment.rb",
|
28
|
+
"lib/sequel_paperclip/interpolations.rb",
|
29
|
+
"lib/sequel_paperclip/processors/dummy.rb",
|
30
|
+
"lib/sequel_paperclip/processors/image.rb",
|
31
|
+
"lib/sequel_paperclip/validators/activemodel.rb",
|
32
|
+
"sequel_paperclip.gemspec",
|
33
|
+
"spec/sequel_paperclip_spec.rb",
|
34
|
+
"spec/spec.opts",
|
35
|
+
"spec/spec_helper.rb"
|
36
|
+
]
|
37
|
+
s.homepage = %q{http://github.com/gucki/sequel_paperclip}
|
38
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
39
|
+
s.require_paths = ["lib"]
|
40
|
+
s.rubygems_version = %q{1.3.7}
|
41
|
+
s.summary = %q{Easy file attachment management for Sequel}
|
42
|
+
s.test_files = [
|
43
|
+
"spec/spec_helper.rb",
|
44
|
+
"spec/sequel_paperclip_spec.rb"
|
45
|
+
]
|
46
|
+
|
47
|
+
if s.respond_to? :specification_version then
|
48
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
49
|
+
s.specification_version = 3
|
50
|
+
|
51
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
52
|
+
s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
|
53
|
+
else
|
54
|
+
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
55
|
+
end
|
56
|
+
else
|
57
|
+
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
data/spec/spec.opts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sequel_paperclip
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: 0.0.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Corin Langosch
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-08-31 00:00:00 +02:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rspec
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
segments:
|
29
|
+
- 1
|
30
|
+
- 2
|
31
|
+
- 9
|
32
|
+
version: 1.2.9
|
33
|
+
type: :development
|
34
|
+
version_requirements: *id001
|
35
|
+
description: Sequel plugin which provides Paperclip (attachments, thumbnail resizing, etc.) functionality for model.
|
36
|
+
email: info@netskin.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
|
+
- lib/sequel_paperclip.rb
|
52
|
+
- lib/sequel_paperclip/attachment.rb
|
53
|
+
- lib/sequel_paperclip/interpolations.rb
|
54
|
+
- lib/sequel_paperclip/processors/dummy.rb
|
55
|
+
- lib/sequel_paperclip/processors/image.rb
|
56
|
+
- lib/sequel_paperclip/validators/activemodel.rb
|
57
|
+
- sequel_paperclip.gemspec
|
58
|
+
- spec/sequel_paperclip_spec.rb
|
59
|
+
- spec/spec.opts
|
60
|
+
- spec/spec_helper.rb
|
61
|
+
has_rdoc: true
|
62
|
+
homepage: http://github.com/gucki/sequel_paperclip
|
63
|
+
licenses: []
|
64
|
+
|
65
|
+
post_install_message:
|
66
|
+
rdoc_options:
|
67
|
+
- --charset=UTF-8
|
68
|
+
require_paths:
|
69
|
+
- lib
|
70
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
71
|
+
none: false
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
segments:
|
76
|
+
- 0
|
77
|
+
version: "0"
|
78
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
79
|
+
none: false
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
segments:
|
84
|
+
- 0
|
85
|
+
version: "0"
|
86
|
+
requirements: []
|
87
|
+
|
88
|
+
rubyforge_project:
|
89
|
+
rubygems_version: 1.3.7
|
90
|
+
signing_key:
|
91
|
+
specification_version: 3
|
92
|
+
summary: Easy file attachment management for Sequel
|
93
|
+
test_files:
|
94
|
+
- spec/spec_helper.rb
|
95
|
+
- spec/sequel_paperclip_spec.rb
|