replaces instance variable in view by methods in controller

Replaces every instance variable in the view layer by a method call to the controller. This is done in order to:

1) better separate concerns.
2) Lazy loading of objects that might be used by a block. As the purpose of the plugin is to allow the user to choose from a list of available blocks, not all blocks will be used all the times. This also means that not all information is required to be loaded all the time. With this commit, we load only what is required for the current block selection by having each block call a method that loads the information it requires.
pull/6827/head
Jens Ulferts 12 years ago
parent 3351fe025a
commit 57f2cf0e44
  1. 181
      app/controllers/my_projects_overviews_controller.rb
  2. 4
      app/views/my_projects_overviews/_page_layout_attachments.rhtml
  3. 20
      app/views/my_projects_overviews/blocks/_issuetracking.rhtml
  4. 10
      app/views/my_projects_overviews/blocks/_members.rhtml
  5. 11
      app/views/my_projects_overviews/blocks/_news.rhtml
  6. 8
      app/views/my_projects_overviews/blocks/_projectdetails.rhtml
  7. 10
      app/views/my_projects_overviews/blocks/_subprojects.html.erb
  8. 32
      app/views/my_projects_overviews/index.rhtml
  9. 8
      app/views/my_projects_overviews/page_layout.rhtml

@ -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
def trackers
@trackers ||= project.rolled_up_trackers
end
@open_issues_by_tracker = Issue.visible.count(:group => :tracker, def open_issues_by_tracker
@open_issues_by_tracker ||= Issue.visible.count(:group => :tracker,
:include => [:project, :status, :tracker], :include => [:project, :status, :tracker],
:conditions => ["(#{cond}) AND #{IssueStatus.table_name}.is_closed=?", false]) :conditions => ["(#{subproject_condition}) AND #{IssueStatus.table_name}.is_closed=?", false])
@total_issues_by_tracker = Issue.visible.count(:group => :tracker, end
def total_issues_by_tracker
@total_issues_by_tracker ||= Issue.visible.count(:group => :tracker,
:include => [:project, :status, :tracker], :include => [:project, :status, :tracker],
:conditions => cond) :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
if User.current.allowed_to?(:view_time_entries, @project) def project
@total_hours = TimeEntry.visible.sum(:hours, :include => :project, :conditions => cond).to_f @project
end 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 end
def find_attachments def overview
@attachments = @overview.attachments || [] @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

@ -1,9 +1,9 @@
<% for attachment in @attachments %> <% for attachment in attachments %>
<p><%= link_to_attachment attachment, :class => 'icon icon-attachment' -%> <p><%= link_to_attachment attachment, :class => 'icon icon-attachment' -%>
<%= h(" - #{attachment.description}") unless attachment.description.blank? %> <%= h(" - #{attachment.description}") unless attachment.description.blank? %>
<span class="size">(<%= number_to_human_size attachment.filesize %>)</span> <span class="size">(<%= number_to_human_size attachment.filesize %>)</span>
<%= link_to_remote image_tag('delete.png'), <%= link_to_remote image_tag('delete.png'),
:url => { :action => 'destroy_attachment', :attachment_id => attachment.id, :id => @project.id }, :url => { :action => 'destroy_attachment', :attachment_id => attachment.id, :id => project.id },
:confirm => l(:text_are_you_sure), :confirm => l(:text_are_you_sure),
:class => 'delete', :class => 'delete',
:title => l(:button_delete), :title => l(:button_delete),

@ -1,24 +1,24 @@
<h2 class="page-layout-only"><%=l(:label_issue_tracking)%></h2> <h2 class="page-layout-only"><%=l(:label_issue_tracking)%></h2>
<h3><%=l(:label_issue_tracking)%></h3> <h3><%=l(:label_issue_tracking)%></h3>
<% if User.current.allowed_to?(:view_issues, @project) %> <% if User.current.allowed_to?(:view_issues, project) %>
<div class="issues overview"> <div class="issues overview">
<ul> <ul>
<% for tracker in @trackers %> <% for tracker in trackers %>
<li><%= link_to h(tracker.name), :controller => 'issues', :action => 'index', :project_id => @project, <li><%= link_to h(tracker.name), :controller => 'issues', :action => 'index', :project_id => project,
:set_filter => 1, :set_filter => 1,
"tracker_id" => tracker.id %>: "tracker_id" => tracker.id %>:
<%= l(:label_x_open_issues_abbr_on_total, :count => @open_issues_by_tracker[tracker].to_i, <%= l(:label_x_open_issues_abbr_on_total, :count => open_issues_by_tracker[tracker].to_i,
:total => @total_issues_by_tracker[tracker].to_i) %> :total => total_issues_by_tracker[tracker].to_i) %>
</li> </li>
<% end %> <% end %>
</ul> </ul>
<p> <p>
<%= link_to l(:label_issue_view_all), :controller => 'issues', :action => 'index', :project_id => @project, :set_filter => 1 %> <%= link_to l(:label_issue_view_all), :controller => 'issues', :action => 'index', :project_id => project, :set_filter => 1 %>
<% if User.current.allowed_to?(:view_calendar, @project, :global => true) %> <% if User.current.allowed_to?(:view_calendar, project, :global => true) %>
| <%= link_to(l(:label_calendar), :controller => 'calendars', :action => 'show', :project_id => @project) %> | <%= link_to(l(:label_calendar), :controller => 'calendars', :action => 'show', :project_id => project) %>
<% end %> <% end %>
<% if User.current.allowed_to?(:view_gantt, @project, :global => true) %> <% if User.current.allowed_to?(:view_gantt, project, :global => true) %>
| <%= link_to(l(:label_gantt), :controller => 'gantts', :action => 'show', :project_id => @project) %> | <%= link_to(l(:label_gantt), :controller => 'gantts', :action => 'show', :project_id => project) %>
<% end %> <% end %>
</p> </p>
</div> </div>

@ -1,9 +1,11 @@
<h2 class="page-layout-only"><%=l(:label_member_plural)%></h2> <h2 class="page-layout-only"><%=l(:label_member_plural)%></h2>
<h3><%=l(:label_member_plural)%></h3> <h3><%=l(:label_member_plural)%></h3>
<% if @users_by_role.any? %> <% if users_by_role.any? %>
<div class="members overview"> <div class="members overview">
<p><% @users_by_role.keys.sort.each do |role| %> <p>
<%=h role %>: <%= @users_by_role[role].sort.collect{|u| link_to_user u}.join(", ") %><br /> <% users_by_role.keys.sort.each do |role| %>
<% end %></p> <%=h role %>: <%= users_by_role[role].sort.collect{ |u| link_to_user u }.join(", ") %><br />
<% end %>
</p>
</div> </div>
<% end %> <% end %>

@ -1,8 +1,13 @@
<h2 class="page-layout-only"><%=l(:label_news)%></h2> <h2 class="page-layout-only"><%=l(:label_news)%></h2>
<h3><%=l(:label_news_latest)%></h3> <h3><%=l(:label_news_latest)%></h3>
<% if @news.any? && authorize_for('news', 'index') %> <% if recent_news.any? && authorize_for('news', 'index') %>
<div class="news overview"> <div class="news overview">
<%= render :partial => 'news/news', :collection => @news %> <%= render :partial => 'news/news', :collection => recent_news %>
<p><%= link_to l(:label_news_view_all), :controller => 'news', :action => 'index', :project_id => @project %></p> <p>
<%= link_to l(:label_news_view_all),
:controller => 'news',
:action => 'index',
:project_id => project %>
</p>
</div> </div>
<% end %> <% end %>

@ -2,12 +2,12 @@
<h3><%=l(:label_project_details)%></h3> <h3><%=l(:label_project_details)%></h3>
<div class="overview project_details"> <div class="overview project_details">
<ul> <ul>
<% unless @project.homepage.blank? %><li><%=l(:field_homepage)%>: <%= auto_link(h(@project.homepage)) %></li><% end %> <% unless project.homepage.blank? %><li><%=l(:field_homepage)%>: <%= auto_link(h(project.homepage)) %></li><% end %>
<% if @subprojects.any? %> <% if childprojects.any? %>
<li><%=l(:label_subproject_plural)%>: <li><%=l(:label_subproject_plural)%>:
<%= @subprojects.collect{|p| link_to(h(p), project_url(p))}.join(", ") %></li> <%= childprojects.collect{|p| link_to(h(p), project_url(p))}.join(", ") %></li>
<% end %> <% end %>
<% @project.visible_custom_field_values.each do |custom_value| %> <% project.visible_custom_field_values.each do |custom_value| %>
<% if !custom_value.value.blank? %> <% if !custom_value.value.blank? %>
<li><%= h(custom_value.custom_field.name) %>: <%=h show_value(custom_value) %></li> <li><%= h(custom_value.custom_field.name) %>: <%=h show_value(custom_value) %></li>
<% end %> <% end %>

@ -1,7 +1,9 @@
<h2 class="page-layout-only"><%=l(:label_subproject_plural)%></h2> <h2 class="page-layout-only"><%=l(:label_subproject_plural)%></h2>
<h3><%=l(:label_subproject_plural)%></h3> <h3><%=l(:label_subproject_plural)%></h3>
<% if @subprojects.any? %> <% if childprojects.any? %>
<div class="subprojects overview"> <div class="subprojects overview">
<p><%= @subprojects.collect{ |p| link_to(h(p), project_url(p)) }.join(", ") %></p> <p>
</div> <%= childprojects.collect{ |p| link_to(h(p), project_url(p)) }.join(", ") %>
</p>
</div>
<% end %> <% end %>

@ -1,14 +1,14 @@
<div class="contextual"> <div class="contextual">
<%= <%=
context_links = [] context_links = []
if User.current.allowed_to?(:add_subprojects, @project) if User.current.allowed_to?(:add_subprojects, project)
context_links << link_to(l(:label_subproject_new), context_links << link_to(l(:label_subproject_new),
{ :controller => 'projects', { :controller => 'projects',
:action => 'new', :action => 'new',
:parent_id => @project }, :parent_id => project },
:class => 'icon icon-add') :class => 'icon icon-add')
end end
if User.current.allowed_to?(:edit_project, @project) if User.current.allowed_to?(:edit_project, project)
context_links << link_to(l(:label_personalize_page), context_links << link_to(l(:label_personalize_page),
{ :action => 'page_layout' }, { :action => 'page_layout' },
:class => 'icon icon-edit') :class => 'icon icon-edit')
@ -21,42 +21,40 @@
<% ['top', 'left', 'right'].each do |position| %> <% ['top', 'left', 'right'].each do |position| %>
<div id="list-<%= position %>" class="splitcontent<%= position %>"> <div id="list-<%= position %>" class="splitcontent<%= position %>">
<% @blocks[position].each do |b| %> <% blocks[position].each do |b| %>
<div class="mypage-box"> <div class="mypage-box">
<div class="wiki"> <div class="wiki">
<% if MyProjectsOverviewsController::BLOCKS.keys.include? b %> <% if MyProjectsOverviewsController::BLOCKS.keys.include? b %>
<%= render :partial => "my_projects_overviews/blocks/#{b}", <%= render :partial => "my_projects_overviews/blocks/#{b}" %>
:locals => { :project => @project,
:user => @user,
:news => @news } %>
<% elsif b.respond_to? :to_ary %> <% elsif b.respond_to? :to_ary %>
<%= render :partial => "my_projects_overviews/blocks/textilizable", <%= render :partial => "my_projects_overviews/blocks/textilizable",
:locals => { :project => @project, :locals => { :project => project,
:user => @user, :user => user,
:block_title => b[1], :block_title => b[1],
:textile => b.last } %> :textile => b.last } %>
<% end %> <% end %>
</div> </div>
</div> </div>
<% end if @blocks[position] %> <% end if blocks[position] %>
</div> </div>
<% end %> <% end %>
<%= context_menu :controller => 'issues', :action => 'context_menu' %> <%= context_menu :controller => 'issues', :action => 'context_menu' %>
<% content_for :sidebar do %> <% content_for :sidebar do %>
<% if @total_hours.present? %> <% if total_hours.present? %>
<h3><%= l(:label_spent_time) %></h3> <h3><%= l(:label_spent_time) %></h3>
<p><span class="icon icon-time"><%= l_hours(@total_hours) %></span></p> <p><span class="icon icon-time"><%= l_hours(total_hours) %></span></p>
<p> <p>
<%= link_to(l(:label_details), {:controller => 'timelog', :action => 'index', :project_id => @project}) %> | <%= link_to(l(:label_details), {:controller => 'timelog', :action => 'index', :project_id => project}) %> |
<%= link_to(l(:label_report), {:controller => 'time_entry_reports', :action => 'report', :project_id => @project}) %>
<%= link_to(l(:label_report), {:controller => 'time_entry_reports', :action => 'report', :project_id => project}) %>
<% if authorize_for('timelog', 'new') %> <% if authorize_for('timelog', 'new') %>
| <%= link_to l(:button_log_time), {:controller => 'timelog', :action => 'new', :project_id => @project} %> | <%= link_to l(:button_log_time), {:controller => 'timelog', :action => 'new', :project_id => project} %>
<% end %> <% end %>
</p> </p>
<% end %> <% end %>
<%= call_hook(:view_projects_show_sidebar_bottom, :project => @project) %> <%= call_hook(:view_projects_show_sidebar_bottom, :project => project) %>
<% end %> <% end %>
<% content_for :header_tags do %> <% content_for :header_tags do %>

@ -94,7 +94,7 @@ function addBlock() {
<div class="contextual"> <div class="contextual">
<% form_tag({:action => "add_block"}, :id => "block-form") do %> <% form_tag({:action => "add_block"}, :id => "block-form") do %>
<%= select_tag 'block', <%= select_tag 'block',
"<option>--#{t(:button_add)}--</option>" + options_for_select(@block_options), "<option>--#{t(:button_add)}--</option>" + options_for_select(block_options),
:id => "block-select", :id => "block-select",
:onChange => "addBlock();" :onChange => "addBlock();"
%> %>
@ -107,9 +107,9 @@ function addBlock() {
<h4><%=l(:label_visible_elements) %></h4> <h4><%=l(:label_visible_elements) %></h4>
<% (field_list - ['hidden']).each do |f| %> <% (field_list - ['hidden']).each do |f| %>
<div id="list-<%= f %>" class="splitcontent<%= f %> block-receiver"> <div id="list-<%= f %>" class="splitcontent<%= f %> block-receiver">
<% @blocks[f].each do |b| %> <% blocks[f].each do |b| %>
<% if MyProjectsOverviewsController::BLOCKS.keys.include? b %> <% if MyProjectsOverviewsController::BLOCKS.keys.include? b %>
<%= render(:partial => 'block', :locals => {:block_name => b}) %> <%= render(:partial => 'block', :locals => { :block_name => b }) %>
<% elsif b.respond_to? :to_ary %> <% elsif b.respond_to? :to_ary %>
<%= render(:partial => 'block_textilizable', <%= render(:partial => 'block_textilizable',
:locals => {:block_name => b.first, :locals => {:block_name => b.first,
@ -123,7 +123,7 @@ function addBlock() {
<div style="clear: both"></div> <div style="clear: both"></div>
<h4><%=l(:label_hidden_elements) %></h4> <h4><%=l(:label_hidden_elements) %></h4>
<div id="list-hidden" class="block-receiver"> <div id="list-hidden" class="block-receiver">
<% @blocks['hidden'].each do |b| %> <% blocks['hidden'].each do |b| %>
<% if MyProjectsOverviewsController::BLOCKS.keys.include? b %> <% if MyProjectsOverviewsController::BLOCKS.keys.include? b %>
<%= render(:partial => 'block', :locals => {:block_name => b}) %> <%= render(:partial => 'block', :locals => {:block_name => b}) %>
<% elsif b.respond_to? :to_ary %> <% elsif b.respond_to? :to_ary %>

Loading…
Cancel
Save