Module: MiniPaperclip::ClassMethods
- Defined in:
- lib/mini_paperclip/class_methods.rb
Instance Method Summary collapse
- #has_attached_file(attachment_name, option_config = {}) ⇒ Object
- #validates_attachment(attachment_name, content_type: nil, size: nil, presence: nil, geometry: nil, **opts) ⇒ Object
Instance Method Details
#has_attached_file(attachment_name, option_config = {}) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/mini_paperclip/class_methods.rb', line 5 def has_attached_file(, option_config = {}) define_method() do instance_variable_get("@#{}") or instance_variable_set("@#{}", Attachment.new(self, , option_config)) end define_method("#{}=") do |file| a = public_send() a.assign(file) instance_variable_set("@#{}", a) end after_save do if valid? instance_variable_get("@#{}")&.tap do |a| a.dirty? && a.process_and_store end end end before_destroy do public_send().push_delete_files end after_commit(on: :destroy) do public_send().do_delete_files end validates_with Validators::MediaTypeSpoofValidator, { attributes: , if: -> { instance_variable_get("@#{}")&.dirty? } } end |
#validates_attachment(attachment_name, content_type: nil, size: nil, presence: nil, geometry: nil, **opts) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/mini_paperclip/class_methods.rb', line 34 def (, content_type: nil, size: nil, presence: nil, geometry: nil, **opts) if content_type validates_with Validators::ContentTypeValidator, { attributes: .to_sym, **content_type, **opts, } end if size validates_with Validators::FileSizeValidator, { attributes: .to_sym, **size, **opts, } end if !presence.nil? validates_with Validators::PresenceValidator, { attributes: .to_sym, presence: presence, **opts, } end if geometry validates_with Validators::GeometryValidator, { attributes: .to_sym, **geometry, **opts, } end end |