Updates around #12

This commit is contained in:
Daniel Munn 2012-06-22 12:32:16 +01:00
parent 82e53e7fdf
commit a445de4cc8
11 changed files with 52 additions and 38 deletions

View File

@ -28,11 +28,11 @@ class DmsfController < ApplicationController
def show
if @folder.nil?
@subfolders = DmsfFolder.project_root_folders(@project)
@files = DmsfFile.project_root_files(@project)
@subfolders = @project.dmsf_folders #DmsfFolder.project_root_folders(@project)
@files = @project.dmsf_files.visible #DmsfFile.project_root_files(@project)
else
@subfolders = @folder.subfolders
@files = @folder.files
@files = @folder.files.visible
end
@files.sort! do |a,b|
@ -97,7 +97,7 @@ class DmsfController < ApplicationController
unless selected_folders.nil?
if User.current.allowed_to?(:folder_manipulation, @project)
selected_folders.each do |subfolderid|
subfolder = DmsfFolder.find(subfolderid)
subfolder = DmsfFolder.visible.find(subfolderid)
next if subfolder.nil?
if subfolder.project != @project || !subfolder.delete
failed_entries.push(subfolder)
@ -112,7 +112,7 @@ class DmsfController < ApplicationController
unless selected_files.nil?
if User.current.allowed_to?(:file_manipulation, @project)
selected_files.each do |fileid|
file = DmsfFile.find(fileid)
file = DmsfFile.visible.find(fileid)
next if file.nil?
if file.project != @project || !file.delete
failed_entries.push(file)
@ -180,7 +180,7 @@ class DmsfController < ApplicationController
end
def delete
check_project(@delete_folder = DmsfFolder.find(params[:delete_folder_id]))
check_project(@delete_folder = DmsfFolder.visible.find(params[:delete_folder_id]))
if !@delete_folder.nil?
if @delete_folder.delete
flash[:notice] = l(:notice_folder_deleted)
@ -290,7 +290,7 @@ class DmsfController < ApplicationController
end
if selected_files && selected_files.is_a?(Array)
selected_files.each do |selected_file_id|
check_project(file = DmsfFile.find(selected_file_id))
check_project(file = DmsfFile.visible.find(selected_file_id))
zip.add_file(file, (@folder.dmsf_path_str unless @folder.nil?)) unless file.nil?
end
end
@ -316,7 +316,7 @@ class DmsfController < ApplicationController
end
def find_parent
@parent = DmsfFolder.find(params[:parent_id]) if params.keys.include?("parent_id")
@parent = DmsfFolder.visible.find(params[:parent_id]) if params.keys.include?("parent_id")
check_project(@parent)
rescue DmsfAccessError
render_403

View File

@ -40,7 +40,7 @@ class DmsfFilesController < ApplicationController
if params[:download].blank?
@revision = @file.last_revision
else
@revision = DmsfFileRevision.find(params[:download].to_i)
@revision = DmsfFileRevision.visible.find(params[:download].to_i)
if @revision.file != @file
render_403
return
@ -59,7 +59,7 @@ class DmsfFilesController < ApplicationController
# TODO: line bellow is to handle old instalations with errors in data handling
@revision.name = @file.name
@revision_pages = Paginator.new self, @file.revisions.count, params["per_page"] ? params["per_page"].to_i : 25, params["page"]
@revision_pages = Paginator.new self, @file.revisions.visible.count, params["per_page"] ? params["per_page"].to_i : 25, params["page"]
render :layout => !request.xhr?
end

View File

@ -91,7 +91,7 @@ class DmsfUploadController < ApplicationController
name = commited_file["name"];
new_revision = DmsfFileRevision.new
file = DmsfFile.find_file_by_name(@project, @folder, name)
file = DmsfFile.visible.find_file_by_name(@project, @folder, name)
if file.nil?
file = DmsfFile.new
file.project = @project

View File

@ -30,12 +30,12 @@ class DmsfFile < ActiveRecord::Base
belongs_to :folder, :class_name => "DmsfFolder", :foreign_key => "dmsf_folder_id"
has_many :revisions, :class_name => "DmsfFileRevision", :foreign_key => "dmsf_file_id",
:order => "major_version DESC, minor_version DESC, updated_at DESC",
:conditions => { :deleted => false },
:dependent => :destroy
has_many :locks, :class_name => "DmsfFileLock", :foreign_key => "dmsf_file_id",
:order => "updated_at DESC",
:dependent => :destroy
belongs_to :deleted_by_user, :class_name => "User", :foreign_key => "deleted_by_user_id"
scope :visible, lambda {|*args| {:conditions => DmsfFile.visible_condition(args.shift || User.current, *args) }}
validates_presence_of :name
validates_format_of :name, :with => DmsfFolder.invalid_characters,
@ -43,6 +43,10 @@ class DmsfFile < ActiveRecord::Base
validate :validates_name_uniqueness
def self.visible_condition(user, options = {})
"deleted=0"
end
def validates_name_uniqueness
existing_file = DmsfFile.find_file_by_name(self.project, self.folder, self.name)
errors.add(:name, l("activerecord.errors.messages.taken")) unless
@ -80,17 +84,17 @@ class DmsfFile < ActiveRecord::Base
def self.find_file_by_name(project, folder, name)
if folder.nil?
find(:first, :conditions =>
["dmsf_folder_id is NULL and project_id = :project_id and name = :name and deleted = :deleted",
{:project_id => project.id, :name => name, :deleted => false}])
["dmsf_folder_id is NULL and project_id = :project_id and name = :name",
{:project_id => project.id, :name => name}])
else
find(:first, :conditions =>
["dmsf_folder_id = :folder_id and project_id = :project_id and name = :name and deleted = :deleted",
{:project_id => project.id, :folder_id => folder.id, :name => name, :deleted => false}])
["dmsf_folder_id = :folder_id and project_id = :project_id and name = :name",
{:project_id => project.id, :folder_id => folder.id, :name => name}])
end
end
def last_revision
self.revisions.first
self.revisions.visible.first
end
def delete
@ -102,7 +106,7 @@ class DmsfFile < ActiveRecord::Base
CustomValue.find(:all, :conditions => "customized_id = " + self.id.to_s).each do |v|
v.destroy
end
self.revisions.each {|r| r.delete(true)}
self.revisions.visible.each {|r| r.delete(true)}
self.destroy
else
self.deleted = true

View File

@ -24,6 +24,9 @@ class DmsfFileRevision < ActiveRecord::Base
belongs_to :folder, :class_name => "DmsfFolder", :foreign_key => "dmsf_folder_id"
belongs_to :deleted_by_user, :class_name => "User", :foreign_key => "deleted_by_user_id"
belongs_to :project
has_many :access, :class_name => "DmsfFileRevisionAccess", :foreign_key => "dmsf_file_revision_id", :dependent => :destroy
scope :visible, lambda {|*args| {:conditions => DmsfFile.visible_condition(args.shift || User.current, *args) }}
acts_as_customizable
@ -90,13 +93,19 @@ class DmsfFileRevision < ActiveRecord::Base
end
end
# In a static call, we find the first matched record on base object type and
# then run the access_grouped call against it
def self.access_grouped(revision_id)
sql = "select user_id, count(*) as count, min(created_at) as min, max(created_at) as max from #{DmsfFileRevisionAccess.table_name} where dmsf_file_revision_id = ? group by user_id"
self.connection.select_all(self.sanitize_sql_array([sql, revision_id]))
DmsfFileRevision.find(revision_id).first.access_grouped
end
# Get grouped data from dmsf_file_revision_access about file interactions
# - 22-06-2012 - Rather than calling a static, we should use the access
# (has_many) to re-run a query - it makes more sense then executing
# custom SQL into a temporary object
#
def access_grouped
DmsfFileRevision.access_grouped(self.id)
access.select("user_id, count(*) as count, min(created_at) as min, max(created_at) as max").group("user_id")
end
def version

View File

@ -27,7 +27,6 @@ class DmsfFolder < ActiveRecord::Base
has_many :subfolders, :class_name => "DmsfFolder", :foreign_key => "dmsf_folder_id", :order => "title ASC",
:dependent => :destroy
has_many :files, :class_name => "DmsfFile", :foreign_key => "dmsf_folder_id",
:conditions => { :deleted => false },
:dependent => :destroy
belongs_to :user
@ -78,7 +77,7 @@ class DmsfFolder < ActiveRecord::Base
end
def delete
return false if !self.subfolders.empty? || !self.files.empty?
return false if !self.subfolders.empty? || !self.files.visible.empty?
destroy
end
@ -126,7 +125,7 @@ class DmsfFolder < ActiveRecord::Base
end
def deep_file_count
file_count = self.files.length
file_count = self.files.visible.length
self.subfolders.each {|subfolder| file_count += subfolder.deep_file_count}
file_count
end
@ -139,7 +138,7 @@ class DmsfFolder < ActiveRecord::Base
def deep_size
size = 0
self.files.each {|file| size += file.size}
self.files.visible.each {|file| size += file.size}
self.subfolders.each {|subfolder| size += subfolder.deep_size}
size
end
@ -178,7 +177,7 @@ class DmsfFolder < ActiveRecord::Base
return new_folder unless new_folder.save
self.files.each do |f|
self.files.visible.each do |f|
f.copy_to(project, new_folder)
end

View File

@ -11,10 +11,10 @@
<tbody>
<% revision.access_grouped.each do |access| %>
<tr>
<td><%=h(User.find(access["user_id"]))%></td>
<td><%=link_to_user(access.user)%></td>
<td><%=access["count"]%></td>
<td><%=Time.parse(access["min"].to_s).to_s(:db)%></td>
<td><%=Time.parse(access["max"].to_s).to_s(:db)%></td>
<td><%=access.min.to_s(:db)%></td>
<td><%=access.max.to_s(:db)%></td>
</tr>
<% end %>
</tbody>

View File

@ -54,7 +54,7 @@
<h3><%= l(:heading_revisions) %></h3>
<% @file.revisions[@revision_pages.current.offset,@revision_pages.items_per_page].each do |revision| %>
<% @file.revisions.visible[@revision_pages.current.offset,@revision_pages.items_per_page].each do |revision| %>
<div class="box dmsf_detail">
<div style="float:right">
<%= link_to(image_tag("download.png", :plugin => "redmine_dmsf"),
@ -128,7 +128,7 @@
</div>
<% end %>
<p class="pagination"><%= pagination_links_full @revision_pages, @file.revisions.count %></p>
<p class="pagination"><%= pagination_links_full @revision_pages, @file.revisions.visible.count %></p>
<%
sUrl = "jquery.dataTables/en.json"

View File

@ -30,8 +30,10 @@ module RedmineDmsf
unloadable
alias_method_chain :copy, :dmsf
has_many :files, :class_name => "DmsfFile", :foreign_key => "project_id", :conditions => { :deleted => false, :dmsf_folder_id => nil }, :dependent => :destroy
has_many :folders, :class_name => "DmsfFolder", :foreign_key => "project_id", :dependent => :destroy
has_many :dmsf_files, :class_name => "DmsfFile", :foreign_key => "project_id", :conditions => { :deleted => false, :dmsf_folder_id => nil }
#Fix: should only be root folders not, all folders
has_many :dmsf_folders, :class_name => "DmsfFolder", :foreign_key => "project_id", :conditions => {:dmsf_folder_id => nil}, :dependent => :destroy
end
end

View File

@ -39,7 +39,7 @@ module RedmineDmsf
folder.subfolders.map do |p|
@children.push child(p.title, p)
end
folder.files.map do |p|
folder.files.visible.map do |p|
@children.push child(p.name, p)
end
@children
@ -109,12 +109,12 @@ module RedmineDmsf
if f || f.nil? then
# f has a value other than false? - lets use traditional
# DMSF file search by name.
@file = DmsfFile.find_file_by_name(project, f, basename)
@file = DmsfFile.visible.find_file_by_name(project, f, basename)
else
# If folder is false, means it couldn't pick up parent,
# as such its probably fine to bail out, however we'll
# perform a search in this scenario
files = DmsfFile.find(:all, :conditions => ["project_id = :project_id AND name = :file_name AND deleted = :deleted", {:project_id => project.id, :file_name => basename, :deleted => false}], :order => "name ASC")
files = DmsfFile.visible.find(:all, :conditions => ["project_id = :project_id AND name = :file_name", {:project_id => project.id, :file_name => basename}], :order => "name ASC")
files.delete_if {|x| File.dirname('/'+x.dmsf_path_str) != File.dirname(projectless_path)}
if files.length > 0
@file = files[0]

View File

@ -29,10 +29,10 @@ module RedmineDmsf
return @children unless @children.nil?
return [] if project.nil? || project.id.nil?
@children = []
DmsfFolder.project_root_folders(project).map do |p|
project.dmsf_folders.map do |p|
@children.push child(p.title, p)
end
DmsfFile.project_root_files(project).map do |p|
project.dmsf_files.visible.map do |p|
@children.push child(p.name, p)
end
@children