新手问题 有哪些代码质量比较高的、值得学习的开源项目?

diguage · 2013年10月30日 · 最后由 crazyjin 回复于 2014年10月15日 · 7504 次阅读

初学 Rails,看过《Aglie Web Development with Rails》和《Ruby on Rails Tutorial》两本书。没有太多的 Rails 实战经验。希望找些代码质量比较高的开源项目学习一下。

求推荐代码质量比较高的、值得学习的开源项目。 如果方便,希望把链接也发出来。

比如 rubychina

看看酷站里面的开源项目http://ruby-china.org/sites

开源的 rails 项目我至今没找到质量高的,包括我自己的 angel_nest 在内。:(

强烈推荐 ruby-china 和 rabel

ruby china 属于比较复杂的吧

最好有简单的 CURD 操作的开源代码,我就想看看高手是如何读写数据库的,有木有? 当然不是脚手架生成的那么简单的~

#3 楼 @fredwu discourse 如何?

#7 楼 @xstmjh 之前看过,挺糟的。

最简单的“偷窥”方式就是看 controller:

https://github.com/discourse/discourse/blob/af96ef2994c5aa26345727fd0bed14e9ffd6cfa0/app/controllers/posts_controller.rb#L61-L107

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

😱 😱 😱

#8 楼 @fredwu 其实随便找一个复杂点的(这是前提)项目,如果里面的每个方法代码行数平均都能控制在 10 行以内,质量应该就不错。也是个评估别人代码质量的偷懒但有效的办法。

#3 楼 @fredwu 另外,spree 你也看不上眼嘛? :D 感觉这是 rails 项目里代码写得非常棒的一个。

#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

抛开 rails 的话,我是看完 ruhoh源碼才入 ruby 門的。

Rabel 代码质量比较差。。

#14 楼 @daqing Rabel 第二版是不是闭源了?我在你的 Github 帐号上找不到了。呵呵

#16 楼 @diguage 2.0 不打算单独开发了,会合并到 Rabel 的主代码库。

准备看看 rubychina

需要 登录 后方可回复, 如果你还没有账号请 注册新账号