Permalink
Browse files

Initial commit.

  • Loading branch information...
0 parents commit af349a02b7b7403a360d688bc3260fa3fd4c9c4e @tute tute committed Apr 21, 2011
Showing with 10,556 additions and 0 deletions.
  1. +7 −0 .gitignore
  2. +4 −0 Gemfile
  3. +73 −0 Gemfile.lock
  4. +26 −0 README.rdoc
  5. +29 −0 Rakefile
  6. +2 −0 app/models/badge.rb
  7. +93 −0 ideas.rb
  8. +26 −0 lib/generators/talent/install_generator.rb
  9. +19 −0 lib/generators/talent/templates/create_badges.rb
  10. +11 −0 lib/generators/talent/templates/create_badges_users.rb
  11. +16 −0 lib/generators/talent/templates/create_talent_actions.rb
  12. +7 −0 lib/talent.rb
  13. +30 −0 lib/talent/controller_additions.rb
  14. +7 −0 lib/talent/railtie.rb
  15. +51 −0 lib/talent/rules.rb
  16. +9 −0 talent.gemspec
  17. +7 −0 test/dummy/Rakefile
  18. +7 −0 test/dummy/app/controllers/application_controller.rb
  19. +85 −0 test/dummy/app/controllers/comments_controller.rb
  20. +85 −0 test/dummy/app/controllers/users_controller.rb
  21. +2 −0 test/dummy/app/helpers/application_helper.rb
  22. +3 −0 test/dummy/app/models/badge.rb
  23. +3 −0 test/dummy/app/models/comment.rb
  24. +2 −0 test/dummy/app/models/talent_action.rb
  25. +17 −0 test/dummy/app/models/talent_rules.rb
  26. +4 −0 test/dummy/app/models/user.rb
  27. +29 −0 test/dummy/app/views/comments/_form.html.erb
  28. +6 −0 test/dummy/app/views/comments/edit.html.erb
  29. +27 −0 test/dummy/app/views/comments/index.html.erb
  30. +5 −0 test/dummy/app/views/comments/new.html.erb
  31. +20 −0 test/dummy/app/views/comments/show.html.erb
  32. +14 −0 test/dummy/app/views/layouts/application.html.erb
  33. +21 −0 test/dummy/app/views/users/_form.html.erb
  34. +6 −0 test/dummy/app/views/users/edit.html.erb
  35. +23 −0 test/dummy/app/views/users/index.html.erb
  36. +5 −0 test/dummy/app/views/users/new.html.erb
  37. +20 −0 test/dummy/app/views/users/show.html.erb
  38. +4 −0 test/dummy/config.ru
  39. +24 −0 test/dummy/config/application.rb
  40. +10 −0 test/dummy/config/boot.rb
  41. +22 −0 test/dummy/config/database.yml
  42. +5 −0 test/dummy/config/environment.rb
  43. +26 −0 test/dummy/config/environments/development.rb
  44. +49 −0 test/dummy/config/environments/production.rb
  45. +35 −0 test/dummy/config/environments/test.rb
  46. +7 −0 test/dummy/config/initializers/backtrace_silencers.rb
  47. +10 −0 test/dummy/config/initializers/inflections.rb
  48. +5 −0 test/dummy/config/initializers/mime_types.rb
  49. +7 −0 test/dummy/config/initializers/secret_token.rb
  50. +8 −0 test/dummy/config/initializers/session_store.rb
  51. +5 −0 test/dummy/config/locales/en.yml
  52. +5 −0 test/dummy/config/routes.rb
  53. +12 −0 test/dummy/db/migrate/20110421191249_create_users.rb
  54. +15 −0 test/dummy/db/migrate/20110421191250_create_comments.rb
  55. +53 −0 test/dummy/db/schema.rb
  56. +16 −0 test/dummy/db/seeds.rb
  57. +26 −0 test/dummy/public/404.html
  58. +26 −0 test/dummy/public/422.html
  59. +26 −0 test/dummy/public/500.html
  60. 0 test/dummy/public/favicon.ico
  61. +2 −0 test/dummy/public/javascripts/application.js
  62. +965 −0 test/dummy/public/javascripts/controls.js
  63. +974 −0 test/dummy/public/javascripts/dragdrop.js
  64. +1,123 −0 test/dummy/public/javascripts/effects.js
  65. +6,001 −0 test/dummy/public/javascripts/prototype.js
  66. +191 −0 test/dummy/public/javascripts/rails.js
  67. 0 test/dummy/public/stylesheets/.gitkeep
  68. +56 −0 test/dummy/public/stylesheets/scaffold.css
  69. +6 −0 test/dummy/script/rails
  70. +7 −0 test/integration/navigation_test.rb
  71. +5 −0 test/support/integration_case.rb
  72. +7 −0 test/talent_test.rb
  73. +22 −0 test/test_helper.rb
@@ -0,0 +1,7 @@
+.DS_Store
+.bundle/
+log/*.log
+pkg/
+test/dummy/db/*.sqlite3
+test/dummy/log/*.log
+test/dummy/tmp/
@@ -0,0 +1,4 @@
+source "http://rubygems.org"
+
+gem "rails", "3.0.7"
+gem "sqlite3"
@@ -0,0 +1,73 @@
+GEM
+ remote: http://rubygems.org/
+ specs:
+ abstract (1.0.0)
+ actionmailer (3.0.7)
+ actionpack (= 3.0.7)
+ mail (~> 2.2.15)
+ actionpack (3.0.7)
+ activemodel (= 3.0.7)
+ activesupport (= 3.0.7)
+ builder (~> 2.1.2)
+ erubis (~> 2.6.6)
+ i18n (~> 0.5.0)
+ rack (~> 1.2.1)
+ rack-mount (~> 0.6.14)
+ rack-test (~> 0.5.7)
+ tzinfo (~> 0.3.23)
+ activemodel (3.0.7)
+ activesupport (= 3.0.7)
+ builder (~> 2.1.2)
+ i18n (~> 0.5.0)
+ activerecord (3.0.7)
+ activemodel (= 3.0.7)
+ activesupport (= 3.0.7)
+ arel (~> 2.0.2)
+ tzinfo (~> 0.3.23)
+ activeresource (3.0.7)
+ activemodel (= 3.0.7)
+ activesupport (= 3.0.7)
+ activesupport (3.0.7)
+ arel (2.0.9)
+ builder (2.1.2)
+ erubis (2.6.6)
+ abstract (>= 1.0.0)
+ i18n (0.5.0)
+ mail (2.2.17)
+ activesupport (>= 2.3.6)
+ i18n (>= 0.4.0)
+ mime-types (~> 1.16)
+ treetop (~> 1.4.8)
+ mime-types (1.16)
+ polyglot (0.3.1)
+ rack (1.2.2)
+ rack-mount (0.6.14)
+ rack (>= 1.0.0)
+ rack-test (0.5.7)
+ rack (>= 1.0)
+ rails (3.0.7)
+ actionmailer (= 3.0.7)
+ actionpack (= 3.0.7)
+ activerecord (= 3.0.7)
+ activeresource (= 3.0.7)
+ activesupport (= 3.0.7)
+ bundler (~> 1.0)
+ railties (= 3.0.7)
+ railties (3.0.7)
+ actionpack (= 3.0.7)
+ activesupport (= 3.0.7)
+ rake (>= 0.8.7)
+ thor (~> 0.14.4)
+ rake (0.8.7)
+ sqlite3 (1.3.3)
+ thor (0.14.6)
+ treetop (1.4.9)
+ polyglot (>= 0.3.1)
+ tzinfo (0.3.26)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ rails (= 3.0.7)
+ sqlite3
@@ -0,0 +1,26 @@
+= Talent
+
+== Installation
+
+# Add 'talent' to your Gemfile.
+# Run 'rails g badges:install'
+# Add 'grant_badges' to controllers where you want this behavior triggered.
+# Run 'rake db:migrate'
+
+
+== Test application
+
+To run the test application inside this gem follow:
+
+# cd test/dummy
+# rails g talent:install
+# rake db:drop ; rake db:create ; rake db:migrate ; rake db:seed
+# rails s
+
+
+== To-do list
+
+* Persistir last_action_checked (se resetea!).
+* Add has_and_belongs_to_many :badges to related models.
+* Add talent_action model?
+* Migration should accept any model (not just 'user').
@@ -0,0 +1,29 @@
+# encoding: UTF-8
+require 'rubygems'
+begin
+ require 'bundler/setup'
+rescue LoadError
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
+end
+
+require 'rake'
+require 'rake/rdoctask'
+
+require 'rake/testtask'
+
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'lib'
+ t.libs << 'test'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = false
+end
+
+task :default => :test
+
+Rake::RDocTask.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'Talent'
+ rdoc.options << '--line-numbers' << '--inline-source'
+ rdoc.rdoc_files.include('README.rdoc')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
@@ -0,0 +1,2 @@
+class Badge < ActiveRecord::Base
+end
@@ -0,0 +1,93 @@
+# 1- Ir llenando una tabla "acciones" del estilo de:
+# _______________________________________________
+# source (user_id) | action (method, value) | target (model, id)
+# 1 | comment nil | List 8
+# 1 | vote 3 | List 12
+# 3 | follow nil | User 1
+# X | create nil | User nil # se registra un usuario, X es el id generado
+# _______________________________________________
+#
+# Se define las acciones a trackear en los controladores, por ejemplo:
+# class UsersController < ApplicationController
+# grant_badges :only => %w(create follow)
+# end
+#
+# La gema también crearía una tabla Badges, más o menos como sigue:
+# ___________________
+# id | name | level | image
+# 1 | creador | inspirado | creador-inspirado.png
+# 2 | creador | blogger | creador-blogger.png
+# 2 | creador | best-seller | creador-bestseller.png
+# ___________________
+#
+#
+# 2- La interfaz para especificar desde la aplicación cuándo aplicar cada badge
+# sería un archivo tipo el ability.rb de cancan, que estaría en
+# app/models/talent.rb, con una sintaxis tipo:
+#
+# grant_on 'users#create', :badge => 'just', :level => 'registered' do
+# # Code block
+# end
+#
+# Donde el procedimiento es un método que chequea la lógica necesaria para dar
+# ese badge, y evalúa a boolean. Obviamente tiene que saber de qué objetos
+# estamos hablando (qué user y qué lista, por ejemplo), cabo que por ahora no
+# queda atado.
+#
+#
+# 3- Luego, podemos ir chequeando esas condiciones sincronizadamente, o
+# mediante un proceso en background, por ejemplo cada 5 minutos, guardando
+# hasta qué entrada de la primera tabla se procesó la vez anterior.
+
+
+# ========== Posible sintaxis ==========
+
+# app/models/talent.rb (onda cancan)
+class Talent
+ include Talent::Badges
+
+ def initialize(user)
+ user ||= User.new
+
+ grant_on 'users#create', :badge => 'just', :level => 'registered' do
+ true
+ end
+
+ grant_on 'comments#create', :badge => 'commenter', :level => 10 do
+ current_user.comments.count == 10
+ end
+ end
+end
+
+# app/controllers/users_controller.rb
+class UsersController < ApplicationController
+ grant_badges :only => %w(index create edit)
+end
+
+
+# ========== Tests ==========
+u = User.first
+u.badges
+c = Comment.new{ :user_id = u.id }
+c.save
+u.badges
+
+u = User.first
+u.badges
+TalentRules.new(u) # Not loading all rules (?)
+include Talent::Rules
+check_last_actions
+u.badges
+
+
+# ========== URLs ==========
+# http://www.cowboycoded.com/2011/01/31/developing-is_able-or-acts_as-plugins-for-rails/
+# http://www.stubbleblog.com/index.php/2011/04/writing-rails-engines-getting-started/
+# https://gist.github.com/af7e572c2dc973add221
+# http://www.themodestrubyist.com/2010/03/01/rails-3-plugins---part-1---the-big-picture/
+# http://www.themodestrubyist.com/2010/03/05/rails-3-plugins---part-2---writing-an-engine/
+# http://www.themodestrubyist.com/2010/03/16/rails-3-plugins---part-3---rake-tasks-generators-initializers-oh-my/
+# https://github.com/krschacht/rails_3_engine_demo
+
+https://github.com/dcadenas/state_pattern
+https://github.com/rubyist/aasm
@@ -0,0 +1,26 @@
+require 'rails/generators/migration'
+
+module Talent
+ module Generators
+ class InstallGenerator < ::Rails::Generators::Base
+ include Rails::Generators::Migration
+ source_root File.expand_path('../templates', __FILE__)
+ desc "add the migrations"
+
+ def self.next_migration_number(path)
+ unless @prev_migration_nr
+ @prev_migration_nr = Time.now.utc.strftime("%Y%m%d%H%M%S").to_i
+ else
+ @prev_migration_nr += 1
+ end
+ @prev_migration_nr.to_s
+ end
+
+ def copy_migrations
+ migration_template 'create_talent_actions.rb', 'db/migrate/create_talent_actions.rb'
+ migration_template 'create_badges.rb', 'db/migrate/create_badges.rb'
+ migration_template 'create_badges_users.rb', 'db/migrate/create_badges_users.rb'
+ end
+ end
+ end
+end
@@ -0,0 +1,19 @@
+class CreateBadges < ActiveRecord::Migration
+ def self.up
+ create_table :badges do |t|
+ t.string :name
+ t.string :level
+ t.string :image
+ t.string :description
+ t.timestamps
+ end
+
+ # First badge
+ Badge.create( :name => 'just', :level => 'registered' ) # just-did-something
+ Badge.create( :name => 'creator', :level => 'inspired', :image => 'http://upload.wikimedia.org/wikipedia/commons/9/94/Luca_prodan.jpg' )
+ end
+
+ def self.down
+ drop_table :badges
+ end
+end
@@ -0,0 +1,11 @@
+class CreateBadgesUsers < ActiveRecord::Migration
+ def self.up
+ create_table :badges_users, :id => false do |t|
+ t.integer :badge_id, :user_id
+ end
+ end
+
+ def self.down
+ drop_table :badges_users
+ end
+end
@@ -0,0 +1,16 @@
+class CreateTalentActions < ActiveRecord::Migration
+ def self.up
+ create_table :talent_actions do |t|
+ t.integer :user_id # source
+ t.string :action_method
+ t.integer :action_value
+ t.string :target_model
+ t.integer :target_id
+ t.timestamps
+ end
+ end
+
+ def self.down
+ drop_table :talent_actions
+ end
+end
@@ -0,0 +1,7 @@
+require 'talent/controller_additions'
+require 'talent/rules'
+
+module Talent
+ class Engine < Rails::Engine
+ end
+end
@@ -0,0 +1,30 @@
+module Talent
+ # This module is automatically included into all controllers.
+ module ControllerMethods
+ # Sets up a before filter to update talent_actions table
+ #
+ # class UsersController < ApplicationController
+ # grant_badges :only => %w(create follow)
+ # end
+ #
+ def grant_badges(*args)
+ # Initialize rules
+ ::TalentRules.new(current_user) # FIXME: debería ser Singleton?
+
+ options = args.extract_options!
+ self.before_filter(options) do |controller|
+ TalentAction.create(
+ :user_id => ApplicationController.current_user.try(:id),
+ :action_method => action_name,
+ :action_value => nil, # FIXME: Aún no implementado
+ :target_model => controller.controller_name,
+ :target_id => controller.params[:id]
+ )
+ end
+ end
+ end
+end
+
+if defined? ActionController
+ ActionController::Base.extend Talent::ControllerMethods
+end
@@ -0,0 +1,7 @@
+require 'talent'
+require 'rails'
+
+module Talent
+ class Engine < Rails::Engine
+ end
+end
Oops, something went wrong.

0 comments on commit af349a0

Please sign in to comment.