Gem merit - 用更优雅的方式构建的网站的积分,等级,徽章系统

huacnlee · May 31, 2012 · Last by tangmonk replied at January 02, 2016 · 8387 hits
Topic has been selected as the excellent topic by the admin.

昨天在 Github 上面发现这个东西 merit 用于实现积分,等级功能的设计。

它的设计理念感觉和 CanCan 很想,一个中心文件管理积分,等级的逻辑,调用是通过关注 Controller 的事件来执行,写出来的代码会变得干干净净,不会在很多的 Callback 里面塞积分加减的事件。

难得的是,这玩意支持 ActiveRecord 和 MongoMapper,而我昨天稍加修改后现在可以支持 Mongoid 了。

比如积分的业务定义像这样:

module Merit
  class PointRules
    include Merit::PointRulesMethods
    def initialize
      # 修改个人信息
      # score 5, :on => 'users#update'
      # 发评论
      score 1, :on => 'comments#create'
      # 上传图片
      score 20, :on => 'photos#create'
      # 论坛发帖
      score 2, :on => 'topics#create'
      score 1, :on => 'replies#create'
    end
  end
end

徽章的业务定义:

module Merit
  class BadgeRules
    include Merit::BadgeRulesMethods

    def initialize
      grant_on 'users#create', :badge => 'just-registered', :to => :itself

      grant_on 'comments#create', :badge => 'commenter', :level => 10 do |comment|
         comment.user.comments.count == 10
      end

      grant_on 'comments#vote', :badge => 'relevant-commenter', :to => :user do |comment|
        comment.votes.count == 5
      end

      grant_on 'registrations#update', :badge => 'autobiographer', :temporary => true, :model_name => 'User' do |user|
        user.name.length > 4
      end
    end
  end
end

https://github.com/tute/merit

这个赞

或许可以给 Ruby China 上一套这种机制

@huacnlee 支持加上。可以先在社区中起草一个详细讨论。

@qichunren 最要还是如何定义规则

可以考虑 HN 那样的 up-vote 感觉挺简洁的

Unknow user #8 May 31, 2012

这个貌似更加受关注一些:https://github.com/twitter/activerecord-reputation-system

@tylerlong 两者不一样吧。twitter 那个是活跃指数。

Unknow user #10 May 31, 2012

@lepture 我说关注人数更多一些

@tylerlong merit 是用于设计积分等级功能的,activerecord-reputation-system 是用户活跃度,有差别的

Unknow user #12 May 31, 2012

@huacnlee 从名字来看,twitter 那个是声望系统。

Unknow user #13 May 31, 2012

怎么删除回复啊?我发现我回复的东西带来了困惑。发现不能删啊

@tylerlong 修改,加删除线 用 ~~文字~~

我是删除线,这样删好累

#4 楼 @huacnlee Reputation system, Yes! +1

这个真好玩,前几天 twitter 推出的类似 gem,比这个好像要深奥些。不过还没有研究,等以后项目用的着的时候玩玩。

@chucai 请问 twitter 那个 gem 叫什么名字啊

不支持 grape...

You need to Sign in before reply, if you don't have an account, please Sign up first.