If you have complex view logic, this can easily lead to helper methods which call each other. See how to refactor this out into another object in this episode.
# application_helper.rb
def render_stars(rating)
StarsRenderer.new(rating, self).render_stars
end
# helpers/stars_renderer.rb
class StarsRenderer
def initialize(rating, template)
@rating = rating
@template = template
end
def render_stars
content_tag :div, star_images, :class => 'stars'
end
private
def star_images
(0...5).map do |position|
star_image(((@rating-position)*2).round)
end.join
end
def star_image(value)
image_tag "/images/#{star_type(value)}_star.gif", :size => '15x15'
end
def star_type(value)
if value <= 0
'empty'
elsif value == 1
'half'
else
'full'
end
end
def method_missing(*args, &block)
@template.send(*args, &block)
end
分享到:
相关推荐
Existing Code shows how refactoring can make object-oriented code simpler and easier to maintain. Today refactoring requires considerable design know-how, but once tools become available, all ...
Refactoring is an effective way to quickly uncover problematic code and fix it. In this first book to provide a hands-on approach to refactoring in C# and ASP.NET, you’ll discover to apply ...
Refactoring Ruby
系统重构架构设计,A guide to refactoring, the process of changing a software system so that it does not alter the external behavior of the code yet improves its internal structure, for professional ...
a book about SQL refactoring
refactoring to patterns
PHP Refactoring Browser ,这是 PHP 代码的重构工具,通过生成 diffs 来描述重构的步骤。为了避免重构过程中的简单错误,...
This eagerly awaited new edition has been fully updated to reflect crucial changes in the programming landscape.Refactoring, Second Edition,features an updated catalog of refactorings and includes ...
Refactoring.in.Ruby.pdf
Refactoring2-free-chapter_cn
Refactoring(Improving the Design of Existing Code) 我加上了每个重构方法的书签,方便查阅
UI设计(UserInterfaceDesign),即界面设计,是指对软件的人机交互、操作逻辑、界面美观的整体设计。它可以分为实体UI和虚拟UI两类,互联网中所提及的UI设计即是虚拟UI。 好的UI设计不仅可以让软件变得更有个性、有...
原版Refactoring思想和实践,深入了解Refact 精髓。
Refactoring to Patterns
Refactoring_2_Patter
Refactoring in Large Software Projects
--表中的内容## Installation## Usage## Refactoring## License安装[Git repo] (https://github.com/mishs/github-finder.git)Use the package manager [npm] (https://docs.npmjs.com/cli/v6/commands/npm-install...