merit 1.6.1 → 1.6.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0d0a820341d6408878856c250741dc432cf69e87
4
+ data.tar.gz: 5c62d13ececa1fbc100635867a24f1ede459ea7b
5
+ SHA512:
6
+ metadata.gz: 8701d647ca74706f02beabb79392e7e35015719b05aef83476a97ae2f86810bace21184065bf1aed45a6f08b40ace8a94e5957a04cd743162e158f0ee433f4b4
7
+ data.tar.gz: 321f7574c41b967a5e13af373f387b48b47cc7817397b5777d1c84ef75192edaa97de25cd96ca87b370e2783d59872636fac5fcc1b319a7d5d5a909d02ce70c3
data/Gemfile CHANGED
@@ -3,11 +3,12 @@ source 'https://rubygems.org'
3
3
  gemspec
4
4
 
5
5
  version = ENV['RAILS_VERSION'] || '3.2'
6
-
7
6
  rails = case version
8
7
  when 'master'
9
8
  { github: 'rails/rails' }
10
- else
9
+ when '4.0'
10
+ "~> #{version}.0"
11
+ when '3.2'
11
12
  gem 'strong_parameters'
12
13
  "~> #{version}.0"
13
14
  end
data/README.md CHANGED
@@ -1,4 +1,6 @@
1
- # Merit Gem: Reputation rules (badges, points and rankings) for Rails applications
1
+ # Merit
2
+ Merit is a reputation Ruby gem that supports Badges, Points, and Rankings.
3
+
2
4
 
3
5
  ![Merit gem](http://i567.photobucket.com/albums/ss118/DeuceBigglebags/th_nspot26_300.jpg)
4
6
 
@@ -11,36 +13,61 @@
11
13
  2. Run `rails g merit:install`
12
14
  3. Run `rails g merit MODEL_NAME` (e.g. `user`)
13
15
  4. Run `rake db:migrate`
14
- 5. Define badges you will use in `config/initializers/merit.rb`
16
+ 5. Define badges in `config/initializers/merit.rb`
15
17
  6. Configure reputation rules for your application in `app/models/merit/*`
16
18
 
17
19
  ---
18
20
 
19
- # Defining badge rules
20
-
21
- You may give badges to any resource on your application if some condition
22
- holds. Badges may have levels, and may be temporary. Define rules on
23
- `app/models/merit/badge_rules.rb`:
21
+ # Badges
22
+ ## Creating Badges
23
+ Create badges in `config/initializers/merit.rb`
24
24
 
25
- `grant_on` accepts:
25
+ `Merit::Badge.create!` takes a hash describing the badge:
26
+ * `:id` integer (reqired)
27
+ * `:name` this is how you reference the badge (required)
28
+ * `:level` (optional)
29
+ * `:description` (optional)
30
+ * `:custom_fields` hash of anything else you want associated with the badge (optional)
26
31
 
27
- * `'controller#action'` string (similar to Rails routes)
28
- * `:badge` for badge name
29
- * `:level` for badge level
30
- * `:to` method name over target_object which obtains object(s) to badge
31
- * `:model_name` (string) define controller's name if it differs from
32
- the model (like `RegistrationsController` for `User` model).
33
- * `:multiple` (boolean) badge may be granted multiple times
34
- * `:temporary` (boolean) if the receiver had the badge but the condition
35
- doesn't hold anymore, remove it. `false` by default (badges are kept
36
- forever).
37
- * `&block`
38
- * empty (always grants)
39
- * a block which evaluates to boolean (recieves target object as parameter)
40
- * a block with a hash composed of methods to run on the target object with
41
- expected values
32
+ ### Example
33
+ ```ruby
34
+ Merit::Badge.create! ({
35
+ id: 1,
36
+ name: "Yearling",
37
+ description: "Active member for a year",
38
+ custom_fields: { difficulty: :silver }
39
+ })
40
+ ```
42
41
 
43
- ## Examples
42
+ ## Defining Badge Rules
43
+ Badges can be automatically given to any resource in your application based on rules and conditions you create.
44
+ Badges can also have levels, and be permanent or temporary (A temporary badge is revoked when the conditions of the badge are no longer met).
45
+
46
+ Badge rules / conditions are defined in `app/models/merit/badge_rules.rb` `initialize` block by calling `grant_on` with the following parameters:
47
+
48
+ * `'controller#action'` a string similar to Rails routes
49
+ * `:badge` corresponds to the `:name` of the badge
50
+ * `:level` corresponds to the `:level` of the badge
51
+ * `:to` the object's field to give the badge to
52
+ * If you are putting badges on the related user then this field is probably
53
+ `:user`.
54
+ * Needs a variable named `@model` in the associated controller action, like
55
+ `@post` for `posts_controller.rb` or `@comment` for `comments_controller.rb`.
56
+ Implementation note: Merit finds the object with following snippet:
57
+ `instance_variable_get(:"@#{controller_name.singularize}")`.
58
+ * `:model_name` define the controller's name if it's different from
59
+ the model's (e.g. `RegistrationsController` for the `User` model).
60
+ * `:multiple` whether or not the badge may be granted multiple times. `false` by default.
61
+ * `:temporary` whether or not the badge should be revoked if the condition no
62
+ longer holds. `false` -badges are kept for ever- by default.
63
+ * `&block` can be one of the following:
64
+ * empty / not included: always grant the badge
65
+ * a block which evaluates to boolean. It recieves the target object as
66
+ parameter (e.g. `@post` if you're working with a PostsController action).
67
+ * a block with a hash composed of methods to run on the target object and
68
+ expected method return values
69
+
70
+ ### Examples
44
71
 
45
72
  ```ruby
46
73
  # app/models/merit/badge_rules.rb
@@ -49,10 +76,12 @@ grant_on 'comments#vote', badge: 'relevant-commenter', to: :user do |comment|
49
76
  end
50
77
 
51
78
  grant_on ['users#create', 'users#update'], badge: 'autobiographer', temporary: true do |user|
52
- user.name.present? && user.address.present?
79
+ user.name.present? && user.email.present?
53
80
  end
54
81
  ```
55
82
 
83
+ ## Other Badge Actions
84
+
56
85
  ```ruby
57
86
  # Check granted badges
58
87
  current_user.badges # Returns an array of badges
@@ -69,7 +98,7 @@ Badge.last_granted
69
98
  # List 20 badge grants in the last week
70
99
  Badge.last_granted(since_date: 1.week.ago, limit: 20)
71
100
 
72
- # Get related entries of a given badge (unreleased)
101
+ # Get related entries of a given badge
73
102
  Badge.find(1).users
74
103
  ```
75
104
 
@@ -85,6 +114,9 @@ action user or to the method(s) defined in the `:to` option. Define rules on
85
114
 
86
115
  * `:on` action as string or array of strings (similar to Rails routes)
87
116
  * `:to` method(s) to send to the target_object (who should be scored?)
117
+ * `:model_name` (optional) to specify the model name if it cannot be guessed from the controller.
118
+ (e.g. `model_name: 'User'` for `RegistrationsController`, or
119
+ `model_name: 'Comment'` for `Api::CommentsController`)
88
120
  * `&block`
89
121
  * empty (always scores)
90
122
  * a block which evaluates to boolean (recieves target object as parameter)
@@ -167,6 +199,7 @@ end
167
199
 
168
200
  # To-do list
169
201
 
202
+ * Finish Observer implementation for `Judge`.
170
203
  * Move level from meritable model into Sash
171
204
  * `ActivityLog` should replace `add_points` `log` parameter
172
205
  * FIXMES and TODOS.
@@ -4,7 +4,7 @@ module Merit
4
4
 
5
5
  has_many :activity_logs, class_name: Merit::ActivityLog
6
6
 
7
- if Rails.version < '4'
7
+ unless defined?(ActionController::StrongParameters)
8
8
  attr_accessible :user_id, :action_method, :action_value, :had_errors,
9
9
  :target_model, :target_id, :processed, :log
10
10
  end
@@ -5,7 +5,7 @@ module Merit
5
5
  belongs_to :action, class_name: Merit::Action
6
6
  belongs_to :related_change, polymorphic: true
7
7
 
8
- if Rails.version < '4'
8
+ unless defined?(ActionController::StrongParameters)
9
9
  attr_accessible :action_id, :related_change, :description, :created_at
10
10
  end
11
11
  end
@@ -5,7 +5,7 @@ module Merit
5
5
  class_name: Merit::ActivityLog,
6
6
  as: :related_change
7
7
 
8
- if Rails.version < '4'
8
+ unless defined?(ActionController::StrongParameters)
9
9
  attr_accessible :badge_id
10
10
  end
11
11
 
@@ -15,6 +15,7 @@ module Merit
15
15
  rule.score = points
16
16
  rule.to = to
17
17
  rule.block = block
18
+ rule.model_name = options[:model_name] if options[:model_name]
18
19
 
19
20
  actions.each do |action|
20
21
  defined_rules[action] ||= []
data/merit.gemspec CHANGED
@@ -4,7 +4,7 @@ Gem::Specification.new do |s|
4
4
  s.description = "Manage badges, points and rankings (reputation) of resources in a Rails application."
5
5
  s.homepage = "http://github.com/tute/merit"
6
6
  s.files = `git ls-files`.split("\n").reject{|f| f =~ /^\./ }
7
- s.version = '1.6.1'
7
+ s.version = '1.6.2'
8
8
  s.authors = ["Tute Costa"]
9
9
  s.email = 'tutecosta@gmail.com'
10
10
 
@@ -13,10 +13,8 @@ Gem::Specification.new do |s|
13
13
  s.add_dependency 'ambry', '~> 0.3.0'
14
14
  s.add_development_dependency 'rails', '~> 3.2.0'
15
15
  s.add_development_dependency 'sqlite3'
16
- s.add_development_dependency 'haml'
17
16
  s.add_development_dependency 'capybara'
18
17
  s.add_development_dependency 'simplecov'
19
- s.add_development_dependency 'minitest'
20
- s.add_development_dependency 'minitest-spec'
18
+ s.add_development_dependency 'minitest-rails'
21
19
  s.add_development_dependency 'mocha', '0.13.3'
22
20
  end
@@ -0,0 +1,5 @@
1
+ class Api::CommentsController < ApplicationController
2
+ def show
3
+ render json: {}
4
+ end
5
+ end
@@ -3,7 +3,7 @@ class Comment < ActiveRecord::Base
3
3
 
4
4
  belongs_to :user
5
5
 
6
- if Rails.version < '4'
6
+ unless defined?(ActionController::StrongParameters)
7
7
  attr_accessible :name, :comment, :user_id, :votes
8
8
  end
9
9
 
@@ -16,6 +16,9 @@ module Merit
16
16
  # Points to voted user
17
17
  score 5, to: :user, on: 'comments#vote'
18
18
 
19
+ # Example rule for using model_name in the case of namespaced controllers
20
+ score 1, to: :user, model_name: 'Comment', on: 'api/comments#show'
21
+
19
22
  score 20, on: [
20
23
  'comments#create',
21
24
  'registrations#update'
@@ -3,7 +3,7 @@ class User < ActiveRecord::Base
3
3
 
4
4
  has_many :comments
5
5
 
6
- if Rails.version < '4'
6
+ unless defined?(ActionController::StrongParameters)
7
7
  attr_accessible :name
8
8
  end
9
9
 
@@ -4,6 +4,7 @@ Dummy::Application.routes.draw do
4
4
  end
5
5
  namespace :api do
6
6
  get '/users' => 'users#index'
7
+ resources :comments, only: [:show]
7
8
  end
8
9
  resources :users, :except => :update
9
10
  resources :registrations, :only => :update, :as => :registrations_user
@@ -227,6 +227,15 @@ class NavigationTest < ActiveSupport::IntegrationCase
227
227
  comment_2.reload.points.must_be :==, 2
228
228
  end
229
229
 
230
+ test 'api/comments#show should grant 1 point to user' do
231
+ user = User.create(name: 'test-user')
232
+ assert_equal 0, user.points
233
+ comment = user.comments.create!(name: 'test-comment', comment: 'comment body')
234
+
235
+ visit "/api/comments/#{comment.id}"
236
+ assert_equal 1, user.points
237
+ end
238
+
230
239
  def badges_by_name(user, name)
231
240
  user.reload.badges.select{|b| b.name == name }
232
241
  end
data/test/test_helper.rb CHANGED
@@ -1,37 +1,31 @@
1
1
  # Configure Rails Envinronment
2
- ENV["RAILS_ENV"] = "test"
2
+ ENV['RAILS_ENV'] = 'test'
3
3
  RUBYOPT="-w $RUBYOPT"
4
4
 
5
- require 'simplecov'
6
- SimpleCov.adapters.define 'rubygem' do
7
- # Add app to Merit group
8
- # https://github.com/colszowka/simplecov/pull/104
9
- add_group 'Merit', 'lib'
10
- add_group 'DummyApp', 'test/dummy'
11
- add_filter 'test/dummy/config/initializers'
5
+ if ENV["COVERAGE"]
6
+ require 'simplecov'
7
+ SimpleCov.adapters.define 'rubygem' do
8
+ # Add app to Merit group
9
+ # https://github.com/colszowka/simplecov/pull/104
10
+ add_group 'Merit', 'lib'
11
+ add_group 'DummyApp', 'test/dummy'
12
+ add_filter 'test/dummy/config/initializers'
13
+ end
14
+ SimpleCov.start 'rubygem'
12
15
  end
13
- SimpleCov.start 'rubygem' if ENV["COVERAGE"]
14
16
 
15
- require File.expand_path("../dummy/config/environment.rb", __FILE__)
16
17
 
17
- require "rails/test_help"
18
-
19
- ActionMailer::Base.delivery_method = :test
20
- ActionMailer::Base.perform_deliveries = true
21
- ActionMailer::Base.default_url_options[:host] = "test.com"
18
+ require File.expand_path('../dummy/config/environment.rb', __FILE__)
19
+ require 'rails/test_help'
20
+ require 'minitest/rails'
22
21
 
23
22
  Rails.backtrace_cleaner.remove_silencers!
24
23
 
25
24
  # Configure capybara for integration testing
26
- require "capybara/rails"
25
+ require 'capybara/rails'
27
26
  Capybara.default_driver = :rack_test
28
27
  Capybara.default_selector = :css
29
28
 
30
- require 'minitest/spec'
31
- require 'minitest/autorun'
32
- require 'minitest/mock'
33
- require 'mocha/setup'
34
-
35
29
  ActiveRecord::Migrator.migrate File.expand_path("../dummy/db/migrate/", __FILE__)
36
30
 
37
31
  # Load support files
@@ -54,11 +54,11 @@ class MeritUnitTest < ActiveSupport::TestCase
54
54
  sash_2.add_points(5); sash_2.add_points(5)
55
55
 
56
56
  # Test method options
57
- assert_equal Merit::Score.top_scored(table_name: :sashes),
58
- [{'sash_id'=>sash_1.id, 'sum_points'=>20, 0=>1, 1=>20},
59
- {'sash_id'=>sash_2.id, 'sum_points'=>10, 0=>2, 1=>10}]
60
- assert_equal Merit::Score.top_scored(table_name: :sashes, limit: 1),
61
- [{'sash_id'=>sash_1.id, 'sum_points'=>20, 0=>1, 1=>20}]
57
+ assert_equal [{'sash_id'=>sash_1.id, 'sum_points'=>20, 0=>1, 1=>20},
58
+ {'sash_id'=>sash_2.id, 'sum_points'=>10, 0=>2, 1=>10}],
59
+ Merit::Score.top_scored(table_name: :sashes)
60
+ assert_equal [{'sash_id'=>sash_1.id, 'sum_points'=>20, 0=>1, 1=>20}],
61
+ Merit::Score.top_scored(table_name: :sashes, limit: 1)
62
62
  end
63
63
 
64
64
  test 'unknown ranking raises exception' do
metadata CHANGED
@@ -1,20 +1,18 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: merit
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.6.1
5
- prerelease:
4
+ version: 1.6.2
6
5
  platform: ruby
7
6
  authors:
8
7
  - Tute Costa
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-05-04 00:00:00.000000000 Z
11
+ date: 2013-11-10 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: ambry
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
17
  - - ~>
20
18
  - !ruby/object:Gem::Version
@@ -22,7 +20,6 @@ dependencies:
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
24
  - - ~>
28
25
  - !ruby/object:Gem::Version
@@ -30,7 +27,6 @@ dependencies:
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: rails
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
31
  - - ~>
36
32
  - !ruby/object:Gem::Version
@@ -38,7 +34,6 @@ dependencies:
38
34
  type: :development
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
38
  - - ~>
44
39
  - !ruby/object:Gem::Version
@@ -46,103 +41,62 @@ dependencies:
46
41
  - !ruby/object:Gem::Dependency
47
42
  name: sqlite3
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
- - - ! '>='
45
+ - - '>='
52
46
  - !ruby/object:Gem::Version
53
47
  version: '0'
54
48
  type: :development
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
- - - ! '>='
60
- - !ruby/object:Gem::Version
61
- version: '0'
62
- - !ruby/object:Gem::Dependency
63
- name: haml
64
- requirement: !ruby/object:Gem::Requirement
65
- none: false
66
- requirements:
67
- - - ! '>='
68
- - !ruby/object:Gem::Version
69
- version: '0'
70
- type: :development
71
- prerelease: false
72
- version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
- requirements:
75
- - - ! '>='
52
+ - - '>='
76
53
  - !ruby/object:Gem::Version
77
54
  version: '0'
78
55
  - !ruby/object:Gem::Dependency
79
56
  name: capybara
80
57
  requirement: !ruby/object:Gem::Requirement
81
- none: false
82
58
  requirements:
83
- - - ! '>='
59
+ - - '>='
84
60
  - !ruby/object:Gem::Version
85
61
  version: '0'
86
62
  type: :development
87
63
  prerelease: false
88
64
  version_requirements: !ruby/object:Gem::Requirement
89
- none: false
90
65
  requirements:
91
- - - ! '>='
66
+ - - '>='
92
67
  - !ruby/object:Gem::Version
93
68
  version: '0'
94
69
  - !ruby/object:Gem::Dependency
95
70
  name: simplecov
96
71
  requirement: !ruby/object:Gem::Requirement
97
- none: false
98
- requirements:
99
- - - ! '>='
100
- - !ruby/object:Gem::Version
101
- version: '0'
102
- type: :development
103
- prerelease: false
104
- version_requirements: !ruby/object:Gem::Requirement
105
- none: false
106
- requirements:
107
- - - ! '>='
108
- - !ruby/object:Gem::Version
109
- version: '0'
110
- - !ruby/object:Gem::Dependency
111
- name: minitest
112
- requirement: !ruby/object:Gem::Requirement
113
- none: false
114
72
  requirements:
115
- - - ! '>='
73
+ - - '>='
116
74
  - !ruby/object:Gem::Version
117
75
  version: '0'
118
76
  type: :development
119
77
  prerelease: false
120
78
  version_requirements: !ruby/object:Gem::Requirement
121
- none: false
122
79
  requirements:
123
- - - ! '>='
80
+ - - '>='
124
81
  - !ruby/object:Gem::Version
125
82
  version: '0'
126
83
  - !ruby/object:Gem::Dependency
127
- name: minitest-spec
84
+ name: minitest-rails
128
85
  requirement: !ruby/object:Gem::Requirement
129
- none: false
130
86
  requirements:
131
- - - ! '>='
87
+ - - '>='
132
88
  - !ruby/object:Gem::Version
133
89
  version: '0'
134
90
  type: :development
135
91
  prerelease: false
136
92
  version_requirements: !ruby/object:Gem::Requirement
137
- none: false
138
93
  requirements:
139
- - - ! '>='
94
+ - - '>='
140
95
  - !ruby/object:Gem::Version
141
96
  version: '0'
142
97
  - !ruby/object:Gem::Dependency
143
98
  name: mocha
144
99
  requirement: !ruby/object:Gem::Requirement
145
- none: false
146
100
  requirements:
147
101
  - - '='
148
102
  - !ruby/object:Gem::Version
@@ -150,7 +104,6 @@ dependencies:
150
104
  type: :development
151
105
  prerelease: false
152
106
  version_requirements: !ruby/object:Gem::Requirement
153
- none: false
154
107
  requirements:
155
108
  - - '='
156
109
  - !ruby/object:Gem::Version
@@ -261,6 +214,7 @@ files:
261
214
  - test/dummy-mongoid/script/rails
262
215
  - test/dummy/Rakefile
263
216
  - test/dummy/app/controllers/admin/users_controller.rb
217
+ - test/dummy/app/controllers/api/comments_controller.rb
264
218
  - test/dummy/app/controllers/api/users_controller.rb
265
219
  - test/dummy/app/controllers/application_controller.rb
266
220
  - test/dummy/app/controllers/comments_controller.rb
@@ -334,27 +288,26 @@ files:
334
288
  - test/unit/target_finder_test.rb
335
289
  homepage: http://github.com/tute/merit
336
290
  licenses: []
291
+ metadata: {}
337
292
  post_install_message:
338
293
  rdoc_options: []
339
294
  require_paths:
340
295
  - lib
341
296
  required_ruby_version: !ruby/object:Gem::Requirement
342
- none: false
343
297
  requirements:
344
- - - ! '>='
298
+ - - '>='
345
299
  - !ruby/object:Gem::Version
346
300
  version: 1.9.2
347
301
  required_rubygems_version: !ruby/object:Gem::Requirement
348
- none: false
349
302
  requirements:
350
- - - ! '>='
303
+ - - '>='
351
304
  - !ruby/object:Gem::Version
352
305
  version: '0'
353
306
  requirements: []
354
307
  rubyforge_project:
355
- rubygems_version: 1.8.23
308
+ rubygems_version: 2.0.3
356
309
  signing_key:
357
- specification_version: 3
310
+ specification_version: 4
358
311
  summary: General reputation Rails engine.
359
312
  test_files: []
360
313
  has_rdoc: