document.rb
56 lines
| 2.2 KiB
| text/x-ruby
|
RubyLexer
|
r9454 | # Redmine - project management software | ||
|
r9453 | # Copyright (C) 2006-2012 Jean-Philippe Lang | ||
|
r330 | # | ||
# This program is free software; you can redistribute it and/or | ||||
# modify it under the terms of the GNU General Public License | ||||
# as published by the Free Software Foundation; either version 2 | ||||
# of the License, or (at your option) any later version. | ||||
|
r5674 | # | ||
|
r330 | # This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
# GNU General Public License for more details. | ||||
|
r5674 | # | ||
|
r330 | # You should have received a copy of the GNU General Public License | ||
# along with this program; if not, write to the Free Software | ||||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||||
class Document < ActiveRecord::Base | ||||
|
r9010 | include Redmine::SafeAttributes | ||
|
r330 | belongs_to :project | ||
|
r2677 | belongs_to :category, :class_name => "DocumentCategory", :foreign_key => "category_id" | ||
|
r2114 | acts_as_attachable :delete_permission => :manage_documents | ||
|
r330 | |||
|
r1420 | acts_as_searchable :columns => ['title', "#{table_name}.description"], :include => :project | ||
|
r755 | acts_as_event :title => Proc.new {|o| "#{l(:label_document)}: #{o.title}"}, | ||
|
r10701 | :author => Proc.new {|o| o.attachments.reorder("#{Attachment.table_name}.created_on ASC").first.try(:author) }, | ||
|
r755 | :url => Proc.new {|o| {:controller => 'documents', :action => 'show', :id => o.id}} | ||
|
r1692 | acts_as_activity_provider :find_options => {:include => :project} | ||
|
r5674 | |||
|
r10 | validates_presence_of :project, :title, :category | ||
|
r590 | validates_length_of :title, :maximum => 60 | ||
|
r5674 | |||
|
r9355 | scope :visible, lambda {|*args| { :include => :project, | ||
|
r5204 | :conditions => Project.allowed_to_condition(args.shift || User.current, :view_documents, *args) } } | ||
|
r5674 | |||
|
r9010 | safe_attributes 'category_id', 'title', 'description' | ||
|
r3055 | def visible?(user=User.current) | ||
!user.nil? && user.allowed_to?(:view_documents, project) | ||||
end | ||||
|
r5674 | |||
|
r8168 | def initialize(attributes=nil, *args) | ||
super | ||||
|
r2122 | if new_record? | ||
|
r2677 | self.category ||= DocumentCategory.default | ||
|
r2122 | end | ||
end | ||||
|
r5674 | |||
|
r2981 | def updated_on | ||
unless @updated_on | ||||
|
r8338 | a = attachments.last | ||
|
r2981 | @updated_on = (a && a.created_on) || created_on | ||
end | ||||
@updated_on | ||||
end | ||||
|
r2 | end | ||