初学 Rails,看过《Aglie Web Development with Rails》和《Ruby on Rails Tutorial》两本书。没有太多的 Rails 实战经验。希望找些代码质量比较高的开源项目学习一下。
求推荐代码质量比较高的、值得学习的开源项目。 如果方便,希望把链接也发出来。
最简单的“偷窥”方式就是看 controller:
def update
params.require(:post)
post = Post.where(id: params[:id]).first
post.image_sizes = params[:image_sizes] if params[:image_sizes].present?
guardian.ensure_can_edit!(post)
# to stay consistent with the create api,
# we should allow for title changes and category changes here
# we should also move all of this to a post updater.
if post.post_number == 1 && (params[:title] || params[:post][:category])
post.topic.title = params[:title] if params[:title]
Topic.transaction do
post.topic.change_category(params[:post][:category])
post.topic.save
end
if post.topic.errors.present?
render_json_error(post.topic)
return
end
end
revisor = PostRevisor.new(post)
if revisor.revise!(current_user, params[:post][:raw])
TopicLink.extract_from(post)
end
if post.errors.present?
render_json_error(post)
return
end
post_serializer = PostSerializer.new(post, scope: guardian, root: false)
post_serializer.draft_sequence = DraftSequence.current(current_user, post.topic.draft_key)
link_counts = TopicLink.counts_for(guardian,post.topic, [post])
post_serializer.single_post_link_counts = link_counts[post.id] if link_counts.present?
post_serializer.topic_slug = post.topic.slug if post.topic.present?
result = {post: post_serializer.as_json}
if revisor.category_changed.present?
result[:category] = BasicCategorySerializer.new(revisor.category_changed, scope: guardian, root: false).as_json
end
render_json_dump(result)
end
#10 楼 @yuan Spree 和大部分项目比起来质量算是不错的,但是,几个关键文件都一般般,比如最重要的 Order:https://github.com/spree/spree/blob/master/core/app/models/spree/order.rb
550 行的 class,维护成本太高。
如果抛开 rails 的话,有几个开源项目还是不错的。
Virtus:https://github.com/solnic/virtus DataMapper 系列:https://github.com/rom-rb
还有我的 Datamappify 除了某几处有些已知问题,大部分的代码还勉强可以看:https://github.com/fredwu/datamappify