merit 1.7.1 → 1.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (101) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +6 -1
  3. data/Gemfile +7 -0
  4. data/README.md +31 -4
  5. data/app/models/merit/action.rb +2 -3
  6. data/app/models/merit/badge.rb +2 -2
  7. data/lib/generators/active_record/install_generator.rb +1 -1
  8. data/lib/generators/active_record/merit_generator.rb +2 -2
  9. data/lib/generators/active_record/remove_generator.rb +2 -2
  10. data/lib/generators/active_record/templates/create_badges_sashes.rb +1 -1
  11. data/lib/generators/active_record/templates/create_merit_actions.rb +3 -3
  12. data/lib/generators/active_record/templates/create_scores_and_points.rb +2 -2
  13. data/lib/generators/active_record/templates/remove_merit_tables.rb +1 -1
  14. data/lib/generators/merit/install_generator.rb +1 -1
  15. data/lib/generators/merit/merit_generator.rb +2 -3
  16. data/lib/generators/merit/remove_generator.rb +3 -3
  17. data/lib/generators/merit/templates/merit.rb +10 -4
  18. data/lib/merit.rb +43 -15
  19. data/lib/merit/base_target_finder.rb +1 -3
  20. data/lib/merit/judge.rb +26 -6
  21. data/lib/merit/model_additions.rb +10 -17
  22. data/lib/merit/models/active_record/merit/action.rb +1 -1
  23. data/lib/merit/models/active_record/merit/activity_log.rb +1 -0
  24. data/lib/merit/models/active_record/merit/badges_sash.rb +6 -10
  25. data/lib/merit/models/active_record/merit/sash.rb +2 -38
  26. data/lib/merit/models/active_record/merit/score.rb +6 -5
  27. data/lib/merit/models/base/merit/badges_sash.rb +15 -0
  28. data/lib/merit/models/base/merit/sash.rb +42 -0
  29. data/lib/merit/models/mongoid/merit/action.rb +8 -6
  30. data/lib/merit/models/mongoid/merit/activity_log.rb +11 -0
  31. data/lib/merit/models/mongoid/merit/badges_sash.rb +19 -0
  32. data/lib/merit/models/mongoid/merit/sash.rb +28 -0
  33. data/lib/merit/models/mongoid/merit/score.rb +41 -0
  34. data/lib/merit/reputation_change_observer.rb +11 -0
  35. data/lib/merit/rule.rb +1 -1
  36. data/lib/merit/rules_matcher.rb +0 -2
  37. data/lib/merit/rules_rank_methods.rb +1 -1
  38. data/lib/merit/target_finder.rb +1 -2
  39. data/merit.gemspec +2 -1
  40. data/test/dummy/app/models/comment.rb +16 -1
  41. data/test/dummy/app/models/dummy_observer.rb +3 -0
  42. data/test/dummy/app/models/user.rb +14 -1
  43. data/test/dummy/config/initializers/merit.rb +3 -4
  44. data/test/dummy/config/mongoid.yml +13 -0
  45. data/test/integration/navigation_test.rb +19 -2
  46. data/test/unit/merit_unit_test.rb +1 -1
  47. metadata +25 -56
  48. data/lib/merit/models/mongoid/sash.rb +0 -14
  49. data/lib/merit/observer.rb +0 -13
  50. data/test/dummy-mongoid/Rakefile +0 -7
  51. data/test/dummy-mongoid/app/controllers/application_controller.rb +0 -7
  52. data/test/dummy-mongoid/app/controllers/comments_controller.rb +0 -90
  53. data/test/dummy-mongoid/app/controllers/registrations_controller.rb +0 -15
  54. data/test/dummy-mongoid/app/controllers/users_controller.rb +0 -67
  55. data/test/dummy-mongoid/app/helpers/application_helper.rb +0 -2
  56. data/test/dummy-mongoid/app/models/comment.rb +0 -13
  57. data/test/dummy-mongoid/app/models/merit/badge_rules.rb +0 -49
  58. data/test/dummy-mongoid/app/models/merit/point_rules.rb +0 -20
  59. data/test/dummy-mongoid/app/models/merit/rank_rules.rb +0 -24
  60. data/test/dummy-mongoid/app/models/user.rb +0 -25
  61. data/test/dummy-mongoid/app/views/comments/_form.html.erb +0 -29
  62. data/test/dummy-mongoid/app/views/comments/edit.html.erb +0 -6
  63. data/test/dummy-mongoid/app/views/comments/index.html.erb +0 -35
  64. data/test/dummy-mongoid/app/views/comments/new.html.erb +0 -5
  65. data/test/dummy-mongoid/app/views/comments/show.html.erb +0 -23
  66. data/test/dummy-mongoid/app/views/layouts/application.html.erb +0 -24
  67. data/test/dummy-mongoid/app/views/users/_form.html.erb +0 -22
  68. data/test/dummy-mongoid/app/views/users/edit.html.erb +0 -6
  69. data/test/dummy-mongoid/app/views/users/index.html.erb +0 -26
  70. data/test/dummy-mongoid/app/views/users/new.html.erb +0 -5
  71. data/test/dummy-mongoid/app/views/users/show.html.erb +0 -18
  72. data/test/dummy-mongoid/config.ru +0 -4
  73. data/test/dummy-mongoid/config/application.rb +0 -22
  74. data/test/dummy-mongoid/config/boot.rb +0 -10
  75. data/test/dummy-mongoid/config/environment.rb +0 -5
  76. data/test/dummy-mongoid/config/environments/development.rb +0 -25
  77. data/test/dummy-mongoid/config/environments/production.rb +0 -49
  78. data/test/dummy-mongoid/config/environments/test.rb +0 -35
  79. data/test/dummy-mongoid/config/initializers/backtrace_silencers.rb +0 -7
  80. data/test/dummy-mongoid/config/initializers/inflections.rb +0 -10
  81. data/test/dummy-mongoid/config/initializers/merit.rb +0 -37
  82. data/test/dummy-mongoid/config/initializers/mime_types.rb +0 -5
  83. data/test/dummy-mongoid/config/initializers/secret_token.rb +0 -7
  84. data/test/dummy-mongoid/config/initializers/session_store.rb +0 -8
  85. data/test/dummy-mongoid/config/locales/en.yml +0 -5
  86. data/test/dummy-mongoid/config/mongoid.yml +0 -14
  87. data/test/dummy-mongoid/config/routes.rb +0 -9
  88. data/test/dummy-mongoid/db/seeds.rb +0 -17
  89. data/test/dummy-mongoid/public/404.html +0 -26
  90. data/test/dummy-mongoid/public/422.html +0 -26
  91. data/test/dummy-mongoid/public/500.html +0 -26
  92. data/test/dummy-mongoid/public/favicon.ico +0 -0
  93. data/test/dummy-mongoid/public/javascripts/application.js +0 -2
  94. data/test/dummy-mongoid/public/javascripts/controls.js +0 -965
  95. data/test/dummy-mongoid/public/javascripts/dragdrop.js +0 -974
  96. data/test/dummy-mongoid/public/javascripts/effects.js +0 -1123
  97. data/test/dummy-mongoid/public/javascripts/prototype.js +0 -6001
  98. data/test/dummy-mongoid/public/javascripts/rails.js +0 -191
  99. data/test/dummy-mongoid/public/stylesheets/.gitkeep +0 -0
  100. data/test/dummy-mongoid/public/stylesheets/scaffold.css +0 -56
  101. data/test/dummy-mongoid/script/rails +0 -6
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 123265ab30632290e284deffe94b000aab61af82
4
- data.tar.gz: 3e2dcca325352e75a7237c97f009104945af6215
3
+ metadata.gz: c2c4d73e01e95da0a10952cf3fc7da4e9d1c2685
4
+ data.tar.gz: c87f93f700d5b2fa3ef5360d39e424590fa38bc8
5
5
  SHA512:
6
- metadata.gz: ee7cb5ebfc7acf53f13d03199f3456740d2a10c171a7d7aabb6eafad7abb919ab1c0855ec00d23a5a6a248f36502358ae02b3a6b9a5a70af69aa4bbc47c45a6d
7
- data.tar.gz: 907ebc5ea903eb2977c9240fd5d2f5e9772091cff6352743520198b67b595e7187d56c974826f25bdcf2b88417cb5903229356fd5aba185d730142e472283900
6
+ metadata.gz: ecb5d0edb9dcce2e28315b4801e369ca03e799761db94ceff31402b95cb236d3a983618e0a1761dbd504d00c1825df751b48553c55a2703effe12907a873a1af
7
+ data.tar.gz: 6f18fef800b516684098b70392c3eb606793c2b7fefec4a00185e8311897b6dbb835dfae31ef8bc06cea883205096d7078b178452b03c830c34fdfb9fc5888d5
data/CHANGELOG.md CHANGED
@@ -1,9 +1,14 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.8.0
4
+
5
+ - [#128] Finishes Observer implementation so client applications can be
6
+ notified when reputation changes.
7
+ - [#123] Work towards mongoid support. Still work in progress.
8
+
3
9
  ## 1.7.1
4
10
 
5
11
  - [#121] Adds "Uninstall Merit" rake task.
6
12
  - [#124] Thread safe configuration instance variable.
7
13
  - [#133] Fixes Rails 4 + protected_attributes bug.
8
14
  - Starts this Changelog!
9
- - General code, tests and README improvements.
data/Gemfile CHANGED
@@ -18,6 +18,13 @@ end
18
18
 
19
19
  gem 'rails', rails
20
20
 
21
+ case ENV['ORM']
22
+ when 'active_record'
23
+ gem 'activerecord'
24
+ when 'mongoid'
25
+ gem 'mongoid', '3.0.10'
26
+ end
27
+
21
28
  group :development, :test do
22
29
  gem 'activerecord-jdbcsqlite3-adapter', :platforms => [:jruby]
23
30
  gem 'sqlite3', :platforms => [:ruby, :mswin, :mingw]
data/README.md CHANGED
@@ -9,8 +9,6 @@ and Rankings.
9
9
 
10
10
  # Table of Contents
11
11
 
12
- - [Merit](#merit)
13
- - [Table of Contents](#table-of-contents)
14
12
  - [Installation](#installation)
15
13
  - [Badges](#badges)
16
14
  - [Creating Badges](#creating-badges)
@@ -28,6 +26,7 @@ and Rankings.
28
26
  - [Defining Rules](#defining-rules-2)
29
27
  - [Examples](#examples-2)
30
28
  - [Displaying Rankings](#displaying-rankings)
29
+ - [Getting Notifications](#getting-notifications)
31
30
  - [Uninstalling Merit](#uninstalling-merit)
32
31
  - [To-do List](#to-do-list)
33
32
 
@@ -267,6 +266,36 @@ end
267
266
  ```
268
267
 
269
268
 
269
+ # Getting Notifications
270
+
271
+ You can get observers notified any time merit changes reputation in your
272
+ application.
273
+
274
+ To do so, add your observer (to `app/models` or `app/observers`, for example):
275
+
276
+ ```ruby
277
+ # reputation_change_observer.rb
278
+ class ReputationChangeObserver
279
+ def update(changed_data)
280
+ # `changed_data[:description]` holds information on what changed
281
+ # badges granted or removed, points changed.
282
+
283
+ # `changed_data[:merit_object]` reputation related object created by merit.
284
+ # It responds to `sash_id` and `sash`. From there you can get to your
285
+ # application object that had it's reputation changed, for example:
286
+ # sash_id = changed_data[:merit_object].sash_id
287
+ # User.where(sash_id: sash_id).first
288
+
289
+ # You may use this to fill a timeline with notifications for users, send
290
+ # emails, etc.
291
+ end
292
+ end
293
+ ```
294
+ ```ruby
295
+ # In `config/initializers/merit.rb`
296
+ config.add_observer 'ReputationChangeObserver'
297
+ ```
298
+
270
299
  # Uninstalling Merit
271
300
 
272
301
  1. Run `rails d merit:install`
@@ -278,7 +307,5 @@ end
278
307
 
279
308
  # To-do List
280
309
 
281
- * Finish Observer implementation for `Judge`.
282
310
  * Move level from meritable model into Sash
283
311
  * `ActivityLog` should replace `add_points` `log` parameter
284
- * FIXMES and TODOS.
@@ -15,7 +15,7 @@ require_dependency "merit/models/#{Merit.orm}/merit/action"
15
15
  module Merit
16
16
  class Action
17
17
  def self.check_unprocessed
18
- where(processed: false).map &:check_all_rules
18
+ where(processed: false).map(&:check_all_rules)
19
19
  end
20
20
 
21
21
  # Check rules defined for a merit_action
@@ -44,12 +44,11 @@ module Merit
44
44
  # Mark merit_action as processed
45
45
  def processed!
46
46
  self.processed = true
47
- self.save
47
+ save
48
48
  end
49
49
 
50
50
  def rules_matcher
51
51
  @rules_matcher ||= ::Merit::RulesMatcher.new(target_model, action_method)
52
52
  end
53
-
54
53
  end
55
54
  end
@@ -32,7 +32,7 @@ module Merit
32
32
  badges = badges.by_level(level) unless level.nil?
33
33
  if (badge = badges.first).nil?
34
34
  str = "No badge '#{name}' found. Define it in initializers/merit.rb"
35
- raise ::Merit::BadgeNotFound, str
35
+ fail ::Merit::BadgeNotFound, str
36
36
  end
37
37
  badge
38
38
  end
@@ -48,7 +48,7 @@ module Merit
48
48
  # entries with certain badge. For instance, Badge.find(3).users
49
49
  def _define_related_entries_method(meritable_class_name)
50
50
  define_method(:"#{meritable_class_name.underscore.pluralize}") do
51
- sashes = BadgesSash.where(badge_id: self.id).pluck(:sash_id)
51
+ sashes = BadgesSash.where(badge_id: id).pluck(:sash_id)
52
52
  meritable_class_name.constantize.where(sash_id: sashes)
53
53
  end
54
54
  end
@@ -6,7 +6,7 @@ module ActiveRecord
6
6
  include Rails::Generators::Migration
7
7
 
8
8
  source_root File.expand_path('../templates', __FILE__)
9
- desc "add active_record merit migrations for the root objects"
9
+ desc 'add active_record merit migrations for the root objects'
10
10
 
11
11
  def self.next_migration_number(path)
12
12
  ActiveRecord::Generators::Base.next_migration_number(path)
@@ -6,14 +6,14 @@ module ActiveRecord
6
6
  include Rails::Generators::Migration
7
7
 
8
8
  source_root File.expand_path('../templates', __FILE__)
9
- desc "add active_record merit migrations"
9
+ desc 'add active_record merit migrations'
10
10
 
11
11
  def self.next_migration_number(path)
12
12
  ActiveRecord::Generators::Base.next_migration_number(path)
13
13
  end
14
14
 
15
15
  def copy_migrations_and_model
16
- migration_template "add_fields_to_model.rb", "db/migrate/add_fields_to_#{table_name}"
16
+ migration_template 'add_fields_to_model.rb', "db/migrate/add_fields_to_#{table_name}"
17
17
  end
18
18
  end
19
19
  end
@@ -6,7 +6,7 @@ module ActiveRecord
6
6
  include Rails::Generators::Migration
7
7
 
8
8
  source_root File.expand_path('../templates', __FILE__)
9
- desc "Creates a migration file to remove all traces of Merit on the DB"
9
+ desc 'Creates a migration file to remove all traces of Merit on the DB'
10
10
 
11
11
  def self.next_migration_number(path)
12
12
  ActiveRecord::Generators::Base.next_migration_number(path)
@@ -14,7 +14,7 @@ module ActiveRecord
14
14
 
15
15
  def copy_migrations_and_model
16
16
  migration_template 'remove_merit_tables.rb', 'db/migrate/remove_merit_tables.rb'
17
- migration_template "remove_fields_from_model.rb", "db/migrate/remove_fields_from_#{table_name}"
17
+ migration_template 'remove_fields_from_model.rb', "db/migrate/remove_fields_from_#{table_name}"
18
18
  end
19
19
  end
20
20
  end
@@ -2,7 +2,7 @@ class CreateBadgesSashes < ActiveRecord::Migration
2
2
  def self.up
3
3
  create_table :badges_sashes do |t|
4
4
  t.integer :badge_id, :sash_id
5
- t.boolean :notified_user, :default => false
5
+ t.boolean :notified_user, default: false
6
6
  t.datetime :created_at
7
7
  end
8
8
  add_index :badges_sashes, [:badge_id, :sash_id]
@@ -1,13 +1,13 @@
1
1
  class CreateMeritActions < ActiveRecord::Migration
2
2
  def self.up
3
3
  create_table :merit_actions do |t|
4
- t.integer :user_id # source
4
+ t.integer :user_id
5
5
  t.string :action_method
6
6
  t.integer :action_value
7
- t.boolean :had_errors, :default => false
7
+ t.boolean :had_errors, default: false
8
8
  t.string :target_model
9
9
  t.integer :target_id
10
- t.boolean :processed, :default => false
10
+ t.boolean :processed, default: false
11
11
  t.timestamps
12
12
  end
13
13
  end
@@ -2,12 +2,12 @@ class CreateScoresAndPoints < ActiveRecord::Migration
2
2
  def self.up
3
3
  create_table :merit_scores do |t|
4
4
  t.references :sash
5
- t.string :category, :default => 'default'
5
+ t.string :category, default: 'default'
6
6
  end
7
7
 
8
8
  create_table :merit_score_points do |t|
9
9
  t.references :score
10
- t.integer :num_points, :default => 0
10
+ t.integer :num_points, default: 0
11
11
  t.string :log
12
12
  t.datetime :created_at
13
13
  end
@@ -6,5 +6,5 @@ class RemoveMeritTables < ActiveRecord::Migration
6
6
  drop_table :sashes
7
7
  drop_table :merit_scores
8
8
  drop_table :merit_score_points
9
- end
9
+ end
10
10
  end
@@ -4,7 +4,7 @@ module Merit
4
4
  source_root File.expand_path('../templates', __FILE__)
5
5
  hook_for :orm
6
6
 
7
- desc "Copy config and rules files"
7
+ desc 'Copy config and rules files'
8
8
  def copy_migrations_and_model
9
9
  template 'merit.rb', 'config/initializers/merit.rb'
10
10
  template 'merit_badge_rules.rb', 'app/models/merit/badge_rules.rb'
@@ -1,7 +1,7 @@
1
1
  module Merit
2
2
  module Generators
3
3
  class MeritGenerator < Rails::Generators::NamedBase
4
- source_root File.expand_path("../templates", __FILE__)
4
+ source_root File.expand_path('../templates', __FILE__)
5
5
  hook_for :orm
6
6
 
7
7
  def inject_merit_content
@@ -15,9 +15,8 @@ module Merit
15
15
  end
16
16
 
17
17
  def model_path
18
- @model_path ||= File.join("app", "models", "#{file_path}.rb")
18
+ @model_path ||= File.join('app', 'models', "#{file_path}.rb")
19
19
  end
20
-
21
20
  end
22
21
  end
23
22
  end
@@ -3,15 +3,15 @@ module Merit
3
3
  class RemoveGenerator < Rails::Generators::NamedBase
4
4
  source_root File.expand_path('../templates', __FILE__)
5
5
  hook_for :orm
6
-
7
- private
6
+
7
+ private
8
8
 
9
9
  def model_exists?
10
10
  File.exists?(File.join(destination_root, model_path))
11
11
  end
12
12
 
13
13
  def model_path
14
- @model_path ||= File.join("app", "models", "#{file_path}.rb")
14
+ @model_path ||= File.join('app', 'models', "#{file_path}.rb")
15
15
  end
16
16
  end
17
17
  end
@@ -6,11 +6,17 @@ Merit.setup do |config|
6
6
  # Define ORM. Could be :active_record (default) and :mongoid
7
7
  # config.orm = :active_record
8
8
 
9
- # Define :user_model_name. This model will be used to grand badge if no :to option is given. Default is "User".
10
- # config.user_model_name = "User"
9
+ # Add application observers to get notifications when reputation changes.
10
+ # config.add_observer 'MyObserverClassName'
11
11
 
12
- # Define :current_user_method. Similar to previous option. It will be used to retrieve :user_model_name object if no :to option is given. Default is "current_#{user_model_name.downcase}".
13
- # config.current_user_method = "current_user"
12
+ # Define :user_model_name. This model will be used to grand badge if no
13
+ # `:to` option is given. Default is 'User'.
14
+ # config.user_model_name = 'User'
15
+
16
+ # Define :current_user_method. Similar to previous option. It will be used
17
+ # to retrieve :user_model_name object if no `:to` option is given. Default
18
+ # is "current_#{user_model_name.downcase}".
19
+ # config.current_user_method = 'current_user'
14
20
  end
15
21
 
16
22
  # Create application badges (uses https://github.com/norman/ambry)
data/lib/merit.rb CHANGED
@@ -6,6 +6,7 @@ require 'merit/rules_matcher'
6
6
  require 'merit/controller_extensions'
7
7
  require 'merit/model_additions'
8
8
  require 'merit/judge'
9
+ require 'merit/reputation_change_observer'
9
10
  require 'merit/sash_finder'
10
11
  require 'merit/base_target_finder'
11
12
  require 'merit/target_finder'
@@ -23,7 +24,7 @@ module Merit
23
24
 
24
25
  # # Define ORM
25
26
  def self.orm
26
- @config.orm
27
+ @config.orm || :active_record
27
28
  end
28
29
 
29
30
  # Define user_model_name
@@ -36,17 +37,33 @@ module Merit
36
37
  @config.current_user_method || "current_#{@config.user_model_name.downcase}".to_sym
37
38
  end
38
39
 
40
+ def self.observers
41
+ @config.observers
42
+ end
43
+
44
+ # @param class_name [String] The string version of observer class
45
+ def self.add_observer(class_name)
46
+ @config.add_observer(class_name)
47
+ end
48
+
39
49
  class Configuration
40
- attr_accessor :checks_on_each_request,
41
- :orm, :user_model_name, :current_user_method
50
+ attr_accessor :checks_on_each_request, :orm, :user_model_name, :observers,
51
+ :current_user_method
52
+
42
53
  def initialize
43
54
  @checks_on_each_request = true
44
55
  @orm = :active_record
45
56
  @user_model_name = 'User'
57
+ @observers = []
58
+ end
59
+
60
+ def add_observer(class_name)
61
+ @observers << class_name
46
62
  end
47
63
  end
48
64
 
49
65
  setup
66
+ add_observer('Merit::ReputationChangeObserver')
50
67
 
51
68
  class BadgeNotFound < Exception; end
52
69
  class RankAttributeNotDefined < Exception; end
@@ -55,20 +72,13 @@ module Merit
55
72
  config.app_generators.orm Merit.orm
56
73
 
57
74
  initializer 'merit.controller' do |app|
58
- if Merit.orm == :active_record
59
- require 'merit/models/active_record/merit/activity_log'
60
- require 'merit/models/active_record/merit/badges_sash'
61
- require 'merit/models/active_record/merit/sash'
62
- require 'merit/models/active_record/merit/score'
63
- elsif Merit.orm == :mongoid
64
- require 'merit/models/mongoid/sash'
65
- end
66
-
75
+ require_models
76
+ extend_orm_with_has_merit
67
77
  ActiveSupport.on_load(:action_controller) do
68
78
  begin
69
- # Load application defined rules on application boot up
70
- ::Merit::AppBadgeRules = ::Merit::BadgeRules.new.defined_rules
71
- ::Merit::AppPointRules = ::Merit::PointRules.new.defined_rules
79
+ # Load app rules on boot up
80
+ Merit::AppBadgeRules = Merit::BadgeRules.new.defined_rules
81
+ Merit::AppPointRules = Merit::PointRules.new.defined_rules
72
82
  include Merit::ControllerExtensions
73
83
  rescue NameError => e
74
84
  # Trap NameError if installing/generating files
@@ -76,5 +86,23 @@ module Merit
76
86
  end
77
87
  end
78
88
  end
89
+
90
+ def require_models
91
+ require 'merit/models/base/merit/sash'
92
+ require 'merit/models/base/merit/badges_sash'
93
+ require "merit/models/#{Merit.orm}/merit/activity_log"
94
+ require "merit/models/#{Merit.orm}/merit/badges_sash"
95
+ require "merit/models/#{Merit.orm}/merit/sash"
96
+ require "merit/models/#{Merit.orm}/merit/score"
97
+ end
98
+
99
+ def extend_orm_with_has_merit
100
+ if Object.const_defined?('ActiveRecord')
101
+ ActiveRecord::Base.send :include, Merit
102
+ end
103
+ if Object.const_defined?('Mongoid')
104
+ Mongoid::Document.send :include, Merit
105
+ end
106
+ end
79
107
  end
80
108
  end
@@ -1,8 +1,7 @@
1
1
  module Merit
2
2
  class BaseTargetFinder
3
-
4
3
  def self.find(*args)
5
- self.new(*args).find
4
+ new(*args).find
6
5
  end
7
6
 
8
7
  def initialize(rule, action)
@@ -17,6 +16,5 @@ module Merit
17
16
  rescue => e
18
17
  Rails.logger.warn "[merit] no target found: #{e}. #{caller.first}"
19
18
  end
20
-
21
19
  end
22
20
  end
data/lib/merit/judge.rb CHANGED
@@ -1,9 +1,8 @@
1
- require_relative 'observer'
1
+ require 'observer'
2
2
 
3
3
  module Merit
4
4
  class Judge
5
-
6
- include Observer
5
+ include Observable
7
6
 
8
7
  def initialize(sashes, rule, options = {})
9
8
  @sashes = sashes
@@ -11,6 +10,10 @@ module Merit
11
10
  # FIXME: Too much context?
12
11
  # A Judge should apply reputation independently of the action
13
12
  @action = options[:action]
13
+
14
+ Merit.observers.each do |class_name|
15
+ add_observer class_name.constantize.new
16
+ end
14
17
  end
15
18
 
16
19
  # Grant badge if rule applies. If it doesn't, and the badge is temporary,
@@ -27,7 +30,11 @@ module Merit
27
30
  return unless rule_applies?
28
31
  @sashes.each do |sash|
29
32
  point = sash.add_points points
30
- notify_observers(@action.id, point)
33
+ notify_observers(
34
+ description: "granted #{points} points",
35
+ merit_object: point,
36
+ merit_action_id: @action.id
37
+ )
31
38
  end
32
39
  end
33
40
 
@@ -37,14 +44,22 @@ module Merit
37
44
  @sashes.each do |sash|
38
45
  next unless new_or_multiple?(sash)
39
46
  badge_sash = sash.add_badge badge.id
40
- notify_observers(@action.id, badge_sash, 'granted')
47
+ notify_observers(
48
+ description: "granted #{badge.name} badge",
49
+ merit_object: badge_sash,
50
+ merit_action_id: @action.id
51
+ )
41
52
  end
42
53
  end
43
54
 
44
55
  def remove_badges
45
56
  @sashes.each do |sash|
46
57
  badge_sash = sash.rm_badge badge.id
47
- notify_observers(@action.id, badge_sash, 'removed')
58
+ notify_observers(
59
+ description: "removed #{badge.name} badge",
60
+ merit_object: badge_sash,
61
+ merit_action_id: @action.id
62
+ )
48
63
  end
49
64
  end
50
65
 
@@ -71,5 +86,10 @@ module Merit
71
86
  def badge
72
87
  @rule.badge
73
88
  end
89
+
90
+ def notify_observers(changed_data)
91
+ changed
92
+ super
93
+ end
74
94
  end
75
95
  end