剖析短迭代
敏捷教练Dave Nicolette提出:我们应该如何设定迭代长度?是要根据发布周期的时间么?使用短迭代又有哪些好处?
作者 Rick DeNatale译者 贾晓楠 发布于 2008年2月4日 下午8时59分
class StandardsController < ApplicationController
# GET /standards
# GET /standards.xml
def index
@standards = Standard.find(:all)
respond_to do|format|
format.html # index.html.erb
format.xml { render :xml => @standards }
end
end
# GET /standards/1
# GET /standards/1.xml
def show
@standard = Standard.find(params[:id])
respond_to do|format|
format.html # show.html.erb
format.xml { render :xml => @standard }
end
end
# GET /standards/new
# GET /standards/new.xml
def new
@standard = Standard.new
respond_to do|format|
format.html # new.html.erb
format.xml { render :xml => @standard }
end
end
# GET /standards/1/edit
def edit
@standard = Standard.find(params[:id])
end
# POST /standards
# POST /standards.xml
def create
@standard = Standard.new(params[:standard])
respond_to do|format|
if @standard.save
flash[:notice] = 'Standard was successfully created.'
format.html { redirect_to(@standard) }
format.xml { render :xml => @standard, :status => :created, :location => @standard }
else
format.html { render :action => "new" }
format.xml { render :xml => @standard.errors, :status => :unprocessable_entity }
end
end
end
# PUT /standards/1
# PUT /standards/1.xml
def update
@standard = Standard.find(params[:id])
respond_to do|format|
if @standard.update_attributes(params[:standard])
flash[:notice] = 'Standard was successfully updated.'
format.html { redirect_to(@standard) }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @standard.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /standards/1
# DELETE /standards/1.xml
def destroy
@standard = Standard.find(params[:id])
@standard.destroy
respond_to do|format|
format.html { redirect_to(standards_url) }
format.xml { head :ok }
end
end
end
除了特别的名字以外,所有自动生成的控制器代码都是这样的。
使用自动生成的控制器非常简单。在许多情况下,很少或者不需要对生成的代码做任何改变,尤其是当你把“精瘦的控制器”这个理念铭记于心时。
另一方面,Ruby/Rails还有一条理念,就是 “不要重复自己(DRY)”。 如果存在几乎重复的代码,即便不是你自己写的,也是违背DRY原则的。
输入:resource_controller。James Golick贡献了一个新的rails插件,称为resource_controller,它可以实现与上面同样的控制器,代码如下:
class StandardsController < ApplicationController
resource_controller
end
然而,这里仍有一个小小的瑕疵。 它没有提供标准的xml响应能力,但可以用一小段代码来实现:
class StandardsController < ApplicationController
resource_controller
index.wants.xml { render :xml => @standards }
[new, show].each do|action|
action.wants.xml { render :xml => @standard })
end
create.wants.xml { render :xml => @standard, :status => :created, :location => @standard }
[update, destroy].each do|action|
action.wants.xml { head :ok }
end
[create_fails, update_fails].each do|action|
action.wants.xml { render :xml => @standard.errors, :status => :unprocessable_entity }
end
end
有了这个插件,写控制器代码如同写模型代码一样,只需加上像resource_controller这样的声明的类方法,以及action.wants之类的“回调”。这个插件自动为控制器的每个方法分配实例变量。在上面的代码中,给index方法分配了@standards ,给其他方法分配了@standard。
Rails有一些公用的模式强迫改变控制器代码。其中包括嵌套资源。很容易在config/routes.rb中设置路由:
map.resources :organization, :has_many => :standards
但是,一旦你这样做了,你就需要更改控制器来获取和使用上层资源,并在每个动作中正确使用。resource_controller插件简化了这些。在如上面那样更改路由后,你只需添加一个声明来调用我们的控制器:
class StandardsController < ApplicationController
resource_controller
belongs_to :organization
end
belongs_to声明允许嵌套资源使用控制器。现在,当一个控制器动作通过嵌套资源URL被访问时,例如/organization/1234 /standards,控制器会自动创建一个名为@organization的实例变量,适当地设置好,并使用standards所关联地父对象来查找和 建立Standard模型的实例。
注意, 同样的控制器也工作在非嵌套的URL下,因此我们可以在routes.rb中做另一个映射,来允许在organization之外访问starnards:
map.resources :standard
map.resources :organization, :has_many :standards
这样resource控制器就会自动地工作在这两种上下文中了。
这个插件也处理命名空间控制器、多态嵌套资源(与ActiveRecord多态关联相似和相关)和其他一些奇妙地东西。你也可以获得URL以及工作在请求的URL上下文中的路径辅助函数。
看来Resource_controller是个有用的插件,毫无疑问随着它的成熟,会变得越来越好。细节见James Golicks的博客。另外还有Fabio Akita制作的一段屏幕录像,演示了这个插件的实际使用情况。
查看英文原文:Rails: Resource_controller Plugin Puts Controllers on a Diet
本文主要讲述了如何用JBoss Portlet Container 和JBoss Portlet Bridge创建新项目,怎样配置一个JSF应用去使用JBoss Portlet Bridge,以及JBoss Portlet Bridge所具备的功能。
在这篇文章里,Bryon Jacob和Chris Berry将和我们继续探讨AtomServer,它是基于Apache Abdera的完整Atom存储实现。作者还创建了几个Atompub规范扩展,其中包括自动标记、批处理和Feeds聚合。
InfoQ中文站的电子杂志《架构师》试刊第二期出版了!相比于上期,我们在内容的选择安排和版式上都根据读者的意见重新做了修正。“细节决定成败”,我们希望基于InfoQ中文站的专业内容,《架构师》能逐渐成为大家喜欢的电子刊物!
在本文中,Steven Haines探讨了Web应用性能调优问题。该领域过去更像是一门艺术而不是一门科学。他提出了一种称为基于等待调优的方法,使整个调优过程更加可度量,也因此更具科学性。
通常来说,改变技术路线时最艰难的部分是辨别语言语法之间的不同。这篇文章就为Java开发者提供了一份如何转向Flex基础语言ActionScript的指南。
本视频主要以财帮子为例,介绍了如何创建一个PV为百万级的Rails应用。其中包括:Rails应用的服务器架构、Rails Cache的优化、负载均衡的处理、Web服务器的调试、分布式解决方案、Open API的设计等等。
InfoQ首席架构师Alexandru Popescu在采访中谈论了InfoQ架构、Webwork与DWR、Hibernate与JCR、Hibernate可扩展性、最新的InfoQ视频流系统和InfoQ的未来规划。
没有回复
回复