diff --git a/db/migrate/20150330102818_create_badges.rb b/db/migrate/20150330102818_create_badges.rb new file mode 100644 index 0000000..7ed30ea --- /dev/null +++ b/db/migrate/20150330102818_create_badges.rb @@ -0,0 +1,12 @@ +class CreateBadges < ActiveRecord::Migration + def change + create_table :gamification_plugin_badges do |t| + t.string :name + t.integer :level + t.string :description + t.string :custom_fields + t.references :owner, :polymorphic => true + t.timestamps + end + end +end diff --git a/lib/ext/environment.rb b/lib/ext/environment.rb new file mode 100644 index 0000000..f1fb8fb --- /dev/null +++ b/lib/ext/environment.rb @@ -0,0 +1,7 @@ +require_dependency 'environment' + +class Environment + + has_many :gamification_plugin_badges, :class_name => 'GamificationPlugin::Badge', :foreign_key => 'owner_id' + +end diff --git a/lib/gamification_plugin.rb b/lib/gamification_plugin.rb index ca08b43..6f9329f 100644 --- a/lib/gamification_plugin.rb +++ b/lib/gamification_plugin.rb @@ -37,27 +37,4 @@ class GamificationPlugin < Noosfero::Plugin require 'merit_ext' - Merit::Badge.create!( - id: 1, - name: "comment_author", - description: "Commenter" - ) - Merit::Badge.create!( - id: 2, - name: "relevant_commenter", - description: "Relevant Commenter" - ) - Merit::Badge.create!( - id: 3, - name: "article_author", - description: "Article Creator", - level: 1 - ) - Merit::Badge.create!( - id: 4, - name: "article_author", - description: "Article Creator", - level: 2 - ) - end diff --git a/lib/gamification_plugin/badge.rb b/lib/gamification_plugin/badge.rb new file mode 100644 index 0000000..d80fa5e --- /dev/null +++ b/lib/gamification_plugin/badge.rb @@ -0,0 +1,9 @@ +class GamificationPlugin::Badge < Noosfero::Plugin::ActiveRecord + + belongs_to :owner, :polymorphic => true + + attr_accessible :owner, :name, :description, :level, :custom_fields + + serialize :custom_fields + +end diff --git a/lib/merit/badge_rules.rb b/lib/merit/badge_rules.rb index ff3b9c4..5bf5d1c 100644 --- a/lib/merit/badge_rules.rb +++ b/lib/merit/badge_rules.rb @@ -41,22 +41,19 @@ module Merit def initialize(environment=nil) return if environment.nil? @environment = environment + # FIXME avoid this + Merit::Badge.all.each { |badge| badge.destroy } + + GamificationPlugin::Badge.all.each do |badge| + # FIXME avoid this + Merit::Badge.create!(:name => badge.name, :id => badge.id, :level => badge.level) # FIXME conflict with multitenancy? - Merit::Badge.all.each do |badge| setting = AVAILABLE_RULES[badge.name.to_sym] - grant_on setting[:action], :badge => badge.name do |source| - setting[:value].call(source) >= setting[:default_threshold] + grant_on setting[:action], :badge => badge.name, :level => badge.level do |source| + setting[:value].call(source) >= (badge.custom_fields || {}).fetch(:threshold, setting[:default_threshold]) end end - grant_on 'article#create', badge: 'article_author', level: 1 do |article| - article.author.present? && article.author.articles.count >= 5 - end - - grant_on 'article#create', badge: 'article_author', level: 2 do |article| - article.author.present? && article.author.articles.count >= 10 - end - grant_on 'vote_plugin_profile#vote', badge: 'relevant-commenter', model_name: 'comment', to: 'author' do |voteable| return false if voteable.nil? || !voteable.kind_of?(Comment) voteable.votes.count >= 2 diff --git a/test/functional/gamification_plugin_profile_controller_test.rb b/test/functional/gamification_plugin_profile_controller_test.rb index d1016dd..90b3553 100644 --- a/test/functional/gamification_plugin_profile_controller_test.rb +++ b/test/functional/gamification_plugin_profile_controller_test.rb @@ -5,10 +5,11 @@ class GamificationPluginProfileControllerTest < ActionController::TestCase def setup @profile = fast_create(Profile) @person = create_user('person').person + @environment = Environment.default login_as(@person.identifier) end - attr_accessor :profile, :person + attr_accessor :profile, :person, :environment should 'display points in gamification info page' do person.add_points(20, :category => :comment_author) @@ -26,8 +27,12 @@ class GamificationPluginProfileControllerTest < ActionController::TestCase end should 'display person badges' do - person.add_badge(1) - person.add_badge(2) + badge1 = GamificationPlugin::Badge.create!(:owner => environment, :name => 'article_author', :level => 1) + badge2 = GamificationPlugin::Badge.create!(:owner => environment, :name => 'article_author', :level => 2, :custom_fields => {:threshold => 10}) + GamificationPlugin.gamification_set_rules(environment) + + person.add_badge(badge1.id) + person.add_badge(badge2.id) get :info, :profile => profile.identifier assert_select '.badges .badge-list .badge', 2 end diff --git a/test/unit/article_test.rb b/test/unit/article_test.rb index 7d50014..4fc71c1 100644 --- a/test/unit/article_test.rb +++ b/test/unit/article_test.rb @@ -4,10 +4,11 @@ class ArticleTest < ActiveSupport::TestCase def setup @person = create_user('testuser').person - GamificationPlugin.gamification_set_rules(Environment.default) + @environment = Environment.default + GamificationPlugin.gamification_set_rules(@environment) end - attr_accessor :person + attr_accessor :person, :environment should 'add merit points to author when create a new article' do create(Article, :profile_id => person.id, :author => person) @@ -23,12 +24,19 @@ class ArticleTest < ActiveSupport::TestCase end should 'add merit badge to author when create 5 new articles' do + GamificationPlugin::Badge.create!(:owner => environment, :name => 'article_author', :level => 1) + GamificationPlugin.gamification_set_rules(environment) + 5.times { create(Article, :profile_id => person.id, :author => person) } assert_equal 'article_author', person.badges.first.name assert_equal 1, person.badges.first.level end should 'add merit badge level 2 to author when create 10 new articles' do + GamificationPlugin::Badge.create!(:owner => environment, :name => 'article_author', :level => 1) + GamificationPlugin::Badge.create!(:owner => environment, :name => 'article_author', :level => 2, :custom_fields => {:threshold => 10}) + GamificationPlugin.gamification_set_rules(environment) + 10.times { create(Article, :profile_id => person.id, :author => person) } assert_equal ['article_author'], person.badges.map(&:name).uniq assert_equal [1, 2], person.badges.map(&:level) diff --git a/test/unit/comment_test.rb b/test/unit/comment_test.rb index a47e5dc..6e68457 100644 --- a/test/unit/comment_test.rb +++ b/test/unit/comment_test.rb @@ -5,9 +5,10 @@ class CommentTest < ActiveSupport::TestCase def setup @person = create_user('testuser').person @article = create(TextileArticle, :profile_id => person.id) - GamificationPlugin.gamification_set_rules(Environment.default) + @environment = Environment.default + GamificationPlugin.gamification_set_rules(@environment) end - attr_accessor :person, :article + attr_accessor :person, :article, :environment should 'add merit points to author when create a new comment' do create(Comment, :source => article, :author_id => person.id) @@ -23,6 +24,9 @@ class CommentTest < ActiveSupport::TestCase end should 'add merit badge to author when create 5 new comments' do + GamificationPlugin::Badge.create!(:owner => environment, :name => 'comment_author') + GamificationPlugin.gamification_set_rules(environment) + 5.times { create(Comment, :source => article, :author_id => person.id) } assert_equal 'comment_author', person.badges.first.name end -- libgit2 0.21.2