migrated the planning_element_types to rabl

pull/527/head
Stefan Frank 11 years ago
parent 9c56717cee
commit 163e170e4a
  1. 5
      app/controllers/api/v2/planning_elements_controller.rb
  2. 55
      app/views/api/v2/planning_element_types/_planning_element_type.api.rabl
  3. 5
      app/views/api/v2/planning_element_types/index.api.rabl
  4. 8
      app/views/api/v2/planning_element_types/show.api.rabl
  5. 46
      spec/views/api/v2/planning_element_types/index_api_json_spec.rb
  6. 104
      spec/views/api/v2/planning_element_types/index_api_rsb_spec.rb
  7. 53
      spec/views/api/v2/planning_element_types/show_api_json_spec.rb

@ -214,8 +214,9 @@ module Api
def optimize_planning_elements_for_less_db_queries
# triggering full load to avoid separate queries for count or related models
# historical packages are already loaded correctly and only need to be optimised, so they do not need to fetched again, only optimised
@planning_elements = @planning_elements.all(:include => [:type, :status, :project, :responsible]) unless @planning_elements.class == Array
# historical packages are already loaded correctly and only need to be optimised, so they do not need to be fetched again, only optimised
# TODO :responsible, :assigned_to, :author are all three users: this can be further optimised by collecting all user-ids and replacing them in one go
@planning_elements = @planning_elements.all(:include => [:type, :status, :project, :responsible, :assigned_to, :author]) unless @planning_elements.class == Array
# Replacing association proxies with already loaded instances to avoid
# further db calls.

@ -0,0 +1,55 @@
#-- copyright
# OpenProject is a project management system.
# Copyright (C) 2012-2013 the OpenProject Foundation (OPF)
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 3.
#
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
# Copyright (C) 2006-2013 Jean-Philippe Lang
# Copyright (C) 2010-2013 the ChiliProject Team
#
# 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.
#
# 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.
#
# 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.
#
# See doc/COPYRIGHT.rdoc for more details.
#++
object @type
attributes :id, :name, :in_aggregation, :is_milestone, :position, :is_default
node :color, if: lambda{|type| type.color.present?} do |type|
{id: type.color.id, name: type.color.name, hexcode: type.color.hexcode}
end
node :created_at, if: lambda{|project| project.created_at.present?} {|project| project.created_at.utc.iso8601}
node :updated_at, if: lambda{|project| project.updated_at.present?} {|project| project.updated_at.utc.iso8601}
#api.planning_element_type do
# api.id(planning_element_type.id)
# api.name(planning_element_type.name)
#
# api.in_aggregation(planning_element_type.in_aggregation)
# api.is_milestone(planning_element_type.is_milestone)
# api.is_default(planning_element_type.is_default)
#
# api.position(planning_element_type.position)
#
# color = planning_element_type.color
# if color.present?
# api.color(:id => color.id, :name => color.name, :hexcode => color.hexcode)
# end
#
# api.created_at(planning_element_type.created_at.utc) if planning_element_type.created_at
# api.updated_at(planning_element_type.updated_at.utc) if planning_element_type.updated_at
#end

@ -26,5 +26,6 @@
# See doc/COPYRIGHT.rdoc for more details.
#++
render(:partial => '/api/v2/planning_element_types/planning_element_type.api',
:object => @planning_element_type)
collection @types => :planning_element_types
extends '/api/v2/planning_element_types/planning_element_type'

@ -26,9 +26,7 @@
# See doc/COPYRIGHT.rdoc for more details.
#++
api.array :planning_element_types, :size => @types.size do
@types.each do |planning_element_type|
render(:partial => '/api/v2/planning_element_types/planning_element_type.api',
:object => planning_element_type)
end
object @type
node :planning_element_type do |type|
partial "api/v2/planning_element_types/planning_element_type", object: type
end

@ -26,21 +26,43 @@
# See doc/COPYRIGHT.rdoc for more details.
#++
api.planning_element_type do
api.id(planning_element_type.id)
api.name(planning_element_type.name)
require File.expand_path('../../../../../spec_helper', __FILE__)
api.in_aggregation(planning_element_type.in_aggregation)
api.is_milestone(planning_element_type.is_milestone)
api.is_default(planning_element_type.is_default)
describe 'api/v2/planning_element_types/index.api.rabl' do
api.position(planning_element_type.position)
before do
params[:format] = 'json'
end
describe 'with no planning element types available' do
it 'renders an empty planning_element_types document' do
assign(:types, [])
render
color = planning_element_type.color
if color.present?
api.color(:id => color.id, :name => color.name, :hexcode => color.hexcode)
response.should have_json_size(0).at_path('planning_element_types')
end
end
api.created_at(planning_element_type.created_at.utc) if planning_element_type.created_at
api.updated_at(planning_element_type.updated_at.utc) if planning_element_type.updated_at
describe 'with 3 planning element types available' do
let(:types) do
[
FactoryGirl.build(:type),
FactoryGirl.build(:type),
FactoryGirl.build(:type)
]
end
before do
assign(:types, types)
render
end
subject{response.body}
it 'renders 3 planning_element_types' do
should have_json_size(3).at_path('planning_element_types')
end
end
end

@ -1,104 +0,0 @@
#-- copyright
# OpenProject is a project management system.
# Copyright (C) 2012-2013 the OpenProject Foundation (OPF)
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 3.
#
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
# Copyright (C) 2006-2013 Jean-Philippe Lang
# Copyright (C) 2010-2013 the ChiliProject Team
#
# 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.
#
# 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.
#
# 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.
#
# See doc/COPYRIGHT.rdoc for more details.
#++
require File.expand_path('../../../../../spec_helper', __FILE__)
describe 'api/v2/planning_element_types/index.api.rsb' do
before do
view.extend TimelinesHelper
end
before do
params[:format] = 'xml'
end
describe 'with no planning element types available' do
it 'renders an empty planning_element_types document' do
assign(:types, [])
render
response.should have_selector('planning_element_types', :count => 1)
response.should have_selector('planning_element_types[type=array][size="0"]') do
without_tag 'planning_element_type'
end
end
end
describe 'with 3 planning element types available' do
let(:types) do
[
FactoryGirl.build(:type),
FactoryGirl.build(:type),
FactoryGirl.build(:type)
]
end
it 'renders a planning_element_types document with the size 3 of type array' do
assign(:types, types)
render
response.should have_selector('planning_element_types', :count => 1)
response.should have_selector('planning_element_types[type=array][size="3"]')
end
it 'renders a planning_element_type for each assigned planning element' do
assign(:types, types)
render
response.should have_selector('planning_element_types planning_element_type', :count => 3)
end
it 'renders the _planning_element_type template for each assigned planning element type' do
assign(:types, types)
view.should_receive(:render).exactly(3).times.with(hash_including(:partial => '/api/v2/planning_element_types/planning_element_type.api')).and_return('')
# just to render the speced template despite the should receive expectations above
view.should_receive(:render).once.with({:template=>"api/v2/planning_element_types/index", :handlers=>["rsb"], :formats=>["api"]}, {}).and_call_original
render
end
it 'passes the planning element types as local var to the partial' do
assign(:types, types)
view.should_receive(:render).once.with(hash_including(:object => types.first)).and_return('')
view.should_receive(:render).once.with(hash_including(:object => types.second)).and_return('')
view.should_receive(:render).once.with(hash_including(:object => types.third)).and_return('')
# just to render the speced template despite the should receive expectations above
view.should_receive(:render).once.with({:template=>"api/v2/planning_element_types/index", :handlers=>["rsb"], :formats=>["api"]}, {}).and_call_original
render
end
end
end

@ -28,47 +28,52 @@
require File.expand_path('../../../../../spec_helper', __FILE__)
describe 'api/v2/planning_element_types/show.api.rsb' do
before do
view.extend TimelinesHelper
end
describe 'api/v2/planning_element_types/show.api.rabl' do
before do
params[:format] = 'xml'
params[:format] = 'json'
end
describe 'with an assigned planning element type' do
let(:planning_element_type) { FactoryGirl.build(:type) }
before do
assign(:planning_element_type, planning_element_type)
end
let(:planning_element_type) do
FactoryGirl.build(:type,
:id => 1,
:name => 'Awesometastic Planning Element Type',
it 'renders a planning_element_type document' do
:in_aggregation => false,
:is_milestone => true,
:is_default => true,
render
:position => 100,
response.should have_selector('planning_element_type', :count => 1)
:created_at => Time.parse('Thu Jan 06 12:35:00 +0100 2011'),
:updated_at => Time.parse('Fri Jan 07 12:35:00 +0100 2011'))
end
it 'renders the _planning_element_type template once' do
view.should_receive(:render).once.with(hash_including(:partial => '/api/v2/planning_element_types/planning_element_type.api')).and_return('')
before do
assign(:type, planning_element_type)
render
end
# just to render the speced template despite the should receive expectations above
view.should_receive(:render).once.with({:template=>"api/v2/planning_element_types/show", :handlers=>["rsb"], :formats=>["api"]}, {}).and_call_original
subject { response.body }
render
it 'renders a planning_element_type document' do
should have_json_path('planning_element_type')
end
it 'passes the planning element type as local var to the partial' do
it 'should render all detail-information for the planning-element-type' do
expected_json = {:name => 'Awesometastic Planning Element Type',
view.should_receive(:render).once.with(hash_including(:object => planning_element_type)).and_return('')
:in_aggregation => false,
:is_milestone => true,
:is_default => true,
# just to render the speced template despite the should receive expectations above
view.should_receive(:render).once.with({:template=>"api/v2/planning_element_types/show", :handlers=>["rsb"], :formats=>["api"]}, {}).and_call_original
:position => 100,
}.to_json
render
should be_json_eql(expected_json).at_path('planning_element_type')
end
end
end
Loading…
Cancel
Save