Module: DynamicImage::BelongsTo::ClassMethods
- Defined in:
- lib/dynamic_image/belongs_to.rb
Instance Method Summary collapse
-
#belongs_to_image(name, scope = nil) ⇒ void
Declares an association to an image.
Instance Method Details
#belongs_to_image(name, scope = nil) ⇒ void
This method returns an undefined value.
Declares an association to an image. Behaves like belongs_to and takes the same arguments, with one
addition: assigning anything that isn't a Model builds the associated record from it,
treating it as an uploaded file. A file can therefore be posted straight to the parent model.
The image is built, not saved, and is written when the parent is. Add validates_associated if an invalid
upload should invalidate the parent; without it the assignment is silently dropped on save.
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/dynamic_image/belongs_to.rb', line 32 def belongs_to_image(name, scope = nil, **) belongs_to(name, scope, **) define_method "#{name}=" do |new_image| if new_image.present? && !new_image.is_a?(DynamicImage::Model) new_image = send("build_#{name}", file: new_image) end super(new_image) end end |