jm81-attach 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/Gemfile +9 -0
- data/Gemfile.lock +39 -0
- data/LICENSE +20 -0
- data/README.rdoc +17 -0
- data/Rakefile +53 -0
- data/examples/attach/attachment_example.rb +62 -0
- data/examples/attach/samples.rb +12 -0
- data/examples/attach_example.rb +5 -0
- data/examples/example_helper.rb +14 -0
- data/jm81-attach.gemspec +81 -0
- data/lib/attach.rb +12 -0
- data/lib/attach/attachable.rb +126 -0
- data/lib/attach/attachment.rb +51 -0
- data/lib/attach/version.rb +3 -0
- data/lib/extensions/resize_matte.rb +35 -0
- metadata +198 -0
data/.document
ADDED
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
addressable (2.2.0)
|
5
|
+
dm-aggregates (1.0.2)
|
6
|
+
dm-core (~> 1.0.2)
|
7
|
+
dm-core (1.0.2)
|
8
|
+
addressable (~> 2.2)
|
9
|
+
extlib (~> 0.9.15)
|
10
|
+
dm-migrations (1.0.2)
|
11
|
+
dm-core (~> 1.0.2)
|
12
|
+
dm-timestamps (1.0.2)
|
13
|
+
dm-core (~> 1.0.2)
|
14
|
+
dm-types (1.0.2)
|
15
|
+
dm-core (~> 1.0.2)
|
16
|
+
fastercsv (~> 1.5.3)
|
17
|
+
json_pure (~> 1.4)
|
18
|
+
stringex (~> 1.1.0)
|
19
|
+
uuidtools (~> 2.1.1)
|
20
|
+
dm-validations (1.0.2)
|
21
|
+
dm-core (~> 1.0.2)
|
22
|
+
extlib (0.9.15)
|
23
|
+
fastercsv (1.5.3)
|
24
|
+
json_pure (1.4.6)
|
25
|
+
micronaut (0.3.0)
|
26
|
+
stringex (1.1.0)
|
27
|
+
uuidtools (2.1.1)
|
28
|
+
|
29
|
+
PLATFORMS
|
30
|
+
ruby
|
31
|
+
|
32
|
+
DEPENDENCIES
|
33
|
+
dm-aggregates (~> 1.0.2)
|
34
|
+
dm-core (~> 1.0.2)
|
35
|
+
dm-migrations (~> 1.0.2)
|
36
|
+
dm-timestamps (~> 1.0.2)
|
37
|
+
dm-types (~> 1.0.2)
|
38
|
+
dm-validations (~> 1.0.2)
|
39
|
+
micronaut (>= 0.3.0)
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Jared Morgan
|
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
|
+
= attach
|
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 Jared Morgan. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require File.join(File.dirname(__FILE__), 'lib', 'attach', 'version')
|
4
|
+
|
5
|
+
begin
|
6
|
+
require 'jeweler'
|
7
|
+
Jeweler::Tasks.new do |gem|
|
8
|
+
gem.name = "jm81-attach"
|
9
|
+
gem.version = Attach::VERSION.dup
|
10
|
+
gem.summary = %Q{Yet another Attachments library (for DataMapper)}
|
11
|
+
gem.description = %Q{This is a library I've developed for attachments, because I just don't like the others I've tried.}
|
12
|
+
gem.email = "jmorgan@morgancreative.net"
|
13
|
+
gem.homepage = "http://github.com/jm81/attach"
|
14
|
+
gem.authors = ["Jared Morgan"]
|
15
|
+
gem.add_dependency('dm-core', '~> 1.0.2')
|
16
|
+
gem.add_dependency('dm-aggregates', '~> 1.0.2')
|
17
|
+
gem.add_dependency('dm-timestamps', '~> 1.0.2')
|
18
|
+
gem.add_dependency('dm-types', '~> 1.0.2')
|
19
|
+
gem.add_dependency('dm-validations', '~> 1.0.2')
|
20
|
+
gem.add_development_dependency('dm-migrations', '~> 1.0.2')
|
21
|
+
gem.add_development_dependency('micronaut', '>= 0.3.0')
|
22
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
23
|
+
end
|
24
|
+
Jeweler::GemcutterTasks.new
|
25
|
+
rescue LoadError
|
26
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
27
|
+
end
|
28
|
+
|
29
|
+
require 'micronaut/rake_task'
|
30
|
+
Micronaut::RakeTask.new(:examples) do |examples|
|
31
|
+
examples.pattern = 'examples/**/*_example.rb'
|
32
|
+
examples.ruby_opts << '-Ilib -Iexamples'
|
33
|
+
end
|
34
|
+
|
35
|
+
Micronaut::RakeTask.new(:rcov) do |examples|
|
36
|
+
examples.pattern = 'examples/**/*_example.rb'
|
37
|
+
examples.rcov_opts = '-Ilib -Iexamples'
|
38
|
+
examples.rcov = true
|
39
|
+
end
|
40
|
+
|
41
|
+
task :examples => :check_dependencies
|
42
|
+
|
43
|
+
task :default => :examples
|
44
|
+
|
45
|
+
require 'rake/rdoctask'
|
46
|
+
Rake::RDocTask.new do |rdoc|
|
47
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
48
|
+
|
49
|
+
rdoc.rdoc_dir = 'rdoc'
|
50
|
+
rdoc.title = "attach #{version}"
|
51
|
+
rdoc.rdoc_files.include('README*')
|
52
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
53
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'example_helper'
|
2
|
+
require 'attach/samples'
|
3
|
+
|
4
|
+
describe Attach::Attachment do
|
5
|
+
before(:each) do
|
6
|
+
@klass = Attach::Example::Photo
|
7
|
+
|
8
|
+
@attrs = {
|
9
|
+
:filename => 'test.jpg',
|
10
|
+
:content_type => 'image/jpeg',
|
11
|
+
:size => 100,
|
12
|
+
:attachable_id => 1,
|
13
|
+
:attachable_type => 'Attach::Spec::Person',
|
14
|
+
:field_name => 'id_photo'
|
15
|
+
}
|
16
|
+
|
17
|
+
@photo = @klass.new(@attrs)
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'should be valid' do
|
21
|
+
@photo.should be_valid
|
22
|
+
end
|
23
|
+
|
24
|
+
describe '#position' do
|
25
|
+
before(:each) do
|
26
|
+
@klass.all.destroy!
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'should default to 0 if first record' do
|
30
|
+
@klass.all.destroy!
|
31
|
+
@photo.save
|
32
|
+
@photo.position.should == 0
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'should be 1 greater than largest position (yes, unnecessarily high)' do
|
36
|
+
@klass.all.destroy!
|
37
|
+
@klass.create(:position => 3)
|
38
|
+
@klass.create(:position => 6)
|
39
|
+
@photo.save
|
40
|
+
@photo.position.should == 7
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
describe '#created_at and #updated_at' do
|
45
|
+
it 'should record timestamps' do
|
46
|
+
@photo.save
|
47
|
+
@photo.created_at.should be_kind_of(DateTime)
|
48
|
+
@photo.updated_at.should be_kind_of(DateTime)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
describe '#attachment_properties' do
|
53
|
+
it 'should add properties' do
|
54
|
+
klass = Class.new
|
55
|
+
klass.__send__(:include, DataMapper::Resource)
|
56
|
+
klass.__send__(:include, Attach::Attachment)
|
57
|
+
klass.attachment_properties
|
58
|
+
klass.properties.named?(:attachable_type).should be_true
|
59
|
+
klass.properties.named?(:attachable_id).should be_true
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
DataMapper.setup(:default, 'sqlite3::memory:')
|
2
|
+
require 'dm-timestamps' # Only needed to make #created_at and #updated_at auto-update
|
3
|
+
|
4
|
+
module Attach::Example
|
5
|
+
class Photo
|
6
|
+
include DataMapper::Resource
|
7
|
+
include Attach::Attachment
|
8
|
+
attachment_properties
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
DataMapper.auto_migrate!
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'micronaut'
|
3
|
+
require 'attach'
|
4
|
+
require 'dm-validations'
|
5
|
+
require 'dm-migrations'
|
6
|
+
|
7
|
+
def not_in_editor?
|
8
|
+
!(ENV.has_key?('TM_MODE') || ENV.has_key?('EMACS') || ENV.has_key?('VIM'))
|
9
|
+
end
|
10
|
+
|
11
|
+
Micronaut.configure do |c|
|
12
|
+
c.color_enabled = not_in_editor?
|
13
|
+
c.filter_run :focused => true
|
14
|
+
end
|
data/jm81-attach.gemspec
ADDED
@@ -0,0 +1,81 @@
|
|
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{jm81-attach}
|
8
|
+
s.version = "0.2.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Jared Morgan"]
|
12
|
+
s.date = %q{2010-09-09}
|
13
|
+
s.description = %q{This is a library I've developed for attachments, because I just don't like the others I've tried.}
|
14
|
+
s.email = %q{jmorgan@morgancreative.net}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
22
|
+
"Gemfile",
|
23
|
+
"Gemfile.lock",
|
24
|
+
"LICENSE",
|
25
|
+
"README.rdoc",
|
26
|
+
"Rakefile",
|
27
|
+
"examples/attach/attachment_example.rb",
|
28
|
+
"examples/attach/samples.rb",
|
29
|
+
"examples/attach_example.rb",
|
30
|
+
"examples/example_helper.rb",
|
31
|
+
"jm81-attach.gemspec",
|
32
|
+
"lib/attach.rb",
|
33
|
+
"lib/attach/attachable.rb",
|
34
|
+
"lib/attach/attachment.rb",
|
35
|
+
"lib/attach/version.rb",
|
36
|
+
"lib/extensions/resize_matte.rb"
|
37
|
+
]
|
38
|
+
s.homepage = %q{http://github.com/jm81/attach}
|
39
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
40
|
+
s.require_paths = ["lib"]
|
41
|
+
s.rubygems_version = %q{1.3.7}
|
42
|
+
s.summary = %q{Yet another Attachments library (for DataMapper)}
|
43
|
+
s.test_files = [
|
44
|
+
"examples/attach/attachment_example.rb",
|
45
|
+
"examples/attach/samples.rb",
|
46
|
+
"examples/attach_example.rb",
|
47
|
+
"examples/example_helper.rb"
|
48
|
+
]
|
49
|
+
|
50
|
+
if s.respond_to? :specification_version then
|
51
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
52
|
+
s.specification_version = 3
|
53
|
+
|
54
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
55
|
+
s.add_runtime_dependency(%q<dm-core>, ["~> 1.0.2"])
|
56
|
+
s.add_runtime_dependency(%q<dm-aggregates>, ["~> 1.0.2"])
|
57
|
+
s.add_runtime_dependency(%q<dm-timestamps>, ["~> 1.0.2"])
|
58
|
+
s.add_runtime_dependency(%q<dm-types>, ["~> 1.0.2"])
|
59
|
+
s.add_runtime_dependency(%q<dm-validations>, ["~> 1.0.2"])
|
60
|
+
s.add_development_dependency(%q<dm-migrations>, ["~> 1.0.2"])
|
61
|
+
s.add_development_dependency(%q<micronaut>, [">= 0.3.0"])
|
62
|
+
else
|
63
|
+
s.add_dependency(%q<dm-core>, ["~> 1.0.2"])
|
64
|
+
s.add_dependency(%q<dm-aggregates>, ["~> 1.0.2"])
|
65
|
+
s.add_dependency(%q<dm-timestamps>, ["~> 1.0.2"])
|
66
|
+
s.add_dependency(%q<dm-types>, ["~> 1.0.2"])
|
67
|
+
s.add_dependency(%q<dm-validations>, ["~> 1.0.2"])
|
68
|
+
s.add_dependency(%q<dm-migrations>, ["~> 1.0.2"])
|
69
|
+
s.add_dependency(%q<micronaut>, [">= 0.3.0"])
|
70
|
+
end
|
71
|
+
else
|
72
|
+
s.add_dependency(%q<dm-core>, ["~> 1.0.2"])
|
73
|
+
s.add_dependency(%q<dm-aggregates>, ["~> 1.0.2"])
|
74
|
+
s.add_dependency(%q<dm-timestamps>, ["~> 1.0.2"])
|
75
|
+
s.add_dependency(%q<dm-types>, ["~> 1.0.2"])
|
76
|
+
s.add_dependency(%q<dm-validations>, ["~> 1.0.2"])
|
77
|
+
s.add_dependency(%q<dm-migrations>, ["~> 1.0.2"])
|
78
|
+
s.add_dependency(%q<micronaut>, [">= 0.3.0"])
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
data/lib/attach.rb
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
require 'ftools'
|
2
|
+
require 'fileutils'
|
3
|
+
require 'RMagick'
|
4
|
+
|
5
|
+
# Example:
|
6
|
+
#
|
7
|
+
# class Person
|
8
|
+
# include Attach::Attachable
|
9
|
+
|
10
|
+
# attachment :id_photo, Photo, [[:full, 400, 300, '#eee'], [:medium, 260, 195, '#eee'], [:thumb, 96, 72, '#eee']]
|
11
|
+
# attachments :photos, Photo, [[:full, 400, 300, '#eee'], [:medium, 260, 195, '#eee'], [:thumb, 96, 72, '#eee']]
|
12
|
+
# end
|
13
|
+
#
|
14
|
+
# Person.id_photo => Photo
|
15
|
+
# Person.photos => Array/Collection of Photo
|
16
|
+
# Person.photo => first Photo
|
17
|
+
|
18
|
+
module Attach
|
19
|
+
module Attachable
|
20
|
+
include FileUtils
|
21
|
+
|
22
|
+
class << self
|
23
|
+
def included(klass) # Set a few 'magic' properties
|
24
|
+
klass.extend(::Attach::Attachable::ClassMethods)
|
25
|
+
klass.after(:create, :assign_attachment_ids)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
module ClassMethods
|
30
|
+
DEFAULT_SIZES = [[:full, 400, 300, '#eee'], [:thumb, 96, 72, '#eee']]
|
31
|
+
|
32
|
+
# For models with single photo
|
33
|
+
def attachment(fld, klass, sizes = nil)
|
34
|
+
self.__send__(:instance_variable_set, "@#{fld}_sizes".to_sym, sizes)
|
35
|
+
|
36
|
+
# def photo
|
37
|
+
define_method(fld) do
|
38
|
+
return nil unless self.id
|
39
|
+
klass.first(:attachable_type => self.class.name, :attachable_id => self.id, :field_name => fld, :order => [:position.asc])
|
40
|
+
end
|
41
|
+
|
42
|
+
# def photos
|
43
|
+
define_method(fld.to_s.pluralize) do
|
44
|
+
if self.id
|
45
|
+
klass.all(:attachable_type => self.class, :attachable_id => self.id, :field_name => fld, :order => [:position.asc])
|
46
|
+
else
|
47
|
+
[]
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
# def klass.photo_sizes
|
52
|
+
singleton_class = class << self; self; end
|
53
|
+
singleton_class.send(:define_method, "#{fld}_sizes") do
|
54
|
+
instance_variable_get("@#{fld}_sizes".to_sym) || DEFAULT_SIZES
|
55
|
+
end
|
56
|
+
|
57
|
+
# def new_photo=
|
58
|
+
define_method("new_#{fld}=") do |upload|
|
59
|
+
photo = klass.create( :attachable_type => self.class,
|
60
|
+
:attachable_id => self.id,
|
61
|
+
:filename => upload.original_filename.split(/[\\\/]/)[-1],
|
62
|
+
:content_type => upload.content_type,
|
63
|
+
:size => upload.size,
|
64
|
+
:field_name => fld
|
65
|
+
)
|
66
|
+
|
67
|
+
if photo.filename[-4..-1].downcase == ".pdf"
|
68
|
+
photo.update(:filename => photo.filename[0..-4] + "png")
|
69
|
+
end
|
70
|
+
|
71
|
+
@attachments_needing_id ||= Hash.new{ |hash, key| hash[key] = Array.new }
|
72
|
+
@attachments_needing_id[fld.to_s] << photo unless self.id
|
73
|
+
|
74
|
+
self.class.__send__("#{fld}_sizes").each do |style|
|
75
|
+
dir_path = "public/assets/#{photo.id}/#{style[0]}"
|
76
|
+
File.makedirs(dir_path)
|
77
|
+
img = Magick::Image.read(upload.path)
|
78
|
+
img[0].
|
79
|
+
resize_matte(style[1], style[2], style[3]).
|
80
|
+
write(photo.path(style[0]))
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
# def replace_photo=
|
85
|
+
# Deletes any current photos and adds a new photo
|
86
|
+
define_method("replace_#{fld}=") do |upload|
|
87
|
+
return false if upload.blank?
|
88
|
+
self.__send__(fld.to_s.pluralize).each {|del_photo| del_photo.destroy}
|
89
|
+
self.__send__("new_#{fld}=", upload)
|
90
|
+
end
|
91
|
+
|
92
|
+
# def destroy_photo_attachments=
|
93
|
+
#
|
94
|
+
# Delete all attachments of attachment class. Called by before :destroy
|
95
|
+
define_method("destroy_#{fld}_attachments") do
|
96
|
+
self.__send__(fld.to_s.pluralize).destroy!
|
97
|
+
end
|
98
|
+
|
99
|
+
self.before(:destroy, "destroy_#{fld}_attachments".to_sym)
|
100
|
+
end
|
101
|
+
|
102
|
+
def attachments(fld, klass, sizes = nil)
|
103
|
+
attachment(fld.to_s.singularize, klass, sizes)
|
104
|
+
|
105
|
+
define_method("new_#{fld}=") do |uploads|
|
106
|
+
uploads.each { |upload| self.__send__("new_#{fld.to_s.singularize}=", upload) }
|
107
|
+
end
|
108
|
+
|
109
|
+
define_method("delete_#{fld}=") do |del_photos|
|
110
|
+
del_photos.each { |id| self.__send__(fld).get(id).destroy }
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
def assign_attachment_ids
|
116
|
+
return unless @attachments_needing_id
|
117
|
+
@attachments_needing_id.each do |fld, ary|
|
118
|
+
(ary || []).each do |att|
|
119
|
+
att.update(:attachable_id => self.id)
|
120
|
+
end
|
121
|
+
end
|
122
|
+
@attachments_needing_id = nil
|
123
|
+
end
|
124
|
+
|
125
|
+
end
|
126
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module Attach
|
2
|
+
module Attachment
|
3
|
+
class << self
|
4
|
+
def included(klass)
|
5
|
+
klass.extend(ClassMethods)
|
6
|
+
klass.before(:destroy, :delete_files)
|
7
|
+
end
|
8
|
+
end # class << self
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
|
12
|
+
# Setup properties. Already added properties are not overriden.
|
13
|
+
def attachment_properties()
|
14
|
+
klass = self
|
15
|
+
[
|
16
|
+
[:id, DataMapper::Property::Serial],
|
17
|
+
[:filename, String],
|
18
|
+
[:content_type, String],
|
19
|
+
[:size, Integer],
|
20
|
+
[:attachable_id, Integer],
|
21
|
+
[:attachable_type, String],
|
22
|
+
[:field_name, String],
|
23
|
+
# Default position may be rather high, but I don't really care
|
24
|
+
[:position, Integer, {:default => Proc.new { |photo, prop| (klass.max(:position) || -1) + 1 }}],
|
25
|
+
[:created_at, DateTime],
|
26
|
+
[:updated_at, DateTime]
|
27
|
+
].each do |args|
|
28
|
+
unless self.properties.named?(args[0])
|
29
|
+
self.property(*args)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end # module ClassMethods
|
34
|
+
|
35
|
+
def url(format = :full)
|
36
|
+
"/assets/#{self.id}/#{format}/#{self.filename}"
|
37
|
+
end
|
38
|
+
|
39
|
+
def dir
|
40
|
+
"public/assets/#{self.id}"
|
41
|
+
end
|
42
|
+
|
43
|
+
def path(format = :full)
|
44
|
+
"#{self.dir}/#{format}/#{self.filename}"
|
45
|
+
end
|
46
|
+
|
47
|
+
def delete_files
|
48
|
+
FileUtils.rmtree(self.dir)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# Add Magick::Image#resize_matte method, to resize with a colored matte, if
|
2
|
+
# needed (instead of cropping image).
|
3
|
+
class Magick::Image
|
4
|
+
|
5
|
+
# Resizes and adds background to full dimensions
|
6
|
+
# Returns a new image
|
7
|
+
def resize_matte(width, height, bg_color = "white", to_png = false)
|
8
|
+
|
9
|
+
bg_color = "white" if bg_color.nil?
|
10
|
+
|
11
|
+
new_cols = 0
|
12
|
+
new_rows = 0
|
13
|
+
|
14
|
+
# http://www.imagemagick.org/RMagick/doc/image1.html#change_geometry
|
15
|
+
new_image = self.change_geometry(Magick::Geometry.new(width, height)) do |cols, rows, img|
|
16
|
+
new_cols = cols; new_rows = rows;
|
17
|
+
img.resize(cols, rows)
|
18
|
+
end
|
19
|
+
|
20
|
+
bg_color = "none" if to_png
|
21
|
+
|
22
|
+
canvas = Magick::Image.new(width, height) { self.background_color = bg_color }
|
23
|
+
if to_png
|
24
|
+
canvas.format = "PNG"
|
25
|
+
new_image.format = "PNG"
|
26
|
+
end
|
27
|
+
|
28
|
+
# http://www.imagemagick.org/RMagick/doc/image1.html#composite
|
29
|
+
# http://www.imagemagick.org/RMagick/doc/constants.html#CompositeOperator
|
30
|
+
composited = canvas.composite(new_image, Magick::CenterGravity, Magick::OverCompositeOp)
|
31
|
+
composited.format = "PNG" if to_png
|
32
|
+
composited
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
metadata
ADDED
@@ -0,0 +1,198 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jm81-attach
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 23
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 2
|
9
|
+
- 0
|
10
|
+
version: 0.2.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Jared Morgan
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-09-09 00:00:00 -05:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: dm-core
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 19
|
30
|
+
segments:
|
31
|
+
- 1
|
32
|
+
- 0
|
33
|
+
- 2
|
34
|
+
version: 1.0.2
|
35
|
+
type: :runtime
|
36
|
+
version_requirements: *id001
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: dm-aggregates
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
hash: 19
|
46
|
+
segments:
|
47
|
+
- 1
|
48
|
+
- 0
|
49
|
+
- 2
|
50
|
+
version: 1.0.2
|
51
|
+
type: :runtime
|
52
|
+
version_requirements: *id002
|
53
|
+
- !ruby/object:Gem::Dependency
|
54
|
+
name: dm-timestamps
|
55
|
+
prerelease: false
|
56
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
hash: 19
|
62
|
+
segments:
|
63
|
+
- 1
|
64
|
+
- 0
|
65
|
+
- 2
|
66
|
+
version: 1.0.2
|
67
|
+
type: :runtime
|
68
|
+
version_requirements: *id003
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: dm-types
|
71
|
+
prerelease: false
|
72
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ~>
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
hash: 19
|
78
|
+
segments:
|
79
|
+
- 1
|
80
|
+
- 0
|
81
|
+
- 2
|
82
|
+
version: 1.0.2
|
83
|
+
type: :runtime
|
84
|
+
version_requirements: *id004
|
85
|
+
- !ruby/object:Gem::Dependency
|
86
|
+
name: dm-validations
|
87
|
+
prerelease: false
|
88
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ~>
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
hash: 19
|
94
|
+
segments:
|
95
|
+
- 1
|
96
|
+
- 0
|
97
|
+
- 2
|
98
|
+
version: 1.0.2
|
99
|
+
type: :runtime
|
100
|
+
version_requirements: *id005
|
101
|
+
- !ruby/object:Gem::Dependency
|
102
|
+
name: dm-migrations
|
103
|
+
prerelease: false
|
104
|
+
requirement: &id006 !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ~>
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
hash: 19
|
110
|
+
segments:
|
111
|
+
- 1
|
112
|
+
- 0
|
113
|
+
- 2
|
114
|
+
version: 1.0.2
|
115
|
+
type: :development
|
116
|
+
version_requirements: *id006
|
117
|
+
- !ruby/object:Gem::Dependency
|
118
|
+
name: micronaut
|
119
|
+
prerelease: false
|
120
|
+
requirement: &id007 !ruby/object:Gem::Requirement
|
121
|
+
none: false
|
122
|
+
requirements:
|
123
|
+
- - ">="
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
hash: 19
|
126
|
+
segments:
|
127
|
+
- 0
|
128
|
+
- 3
|
129
|
+
- 0
|
130
|
+
version: 0.3.0
|
131
|
+
type: :development
|
132
|
+
version_requirements: *id007
|
133
|
+
description: This is a library I've developed for attachments, because I just don't like the others I've tried.
|
134
|
+
email: jmorgan@morgancreative.net
|
135
|
+
executables: []
|
136
|
+
|
137
|
+
extensions: []
|
138
|
+
|
139
|
+
extra_rdoc_files:
|
140
|
+
- LICENSE
|
141
|
+
- README.rdoc
|
142
|
+
files:
|
143
|
+
- .document
|
144
|
+
- .gitignore
|
145
|
+
- Gemfile
|
146
|
+
- Gemfile.lock
|
147
|
+
- LICENSE
|
148
|
+
- README.rdoc
|
149
|
+
- Rakefile
|
150
|
+
- examples/attach/attachment_example.rb
|
151
|
+
- examples/attach/samples.rb
|
152
|
+
- examples/attach_example.rb
|
153
|
+
- examples/example_helper.rb
|
154
|
+
- jm81-attach.gemspec
|
155
|
+
- lib/attach.rb
|
156
|
+
- lib/attach/attachable.rb
|
157
|
+
- lib/attach/attachment.rb
|
158
|
+
- lib/attach/version.rb
|
159
|
+
- lib/extensions/resize_matte.rb
|
160
|
+
has_rdoc: true
|
161
|
+
homepage: http://github.com/jm81/attach
|
162
|
+
licenses: []
|
163
|
+
|
164
|
+
post_install_message:
|
165
|
+
rdoc_options:
|
166
|
+
- --charset=UTF-8
|
167
|
+
require_paths:
|
168
|
+
- lib
|
169
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
170
|
+
none: false
|
171
|
+
requirements:
|
172
|
+
- - ">="
|
173
|
+
- !ruby/object:Gem::Version
|
174
|
+
hash: 3
|
175
|
+
segments:
|
176
|
+
- 0
|
177
|
+
version: "0"
|
178
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
179
|
+
none: false
|
180
|
+
requirements:
|
181
|
+
- - ">="
|
182
|
+
- !ruby/object:Gem::Version
|
183
|
+
hash: 3
|
184
|
+
segments:
|
185
|
+
- 0
|
186
|
+
version: "0"
|
187
|
+
requirements: []
|
188
|
+
|
189
|
+
rubyforge_project:
|
190
|
+
rubygems_version: 1.3.7
|
191
|
+
signing_key:
|
192
|
+
specification_version: 3
|
193
|
+
summary: Yet another Attachments library (for DataMapper)
|
194
|
+
test_files:
|
195
|
+
- examples/attach/attachment_example.rb
|
196
|
+
- examples/attach/samples.rb
|
197
|
+
- examples/attach_example.rb
|
198
|
+
- examples/example_helper.rb
|