Skip to content

Instantly share code, notes, and snippets.

@takkkun
Created March 1, 2014 15:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takkkun/9291430 to your computer and use it in GitHub Desktop.
Save takkkun/9291430 to your computer and use it in GitHub Desktop.
module Paperclip
module Model
extend ActiveSupport::Concern
module ClassMethods
def has_attached_file(name)
{
file_name: :original_filename,
content_type: :content_type,
file_size: :size
}.each do |(method_name, attachment_method_name)|
define_method(:"#{name}_#{method_name}") { __send__(name).__send__(attachment_method_name) }
end
end
end
end
class AnyModel
include ActiveModel::Model
include Paperclip::Model
attr_accessor :file
has_attached_file :file
{
presence: {},
content_type: {content_type: %w(image/png image/jpeg image/gif)},
size: {in: 0..5.megabytes}
}.each do |(validator, options)|
validator_class = Paperclip::Validators.const_get("Attachment#{validator.to_s.camelize}Validator")
validates_with validator_class, {attributes: :file}.merge(options)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment