Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: ruby-china/ruby-china
base: master
...
head fork: xEasy/ruby-china
compare: merit
Checking mergeability… Don’t worry, you can still create the pull request.
  • 1 commit
  • 9 files changed
  • 0 commit comments
  • 1 contributor
View
3  Gemfile
@@ -29,6 +29,9 @@ gem "mongoid_colored_logger", "0.1.1"
# 用户系统
gem 'devise', '1.5.2'
+# Rank,勋章系统
+gem 'merit', :git => "git://github.com/huacnlee/merit.git"
+
# 分页
gem 'will_paginate', '3.0.2'
gem 'will_paginate_mongoid', '~> 1.0.2'
View
9 Gemfile.lock
@@ -1,4 +1,11 @@
GIT
+ remote: git://github.com/huacnlee/merit.git
+ revision: c229bee9f3004c4781177887cb22800e96c81e33
+ specs:
+ merit (0.7.1)
+ ambry
+
+GIT
remote: git://github.com/intridea/grape.git
revision: d24bd2f758544244ac65f19c69b94f0ffc34e71b
branch: frontier
@@ -41,6 +48,7 @@ GEM
i18n (~> 0.6)
multi_json (~> 1.0)
addressable (2.2.6)
+ ambry (0.3.0)
anjlab-bootstrap-rails (2.0.3.2)
railties (>= 3.0)
sass
@@ -353,6 +361,7 @@ DEPENDENCIES
jquery-atwho-rails (= 0.1.3)
jquery-rails (= 2.0.1)
memcache-client (= 1.8.5)
+ merit!
mini_magick (= 3.3)
mongo-rails-instrumentation (= 0.2.4)
mongoid (= 2.4.8)
View
12 app/controllers/likes_controller.rb
@@ -4,13 +4,13 @@ class LikesController < ApplicationController
before_filter :find_likeable
def create
- current_user.like(@item)
- render :text => @item.reload.likes_count
+ current_user.like(@like)
+ render :text => @like.reload.likes_count
end
def destroy
- current_user.unlike(@item)
- render :text => @item.reload.likes_count
+ current_user.unlike(@like)
+ render :text => @like.reload.likes_count
end
private
@@ -23,8 +23,8 @@ def find_likeable
end
klass = params[:type].constantize
- @item = klass.find_by_id(params[:id])
- if @item.blank?
+ @like = klass.find_by_id(params[:id])
+ if @like.blank?
render :text => "-2"
return false
end
View
5 app/controllers/topics_controller.rb
@@ -130,10 +130,11 @@ def destroy
end
def favorite
+ @topic = Topic.find(params[:id])
if params[:type] == "unfavorite"
- current_user.unfavorite_topic(params[:id])
+ current_user.unfavorite_topic(@topic.id)
else
- current_user.favorite_topic(params[:id])
+ current_user.favorite_topic(@topic.id)
end
render :text => "1"
end
View
42 app/models/merit/badge_rules.rb
@@ -0,0 +1,42 @@
+# +grant_on+ accepts:
+# * Nothing (always grants)
+# * A block which evaluates to boolean (recieves the object as parameter)
+# * A block with a hash composed of methods to run on the target object with
+# expected values (+:votes => 5+ for instance).
+#
+# +grant_on+ can have a +:to+ method name, which called over the target object
+# should retrieve the object to badge (could be +:user+, +:self+, +:follower+,
+# etc). If it's not defined merit will apply the badge to the user who
+# triggered the action (:action_user by default). If it's :itself, it badges
+# the created object (new user for instance).
+#
+# The :temporary option indicates that if the condition doesn't hold but the
+# badge is granted, then it's removed. It's false by default (badges are kept
+# forever).
+
+module Merit
+ class BadgeRules
+ include Merit::BadgeRulesMethods
+
+ def initialize
+ # If it creates user, grant badge
+ # Should be "current_user" after registration for badge to be granted.
+ # grant_on 'users#create', :badge => 'just-registered', :to => :itself
+
+ # If it has 10 comments, grant commenter-10 badge
+ # grant_on 'comments#create', :badge => 'commenter', :level => 10 do |comment|
+ # comment.user.comments.count == 10
+ # end
+
+ # If it has 5 votes, grant relevant-commenter badge
+ # grant_on 'comments#vote', :badge => 'relevant-commenter', :to => :user do |comment|
+ # comment.votes.count == 5
+ # end
+
+ # Changes his name by one wider than 4 chars (arbitrary ruby code case)
+ # grant_on 'registrations#update', :badge => 'autobiographer', :temporary => true, :model_name => 'User' do |user|
+ # user.name.length > 4
+ # end
+ end
+ end
+end
View
26 app/models/merit/point_rules.rb
@@ -0,0 +1,26 @@
+# coding: utf-8
+module Merit
+ class PointRules
+ include Merit::PointRulesMethods
+
+ def initialize
+ # 发帖
+ score 3, :on => 'topics#create'
+ # 发帖被删除
+ score -5, :on => 'topics#destroy', :to => :user
+ # 回帖
+ score 5, :on => 'replies#create'
+ # 回帖被删除
+ score -8, :on => 'replies#destroy', :to => :user
+
+ # 话题被收藏
+ score 13, :on => 'topics#favorite', :to => :user
+ # 话题被关注
+ score 3, :on => 'topics#follow', :to => :user
+ # 收到喜欢
+ score 10, :on => 'likes#create', :to => :user
+ # 第一次修改个人信息
+ score 100, :on => 'users#update'
+ end
+ end
+end
View
29 app/models/merit/rank_rules.rb
@@ -0,0 +1,29 @@
+# 5 stars is a common ranking use case. They are not given at specified
+# actions like badges, you should define a cron job to test if ranks are to be
+# granted.
+#
+# +set_rank+ accepts:
+# * :+level+ ranking level (greater is better)
+# * :+to+ model or scope to check if new rankings apply
+# * :+level_name+ attribute name (default is empty and results in 'level'
+# attribute, if set it's appended like 'level_#{level_name}')
+
+module Merit
+ class RankRules
+ include Merit::RankRulesMethods
+
+ def initialize
+ # set_rank :level => 1, :to => Commiter.active do |commiter|
+ # commiter.repositories.count > 1 && commiter.followers >= 10
+ # end
+ #
+ # set_rank :level => 2, :to => Commiter.active do |commiter|
+ # commiter.branches.count > 1 && commiter.followers >= 10
+ # end
+ #
+ # set_rank :level => 3, :to => Commiter.active do |commiter|
+ # commiter.branches.count > 2 && commiter.followers >= 20
+ # end
+ end
+ end
+end
View
2  app/models/user.rb
@@ -7,6 +7,8 @@ class User
include Mongoid::BaseModel
include Redis::Objects
extend OmniauthCallbacks
+ has_merit
+
cache
devise :database_authenticatable, :registerable,
View
14 config/initializers/merit.rb
@@ -0,0 +1,14 @@
+# Use this hook to configure merit parameters
+Merit.setup do |config|
+ # Check rules on each request or in background
+ # config.checks_on_each_request = true
+
+ # Define ORM. Could be :active_record (default) and :mongo_mapper and :mongoid
+ config.orm = :mongoid
+end
+
+# Create application badges (uses https://github.com/norman/ambry)
+# Badge.create!({
+# :id => 1,
+# :name => 'just-registered'
+# })

No commit comments for this range

Something went wrong with that request. Please try again.