Sha256: df8bfc7adbe6a2e7dce2a63d8f64c3deb0654f679db1f31895abc1106da93173

Contents?: true

Size: 1.94 KB

Versions: 32

Compression:

Stored size: 1.94 KB

Contents

# +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.
      # Example rule with block with no parameters
      grant_on 'users#create', :badge => 'just-registered', :to => :itself do
        Date.today > 1.year.ago.to_date
      end

      # Example rule for multiple badge granting
      grant_on 'users#index', :badge => 'gossip', :multiple => true

      # 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 at least 10 votes, grant relevant-commenter badge
      grant_on 'comments#vote', :badge => 'relevant-commenter', :to => :user do |comment|
        comment.votes >= 10
      end

      # Changes his name by one wider than 4 chars (arbitrary ruby code and custom model_name)
      # This badge is temporary (user may lose it)
      grant_on 'registrations#update', :badge => 'autobiographer', :temporary => true, :model_name => 'User' do |user|
        user.name.length > 4
      end
    end
  end
end

Version data entries

32 entries across 30 versions & 1 rubygems

Version Path
merit-1.7.1 test/dummy-mongoid/app/models/merit/badge_rules.rb
merit-1.7.0 test/dummy-mongoid/app/models/merit/badge_rules.rb
merit-1.6.2 test/dummy-mongoid/app/models/merit/badge_rules.rb
merit-1.6.1 test/dummy-mongoid/app/models/merit/badge_rules.rb
merit-1.6.0 test/dummy-mongoid/app/models/merit/badge_rules.rb
merit-1.5.0 test/dummy-mongoid/app/models/merit/badge_rules.rb
merit-1.4.0 test/dummy-mongoid/app/models/merit/badge_rules.rb
merit-1.3.1 test/dummy-mongoid/app/models/merit/badge_rules.rb
merit-1.3.0 test/dummy-mongoid/app/models/merit/badge_rules.rb
merit-1.2.3 test/dummy-mongoid/app/models/merit/badge_rules.rb
merit-1.2.2 test/dummy-mongoid/app/models/merit/badge_rules.rb
merit-1.2.1 test/dummy-mongoid/app/models/merit/badge_rules.rb
merit-1.2.0 test/dummy-mongoid/app/models/merit/badge_rules.rb
merit-1.1.2 test/dummy-mongoid/app/models/merit/badge_rules.rb
merit-1.1.1 test/dummy-mongoid/app/models/merit/badge_rules.rb
merit-1.1.0 test/dummy-mongoid/app/models/merit/badge_rules.rb
merit-1.0.1 test/dummy-mongoid/app/models/merit/badge_rules.rb
merit-1.0.0 test/dummy-mongoid/app/models/merit/badge_rules.rb
merit-0.10.1 test/dummy-mongoid/app/models/merit/badge_rules.rb
merit-0.10.0 test/dummy-mongoid/app/models/merit/badge_rules.rb