@ -18,9 +18,7 @@ class MyProjectsOverviewsController < ApplicationController
unloadable
unloadable
before_filter :find_project , :find_user , :find_my_project_overview
before_filter :find_project , :find_user
before_filter :find_page_blocks , :find_project_details
before_filter :find_attachments
before_filter :authorize
before_filter :authorize
before_filter :jump_to_project_menu_item , :only = > :index
before_filter :jump_to_project_menu_item , :only = > :index
@ -47,9 +45,6 @@ class MyProjectsOverviewsController < ApplicationController
# User's page layout configuration
# User's page layout configuration
def page_layout
def page_layout
@block_options = [ ]
BLOCKS . each { | k , v | @block_options << [ l ( " my.blocks. #{ v } " , :default = > [ v , v . to_s . humanize ] ) , k . dasherize ] }
@block_options << [ l ( :label_custom_element ) , :custom_element ]
end
end
def update_custom_element
def update_custom_element
@ -59,13 +54,13 @@ class MyProjectsOverviewsController < ApplicationController
if params [ " attachments " ]
if params [ " attachments " ]
# Attach files and save them
# Attach files and save them
attachments = Attachment . attach_files ( @ overview, params [ " attachments " ] )
attachments = Attachment . attach_files ( overview , params [ " attachments " ] )
unless attachments [ :unsaved ] . blank?
unless attachments [ :unsaved ] . blank?
flash [ :error ] = l ( :warning_attachments_not_saved , attachments [ :unsaved ] . size )
flash [ :error ] = l ( :warning_attachments_not_saved , attachments [ :unsaved ] . size )
end
end
end
end
@ overview. save_custom_element ( block_name , block_title , textile )
overview . save_custom_element ( block_name , block_title , textile )
redirect_to :back
redirect_to :back
end
end
@ -77,23 +72,21 @@ class MyProjectsOverviewsController < ApplicationController
block = params [ :block ] . to_s . underscore
block = params [ :block ] . to_s . underscore
if ( BLOCKS . keys . include? block )
if ( BLOCKS . keys . include? block )
# remove if already present in a group
# remove if already present in a group
%w( top left right hidden ) . each { | f | @ overview. send ( f ) . delete block }
%w( top left right hidden ) . each { | f | overview . send ( f ) . delete block }
# add it hidden
# add it hidden
@overview . hidden . unshift block
overview . hidden . unshift block
@overview . save!
overview . save!
render ( :partial = > " block " ,
render :partial = > " block " ,
:locals = > { :user = > @user ,
:locals = > { :block_name = > block }
:project = > @project ,
:block_name = > block } )
elsif block == " custom_element "
elsif block == " custom_element "
@ overview. hidden . unshift @ overview. new_custom_element
overview . hidden . unshift overview . new_custom_element
@ overview. save!
overview . save!
render ( :partial = > " block_textilizable " ,
render ( :partial = > " block_textilizable " ,
:locals = > { :user = > @ user,
:locals = > { :user = > user ,
:project = > @ project,
:project = > project ,
:block_title = > l ( :label_custom_element ) ,
:block_title = > l ( :label_custom_element ) ,
:block_name = > @ overview. hidden . first . first ,
:block_name = > overview . hidden . first . first ,
:textile = > @ overview. hidden . first . last } )
:textile = > overview . hidden . first . last } )
else
else
render :nothing = > true
render :nothing = > true
end
end
@ -103,8 +96,8 @@ class MyProjectsOverviewsController < ApplicationController
# params[:block] : id of the block to remove
# params[:block] : id of the block to remove
def remove_block
def remove_block
block = param_to_block ( params [ :block ] )
block = param_to_block ( params [ :block ] )
%w( top left right hidden ) . each { | f | @ overview. send ( f ) . delete block }
%w( top left right hidden ) . each { | f | overview . send ( f ) . delete block }
@ overview. save!
overview . save!
render :nothing = > true
render :nothing = > true
end
end
@ -115,14 +108,14 @@ class MyProjectsOverviewsController < ApplicationController
group = params [ :group ]
group = params [ :group ]
if group . is_a? ( String )
if group . is_a? ( String )
group_items = ( params [ " list- #{ group } " ] || [ ] ) . collect { | x | param_to_block ( x ) }
group_items = ( params [ " list- #{ group } " ] || [ ] ) . collect { | x | param_to_block ( x ) }
unless group_items . size < @ overview. send ( group ) . size
unless group_items . size < overview . send ( group ) . size
# We are adding or re-ordering, not removing
# We are adding or re-ordering, not removing
# Remove group blocks if they are presents in other groups
# Remove group blocks if they are presents in other groups
@ overview. update_attributes ( 'top' = > ( @ overview. top - group_items ) ,
overview . update_attributes ( 'top' = > ( overview . top - group_items ) ,
'left' = > ( @ overview. left - group_items ) ,
'left' = > ( overview . left - group_items ) ,
'right' = > ( @ overview. right - group_items ) ,
'right' = > ( overview . right - group_items ) ,
'hidden' = > ( @ overview. hidden - group_items ) )
'hidden' = > ( overview . hidden - group_items ) )
@ overview. update_attribute ( group , group_items )
overview . update_attribute ( group , group_items )
end
end
end
end
render :nothing = > true
render :nothing = > true
@ -131,69 +124,129 @@ class MyProjectsOverviewsController < ApplicationController
def param_to_block ( param )
def param_to_block ( param )
block = param . to_s . underscore
block = param . to_s . underscore
unless ( BLOCKS . keys . include? block )
unless ( BLOCKS . keys . include? block )
block = @ overview. custom_elements . detect { | ary | ary . first == block }
block = overview . custom_elements . detect { | ary | ary . first == block }
end
end
block
block
end
end
def destroy_attachment
def destroy_attachment
if @ user. allowed_to? ( :edit_project , @ project)
if user . allowed_to? ( :edit_project , project )
begin
begin
att = Attachment . find ( params [ :attachment_id ] . to_i )
att = Attachment . find ( params [ :attachment_id ] . to_i )
@ overview. attachments . delete ( att )
overview . attachments . delete ( att )
@ overview. save
overview . save
rescue ActiveRecord :: RecordNotFound
rescue ActiveRecord :: RecordNotFound
end
end
end
end
@ attachments -= [ att ]
attachments -= [ att ]
render :partial = > 'page_layout_attachments'
render :partial = > 'page_layout_attachments'
end
end
private
helper_method :users_by_role ,
:childprojects ,
:recent_news ,
:trackers ,
:open_issues_by_tracker ,
:total_issues_by_tracker ,
:assigned_issues ,
:total_hours ,
:project ,
:user ,
:blocks ,
:block_options ,
:overview ,
:attachments ,
:render_block ,
:object_callback
def find_my_project_overview
def render_block name
@overview = MyProjectsOverview . find ( :first , :conditions = > " project_id = #{ @project . id } " )
# Auto-create missing overviews
@overview || = MyProjectsOverview . create! ( :project_id = > @project . id )
end
end
def find_user
def users_by_role
@user = User . current
@users_by_role || = project . users_by_role
end
end
def find_page_blocks
def childprojects
@blocks = {
@childprojects || = project . children . visible . all
'top' = > @overview . top ,
'left' = > @overview . left ,
'right' = > @overview . right ,
'hidden' = > @overview . hidden
}
end
end
def find_project_details
def recent_news
@users_by_role = @project . users_by_role
@news || = project . news . all :limit = > 5 ,
@subprojects = @project . children . visible . all
@news = @project . news . find ( :all , :limit = > 5 ,
:include = > [ :author , :project ] ,
:include = > [ :author , :project ] ,
:order = > " #{ News . table_name } .created_on DESC " )
:order = > " #{ News . table_name } .created_on DESC "
@trackers = @project . rolled_up_trackers
cond = @project . project_condition ( Setting . display_subprojects_issues? )
end
@open_issues_by_tracker = Issue . visible . count ( :group = > :tracker ,
def trackers
:include = > [ :project , :status , :tracker ] ,
@trackers || = project . rolled_up_trackers
:conditions = > [ " ( #{ cond } ) AND #{ IssueStatus . table_name } .is_closed=? " , false ] )
end
@total_issues_by_tracker = Issue . visible . count ( :group = > :tracker ,
:include = > [ :project , :status , :tracker ] ,
:conditions = > cond )
if User . current . allowed_to? ( :view_time_entries , @project )
def open_issues_by_tracker
@total_hours = TimeEntry . visible . sum ( :hours , :include = > :project , :conditions = > cond ) . to_f
@open_issues_by_tracker || = Issue . visible . count ( :group = > :tracker ,
:include = > [ :project , :status , :tracker ] ,
:conditions = > [ " ( #{ subproject_condition } ) AND #{ IssueStatus . table_name } .is_closed=? " , false ] )
end
def total_issues_by_tracker
@total_issues_by_tracker || = Issue . visible . count ( :group = > :tracker ,
:include = > [ :project , :status , :tracker ] ,
:conditions = > subproject_condition )
end
def assigned_issues
@assigned_issues || = Issue . visible . open . find ( :all ,
:conditions = > { :assigned_to_id = > User . current . id } ,
:limit = > 10 ,
:include = > [ :status , :project , :tracker , :priority ] ,
:order = > " #{ IssuePriority . table_name } .position DESC, #{ Issue . table_name } .updated_on DESC " )
end
def total_hours
if User . current . allowed_to? ( :view_time_entries , project )
@total_hours || = TimeEntry . visible . sum ( :hours , :include = > :project , :conditions = > subproject_condition ) . to_f
end
end
end
end
def find_attachments
def project
@attachments = @overview . attachments || [ ]
@project
end
def user
@user
end
def blocks
@blocks || = {
'top' = > overview . top ,
'left' = > overview . left ,
'right' = > overview . right ,
'hidden' = > overview . hidden
}
end
def block_options
@block_options = [ ]
BLOCKS . each { | k , v | @block_options << [ l ( " my.blocks. #{ v } " , :default = > [ v , v . to_s . humanize ] ) , k . dasherize ] }
@block_options << [ l ( :label_custom_element ) , :custom_element ]
end
def overview
@overview || = MyProjectsOverview . find_or_create_by_project_id ( project . id )
end
def attachments
@attachments = overview . attachments || [ ]
end
private
def subproject_condition
@subproject_condition || = project . project_condition ( Setting . display_subprojects_issues? )
end
def find_user
@user = User . current
end
end
def default_breadcrumb
def default_breadcrumb
@ -203,7 +256,7 @@ class MyProjectsOverviewsController < ApplicationController
def jump_to_project_menu_item
def jump_to_project_menu_item
if params [ :jump ]
if params [ :jump ]
# try to redirect to the requested menu item
# try to redirect to the requested menu item
redirect_to_project_menu_item ( @ project, params [ :jump ] ) && return
redirect_to_project_menu_item ( project , params [ :jump ] ) && return
end
end
end
end
end
end