Module: Redmine::Acts::Attachable::InstanceMethods

Defined in:
lib/plugins/acts_as_attachable/lib/acts_as_attachable.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object


41
42
43
# File 'lib/plugins/acts_as_attachable/lib/acts_as_attachable.rb', line 41

def self.included(base)
  base.extend ClassMethods
end

Instance Method Details

#attach_saved_attachmentsObject


109
110
111
112
113
# File 'lib/plugins/acts_as_attachable/lib/acts_as_attachable.rb', line 109

def attach_saved_attachments
  saved_attachments.each do |attachment|
    self.attachments << attachment
  end
end

#attachments_deletable?(user = User.current) ⇒ Boolean

Returns:

  • (Boolean)

55
56
57
58
# File 'lib/plugins/acts_as_attachable/lib/acts_as_attachable.rb', line 55

def attachments_deletable?(user=User.current)
  (respond_to?(:visible?) ? visible?(user) : true) &&
    user.allowed_to?(self.class.attachable_options[:delete_permission], self.project)
end

#attachments_editable?(user = User.current) ⇒ Boolean

Returns:

  • (Boolean)

50
51
52
53
# File 'lib/plugins/acts_as_attachable/lib/acts_as_attachable.rb', line 50

def attachments_editable?(user=User.current)
  (respond_to?(:visible?) ? visible?(user) : true) &&
    user.allowed_to?(self.class.attachable_options[:edit_permission], self.project)
end

#attachments_visible?(user = User.current) ⇒ Boolean

Returns:

  • (Boolean)

45
46
47
48
# File 'lib/plugins/acts_as_attachable/lib/acts_as_attachable.rb', line 45

def attachments_visible?(user=User.current)
  (respond_to?(:visible?) ? visible?(user) : true) &&
    user.allowed_to?(self.class.attachable_options[:view_permission], self.project)
end

#save_attachments(attachments, author = User.current) ⇒ Object


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/plugins/acts_as_attachable/lib/acts_as_attachable.rb', line 68

def save_attachments(attachments, author=User.current)
  if attachments.is_a?(Hash)
    attachments = attachments.stringify_keys
    attachments = attachments.to_a.sort {|a, b|
      if a.first.to_i > 0 && b.first.to_i > 0
        a.first.to_i <=> b.first.to_i
      elsif a.first.to_i > 0
        1
      elsif b.first.to_i > 0
        -1
      else
        a.first <=> b.first
      end
    }
    attachments = attachments.map(&:last)
  end
  if attachments.is_a?(Array)
    attachments.each do |attachment|
      next unless attachment.is_a?(Hash)
      a = nil
      if file = attachment['file']
        next unless file.size > 0
        a = Attachment.create(:file => file, :author => author)
      elsif token = attachment['token']
        a = Attachment.find_by_token(token)
        next unless a
        a.filename = attachment['filename'] unless attachment['filename'].blank?
        a.content_type = attachment['content_type'] unless attachment['content_type'].blank?
      end
      next unless a
      a.description = attachment['description'].to_s.strip
      if a.new_record?
        unsaved_attachments << a
      else
        saved_attachments << a
      end
    end
  end
  {:files => saved_attachments, :unsaved => unsaved_attachments}
end

#saved_attachmentsObject


60
61
62
# File 'lib/plugins/acts_as_attachable/lib/acts_as_attachable.rb', line 60

def saved_attachments
  @saved_attachments ||= []
end

#unsaved_attachmentsObject


64
65
66
# File 'lib/plugins/acts_as_attachable/lib/acts_as_attachable.rb', line 64

def unsaved_attachments
  @unsaved_attachments ||= []
end