我总是在Rails models的顶端有很多行代码.我正在寻找用标准Ruby风格打破它们的最佳方式的建议.例如,我现在看到的一行是:

delegate :occupation, :location, :picture_url, :homepage_url, :headline, :full_name, :to => :profile, :prefix => true, :allow_nil => true

打破这些长长的方法调用线的传统方式是什么?

推荐答案

大致如下:

delegate :occupation, :location, :picture_url, 
         :homepage_url, :headline, :full_name, 
         :to => :profile, :prefix => true, :allow_nil => true

或者,如果您想突出显示选项哈希(合理的事情):

delegate :occupation, :location, :picture_url, 
         :homepage_url, :headline, :full_name, 
     :to => :profile, :prefix => true, :allow_nil => true

把这些都放在一行的 idea 让我觉得是个糟糕的 idea ,这意味着你必须滚动一个任意的数量,才能看到授权的内容.Electron 战.

我可能也会把东西排成一行,或者按字母顺序排列.

delegate :full_name, :headline,   :homepage_url,
         :location,  :occupation, :picture_url,
     :to => :profile, :prefix => true, :allow_nil => true

如果该文件没有太多/任何其他实质性内容,我可能会将每个方法符号放在自己的行中,以便于编辑.在更大的文件中,我不想占用空间.

我从来没有想过这种事.

我想是的:/

如今,我可能会根据"相似性"对委托方法进行分组,大致如下:

delegate :full_name, :headline,
         :location,  :occupation,
         :homepage_url, picture_url,
     to: :profile, prefix: true, allow_nil: true

My jury's hung on 1.9 hash syntax when the value is also a symbol; I think it looks funny. I'm also not sure where I'd indent it to–might lose it anyway during an IDE reformatting, but I kind of like how it looks above if I'm using the new syntax.

Ruby-on-rails相关问答推荐

ActiveRecord::声明在类别中无效#new

Rails 创建自定义函数/方法以及存储位置

如何在测试期间为 Rails 应用程序启用 Rails.cache 会话对象

序列化来自关联的不可变数据是个好主意吗?

Rails 7 共享刺激控制器功能

Rails 'includes' 和 'where' 与 belongs_to 和 has_many 关联

Rails 将字符串与视图中的变量连接的最佳方法

如何避免 RSpec 3.0 中 stub_chain 的弃用警告?

使用devise gem点击确认链接后避免登录?

如何在rails中发回js.haml

使用 RSpec 2 关闭一个规范的事务性固定装置

Redis 引发需要 NOAUTH 身份验证错误,但没有设置密码

Rails:立即渲染并退出

Rails:将所有未知路由重定向到 root_url

验证一个对象是否有一个或多个关联对象

Rails 中是否有 HTML 安全截断方法?

Ruby中的参数化获取请求?

是否可以设置 travis 来运行多种语言的测试?

:javascript haml 标签中的内联Ruby ?

何时(如果)合并 ActiveRecord 迁移?