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

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

Overview

Since:

  • 2.0.0

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#attach_saved_attachmentsObject



114
115
116
117
118
# File 'lib/plugins/acts_as_attachable/lib/acts_as_attachable.rb', line 114

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

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

Returns:

  • (Boolean)


57
58
59
60
# File 'lib/plugins/acts_as_attachable/lib/acts_as_attachable.rb', line 57

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)

Since:

  • 3.0.0



52
53
54
55
# File 'lib/plugins/acts_as_attachable/lib/acts_as_attachable.rb', line 52

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)


47
48
49
50
# File 'lib/plugins/acts_as_attachable/lib/acts_as_attachable.rb', line 47

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

#detach_saved_attachmentsObject

Since:

  • 3.4.0



120
121
122
123
124
125
126
# File 'lib/plugins/acts_as_attachable/lib/acts_as_attachable.rb', line 120

def detach_saved_attachments
  saved_attachments.each do |attachment|
    # TODO: use #reload instead, after upgrading to Rails 5
    # (after_rollback is called when running transactional tests in Rails 4)
    attachment.container = nil
  end
end

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



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
108
109
110
111
112
# File 'lib/plugins/acts_as_attachable/lib/acts_as_attachable.rb', line 70

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)
    @failed_attachment_count = 0
    attachments.each do |attachment|
      next unless attachment.is_a?(Hash)
      a = nil
      if file = attachment['file']
        a = Attachment.create(:file => file, :author => author)
      elsif token = attachment['token'].presence
        a = Attachment.find_by_token(token)
        unless a
          @failed_attachment_count += 1
          next
        end
        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



62
63
64
# File 'lib/plugins/acts_as_attachable/lib/acts_as_attachable.rb', line 62

def saved_attachments
  @saved_attachments ||= []
end

#unsaved_attachmentsObject



66
67
68
# File 'lib/plugins/acts_as_attachable/lib/acts_as_attachable.rb', line 66

def unsaved_attachments
  @unsaved_attachments ||= []
end

#warn_about_failed_attachmentsObject

Since:

  • 3.3.0



128
129
130
131
132
# File 'lib/plugins/acts_as_attachable/lib/acts_as_attachable.rb', line 128

def warn_about_failed_attachments
  if @failed_attachment_count && @failed_attachment_count > 0
    errors.add :base, ::I18n.t('warning_attachments_not_saved', count: @failed_attachment_count)
  end
end