Skip to content

Instantly share code, notes, and snippets.

@mcasimir
Created May 7, 2012 18:18
Show Gist options
  • Save mcasimir/2629431 to your computer and use it in GitHub Desktop.
Save mcasimir/2629431 to your computer and use it in GitHub Desktop.
PaperclipSchema
Gem::Specification.new do |s|
s.name = 'paperclip_schema'
s.summary = 'A Ruby Micro Gem for use Paperclip with ActiveRecordSchema and S3.'
s.version = '0.0.2'
s.platform = Gem::Platform::RUBY
s.files = %w(paperclip_schema.rb)
s.require_path = '.'
s.author = 'Maurizio Casimirri'
s.email = '[email protected]'
s.add_dependency 'paperclip'
s.add_dependency "active_record_schema"
end
# Author:: Maurizio Casimirri (mailto:[email protected])
# Copyright:: Copyright (c) 2012 Maurizio Casimirri
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
module PaperclipSchema
extend ActiveSupport::Concern
module ClassMethods
def has_attached(name, options = {})
options[:url] ||= "/attachments/:class/:id/:attachment/:style/:basename.:extension"
options[:path] ||= ":rails_root/public/attachments/:class/:id/:attachment/:style/:basename.:extension"
if Rails.env.production?
options[:storage] = 's3'
options[:s3_credentials] = Rails.root.join("config", "s3.yml")
else
options[:storage] = 'filesystem'
end
has_attached_file(name, options)
field :"#{name}_file_name"
field :"#{name}_content_type"
field :"#{name}_file_size", :as => :integer
field :"#{name}_updated_at", :as => :datetime
validate do |record|
if record.send(name) && !record.send(name).errors.empty?
file_name = record.send(:"#{name}_file_name")
record.errors.add name, "Paperclip returned errors for file '#{file_name}' - check ImageMagick installation or image source file."
false
end
end
end
end # ~ ClassMethods
end
ActiveRecord::Base.send :include, PaperclipSchema
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment