- 浏览: 17696 次
- 性别:
- 来自: 北京
最近访客 更多访客>>
最新评论
-
dazuiba:
rails-1.2.4update to 1.2.5updat ...
诡异的"already initialized constant OPTIONS"及解决方案
used "rails best practices " today. details see:
http://github.com/flyerhzm/rails_best_practices
below are the refactor advices for my app :
./config/routes.rb:58 - not use default route
./config/routes.rb:59 - not use default route
./app/sweepers/backup_sweeper.rb:4 - law of demeter
./app/controllers/admin/self_study/page_groups_controller.rb:33 - move model logic into model (page_group called_count > 4)
./app/controllers/admin/self_study/page_parts_controller.rb:3 - move model logic into model (@page_part called_count > 4)
./db/migrate/20090108084125_create_scenarios.rb:2 - always add db index (task_cards => trainer_task_card_id)
........................400+ index adding........................
./db/migrate/20091105042309_add_extra_type_to_page_group.rb:2 - always add db index (headings => prompt_page_id)
./app/models/page.rb:212 - keep finders on their own model
./app/models/page.rb:234 - keep finders on their own model
./app/models/page_group.rb:111 - keep finders on their own model
./app/models/page_group.rb:115 - keep finders on their own model
./app/controllers/admin/self_study/page_group_pages_controller.rb:21 - move finder to named_scope
./app/controllers/admin/self_study/page_group_pages_controller.rb:34 - move finder to named_scope
./app/controllers/admin/self_study/page_statistics_controller.rb:16 - move finder to named_scope
Found 462 errors.
=========================================
Below are some refactors I made according the advices, I think most advices are valuable.
refactor on "page_group called_count > 4"
old code
def expire_cache_of(page_group) case page_group.class.to_s when "Preparation" expire_action(:controller => "/flex/preparations/#{page_group.id}") when "PublicContent" expire_action(:controller => "/flex/public_contents/#{page_group.id}") when "ExtraContent" page_group.pages.each do |page| expire_action(:controller => "/flex/extras/#{page.id}") end when "TaskCard" page_group.pages.each do |page| expire_action(:controller => "/flex/task_cards/#{page.id}") end end end
new code
def expire_cache_of(page_group) expire_action(:controller => "/flex/#{page_group.class_name.tableize}/#{page_group.id}") page_group.pages.map(&:id).each { |id| expire_action(:controller => "/flex/task_cards/#{id}") } if page_group.class == TaskCard # not finished end
refactor on "@page_part called_count > 4"
old code
in controller
def set_content @page_part = PagePart.find_by_id(params[:page_part]) value = @page_part.part_value @page_part.update_attributes(:part_value => params[:value]) @page_part.page.page_groups.each do |page_group| page_group.page_group_changes << PageGroupChange.create(:user => current_user.id, :action => "<b>updated</b> page '#{@page_part.page.title}' part #{@page_part.part_key}'s value form '#{value}' to '#{@page_part.part_value}'") end render :text => @page_part.part_value.format_for_html end
new code
in controller
def set_content @page_part = PagePart.find_by_id(params[:page_part]) @page_part.set_value(params[:value], current_user.id) render :text => @page_part.part_value.format_for_html end
in model
def set_value(value, user) old_value = self.part_value self.update_attributes(:part_value => value) self.page.page_groups.each do |page_group| page_group.page_group_changes << PageGroupChange.create(:user => user, :action => "<b>updated</b> page '#{self.page.title}' part #{self.part_key}'s value form '#{old_value}' to '#{self.part_value}'") end end
test code
describe "set_value(value, user)" do it "should sent part value well" do @page_group = PageGroup.create @page = @page_group.pages.create(:title => "test set_value") @page_part = @page.page_parts.create(:part_key => "key", :part_value => "old_value") @page_part.set_value("new_value", 1) @page_part.part_value.should == "new_value" @page_group.page_group_changes.first.user.should == 1 @page_group.page_group_changes.first.action.should == "<b>updated</b> page 'test set_value' part key's value form 'old_value' to 'new_value'" end end
refacor on "keep finders on their own model"
old code
def last_published_at if last_published = self.page_group_changes.find(:last, :conditions => ["action=?", "<b>published</b>"]) last_published_at = last_published.created_at else last_published_at = "" end end
new code
in page_group_change model: named_scope :published_changes, :conditions => { :action => "<b>published</b>" } def last_published_at self.page_group_changes.published_changes.last.try(:created_at) || "" end
old code
def unpublished_changes return self.page_group_changes.find(:all, :conditions => ["created_at > ?", last_published_at], :order => "created_at DESC").map(&:description).join("<br/>") end
new code
in page_group_change model: named_scope :unpublished_changes_from, lambda { |time| { :conditions => [ "created_at > ?", time], :order => "created_at DESC"} } def unpublished_changes page_group_changes.unpublished_changes_from(last_published_at).map(&:description).join("<br/>") end
发表评论
-
useful cheat sheets.
2009-11-11 14:40 848no need download cheat anymore: ... -
code refactor(keep adding)
2009-11-05 10:20 8871.old code def page_group_to ... -
define_method with parameters
2009-11-03 18:07 763refactor below code: def ... -
example how to use 'stub' and 'mock' in sepc
2009-10-16 11:58 917Use sometime to learn stub and ... -
spec tips(keep adding)
2009-10-13 17:20 8921.stub meet "one to many&q ... -
do not rewrite exists methods
2009-09-20 23:30 778In order to name method like a ... -
never use not + not.
2009-09-19 22:57 832A bug take a long time to find ... -
use screen
2009-09-19 02:28 915Doing long time rake, screen is ... -
ruby trickes(keep adding)
2009-09-08 12:28 798some ruby tricke: 1.sho ... -
how to test gem
2009-08-31 14:09 802I wrote some gems in my project ... -
things learned from data migration
2009-08-20 13:57 840Spend a week for data migration ... -
我的emacs配置
2009-08-03 15:45 1971学习ruby on rails 两年了,期间使用过Eclips ... -
诡异的"already initialized constant OPTIONS"及解决方案
2007-10-15 12:40 2266最近要为公司一个程序加数据统计代码。在Ubuntu环境下che ...
相关推荐
refactor(重构-改善既有代码的设计),中文版共15章
【标题】"BuildSql Refactor" 是一个关于SQL构建和重构的工具或技术,它旨在提高数据库查询代码的效率和可维护性。SQL重构通常涉及优化查询语句、规范化数据库结构以及改善数据访问模式,以提升应用性能。在这个场景...
《Refactor Pro 1.0.31:.Net开发中的高效重构利器》 Refactor Pro,作为一款专为.Net开发者设计的插件,旨在提升编程效率和代码质量。其1.0.31版本的发布,带来了诸多新功能和改进,让开发者在日常编码过程中能够...
标题"refactor(FrameWork4.0)"暗示了这次我们将讨论的是一个针对Framework 4.0的重构过程。 重构是对现有代码进行修改,以改善其内部结构,而不改变其外部行为的过程。在.NET Framework 4.0的背景下,重构可能是...
CodeRush 和 Refactor Pro的最新版本11.2.8,我从网站上下载的,要注册才能下载,要的朋友就不用注册了,免费下载了。
js2-refactor.el, emacs的JavaScript重构库 js2-refactor.el emacs的JavaScript重构库。这是一个小型重构函数的Collection,以进一步了解从js2-mode开始的Emacs中的JavaScript IDE 。在 0.8.0中更改添加 e
CodeRush™ with Refactor!™ Pro Powerful. Efficient. Fast. <br>Today, software consumers demand more than ever before from developers. The pressure to deliver more features with ...
sublime-text-refactor, 用于Javascript代码的sublime text 重构插件 用于 sublime-text-2和 3的 Javascript重构插件 [[Package Control] ( https://packagecontrol.herokuapp.com/downloads/JavaScript%2
《Refactor!Pro 3.0.5:提升Visual Studio开发效率的专业工具》 Refactor!Pro 3.0.5是一款专为Visual Studio 2005和2008设计的高效代码重构工具,它极大地提升了开发人员在.NET框架下的编程效率。这款软件以其强大...
《Refactor Pro 1.0.31:深入理解.Net开发插件的授权与激活机制》 Refactor Pro是一款专为.Net开发环境设计的高效插件,版本1.0.31带来了诸多改进和增强,旨在提升开发人员的代码质量和工作效率。在软件开发领域,...
《DeveExpress Refactor!Pro 3.0.5:提升Visual Studio开发效率的专业重构工具》 DeveExpress Refactor!Pro是一款专为Visual Studio设计的高效代码重构工具,其3.0.5版本在2008年1月15日进行了官方更新。这个强大的...
代码重构是软件开发中的一项持续性工作,目的是改善现有代码的质量而不改变其外部行为。重构的重要性不言而喻,它能帮助我们增强代码的可维护性、可读性和可扩展性。在本篇中,我们将详细介绍重构的定义、方法、原则...
### 如何重构以及我们如何了解重构过程 #### 引言 重构是指在不改变程序行为的前提下,更改程序结构的过程。本文旨在深入探讨软件开发领域中重构活动的实际状况,并基于四个数据集进行了研究分析,这些数据集涉及...
"emacs-refactor"是Emacs生态系统中的一个工具,它为Emacs提供特定于语言的代码重构支持,使开发人员能够在Emacs环境中进行高效的代码改进和优化。在本文中,我们将深入探讨emacs-refactor及其对Emacs用户的意义,...
《Python库:深入理解ast-refactor-0.1.0》 在Python的世界里,库是开发者们构建复杂应用的基础,它们提供了丰富的功能,简化了编程任务。今天我们要探讨的是"ast-refactor-0.1.0",这是一个Python库,它的主要功能...
"Android Studio代码重构菜单Refactor功能详解" Android Studio中代码重构菜单Refactor功能是一个功能强大且实用的工具,旨在帮助开发者提高代码质量和开发效率。在Android Studio的主菜单栏中有一项“Refactor”...
Register and Vote for refactor feature for VS2008 now!
DevExpress IDETools 9.1.2包含DevExpress所有的IDE工具(CodeRush,CodeRush Xpress,DXCore,Refactor!Pro,Refactor! for ASP.NET,Refactor! for C++,Refactor!for Visual Basic)