Created
March 1, 2014 15:26
-
-
Save takkkun/9291430 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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