Removes PE controller and related specs

pull/367/head
Hagen Schink 11 years ago
parent ae39cc163f
commit 79ae3a7c0a
  1. 377
      app/controllers/planning_elements_controller.rb
  2. 434
      spec/controllers/planning_elements_controller_spec.rb

@ -1,377 +0,0 @@
#-- copyright
# OpenProject is a project management system.
#
# Copyright (C) 2012-2013 the OpenProject Team
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 3.
#
# See doc/COPYRIGHT.rdoc for more details.
#++
class PlanningElementsController < ApplicationController
unloadable
helper :timelines, :planning_elements
include ExtendedHTTP
menu_item :planning_elements
menu_item :recycle_bin, :only => [:move_to_trash, :confirm_move_to_trash,
:recycle_bin, :destroy_all, :confirm_destroy_all,
:restore_all, :confirm_restore_all]
before_filter :disable_api
before_filter :find_project_by_project_id,
:authorize,
:assign_planning_elements, :except => [:index, :list]
before_filter :apply_at_timestamp, :only => [:show]
# Attention: find_all_projects_by_project_id needs to mimic all of the above
# before filters !!!
before_filter :find_all_projects_by_project_id, :only => :index
helper :timelines
helper :timelines_journals
accept_key_auth :index, :create, :show, :update, :destroy, :list
def index
optimize_planning_elements_for_less_db_queries
respond_to do |format|
format.html
end
end
def all
respond_to do |format|
format.html { render :action => 'index' }
end
end
def recycle_bin
@planning_elements = @project.planning_elements.deleted
respond_to do |format|
format.html
end
end
def new
@planning_element = @planning_elements.build()
respond_to do |format|
format.html
format.js { render :partial => 'new' }
end
end
def create
@planning_element = @planning_elements.new(permitted_params.planning_element)
successfully_created = @planning_element.save
respond_to do |format|
format.html do
if successfully_created
flash[:notice] = l(:notice_successful_create)
redirect_to project_planning_element_path(@project, @planning_element)
else
flash.now[:error] = l('timelines.planning_element_could_not_be_saved')
render :action => "new"
end
end
end
end
def show
@planning_element = @project.planning_elements.find(params[:id])
respond_to do |format|
format.html
format.js { render :partial => 'show'}
end
end
def edit
@planning_element = @planning_elements.find(params[:id])
respond_to do |format|
format.html
format.js { render :partial => 'edit' }
end
end
def update
@planning_element = @planning_elements.find(params[:id])
@planning_element.attributes = permitted_params.planning_element
successfully_updated = @planning_element.save
respond_to do |format|
format.html do
if successfully_updated
flash[:notice] = l(:notice_successful_update)
redirect_to project_planning_element_path(@project, @planning_element)
else
flash.now[:error] = l('timelines.planning_element_could_not_be_saved')
render :action => "edit"
end
end
end
end
def list
options = {:order => 'id'}
projects = Project.visible.select do |project|
User.current.allowed_to?(:view_planning_elements, project)
end
if params[:ids]
ids = params[:ids].split(/,/).map(&:strip).select { |s| s =~ /^\d*$/ }.map(&:to_i).sort
project_ids = projects.map(&:id).sort
options[:conditions] = ["id IN (?) AND project_id IN (?)", ids, project_ids]
end
@planning_elements = PlanningElement.all(options)
respond_to do |format|
format.html { render :action => :index }
end
end
def confirm_move_to_trash
@planning_element = @planning_elements.find(params[:id])
respond_to do |format|
format.html
end
end
def confirm_destroy
@planning_element = @project.planning_elements.find(params[:id])
respond_to do |format|
format.html
end
end
def destroy
@planning_element = @project.planning_elements.find(params[:id])
@planning_element.destroy
respond_to do |format|
format.html do
flash[:notice] = l(:notice_successful_delete)
redirect_to project_planning_elements_path(@project)
end
end
end
def confirm_destroy_all
@planning_elements = @project.planning_elements.deleted
respond_to do |format|
format.html
end
end
def destroy_all
@project.planning_elements.deleted.each do |element|
element.destroy
end
flash[:notice] = l("timelines.notice_successful_deleted_all_elements")
redirect_to(recycle_bin_project_planning_elements_path(@project))
end
def move_to_trash
@planning_element = @planning_elements.find(params[:id])
@planning_element.trash
respond_to do |format|
format.html do
flash[:notice] = l("timelines.notice_successful_moved_to_trash")
redirect_to project_planning_elements_path(@project)
end
end
end
def restore
@planning_element = @project.planning_elements.find(params[:id])
successfully_restored = @planning_element.restore!
respond_to do |format|
format.html do
if successfully_restored
flash[:notice] = l("timelines.notice_successful_restored")
else
flash.now[:error] = l('timelines.planning_element_could_not_be_restored')
end
redirect_to(recycle_bin_project_planning_elements_path(@project))
end
end
end
def confirm_restore_all
@planning_elements = @project.planning_elements.deleted
respond_to do |format|
format.html
end
end
def restore_all
@project.planning_elements.deleted.each do |element|
element.restore!
end
flash[:notice] = l("timelines.notice_successful_restored_all_elements")
redirect_to(recycle_bin_project_planning_elements_path(@project))
end
protected
# Filters
def find_all_projects_by_project_id
if params[:format] == 'html' or params[:project_id] !~ /,/
find_project_by_project_id unless performed?
authorize unless performed?
assign_planning_elements unless performed?
apply_at_timestamp unless performed?
else
# find_project_by_project_id
ids, identifiers = params[:project_id].split(/,/).map(&:strip).partition { |s| s =~ /^\d*$/ }
ids = ids.map(&:to_i).sort
identifiers = identifiers.sort
@projects = []
@projects |= Project.all(:conditions => {:id => ids}) unless ids.empty?
@projects |= Project.all(:conditions => {:identifier => identifiers}) unless identifiers.empty?
if (@projects.map(&:id) & ids).size != ids.size ||
(@projects.map(&:identifier) & identifiers).size != identifiers.size
# => not all projects could be found
render_404
return
end
# authorize
# Ignoring projects, where user has no view_planning_elements permission.
@projects = @projects.select do |project|
User.current.allowed_to?({:controller => params[:controller],
:action => params[:action]},
project)
end
if @projects.blank?
@planning_elements = []
return
end
# assign_planning_elements and apply_at_timestamp
if params[:at].blank?
@planning_elements = PlanningElement.for_projects(@projects).without_deleted
else
begin
time = Time.at(Integer(params[:at]))
# intentionally avoiding without_deleted scope
@planning_elements = PlanningElement.for_projects(@projects).at_time(time)
rescue ArgumentError
render_errors(:at => 'unknown format')
end
end
end
end
def assign_planning_elements
@planning_elements = @project.planning_elements.without_deleted
end
def apply_at_timestamp
return if params[:at].blank?
time = Time.at(Integer(params[:at]))
# intentionally rebuilding scope chain to avoid without_deleted scope
@planning_elements = @project.planning_elements.at_time(time)
rescue ArgumentError
render_errors(:at => 'unknown format')
end
# Helpers
helper_method :include_journals?, :include_scenarios?
def include_journals?
params[:include].tap { |i| i.present? && i.include?("journals") }
end
def include_scenarios?
!params[:exclude].tap { |i| i.present? && i.include?("scenarios") }
end
def default_breadcrumb
l('timelines.project_menu.planning_elements')
end
# Actual protected methods
def render_errors(errors)
options = {:status => :bad_request, :layout => false}
options.merge!(case params[:format]
when 'xml'; {:xml => errors}
when 'json'; {:json => {'errors' => errors}}
else
raise "Unknown format #{params[:format]} in #render_validation_errors"
end
)
render options
end
def optimize_planning_elements_for_less_db_queries
# abort if @planning_elements is already an array, using .class check since
# .is_a? acts weird on named scopes
return if @planning_elements.class == Array
# triggering full load to avoid separate queries for count or related models
@planning_elements = @planning_elements.all(:include => [:type, :project])
# Replacing association proxies with already loaded instances to avoid
# further db calls.
#
# This assumes, that all planning elements within a project where loaded
# and that parent-child relations may only occur within a project.
#
# It is also dependent on implementation details of ActiveRecord::Base,
# so it might break in later versions of Rails.
#
# See association_instance_get/_set in ActiveRecord::Associations
ids_hash = @planning_elements.inject({}) { |h, pe| h[pe.id] = pe; h }
children_hash = Hash.new { |h,k| h[k] = [] }
parent_refl, children_refl = [:parent, :children].map{|assoc| PlanningElement.reflect_on_association(assoc)}
associations = {
:belongs_to => ActiveRecord::Associations::BelongsToAssociation,
:has_many => ActiveRecord::Associations::HasManyAssociation
}
# 'caching' already loaded parent and children associations
@planning_elements.each do |pe|
children_hash[pe.parent_id] << pe
parent = nil
if ids_hash.has_key? pe.parent_id
parent = associations[parent_refl.macro].new(pe, parent_refl)
parent.target = ids_hash[pe.parent_id]
end
pe.send(:association_instance_set, :parent, parent)
children = associations[children_refl.macro].new(pe, children_refl)
children.target = children_hash[pe.id]
pe.send(:association_instance_set, :children, children)
end
end
end

@ -1,434 +0,0 @@
#-- copyright
# OpenProject is a project management system.
#
# Copyright (C) 2012-2013 the OpenProject Team
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 3.
#
# See doc/COPYRIGHT.rdoc for more details.
#++
require File.expand_path('../../spec_helper', __FILE__)
describe PlanningElementsController do
# ===========================================================
# Helpers
def self.become_admin
let(:current_user) { FactoryGirl.create(:admin) }
end
def self.become_non_member(&block)
let(:current_user) { FactoryGirl.create(:user) }
before do
projects = block ? instance_eval(&block) : [project]
projects.each do |p|
current_user.memberships.select {|m| m.project_id == p.id}.each(&:destroy)
end
end
end
def self.become_member_with_view_planning_element_permissions(&block)
let(:current_user) { FactoryGirl.create(:user) }
before do
role = FactoryGirl.create(:role, :permissions => [:view_planning_elements])
projects = block ? instance_eval(&block) : [project]
projects.each do |p|
member = FactoryGirl.build(:member, :user => current_user, :project => p)
member.roles = [role]
member.save!
end
end
end
def self.become_member_with_edit_planning_element_permissions(&block)
let(:current_user) { FactoryGirl.create(:user) }
before do
role = FactoryGirl.create(:role, :permissions => [:edit_planning_elements])
projects = block ? instance_eval(&block) : [project]
projects.each do |p|
member = FactoryGirl.build(:member, :user => current_user, :project => p)
member.roles = [role]
member.save!
end
end
end
def self.become_member_with_delete_planning_element_permissions(&block)
let(:current_user) { FactoryGirl.create(:user) }
before do
role = FactoryGirl.create(:role, :permissions => [:delete_planning_elements])
projects = block ? instance_eval(&block) : [project]
projects.each do |p|
member = FactoryGirl.build(:member, :user => current_user, :project => project)
member.roles = [role]
member.save!
end
end
end
before do
User.stub(:current).and_return current_user
end
# ===========================================================
# UI tests
describe 'index.html' do
describe 'w/o a given project' do
become_admin
it 'renders a 404 Not Found page' do
get 'index'
response.response_code.should == 404
end
end
describe 'w/ an unknown project' do
become_admin
it 'renders a 404 Not Found page' do
get 'index', :project_id => '4711'
response.response_code.should == 404
end
end
describe 'w/ a known project' do
let(:project) { FactoryGirl.create(:project, :identifier => 'test_project') }
describe 'w/o being a member or administrator' do
become_non_member
it 'renders a 403 Forbidden page' do
get 'index', :project_id => project.identifier
response.response_code.should == 403
end
end
describe 'w/ the current user having view_planning_elements permissions' do
become_member_with_view_planning_element_permissions
describe 'w/o any planning elements within the project' do
it 'assigns an empty planning_elements array' do
get 'index', :project_id => project.identifier
assigns(:planning_elements).should == []
end
it 'renders the index builder template' do
get 'index', :project_id => project.identifier
response.should render_template('planning_elements/index', :formats => ["html"], :layout => :base)
end
end
describe 'w/ 3 planning elements within the project' do
before do
@created_planning_elements = [
FactoryGirl.create(:planning_element, :project_id => project.id),
FactoryGirl.create(:planning_element, :project_id => project.id),
FactoryGirl.create(:planning_element, :project_id => project.id)
]
end
it 'assigns a planning_elements array containing all three elements' do
get 'index', :project_id => project.identifier
assigns(:planning_elements).should =~ @created_planning_elements
end
it 'assigns a planning_elements array containing all except the deleted elements' do
destroyed = @created_planning_elements.first.destroy
get 'index', :project_id => project.identifier
assigns(:planning_elements).include?(destroyed).should be_false
end
it 'renders the index builder template' do
get 'index', :project_id => project.identifier
response.should render_template('planning_elements/index', :formats => ["html"], :layout => :base)
end
end
end
end
end
describe 'show.html' do
become_admin
describe 'w/o a valid planning element id' do
describe 'w/o a given project' do
it 'renders a 404 Not Found page' do
get 'show', :id => '4711'
response.response_code.should == 404
end
end
describe 'w/ an unknown project' do
it 'renders a 404 Not Found page' do
get 'show', :project_id => '4711', :id => '1337'
response.response_code.should == 404
end
end
describe 'w/ a known project' do
let(:project) { FactoryGirl.create(:project, :identifier => 'test_project') }
describe 'w/o being a member or administrator' do
become_non_member
it 'renders a 403 Forbidden page' do
get 'show', :project_id => project.id, :id => '1337'
response.response_code.should === 403
end
end
describe 'w/ the current user being a member' do
become_member_with_view_planning_element_permissions
it 'raises ActiveRecord::RecordNotFound errors' do
lambda do
get 'show', :project_id => project.id, :id => '1337'
end.should raise_error(ActiveRecord::RecordNotFound)
end
end
end
end
describe 'w/ a valid planning element id' do
become_admin
let(:project) { FactoryGirl.create(:project, :identifier => 'test_project') }
let(:planning_element) { FactoryGirl.create(:planning_element, :project_id => project.id) }
describe 'w/o a given project' do
it 'renders a 404 Not Found page' do
get 'show', :id => planning_element.id
response.response_code.should == 404
end
end
describe 'w/ a known project' do
describe 'w/o being a member or administrator' do
become_non_member
it 'renders a 403 Forbidden page' do
get 'show', :project_id => project.id, :id => '1337'
response.response_code.should == 403
end
end
describe 'w/ the current user being a member' do
become_member_with_view_planning_element_permissions
it 'assigns the planning_element' do
get 'show', :project_id => project.id, :id => planning_element.id
assigns(:planning_element).should == planning_element
end
it 'renders the show builder template' do
get 'show', :project_id => project.id, :id => planning_element.id
response.should render_template('planning_elements/show', :formats => ["html"], :layout => :base)
end
end
end
end
end
describe 'confirm_destroy.html' do
become_admin
describe 'w/o a valid planning element id' do
describe 'w/o a given project' do
it 'renders a 404 Not Found page' do
get 'confirm_destroy', :id => '4711'
response.response_code.should == 404
end
end
describe 'w/ an unknown project' do
it 'renders a 404 Not Found page' do
get 'confirm_destroy', :project_id => '4711', :id => '1337'
response.response_code.should == 404
end
end
describe 'w/ a known project' do
let(:project) { FactoryGirl.create(:project, :identifier => 'test_project') }
describe 'w/o being a member or administrator' do
become_non_member
it 'renders a 403 Forbidden page' do
get 'confirm_destroy', :project_id => project.id, :id => '1337'
response.response_code.should == 403
end
end
describe 'w/ the current user being a member' do
become_member_with_delete_planning_element_permissions
it 'raises ActiveRecord::RecordNotFound errors' do
lambda do
get 'confirm_destroy', :project_id => project.id, :id => '1337'
end.should raise_error(ActiveRecord::RecordNotFound)
end
end
end
end
describe 'w/ a valid planning element id' do
let(:project) { FactoryGirl.create(:project, :identifier => 'test_project') }
let(:planning_element) { FactoryGirl.create(:planning_element, :project_id => project.id) }
describe 'w/o a given project' do
it 'renders a 404 Not Found page' do
get 'confirm_destroy', :id => planning_element.id
response.response_code.should == 404
end
end
describe 'w/ a known project' do
describe 'w/o being a member or administrator' do
become_non_member
it 'renders a 403 Forbidden page' do
get 'confirm_destroy', :project_id => project.id, :id => planning_element.id
response.response_code.should == 403
end
end
describe 'w/ the current user being a member' do
become_member_with_delete_planning_element_permissions
it 'assigns the planning_element' do
get 'confirm_destroy', :project_id => project.id, :id => planning_element.id
assigns(:planning_element).should == planning_element
end
it 'renders the confirm_destroy view' do
get 'confirm_destroy', :project_id => project.id, :id => planning_element.id
response.should render_template('planning_elements/confirm_destroy', :formats => ["html"], :layout => :base)
end
end
end
end
end
describe 'destroy.html' do
become_admin
describe 'w/o a valid planning element id' do
describe 'w/o a given project' do
it 'renders a 404 Not Found page' do
get 'destroy', :id => '4711'
response.response_code.should == 404
end
end
describe 'w/ an unknown project' do
it 'renders a 404 Not Found page' do
get 'destroy', :project_id => '4711', :id => '1337'
response.response_code.should == 404
end
end
describe 'w/ a known project' do
let(:project) { FactoryGirl.create(:project, :identifier => 'test_project') }
describe 'w/o being a member or administrator' do
become_non_member
it 'renders a 403 Forbidden page' do
get 'destroy', :project_id => project.id, :id => '1337'
response.response_code.should == 403
end
end
describe 'w/ the current user being a member' do
become_member_with_delete_planning_element_permissions
it 'raises ActiveRecord::RecordNotFound errors' do
lambda do
get 'destroy', :project_id => project.id, :id => '1337'
end.should raise_error(ActiveRecord::RecordNotFound)
end
end
end
end
describe 'w/ a valid planning element id' do
let(:project) { FactoryGirl.create(:project, :identifier => 'test_project') }
let(:planning_element) { FactoryGirl.create(:planning_element, :project_id => project.id) }
describe 'w/o a given project' do
it 'renders a 404 Not Found page' do
get 'destroy', :id => planning_element.id
response.response_code.should == 404
end
end
describe 'w/ a known project' do
describe 'w/o being a member or administrator' do
become_non_member
it 'renders a 403 Forbidden page' do
get 'destroy', :project_id => project.id, :id => planning_element.id
response.response_code.should == 403
end
end
describe 'w/ the current user being a member' do
become_member_with_delete_planning_element_permissions
it 'assigns the planning_element' do
get 'destroy', :project_id => project.id, :id => planning_element.id
assigns(:planning_element).should == planning_element
end
it 'redirects to index and adds a notice to the flash' do
get 'destroy', :project_id => project.id, :id => planning_element.id
response.should redirect_to(project_planning_elements_path(project))
flash[:notice].should be_present
end
it 'deletes the record' do
get 'destroy', :project_id => project.id, :id => planning_element.id
lambda do
planning_element.reload
end.should raise_error(ActiveRecord::RecordNotFound)
end
end
end
end
end
end
Loading…
Cancel
Save