Module: Shrine::Plugins::FormAssign::AttacherMethods

Defined in:
lib/shrine/plugins/form_assign.rb

Instance Method Summary collapse

Instance Method Details

#form_assign(fields, result: shrine_class.opts[:form_assign][:result]) ⇒ Object

Helper for setting the attachment from form fields. Returns normalized fields.

attacher = Shrine::Attacher.from_entity(photo, :image)

attacher.form_assign({ image: file, title: "Title" })
#=> { image: '{...}', title: "Title" }

attacher.form_assign({ image: "", image_remote_url: "...", title: "Title" })
#=> { image: '{...}', title: "Title" }

attacher.form_assign({ image: "", title: "Title" })
#=> { title: "Title" }

You can also return the result in form of attributes to be used for database record creation.

attacher.form_assign({ image: file, title: "Title" }, result: :attributes)
#=> { image_data: '{...}', title: "Title" }

36
37
38
39
40
41
42
43
# File 'lib/shrine/plugins/form_assign.rb', line 36

def form_assign(fields, result: shrine_class.opts[:form_assign][:result])
  form   = create_form_object
  fields = form_write(form, fields)

  form_attach(form)

  form_result(fields, result)
end