TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Put Callback Logic Into Callback Objects [Rails]

24 点作者 nagnatron大约 13 年前

4 条评论

JonWood大约 13 年前
This strikes me as being similar to extracting bits of a model out into modules that then get included back into that model. It looks like you're reducing the complexity of the model, but in fact you're just hiding it elsewhere, without making it any easier to stub things out.
评论 #3679628 未加载
评论 #3679635 未加载
gcao大约 13 年前
Shameless plug here. If you use the Aspector gem I created, you could move validation logic into an aspect and test it. And apply aspect is very easy. Here is what the code will look like.<p>class Request &#60; ActiveRecord::Base<p>end<p>class RequestValidation &#60; Aspector::Base<p><pre><code> target do def has_unique_hash_id? exists?(:hash_id =&#62; hash_id) end end before :create do hash_id = SecureRandom.hex(12) until has_unique_hash_id? end</code></pre> end<p>RequestValidation.apply(Request)
soveran大约 13 年前
You may run into race conditions when two processes end up saving an object with the same hash_id. The check and the object creation should both happen inside a transaction.
评论 #3680607 未加载
bhousel大约 13 年前
I'm kind of curious why you use SecureRandom.hex and then check for uniqueness, rather than just using SecureRandom.uuid?<p>Edit: Nevermind, I realize now that SecureRandom's uuid isn't really a uuid, in that sense. It's just random.