Module: Reactor::Persistence::ClassMethods
- Defined in:
- lib/reactor/persistence.rb
Instance Method Summary collapse
- #sanitize_name(old_name) ⇒ Object
-
#upload(data_or_io, extension, attributes = {}) ⇒ Object
Convenience method: it is equivalent to following call chain:.
Instance Method Details
#sanitize_name(old_name) ⇒ Object
444 445 446 447 448 449 450 451 452 453 454 455 |
# File 'lib/reactor/persistence.rb', line 444 def sanitize_name(old_name) if Reactor::Configuration.sanitize_obj_name character_map = { "ä" => "ae", "ö" => "oe", "ü" => "ue", "ß" => "ss", "Ä" => "Ae", "Ö" => "Oe", "Ü" => "Ue" } old_name.gsub(/[^-$a-zA-Z0-9]/) { |char| character_map[char] || "_" } .gsub(/__+/, "_") .gsub(/^_+/, "") .gsub(/_+$/, "") else old_name end end |
#upload(data_or_io, extension, attributes = {}) ⇒ Object
Convenience method: it is equivalent to following call chain:
i = create(attributes)
i.upload(data_or_io, extension)
i.save!
i
Use it like this:
image = Image.upload(File.open('image.jpg'), 'ext', :name => 'image', :parent => '/')
468 469 470 471 472 473 474 475 476 477 478 479 |
# File 'lib/reactor/persistence.rb', line 468 def upload(data_or_io, extension, attributes = {}) # Try to guess the object name from filename, if it's missing if data_or_io.respond_to?(:path) && !attributes.key?(:name) attributes[:name] = sanitize_name(File.basename(data_or_io.path, File.extname(data_or_io.path))) end instance = create!(attributes) # do |instance| instance.upload(data_or_io, extension) instance.save! # end instance end |