merit 1.7.1 → 1.8.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -1
- data/Gemfile +7 -0
- data/README.md +31 -4
- data/app/models/merit/action.rb +2 -3
- data/app/models/merit/badge.rb +2 -2
- data/lib/generators/active_record/install_generator.rb +1 -1
- data/lib/generators/active_record/merit_generator.rb +2 -2
- data/lib/generators/active_record/remove_generator.rb +2 -2
- data/lib/generators/active_record/templates/create_badges_sashes.rb +1 -1
- data/lib/generators/active_record/templates/create_merit_actions.rb +3 -3
- data/lib/generators/active_record/templates/create_scores_and_points.rb +2 -2
- data/lib/generators/active_record/templates/remove_merit_tables.rb +1 -1
- data/lib/generators/merit/install_generator.rb +1 -1
- data/lib/generators/merit/merit_generator.rb +2 -3
- data/lib/generators/merit/remove_generator.rb +3 -3
- data/lib/generators/merit/templates/merit.rb +10 -4
- data/lib/merit.rb +43 -15
- data/lib/merit/base_target_finder.rb +1 -3
- data/lib/merit/judge.rb +26 -6
- data/lib/merit/model_additions.rb +10 -17
- data/lib/merit/models/active_record/merit/action.rb +1 -1
- data/lib/merit/models/active_record/merit/activity_log.rb +1 -0
- data/lib/merit/models/active_record/merit/badges_sash.rb +6 -10
- data/lib/merit/models/active_record/merit/sash.rb +2 -38
- data/lib/merit/models/active_record/merit/score.rb +6 -5
- data/lib/merit/models/base/merit/badges_sash.rb +15 -0
- data/lib/merit/models/base/merit/sash.rb +42 -0
- data/lib/merit/models/mongoid/merit/action.rb +8 -6
- data/lib/merit/models/mongoid/merit/activity_log.rb +11 -0
- data/lib/merit/models/mongoid/merit/badges_sash.rb +19 -0
- data/lib/merit/models/mongoid/merit/sash.rb +28 -0
- data/lib/merit/models/mongoid/merit/score.rb +41 -0
- data/lib/merit/reputation_change_observer.rb +11 -0
- data/lib/merit/rule.rb +1 -1
- data/lib/merit/rules_matcher.rb +0 -2
- data/lib/merit/rules_rank_methods.rb +1 -1
- data/lib/merit/target_finder.rb +1 -2
- data/merit.gemspec +2 -1
- data/test/dummy/app/models/comment.rb +16 -1
- data/test/dummy/app/models/dummy_observer.rb +3 -0
- data/test/dummy/app/models/user.rb +14 -1
- data/test/dummy/config/initializers/merit.rb +3 -4
- data/test/dummy/config/mongoid.yml +13 -0
- data/test/integration/navigation_test.rb +19 -2
- data/test/unit/merit_unit_test.rb +1 -1
- metadata +25 -56
- data/lib/merit/models/mongoid/sash.rb +0 -14
- data/lib/merit/observer.rb +0 -13
- data/test/dummy-mongoid/Rakefile +0 -7
- data/test/dummy-mongoid/app/controllers/application_controller.rb +0 -7
- data/test/dummy-mongoid/app/controllers/comments_controller.rb +0 -90
- data/test/dummy-mongoid/app/controllers/registrations_controller.rb +0 -15
- data/test/dummy-mongoid/app/controllers/users_controller.rb +0 -67
- data/test/dummy-mongoid/app/helpers/application_helper.rb +0 -2
- data/test/dummy-mongoid/app/models/comment.rb +0 -13
- data/test/dummy-mongoid/app/models/merit/badge_rules.rb +0 -49
- data/test/dummy-mongoid/app/models/merit/point_rules.rb +0 -20
- data/test/dummy-mongoid/app/models/merit/rank_rules.rb +0 -24
- data/test/dummy-mongoid/app/models/user.rb +0 -25
- data/test/dummy-mongoid/app/views/comments/_form.html.erb +0 -29
- data/test/dummy-mongoid/app/views/comments/edit.html.erb +0 -6
- data/test/dummy-mongoid/app/views/comments/index.html.erb +0 -35
- data/test/dummy-mongoid/app/views/comments/new.html.erb +0 -5
- data/test/dummy-mongoid/app/views/comments/show.html.erb +0 -23
- data/test/dummy-mongoid/app/views/layouts/application.html.erb +0 -24
- data/test/dummy-mongoid/app/views/users/_form.html.erb +0 -22
- data/test/dummy-mongoid/app/views/users/edit.html.erb +0 -6
- data/test/dummy-mongoid/app/views/users/index.html.erb +0 -26
- data/test/dummy-mongoid/app/views/users/new.html.erb +0 -5
- data/test/dummy-mongoid/app/views/users/show.html.erb +0 -18
- data/test/dummy-mongoid/config.ru +0 -4
- data/test/dummy-mongoid/config/application.rb +0 -22
- data/test/dummy-mongoid/config/boot.rb +0 -10
- data/test/dummy-mongoid/config/environment.rb +0 -5
- data/test/dummy-mongoid/config/environments/development.rb +0 -25
- data/test/dummy-mongoid/config/environments/production.rb +0 -49
- data/test/dummy-mongoid/config/environments/test.rb +0 -35
- data/test/dummy-mongoid/config/initializers/backtrace_silencers.rb +0 -7
- data/test/dummy-mongoid/config/initializers/inflections.rb +0 -10
- data/test/dummy-mongoid/config/initializers/merit.rb +0 -37
- data/test/dummy-mongoid/config/initializers/mime_types.rb +0 -5
- data/test/dummy-mongoid/config/initializers/secret_token.rb +0 -7
- data/test/dummy-mongoid/config/initializers/session_store.rb +0 -8
- data/test/dummy-mongoid/config/locales/en.yml +0 -5
- data/test/dummy-mongoid/config/mongoid.yml +0 -14
- data/test/dummy-mongoid/config/routes.rb +0 -9
- data/test/dummy-mongoid/db/seeds.rb +0 -17
- data/test/dummy-mongoid/public/404.html +0 -26
- data/test/dummy-mongoid/public/422.html +0 -26
- data/test/dummy-mongoid/public/500.html +0 -26
- data/test/dummy-mongoid/public/favicon.ico +0 -0
- data/test/dummy-mongoid/public/javascripts/application.js +0 -2
- data/test/dummy-mongoid/public/javascripts/controls.js +0 -965
- data/test/dummy-mongoid/public/javascripts/dragdrop.js +0 -974
- data/test/dummy-mongoid/public/javascripts/effects.js +0 -1123
- data/test/dummy-mongoid/public/javascripts/prototype.js +0 -6001
- data/test/dummy-mongoid/public/javascripts/rails.js +0 -191
- data/test/dummy-mongoid/public/stylesheets/.gitkeep +0 -0
- data/test/dummy-mongoid/public/stylesheets/scaffold.css +0 -56
- data/test/dummy-mongoid/script/rails +0 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c2c4d73e01e95da0a10952cf3fc7da4e9d1c2685
|
4
|
+
data.tar.gz: c87f93f700d5b2fa3ef5360d39e424590fa38bc8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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.
|
data/app/models/merit/action.rb
CHANGED
@@ -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
|
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
|
-
|
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
|
data/app/models/merit/badge.rb
CHANGED
@@ -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
|
-
|
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:
|
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
|
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
|
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
|
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
|
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
|
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, :
|
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
|
4
|
+
t.integer :user_id
|
5
5
|
t.string :action_method
|
6
6
|
t.integer :action_value
|
7
|
-
t.boolean :had_errors, :
|
7
|
+
t.boolean :had_errors, default: false
|
8
8
|
t.string :target_model
|
9
9
|
t.integer :target_id
|
10
|
-
t.boolean :processed, :
|
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, :
|
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, :
|
10
|
+
t.integer :num_points, default: 0
|
11
11
|
t.string :log
|
12
12
|
t.datetime :created_at
|
13
13
|
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
|
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(
|
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(
|
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
|
-
|
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(
|
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
|
-
#
|
10
|
-
# config.
|
9
|
+
# Add application observers to get notifications when reputation changes.
|
10
|
+
# config.add_observer 'MyObserverClassName'
|
11
11
|
|
12
|
-
# Define :
|
13
|
-
#
|
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
|
-
|
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
|
-
|
59
|
-
|
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
|
70
|
-
|
71
|
-
|
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
|
-
|
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
|
-
|
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(
|
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(
|
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(
|
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
|