Skip to content

Commit

Permalink
Agrega talent_rules a generators. Admite grant_badges sin bloque (gra…
Browse files Browse the repository at this point in the history
…nts always).
  • Loading branch information
tute committed May 14, 2011
1 parent 529891d commit d12ddfa
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 33 deletions.
12 changes: 6 additions & 6 deletions .gitignore
Expand Up @@ -2,11 +2,11 @@
.bundle/
log/*.log
pkg/
test/dummy/app/models/badge.rb
test/dummy/app/models/badges_user.rb
test/dummy/db/*.sqlite3
test/dummy/db/migrate/*_create_talent_actions.rb
test/dummy/db/migrate/*_create_badges.rb
test/dummy/db/migrate/*_talent_create_badges_users.rb
# test/dummy/app/models/badge.rb
# test/dummy/app/models/badges_user.rb
# test/dummy/db/*.sqlite3
# test/dummy/db/migrate/*_create_talent_actions.rb
# test/dummy/db/migrate/*_create_badges.rb
# test/dummy/db/migrate/*_talent_create_badges_users.rb
test/dummy/log/*.log
test/dummy/tmp/
11 changes: 8 additions & 3 deletions README.rdoc
Expand Up @@ -6,6 +6,7 @@
# Run 'rails g talent:install'
# Run 'rails g talent MODEL_NAME'
# Add 'grant_badges' to controllers where you want this behavior triggered.
# Configure rules to grant badges in 'app/models/talent_rules.rb'
# Run 'rake db:migrate'


Expand All @@ -22,8 +23,12 @@ To run the test application inside this gem follow:

== To-do list

* Agregar talent_rules.rb a generators. Puede haber más de un modelo "talentable"?
* Unificar generators en 'rails g talent MODEL_NAME'.
* Agregar método comments#vote, y entregar medallas al dueño del comentario.
* grant_on 'users#create' quien ejecuta esta acción lleva la medalla. Es un
ejemplo para chequear en el método del controlador (para obtener el id
generado).
* On vote, el dueño del comentario votado se lleva la medalla (no quien vota).
* Puede haber más de un modelo "talentable" (talent_rules.rb)?
* Poder avisar cuando hay nuevas medallas (como un flag de notificado).
* Poder meter la lógica de si aplica una medalla en un método del controlador
(más bajo nivel).
(más bajo nivel).
1 change: 1 addition & 0 deletions lib/generators/talent/install_generator.rb
Expand Up @@ -20,6 +20,7 @@ def copy_migrations_and_model
migration_template 'create_talent_actions.rb', 'db/migrate/create_talent_actions.rb'
migration_template 'create_badges.rb', 'db/migrate/create_badges.rb'
template 'badge.rb', 'app/models/badge.rb'
template 'talent_rules.rb', 'app/models/talent_rules.rb'
end
end
end
Expand Down
22 changes: 22 additions & 0 deletions lib/generators/talent/templates/talent_rules.rb
@@ -0,0 +1,22 @@
class TalentRules
include Talent::Rules

def initialize(user)
user ||= User.new

# If it creates user, grant badge
# grant_on 'users#create', :badge => 'just', :level => 'registered'

# If it has 10 comments, grant commenter-10 badge
# grant_on 'comments#create', :badge => 'commenter', :level => 10 do
# user.comments.count == 10
# end

# If it has 20 comments, grant commenter-20 badge
# grant_on 'comments#create', :badge => 'commenter', :level => 20 do
# user.comments.count == 20
# end

check_new_actions # FIXME: Should be called somewhere else?
end
end
3 changes: 2 additions & 1 deletion lib/talent/rules.rb
Expand Up @@ -25,7 +25,8 @@ def check_rules(action)
Rails.logger.warn "TALENT: Checking #{talent_rules[action_name].count} rules for #{action_name}..."
user = User.find(action.user_id)
badge = Badge.where(:name => rule[:badge], :level => rule[:level]).first
if rule[:block].call && !user.badges.include?(badge)
# Grant if no block given, or it evaluates to true
if (rule[:block].nil? || rule[:block].call) && !user.badges.include?(badge)
user.badges << badge
user.save
Rails.logger.warn "TALENT: Granted badge #{badge.name}-#{badge.level} to #{user.name}!"
Expand Down
23 changes: 0 additions & 23 deletions test/dummy/app/models/talent_rules.rb

This file was deleted.

0 comments on commit d12ddfa

Please sign in to comment.