Sha256: d8827c8d061d83603ab85f9f0462df332f5d2223e5ac40d49cb837948e5f32d1

Contents?: true

Size: 1.1 KB

Versions: 1

Compression:

Stored size: 1.1 KB

Contents

# Attach a file
#
# Example:
#
#   Company.new.logo = File.new…
#
#   Given the file "…" was attached as logo to the company above
#
#
# Example:
#
#   class Gallery
#     has_many :images, :as => :owner
#   end
#
#   class Image
#     belongs_to :owner, polymorphic: true
#   end
#
#   # so container = Image.new; container.file = File.new… , container.owner = object
#
#   Given the file "…" was attached as Image/file to the company above
#
#
Given /^the file "([^"]*)" was attached(?: as (?:([^"]*)\/)?([^"]*))? to the ([^"]*) above(?: at "([^"]*)")?$/ do
  |path_to_file, container_name, relation_name, model_name, time_string|

  object = model_name.camelize.constantize.last
  time = Time.parse(time_string) if time_string.present?

  if container_name.present?
    container = container_name.camelize.constantize.new # Image.file = File... owner: gallery
    container.owner = object
    container.created_at = time if time
  else
    container = object # Person.avatar = File...
  end

  container.send("#{relation_name}=", File.new(path_to_file))
  container.updated_at = time if time
  container.save!
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
spreewald-0.8.0 lib/spreewald/file_attachment_steps.rb