Module: Ramaze::Helper::Attachments

Included in:
CortexReaver::Model::Renderer
Defined in:
lib/cortex_reaver/helper/attachments.rb

Overview

Helps with attachments. Requires CRUD.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/cortex_reaver/helper/attachments.rb', line 7

def self.included(base)
  base.instance_eval do
    # Saves all attachments from a request which match form to model.
    # Returns true if all were successful. Returns an array of
    # attachments which were added.
    def self.add_attachments(model, attachments)
      return unless attachments.respond_to? :each

      attachments.each do |key, file|
        if tempfile = file[:tempfile] and filename = file[:filename] and not filename.blank?
          model.attachment(filename).file = tempfile
        end
      end
      attachments
    end
  end
end

Instance Method Details

#delete_attachment(id, name) ⇒ Object

Deletes an attachment on a model identified by id.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/cortex_reaver/helper/attachments.rb', line 26

def delete_attachment(id, name)
  unless @model = model_class.get(id)
    flash[:error] = "No such #{model_class.to_s.downcase} (#{h id}) exists."
    redirect rs()
  end

  # You need to be able to edit the model before removing attachments!
  for_auth do |u|
    u.can_edit? @model
  end

  unless attachment = @model.attachment(name)
    flash[:error] = "No such attachment (#{h name}) exists."
    redirect @model.url
  end

  begin
    attachment.delete
    flash[:notice] = "Deleted attachment #{h name}."
  rescue => e
    flash[:error] = "Couldn't delete attachment #{h name}: #{h e.message}."
  end

  redirect @model.url
end