qalam_merit 4.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/Gemfile +10 -0
- data/LICENSE.txt +21 -0
- data/README.md +358 -0
- data/RELEASING.md +16 -0
- data/Rakefile +44 -0
- data/TESTING.txt +12 -0
- data/config/locales/en.yml +5 -0
- data/lib/merit.rb +95 -0
- data/lib/merit/badge_rules_methods.rb +30 -0
- data/lib/merit/base_target_finder.rb +31 -0
- data/lib/merit/class_methods.rb +41 -0
- data/lib/merit/controller_extensions.rb +71 -0
- data/lib/merit/generators/active_record/install_generator.rb +40 -0
- data/lib/merit/generators/active_record/merit_generator.rb +25 -0
- data/lib/merit/generators/active_record/remove_generator.rb +30 -0
- data/lib/merit/generators/active_record/templates/add_merit_fields_to_model.erb +7 -0
- data/lib/merit/generators/active_record/templates/create_badges_sashes.erb +14 -0
- data/lib/merit/generators/active_record/templates/create_merit_actions.erb +16 -0
- data/lib/merit/generators/active_record/templates/create_merit_activity_logs.erb +12 -0
- data/lib/merit/generators/active_record/templates/create_merit_badges.erb +17 -0
- data/lib/merit/generators/active_record/templates/create_sashes.erb +8 -0
- data/lib/merit/generators/active_record/templates/create_scores_and_points.erb +16 -0
- data/lib/merit/generators/active_record/templates/remove_merit_fields_from_model.erb +7 -0
- data/lib/merit/generators/active_record/templates/remove_merit_tables.erb +12 -0
- data/lib/merit/generators/install_generator.rb +30 -0
- data/lib/merit/generators/merit_generator.rb +33 -0
- data/lib/merit/generators/remove_generator.rb +23 -0
- data/lib/merit/generators/templates/badge.erb +69 -0
- data/lib/merit/generators/templates/merit.erb +18 -0
- data/lib/merit/generators/templates/merit_badge_rules.erb +50 -0
- data/lib/merit/generators/templates/merit_point_rules.erb +31 -0
- data/lib/merit/generators/templates/merit_rank_rules.erb +32 -0
- data/lib/merit/generators/templates/qalam_badge_sash.erb +20 -0
- data/lib/merit/generators/templates/qalam_sash.erb +14 -0
- data/lib/merit/judge.rb +102 -0
- data/lib/merit/models/action_concern.rb +50 -0
- data/lib/merit/models/active_record/action.rb +11 -0
- data/lib/merit/models/active_record/activity_log.rb +11 -0
- data/lib/merit/models/active_record/badges_sash.rb +13 -0
- data/lib/merit/models/active_record/sash.rb +32 -0
- data/lib/merit/models/active_record/score.rb +25 -0
- data/lib/merit/models/badges_sash_concern.rb +13 -0
- data/lib/merit/models/base/badges_sash.rb +15 -0
- data/lib/merit/models/base/sash.rb +55 -0
- data/lib/merit/models/sash_concern.rb +53 -0
- data/lib/merit/point_rules_methods.rb +33 -0
- data/lib/merit/rank_rules_methods.rb +58 -0
- data/lib/merit/reputation_change_observer.rb +19 -0
- data/lib/merit/rule.rb +35 -0
- data/lib/merit/rules_matcher.rb +24 -0
- data/lib/merit/sash_finder.rb +15 -0
- data/lib/merit/target_finder.rb +43 -0
- data/qalam_merit.gemspec +23 -0
- data/test/dummy/Rakefile +7 -0
- data/test/dummy/app/controllers/admin/users_controller.rb +9 -0
- data/test/dummy/app/controllers/api/comments_controller.rb +5 -0
- data/test/dummy/app/controllers/api/users_controller.rb +5 -0
- data/test/dummy/app/controllers/application_controller.rb +7 -0
- data/test/dummy/app/controllers/comments_controller.rb +56 -0
- data/test/dummy/app/controllers/registrations_controller.rb +21 -0
- data/test/dummy/app/controllers/users_controller.rb +38 -0
- data/test/dummy/app/helpers/application_helper.rb +2 -0
- data/test/dummy/app/models/address.rb +3 -0
- data/test/dummy/app/models/comment.rb +13 -0
- data/test/dummy/app/models/dummy_observer.rb +3 -0
- data/test/dummy/app/models/merit/badge_rules.rb +66 -0
- data/test/dummy/app/models/merit/point_rules.rb +44 -0
- data/test/dummy/app/models/merit/rank_rules.rb +24 -0
- data/test/dummy/app/models/user.rb +23 -0
- data/test/dummy/app/views/admin/users/index.html.erb +26 -0
- data/test/dummy/app/views/comments/_form.html.erb +29 -0
- data/test/dummy/app/views/comments/edit.html.erb +6 -0
- data/test/dummy/app/views/comments/index.html.erb +35 -0
- data/test/dummy/app/views/comments/new.html.erb +5 -0
- data/test/dummy/app/views/comments/show.html.erb +23 -0
- data/test/dummy/app/views/layouts/application.html.erb +24 -0
- data/test/dummy/app/views/users/_form.html.erb +22 -0
- data/test/dummy/app/views/users/edit.html.erb +6 -0
- data/test/dummy/app/views/users/index.html.erb +27 -0
- data/test/dummy/app/views/users/new.html.erb +5 -0
- data/test/dummy/app/views/users/show.html.erb +18 -0
- data/test/dummy/config.ru +4 -0
- data/test/dummy/config/application.rb +26 -0
- data/test/dummy/config/application_api_only.rb +28 -0
- data/test/dummy/config/boot.rb +10 -0
- data/test/dummy/config/database.yml +22 -0
- data/test/dummy/config/environment.rb +5 -0
- data/test/dummy/config/environment_api_only.rb +7 -0
- data/test/dummy/config/environments/development.rb +24 -0
- data/test/dummy/config/environments/production.rb +51 -0
- data/test/dummy/config/environments/test.rb +36 -0
- data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/test/dummy/config/initializers/inflections.rb +10 -0
- data/test/dummy/config/initializers/merit.rb +47 -0
- data/test/dummy/config/initializers/mime_types.rb +5 -0
- data/test/dummy/config/initializers/new_framework_defaults.rb +3 -0
- data/test/dummy/config/initializers/secret_token.rb +12 -0
- data/test/dummy/config/initializers/session_store.rb +8 -0
- data/test/dummy/config/locales/en.yml +5 -0
- data/test/dummy/config/mongoid.yml +13 -0
- data/test/dummy/config/routes.rb +16 -0
- data/test/dummy/db/migrate/20110421191249_create_users.rb +12 -0
- data/test/dummy/db/migrate/20110421191250_create_comments.rb +16 -0
- data/test/dummy/db/migrate/20120318022220_add_fields_to_users.rb +11 -0
- data/test/dummy/db/migrate/20130321082817_add_fields_to_comments.rb +11 -0
- data/test/dummy/db/migrate/20130329224406_create_merit_actions.rb +18 -0
- data/test/dummy/db/migrate/20130329224407_create_merit_activity_logs.rb +15 -0
- data/test/dummy/db/migrate/20130329224408_create_sashes.rb +11 -0
- data/test/dummy/db/migrate/20130329224409_create_badges_sashes.rb +16 -0
- data/test/dummy/db/migrate/20130329224410_create_scores_and_points.rb +20 -0
- data/test/dummy/db/migrate/20140211144001_create_addresses.rb +11 -0
- data/test/dummy/db/migrate/20140819133931_add_target_data_to_merit_actions.rb +5 -0
- data/test/dummy/db/schema.rb +89 -0
- data/test/dummy/db/seeds.rb +19 -0
- data/test/dummy/public/404.html +26 -0
- data/test/dummy/public/422.html +26 -0
- data/test/dummy/public/500.html +26 -0
- data/test/dummy/public/favicon.ico +0 -0
- data/test/dummy/public/rails.js +404 -0
- data/test/dummy/public/stylesheets/.gitkeep +0 -0
- data/test/dummy/public/stylesheets/scaffold.css +56 -0
- data/test/dummy/script/rails +6 -0
- data/test/integration/navigation_test.rb +332 -0
- data/test/support/integration_case.rb +5 -0
- data/test/test_helper.rb +40 -0
- data/test/unit/action_test.rb +12 -0
- data/test/unit/base_target_finder_test.rb +64 -0
- data/test/unit/merit_unit_test.rb +33 -0
- data/test/unit/rule_unit_test.rb +57 -0
- data/test/unit/rules_matcher_test.rb +37 -0
- data/test/unit/sash_finder_test.rb +27 -0
- data/test/unit/sash_test.rb +104 -0
- data/test/unit/score_test.rb +13 -0
- data/test/unit/target_finder_test.rb +98 -0
- metadata +360 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 6d18cb74d9ddf61a7a7d053c66cde90d8595a2b1a4aa707bfe7ab463e8d083e4
|
4
|
+
data.tar.gz: 39f3ccf60a27aebbfed60abee1647143ca4b8cb63aab4b92942d1040e4558458
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 27abe76d15dacfe849056f2fa47a435a4699f8d19bebb990866ec2f7f8f2357f136f130f573ce905ca52f9c9412fce42a039b877dc7ba86253f9f7d846d0f7b8
|
7
|
+
data.tar.gz: 6eaa19dbcb7d43c1e182bc18444420f6bc8bfdce6430a28a85453704e6c8bee391ac34ad3f5e257906868fc9a1565b9125689602eb283ee78f7fd27e81d11a8d
|
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
+
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
8
|
+
|
9
|
+
## Our Standards
|
10
|
+
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
12
|
+
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
18
|
+
|
19
|
+
Examples of unacceptable behavior include:
|
20
|
+
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
22
|
+
advances of any kind
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
24
|
+
* Public or private harassment
|
25
|
+
* Publishing others' private information, such as a physical or email
|
26
|
+
address, without their explicit permission
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
28
|
+
professional setting
|
29
|
+
|
30
|
+
## Enforcement Responsibilities
|
31
|
+
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
33
|
+
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
35
|
+
|
36
|
+
## Scope
|
37
|
+
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
39
|
+
|
40
|
+
## Enforcement
|
41
|
+
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at eng.a.abdelhamid@outlook.com. All complaints will be reviewed and investigated promptly and fairly.
|
43
|
+
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
45
|
+
|
46
|
+
## Enforcement Guidelines
|
47
|
+
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
49
|
+
|
50
|
+
### 1. Correction
|
51
|
+
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
53
|
+
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
55
|
+
|
56
|
+
### 2. Warning
|
57
|
+
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
59
|
+
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
61
|
+
|
62
|
+
### 3. Temporary Ban
|
63
|
+
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
65
|
+
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
67
|
+
|
68
|
+
### 4. Permanent Ban
|
69
|
+
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
71
|
+
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
73
|
+
|
74
|
+
## Attribution
|
75
|
+
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
78
|
+
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
80
|
+
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
82
|
+
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2021 Ahmed Abdelhamid
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,358 @@
|
|
1
|
+
# Merit
|
2
|
+
|
3
|
+
Merit adds reputation behavior to Rails apps in the form of Badges, Points,
|
4
|
+
and Rankings.
|
5
|
+
|
6
|
+
[![Build Status](https://travis-ci.org/merit-gem/merit.svg?branch=master)](http://travis-ci.org/merit-gem/merit)
|
7
|
+
[![Coverage Status](https://coveralls.io/repos/github/merit-gem/merit/badge.svg?branch=master)](https://coveralls.io/github/merit-gem/merit?branch=master)
|
8
|
+
[![Code Climate](https://codeclimate.com/github/tute/merit/badges/gpa.svg)](https://codeclimate.com/github/tute/merit)
|
9
|
+
|
10
|
+
# Table of Contents
|
11
|
+
|
12
|
+
- [Installation](#installation)
|
13
|
+
- [Badges](#badges)
|
14
|
+
- [Creating Badges](#creating-badges)
|
15
|
+
- [Example](#example)
|
16
|
+
- [Defining Rules](#defining-rules)
|
17
|
+
- [Examples](#examples)
|
18
|
+
- [Other Actions](#other-actions)
|
19
|
+
- [Displaying Badges](#displaying-badges)
|
20
|
+
- [Points](#points)
|
21
|
+
- [Defining Rules](#defining-rules-1)
|
22
|
+
- [Examples](#examples-1)
|
23
|
+
- [Other Actions](#other-actions-1)
|
24
|
+
- [Displaying Points](#displaying-points)
|
25
|
+
- [Rankings](#rankings)
|
26
|
+
- [Defining Rules](#defining-rules-2)
|
27
|
+
- [Examples](#examples-2)
|
28
|
+
- [Displaying Rankings](#displaying-rankings)
|
29
|
+
- [How merit finds the target object](#how-merit-finds-the-target-object)
|
30
|
+
- [Getting Notifications](#getting-notifications)
|
31
|
+
- [I18n](#i18n)
|
32
|
+
- [Uninstalling Merit](#uninstalling-merit)
|
33
|
+
|
34
|
+
|
35
|
+
# Installation
|
36
|
+
|
37
|
+
1. Add `gem 'qalam_merit', require: 'merit'` to your `Gemfile`
|
38
|
+
2. Run `rails g merit:install`. This creates several migrations.
|
39
|
+
3. Run `rails g merit MODEL_NAME` (e.g. `user`). This creates a migration and adds `has_merit` to MODEL_NAME.
|
40
|
+
4. Run `rake db:migrate`
|
41
|
+
5. Define badges in `config/initializers/merit.rb`
|
42
|
+
6. Configure reputation rules for your application in `app/models/merit/*`
|
43
|
+
|
44
|
+
|
45
|
+
# Badges
|
46
|
+
|
47
|
+
## Creating Badges
|
48
|
+
|
49
|
+
Create badges in `config/initializers/merit.rb`
|
50
|
+
|
51
|
+
`Merit::Badge.create!` takes a hash describing the badge:
|
52
|
+
* `:id` integer (required)
|
53
|
+
* `:name` this is how you reference the badge (required)
|
54
|
+
* `:level` (optional)
|
55
|
+
* `:description` (optional)
|
56
|
+
* `:custom_fields` hash of anything else you want associated with the badge (optional)
|
57
|
+
|
58
|
+
### Example
|
59
|
+
|
60
|
+
```ruby
|
61
|
+
Merit::Badge.create!(
|
62
|
+
id: 1,
|
63
|
+
name: "year-member",
|
64
|
+
description: "Active member for a year",
|
65
|
+
custom_fields: { difficulty: :silver }
|
66
|
+
)
|
67
|
+
```
|
68
|
+
|
69
|
+
## Defining Rules
|
70
|
+
|
71
|
+
Badges can be automatically given to any resource in your application based on
|
72
|
+
rules and conditions you create. Badges can also have levels, and be permanent
|
73
|
+
or temporary (A temporary badge is revoked when the conditions of the badge
|
74
|
+
are no longer met).
|
75
|
+
|
76
|
+
Badge rules / conditions are defined in `app/models/merit/badge_rules.rb`
|
77
|
+
`initialize` block by calling `grant_on` with the following parameters:
|
78
|
+
|
79
|
+
* `'controller#action'` a string similar to Rails routes (required)
|
80
|
+
* `:badge_id` or `:badge` these correspond to the `:id` or `:name` of the badge respectively
|
81
|
+
* `:level` corresponds to the `:level` of the badge
|
82
|
+
* `:to` the object's field to give the badge to. It needs a variable named
|
83
|
+
`@model` in the associated controller action, like `@post` for
|
84
|
+
`posts_controller.rb` or `@comment` for `comments_controller.rb`.
|
85
|
+
* Can be a method name, which called over the target object should retrieve
|
86
|
+
the object to badge. If it's `:user` for example, merit will internally
|
87
|
+
call `@model.user` to find who to badge.
|
88
|
+
* Can be `:itself`, in which case it badges the target object itself
|
89
|
+
(`@model`).
|
90
|
+
* Is `:action_user` by default, which means `current_user`.
|
91
|
+
* `:model_name` define the model's name if it's different from
|
92
|
+
the controller's (e.g. the `User` model for the `RegistrationsController`).
|
93
|
+
* `:multiple` whether or not the badge may be granted multiple times. `false` by default.
|
94
|
+
* `:temporary` whether or not the badge should be revoked if the condition no
|
95
|
+
longer holds. `false` -badges are kept for ever- by default.
|
96
|
+
* `&block` can be one of the following:
|
97
|
+
* empty / not included: always grant the badge
|
98
|
+
* a block which evaluates to boolean. It recieves the target object as
|
99
|
+
parameter (e.g. `@post` if you're working with a PostsController action).
|
100
|
+
* a block with a hash composed of methods to run on the target object and
|
101
|
+
expected method return values
|
102
|
+
|
103
|
+
### Examples
|
104
|
+
|
105
|
+
```ruby
|
106
|
+
# app/models/merit/badge_rules.rb
|
107
|
+
grant_on 'comments#vote', badge_id: 5, to: :user do |comment|
|
108
|
+
comment.votes.count == 5
|
109
|
+
end
|
110
|
+
|
111
|
+
grant_on ['users#create', 'users#update'], badge: 'autobiographer', temporary: true do |user|
|
112
|
+
user.name? && user.email?
|
113
|
+
end
|
114
|
+
```
|
115
|
+
|
116
|
+
If your controller is under a namespace other than root (example:
|
117
|
+
`Api::ModelController`) then for merit to find your object automatically you
|
118
|
+
must specify the model class and not forget that your action is of the form
|
119
|
+
`namespace/models#action`.
|
120
|
+
|
121
|
+
See an example of a `Post` model that belongs to user:
|
122
|
+
|
123
|
+
```ruby
|
124
|
+
grant_on 'api/posts#create', badge: 'first-post', model_name: 'Post', to: :user do |post|
|
125
|
+
post.user.posts.count >= 1
|
126
|
+
end
|
127
|
+
```
|
128
|
+
|
129
|
+
## Other Actions
|
130
|
+
|
131
|
+
```ruby
|
132
|
+
# Check granted badges
|
133
|
+
current_user.badges # Returns an array of badges
|
134
|
+
|
135
|
+
# Grant or remove manually
|
136
|
+
current_user.add_badge(badge.id)
|
137
|
+
current_user.rm_badge(badge.id)
|
138
|
+
```
|
139
|
+
|
140
|
+
```ruby
|
141
|
+
# Get related entries of a given badge
|
142
|
+
Merit::Badge.find(1).users
|
143
|
+
```
|
144
|
+
|
145
|
+
## Displaying Badges
|
146
|
+
|
147
|
+
Meritable models have a `badges` method which returns an array of associated
|
148
|
+
badges:
|
149
|
+
|
150
|
+
```erb
|
151
|
+
<ul>
|
152
|
+
<% current_user.badges.each do |badge| %>
|
153
|
+
<li><%= badge.name %></li>
|
154
|
+
<% end %>
|
155
|
+
</ul>
|
156
|
+
```
|
157
|
+
|
158
|
+
|
159
|
+
# Points
|
160
|
+
|
161
|
+
## Defining Rules
|
162
|
+
|
163
|
+
Points are given to "meritable" resources on actions-triggered, either to the
|
164
|
+
action user or to the method(s) defined in the `:to` option. Define rules on
|
165
|
+
`app/models/merit/point_rules.rb`:
|
166
|
+
|
167
|
+
`score` accepts:
|
168
|
+
|
169
|
+
* `score`
|
170
|
+
* `Integer`
|
171
|
+
* `Proc`, or any object that accepts `call` which takes one argument, where
|
172
|
+
the target object is passed in and the return value is used as the score.
|
173
|
+
* `:on` action as string or array of strings (like `controller#action`, similar to Rails routes)
|
174
|
+
* `:to` method(s) to send to the target object (who should be scored?)
|
175
|
+
* `:model_name` (optional) to specify the model name if it cannot be guessed
|
176
|
+
from the controller. (e.g. `model_name: 'User'` for `RegistrationsController`,
|
177
|
+
or `model_name: 'Comment'` for `Api::CommentsController`)
|
178
|
+
* `:category` (optional) to categorize earned points. `default` is used by default.
|
179
|
+
* `&block`
|
180
|
+
* empty (always scores)
|
181
|
+
* a block which evaluates to boolean (recieves target object as parameter)
|
182
|
+
|
183
|
+
### Examples
|
184
|
+
|
185
|
+
```ruby
|
186
|
+
# app/models/merit/point_rules.rb
|
187
|
+
score 10, to: :post_creator, on: 'comments#create', category: 'comment_activity' do |comment|
|
188
|
+
comment.title.present?
|
189
|
+
end
|
190
|
+
|
191
|
+
score 20, on: [
|
192
|
+
'comments#create',
|
193
|
+
'photos#create'
|
194
|
+
]
|
195
|
+
|
196
|
+
score 15, on: 'reviews#create', to: [:reviewer, :reviewed]
|
197
|
+
|
198
|
+
proc = lambda { |photo| PhotoPointsCalculator.calculate_score_for(photo) }
|
199
|
+
score proc, on: 'photos#create'
|
200
|
+
```
|
201
|
+
|
202
|
+
## Other Actions
|
203
|
+
|
204
|
+
```ruby
|
205
|
+
# Score manually
|
206
|
+
current_user.add_points(20, category: 'Optional category')
|
207
|
+
current_user.subtract_points(10, category: 'Optional category')
|
208
|
+
```
|
209
|
+
|
210
|
+
```ruby
|
211
|
+
# Query awarded points since a given date
|
212
|
+
score_points = current_user.score_points(category: 'Optional category')
|
213
|
+
score_points.where("created_at > '#{1.month.ago}'").sum(:num_points)
|
214
|
+
```
|
215
|
+
|
216
|
+
## Displaying Points
|
217
|
+
|
218
|
+
Meritable models have a `points` method which returns an integer:
|
219
|
+
|
220
|
+
```erb
|
221
|
+
<%= current_user.points(category: 'Optional category') %>
|
222
|
+
```
|
223
|
+
|
224
|
+
If `category` left empty, it will return the sum of points for every category.
|
225
|
+
|
226
|
+
```erb
|
227
|
+
<%= current_user.points %>
|
228
|
+
```
|
229
|
+
|
230
|
+
# Rankings
|
231
|
+
|
232
|
+
A common ranking use case is 5 stars. They are not given at specified actions
|
233
|
+
like badges, a cron job should be defined to test if ranks are to be granted.
|
234
|
+
|
235
|
+
## Defining Rules
|
236
|
+
|
237
|
+
Define rules on `app/models/merit/rank_rules.rb`:
|
238
|
+
|
239
|
+
`set_rank` accepts:
|
240
|
+
|
241
|
+
* `:level` ranking level (greater is better, Lexicographical order)
|
242
|
+
* `:to` model or scope to check if new rankings apply
|
243
|
+
* `:level_name` attribute name (default is empty and results in
|
244
|
+
'`level`' attribute, if set it's appended like
|
245
|
+
'`level_#{level_name}`')
|
246
|
+
|
247
|
+
Check for rules on a rake task executed in background like:
|
248
|
+
|
249
|
+
```ruby
|
250
|
+
task cron: :environment do
|
251
|
+
Merit::RankRules.new.check_rank_rules
|
252
|
+
end
|
253
|
+
```
|
254
|
+
|
255
|
+
|
256
|
+
### Examples
|
257
|
+
|
258
|
+
```ruby
|
259
|
+
set_rank level: 2, to: Committer.active do |committer|
|
260
|
+
committer.branches > 1 && committer.followers >= 10
|
261
|
+
end
|
262
|
+
|
263
|
+
set_rank level: 3, to: Committer.active do |committer|
|
264
|
+
committer.branches > 2 && committer.followers >= 20
|
265
|
+
end
|
266
|
+
```
|
267
|
+
|
268
|
+
## Displaying Rankings
|
269
|
+
|
270
|
+
```erb
|
271
|
+
<%= current_user.level %>
|
272
|
+
```
|
273
|
+
|
274
|
+
|
275
|
+
# How merit finds the target object
|
276
|
+
|
277
|
+
Merit fetches the rule’s target object (the parameter it receives) from its
|
278
|
+
`:model_name` option, or from the controller’s instance variable.
|
279
|
+
|
280
|
+
To read it from the controller merit searches for the instance variable named
|
281
|
+
after the singularized controller name. For example, a rule like:
|
282
|
+
|
283
|
+
```ruby
|
284
|
+
grant_on 'comments#update', badge_id: 1 do |target_object|
|
285
|
+
# target_object would be better named comment in this sample
|
286
|
+
end
|
287
|
+
```
|
288
|
+
|
289
|
+
Would make merit try to find the `@comment` instance variable in the
|
290
|
+
`CommentsController#update` action. If the rule had the `:model_name` option
|
291
|
+
specified:
|
292
|
+
|
293
|
+
```ruby
|
294
|
+
grant_on 'comments#update', badge_id: 1, model_name: "Article" do |target_object|
|
295
|
+
# target_object would be better named article in this sample
|
296
|
+
end
|
297
|
+
```
|
298
|
+
|
299
|
+
Merit would fetch the `Article` object from the database, found by the `:id`
|
300
|
+
param sent in that `update` action.
|
301
|
+
|
302
|
+
If none of these methods find the target, Merit will log a `no target_obj`
|
303
|
+
warning, with a comment to check the configuration for the rule.
|
304
|
+
|
305
|
+
|
306
|
+
# Getting Notifications
|
307
|
+
|
308
|
+
You can get observers notified any time merit changes reputation in your
|
309
|
+
application.
|
310
|
+
|
311
|
+
It needs to implement the `update` method, which receives as parameter the
|
312
|
+
following hash:
|
313
|
+
|
314
|
+
* `description`, describes what happened. For example: "granted 5 points",
|
315
|
+
"granted just-registered badge", "removed autobiographer badge".
|
316
|
+
* `sash_id`, who saw it's reputation changed.
|
317
|
+
* `granted_at`, date and time when the reputation change took effect.
|
318
|
+
|
319
|
+
Example code (add your observer to `app/models` or `app/observers`):
|
320
|
+
|
321
|
+
```ruby
|
322
|
+
# reputation_change_observer.rb
|
323
|
+
class ReputationChangeObserver
|
324
|
+
def update(changed_data)
|
325
|
+
description = changed_data[:description]
|
326
|
+
|
327
|
+
# If user is your meritable model, you can query for it doing:
|
328
|
+
user = User.where(sash_id: changed_data[:sash_id]).first
|
329
|
+
|
330
|
+
# When did it happened:
|
331
|
+
datetime = changed_data[:granted_at]
|
332
|
+
end
|
333
|
+
end
|
334
|
+
```
|
335
|
+
```ruby
|
336
|
+
# In `config/initializers/merit.rb`
|
337
|
+
config.add_observer 'ReputationChangeObserver'
|
338
|
+
```
|
339
|
+
|
340
|
+
# I18n
|
341
|
+
|
342
|
+
Merit uses default messages with I18n for notify alerts. To customize your app, you can set up your locale file:
|
343
|
+
|
344
|
+
```yaml
|
345
|
+
en:
|
346
|
+
merit:
|
347
|
+
granted_badge: "granted %{badge_name} badge"
|
348
|
+
granted_points: "granted %{points} points"
|
349
|
+
removed_badge: "removed %{badge_name} badge"
|
350
|
+
```
|
351
|
+
|
352
|
+
# Uninstalling Merit
|
353
|
+
|
354
|
+
1. Run `rails d merit:install`
|
355
|
+
2. Run `rails d merit MODEL_NAME` (e.g. `user`)
|
356
|
+
3. Run `rails g merit:remove MODEL_NAME` (e.g. `user`)
|
357
|
+
4. Run `rake db:migrate`
|
358
|
+
5. Remove `merit` from your Gemfile
|