merit 1.5.0 → 1.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. data/Gemfile +13 -1
  2. data/README.md +10 -10
  3. data/UPGRADING.md +14 -0
  4. data/app/models/merit/action.rb +9 -10
  5. data/app/models/merit/badge.rb +9 -6
  6. data/lib/generators/merit/templates/merit.rb +6 -2
  7. data/lib/merit.rb +3 -2
  8. data/lib/merit/controller_extensions.rb +10 -9
  9. data/lib/merit/judge.rb +9 -15
  10. data/lib/merit/model_additions.rb +6 -6
  11. data/lib/merit/models/active_record/merit/action.rb +4 -2
  12. data/lib/merit/models/active_record/merit/activity_log.rb +3 -1
  13. data/lib/merit/models/active_record/merit/badges_sash.rb +6 -2
  14. data/lib/merit/models/active_record/merit/sash.rb +8 -8
  15. data/lib/merit/models/active_record/merit/score.rb +7 -3
  16. data/lib/merit/models/mongo_mapper/merit/action.rb +1 -1
  17. data/lib/merit/models/mongo_mapper/sash.rb +1 -0
  18. data/lib/merit/models/mongoid/merit/action.rb +4 -4
  19. data/lib/merit/models/mongoid/sash.rb +1 -1
  20. data/lib/merit/observer.rb +13 -0
  21. data/lib/merit/rule.rb +1 -1
  22. data/lib/merit/rules_badge_methods.rb +2 -2
  23. data/lib/merit/rules_matcher.rb +24 -0
  24. data/lib/merit/rules_rank_methods.rb +12 -8
  25. data/lib/merit/target_finder.rb +12 -9
  26. data/merit.gemspec +2 -5
  27. data/test/dummy/app/controllers/api/users_controller.rb +5 -0
  28. data/test/dummy/app/controllers/comments_controller.rb +15 -49
  29. data/test/dummy/app/controllers/registrations_controller.rb +7 -1
  30. data/test/dummy/app/controllers/users_controller.rb +11 -40
  31. data/test/dummy/app/models/comment.rb +3 -1
  32. data/test/dummy/app/models/merit/badge_rules.rb +11 -8
  33. data/test/dummy/app/models/merit/point_rules.rb +4 -4
  34. data/test/dummy/app/models/merit/rank_rules.rb +1 -1
  35. data/test/dummy/app/models/user.rb +3 -1
  36. data/test/dummy/app/views/admin/users/index.html.erb +1 -1
  37. data/test/dummy/app/views/comments/index.html.erb +1 -1
  38. data/test/dummy/app/views/users/index.html.erb +1 -1
  39. data/test/dummy/config/application.rb +1 -1
  40. data/test/dummy/config/environments/development.rb +2 -3
  41. data/test/dummy/config/environments/production.rb +2 -0
  42. data/test/dummy/config/environments/test.rb +2 -3
  43. data/test/dummy/config/initializers/merit.rb +27 -24
  44. data/test/dummy/config/initializers/secret_token.rb +6 -1
  45. data/test/dummy/config/routes.rb +5 -2
  46. data/test/integration/navigation_test.rb +67 -55
  47. data/test/test_helper.rb +5 -20
  48. data/test/{base_target_finder_test.rb → unit/base_target_finder_test.rb} +1 -1
  49. data/test/{merit_unit_test.rb → unit/merit_unit_test.rb} +14 -32
  50. data/test/unit/rule_unit_test.rb +44 -0
  51. data/test/{sash_finder_test.rb → unit/sash_finder_test.rb} +1 -1
  52. data/test/{target_finder_test.rb → unit/target_finder_test.rb} +5 -5
  53. metadata +12 -41
  54. data/Gemfile.lock +0 -146
data/test/test_helper.rb CHANGED
@@ -12,11 +12,8 @@ SimpleCov.adapters.define 'rubygem' do
12
12
  end
13
13
  SimpleCov.start 'rubygem' if ENV["COVERAGE"]
14
14
 
15
- if ENV["ORM"] == "mongoid"
16
- require File.expand_path("../dummy-mongoid/config/environment.rb", __FILE__)
17
- else
18
- require File.expand_path("../dummy/config/environment.rb", __FILE__)
19
- end
15
+ require File.expand_path("../dummy/config/environment.rb", __FILE__)
16
+
20
17
  require "rails/test_help"
21
18
 
22
19
  ActionMailer::Base.delivery_method = :test
@@ -33,21 +30,9 @@ Capybara.default_selector = :css
33
30
  require 'minitest/spec'
34
31
  require 'minitest/autorun'
35
32
  require 'minitest/mock'
36
- require "mocha/setup"
37
-
38
- if ENV["ORM"] == "mongoid"
39
- class ActiveSupport::TestCase
40
- def teardown
41
- Mongoid.database.collections.each do |collection|
42
- collection.remove unless collection.name =~ /^system\./
43
- end
44
- end
45
- end
46
- else # ActiveRecord
47
- # Run any available migration
48
- ActiveRecord::Migrator.migrate File.expand_path("../dummy/db/migrate/", __FILE__)
49
- end
33
+ require 'mocha/setup'
34
+
35
+ ActiveRecord::Migrator.migrate File.expand_path("../dummy/db/migrate/", __FILE__)
50
36
 
51
37
  # Load support files
52
38
  Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
53
-
@@ -20,7 +20,7 @@ describe Merit::BaseTargetFinder do
20
20
  end
21
21
 
22
22
  describe 'rule has no model_name' do
23
- it "should fall back to the action#target_model" do
23
+ it 'should fall back to the action#target_model' do
24
24
  rule = Merit::Rule.new
25
25
  rule.to = :itself
26
26
  action = Merit::Action.new(target_model: 'users', target_id: 3)
@@ -1,32 +1,8 @@
1
1
  require 'test_helper'
2
2
 
3
+ # TODO: Split different objects tests in it's own files
3
4
  class MeritUnitTest < ActiveSupport::TestCase
4
- test "Rule#applies? depends on provided block" do
5
- rule = Merit::Rule.new
6
- assert rule.applies?, 'empty conditions should make rule apply'
7
-
8
- str = "string"
9
- rule.block = lambda{|obj| obj.length < 4 }
10
- assert !rule.applies?(str), 'block should make rule fail'
11
-
12
- rule.block = lambda{|obj| obj.length >= 4 }
13
- assert rule.applies?(str), 'block should make rule apply'
14
-
15
- rule.block = lambda{|obj| true }
16
- assert !rule.applies?, 'block needs parameter for rule to pass'
17
- end
18
-
19
- test "Rule#badge gets related badge or raises exception" do
20
- rule = Merit::Rule.new
21
- rule.badge_name = 'inexistent'
22
- assert_raise(Merit::BadgeNotFound) { rule.badge }
23
-
24
- badge = Merit::Badge.create(id: 98, name: 'test-badge-98')
25
- rule.badge_name = badge.name
26
- assert_equal Merit::Badge.find(98), rule.badge
27
- end
28
-
29
- test "extends only meritable ActiveRecord models" do
5
+ test 'extends only meritable ActiveRecord models' do
30
6
  class User < ActiveRecord::Base
31
7
  def self.columns; @columns ||= []; end
32
8
  has_merit
@@ -39,7 +15,7 @@ class MeritUnitTest < ActiveSupport::TestCase
39
15
  assert !Fruit.method_defined?(:points), 'other models aren\'t extended'
40
16
  end
41
17
 
42
- test "Badges get 'related_models' methods" do
18
+ test 'Badges get "related_models" methods' do
43
19
  class Soldier < ActiveRecord::Base
44
20
  def self.columns; @columns ||= []; end
45
21
  has_merit
@@ -52,7 +28,7 @@ class MeritUnitTest < ActiveSupport::TestCase
52
28
  assert Merit::Badge.method_defined?(:players), 'Badge#players should be defined'
53
29
  end
54
30
 
55
- test "Badge#last_granted returns recently granted badges" do
31
+ test 'Badge#last_granted returns recently granted badges' do
56
32
  # Create sashes, badges and badges_sashes
57
33
  sash = Merit::Sash.create
58
34
  badge = Merit::Badge.create(id: 20, name: 'test-badge-21')
@@ -70,7 +46,7 @@ class MeritUnitTest < ActiveSupport::TestCase
70
46
  assert_equal Merit::Badge.last_granted(since_date: 2.weeks.ago, limit: 1), [badge]
71
47
  end
72
48
 
73
- test "Merit::Score.top_scored returns scores leaderboard" do
49
+ test 'Merit::Score.top_scored returns scores leaderboard' do
74
50
  # Create sashes and add points
75
51
  sash_1 = Merit::Sash.create
76
52
  sash_1.add_points(10); sash_1.add_points(10)
@@ -79,10 +55,10 @@ class MeritUnitTest < ActiveSupport::TestCase
79
55
 
80
56
  # Test method options
81
57
  assert_equal Merit::Score.top_scored(table_name: :sashes),
82
- [{"sash_id"=>sash_1.id, "sum_points"=>20, 0=>1, 1=>20},
83
- {"sash_id"=>sash_2.id, "sum_points"=>10, 0=>2, 1=>10}]
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}]
84
60
  assert_equal Merit::Score.top_scored(table_name: :sashes, limit: 1),
85
- [{"sash_id"=>sash_1.id, "sum_points"=>20, 0=>1, 1=>20}]
61
+ [{'sash_id'=>sash_1.id, 'sum_points'=>20, 0=>1, 1=>20}]
86
62
  end
87
63
 
88
64
  test 'unknown ranking raises exception' do
@@ -96,4 +72,10 @@ class MeritUnitTest < ActiveSupport::TestCase
96
72
  WeirdRankRules.new.check_rank_rules
97
73
  end
98
74
  end
75
+
76
+ test 'Badge#custom_fields_hash saves correctly' do
77
+ Merit::Badge.create(id: 99, name: 'test-badge',
78
+ custom_fields: { key_1: 'value1' })
79
+ assert_equal 'value1', Merit::Badge.find(99).custom_fields[:key_1]
80
+ end
99
81
  end
@@ -0,0 +1,44 @@
1
+ require 'test_helper'
2
+
3
+ describe Merit::Rule do
4
+ before do
5
+ @rule = Merit::Rule.new
6
+ end
7
+
8
+ describe '#applies (with block)' do
9
+ before do
10
+ @rule.block = ->(obj) { obj.length < 4 }
11
+ end
12
+
13
+ it 'truthy block should make rule apply' do
14
+ @rule.applies?('str').must_be :==, true
15
+ end
16
+
17
+ it 'falsy block should make rule fail' do
18
+ @rule.applies?('string').must_be :==, false
19
+ end
20
+
21
+ it 'block needs parameter for rule to pass' do
22
+ @rule.applies?.must_be :==, false
23
+ end
24
+ end
25
+
26
+ describe '#applies (without block)' do
27
+ it 'empty condition should make rule apply' do
28
+ @rule.applies?.must_be :==, true
29
+ end
30
+ end
31
+
32
+ describe '#badge' do
33
+ it 'raises exception on inexistent badge' do
34
+ @rule.badge_name = 'inexistent'
35
+ ->{ @rule.badge }.must_raise Merit::BadgeNotFound
36
+ end
37
+
38
+ it 'finds related badge' do
39
+ badge = Merit::Badge.create(id: 98, name: 'test-badge-98')
40
+ @rule.badge_name = badge.name
41
+ @rule.badge.must_be :==, Merit::Badge.find(98)
42
+ end
43
+ end
44
+ end
@@ -4,7 +4,7 @@ describe Merit::SashFinder do
4
4
 
5
5
  it 'should return an array of sashes of the target objects' do
6
6
  sash_1 = Merit::Sash.new
7
- user_1 = User.new(:_sash => sash_1)
7
+ user_1 = User.new
8
8
  user_1.stubs(:_sash).returns(sash_1)
9
9
 
10
10
  sash_2 = Merit::Sash.new
@@ -45,7 +45,7 @@ describe Merit::TargetFinder do
45
45
  rule.to = :action_user
46
46
  action = Merit::Action.new(user_id: 22)
47
47
 
48
- Rails.logger.expects(:warn).with("[merit] no User found with id 22")
48
+ Rails.logger.expects(:warn).with('[merit] no User found with id 22')
49
49
  finder = Merit::TargetFinder.new(rule, action)
50
50
  finder.find.must_be_empty
51
51
  end
@@ -70,7 +70,7 @@ describe Merit::TargetFinder do
70
70
  collection.must_include user
71
71
  end
72
72
 
73
- describe 'the rule#to does not exist as a method on the original target' do
73
+ describe 'rule#to does not exist as a method on the original target' do
74
74
  it 'should warn and return an empty array' do
75
75
  rule = Merit::Rule.new
76
76
  rule.to = :non_existent
@@ -80,10 +80,10 @@ describe Merit::TargetFinder do
80
80
  comment = Comment.new
81
81
  Comment.stubs(:find_by_id).with(40).returns(comment)
82
82
 
83
- message = "[merit] NoMethodError on `Comment#non_existent`"
84
- message << " (called from Merit::TargetFinder#other_target)"
83
+ str = '[merit] NoMethodError on `Comment#non_existent`'
84
+ str << ' (called from Merit::TargetFinder#other_target)'
85
85
 
86
- Rails.logger.expects(:warn).with(message)
86
+ Rails.logger.expects(:warn).with(str)
87
87
  finder = Merit::TargetFinder.new(rule, action)
88
88
  finder.find.must_be_empty
89
89
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: merit
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.0
4
+ version: 1.6.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-03-30 00:00:00.000000000 Z
12
+ date: 2013-04-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: ambry
@@ -34,7 +34,7 @@ dependencies:
34
34
  requirements:
35
35
  - - ~>
36
36
  - !ruby/object:Gem::Version
37
- version: 3.2.3
37
+ version: 3.2.0
38
38
  type: :development
39
39
  prerelease: false
40
40
  version_requirements: !ruby/object:Gem::Requirement
@@ -42,7 +42,7 @@ dependencies:
42
42
  requirements:
43
43
  - - ~>
44
44
  - !ruby/object:Gem::Version
45
- version: 3.2.3
45
+ version: 3.2.0
46
46
  - !ruby/object:Gem::Dependency
47
47
  name: sqlite3
48
48
  requirement: !ruby/object:Gem::Requirement
@@ -155,38 +155,6 @@ dependencies:
155
155
  - - '='
156
156
  - !ruby/object:Gem::Version
157
157
  version: 0.13.3
158
- - !ruby/object:Gem::Dependency
159
- name: bson_ext
160
- requirement: !ruby/object:Gem::Requirement
161
- none: false
162
- requirements:
163
- - - ! '>='
164
- - !ruby/object:Gem::Version
165
- version: '0'
166
- type: :development
167
- prerelease: false
168
- version_requirements: !ruby/object:Gem::Requirement
169
- none: false
170
- requirements:
171
- - - ! '>='
172
- - !ruby/object:Gem::Version
173
- version: '0'
174
- - !ruby/object:Gem::Dependency
175
- name: mongoid
176
- requirement: !ruby/object:Gem::Requirement
177
- none: false
178
- requirements:
179
- - - ~>
180
- - !ruby/object:Gem::Version
181
- version: 2.0.0
182
- type: :development
183
- prerelease: false
184
- version_requirements: !ruby/object:Gem::Requirement
185
- none: false
186
- requirements:
187
- - - ~>
188
- - !ruby/object:Gem::Version
189
- version: 2.0.0
190
158
  description: Manage badges, points and rankings (reputation) of resources in a Rails
191
159
  application.
192
160
  email: tutecosta@gmail.com
@@ -195,7 +163,6 @@ extensions: []
195
163
  extra_rdoc_files: []
196
164
  files:
197
165
  - Gemfile
198
- - Gemfile.lock
199
166
  - MIT-LICENSE
200
167
  - README.md
201
168
  - Rakefile
@@ -231,14 +198,15 @@ files:
231
198
  - lib/merit/models/mongo_mapper/sash.rb
232
199
  - lib/merit/models/mongoid/merit/action.rb
233
200
  - lib/merit/models/mongoid/sash.rb
201
+ - lib/merit/observer.rb
234
202
  - lib/merit/rule.rb
235
203
  - lib/merit/rules_badge_methods.rb
204
+ - lib/merit/rules_matcher.rb
236
205
  - lib/merit/rules_points_methods.rb
237
206
  - lib/merit/rules_rank_methods.rb
238
207
  - lib/merit/sash_finder.rb
239
208
  - lib/merit/target_finder.rb
240
209
  - merit.gemspec
241
- - test/base_target_finder_test.rb
242
210
  - test/dummy-mongoid/Rakefile
243
211
  - test/dummy-mongoid/app/controllers/application_controller.rb
244
212
  - test/dummy-mongoid/app/controllers/comments_controller.rb
@@ -293,6 +261,7 @@ files:
293
261
  - test/dummy-mongoid/script/rails
294
262
  - test/dummy/Rakefile
295
263
  - test/dummy/app/controllers/admin/users_controller.rb
264
+ - test/dummy/app/controllers/api/users_controller.rb
296
265
  - test/dummy/app/controllers/application_controller.rb
297
266
  - test/dummy/app/controllers/comments_controller.rb
298
267
  - test/dummy/app/controllers/registrations_controller.rb
@@ -356,11 +325,13 @@ files:
356
325
  - test/dummy/public/stylesheets/scaffold.css
357
326
  - test/dummy/script/rails
358
327
  - test/integration/navigation_test.rb
359
- - test/merit_unit_test.rb
360
- - test/sash_finder_test.rb
361
328
  - test/support/integration_case.rb
362
- - test/target_finder_test.rb
363
329
  - test/test_helper.rb
330
+ - test/unit/base_target_finder_test.rb
331
+ - test/unit/merit_unit_test.rb
332
+ - test/unit/rule_unit_test.rb
333
+ - test/unit/sash_finder_test.rb
334
+ - test/unit/target_finder_test.rb
364
335
  homepage: http://github.com/tute/merit
365
336
  licenses: []
366
337
  post_install_message:
data/Gemfile.lock DELETED
@@ -1,146 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- merit (1.5.0)
5
- ambry (~> 0.3.0)
6
-
7
- GEM
8
- remote: https://rubygems.org/
9
- specs:
10
- actionmailer (3.2.3)
11
- actionpack (= 3.2.3)
12
- mail (~> 2.4.4)
13
- actionpack (3.2.3)
14
- activemodel (= 3.2.3)
15
- activesupport (= 3.2.3)
16
- builder (~> 3.0.0)
17
- erubis (~> 2.7.0)
18
- journey (~> 1.0.1)
19
- rack (~> 1.4.0)
20
- rack-cache (~> 1.2)
21
- rack-test (~> 0.6.1)
22
- sprockets (~> 2.1.2)
23
- activemodel (3.2.3)
24
- activesupport (= 3.2.3)
25
- builder (~> 3.0.0)
26
- activerecord (3.2.3)
27
- activemodel (= 3.2.3)
28
- activesupport (= 3.2.3)
29
- arel (~> 3.0.2)
30
- tzinfo (~> 0.3.29)
31
- activeresource (3.2.3)
32
- activemodel (= 3.2.3)
33
- activesupport (= 3.2.3)
34
- activesupport (3.2.3)
35
- i18n (~> 0.6)
36
- multi_json (~> 1.0)
37
- addressable (2.2.7)
38
- ambry (0.3.1)
39
- arel (3.0.2)
40
- bson (1.6.2)
41
- bson_ext (1.6.2)
42
- bson (~> 1.6.2)
43
- builder (3.0.0)
44
- capybara (1.1.2)
45
- mime-types (>= 1.16)
46
- nokogiri (>= 1.3.3)
47
- rack (>= 1.0.0)
48
- rack-test (>= 0.5.4)
49
- selenium-webdriver (~> 2.0)
50
- xpath (~> 0.1.4)
51
- childprocess (0.3.2)
52
- ffi (~> 1.0.6)
53
- erubis (2.7.0)
54
- ffi (1.0.11)
55
- haml (3.1.4)
56
- hike (1.2.1)
57
- i18n (0.6.0)
58
- journey (1.0.3)
59
- json (1.7.0)
60
- libwebsocket (0.1.3)
61
- addressable
62
- mail (2.4.4)
63
- i18n (>= 0.4.0)
64
- mime-types (~> 1.16)
65
- treetop (~> 1.4.8)
66
- metaclass (0.0.1)
67
- mime-types (1.18)
68
- minitest (4.7.0)
69
- minitest-spec (0.0.2.1)
70
- minitest (>= 3.0)
71
- mocha (0.13.3)
72
- metaclass (~> 0.0.1)
73
- mongo (1.6.2)
74
- bson (~> 1.6.2)
75
- mongoid (2.0.2)
76
- activemodel (~> 3.0)
77
- mongo (~> 1.3)
78
- tzinfo (~> 0.3.22)
79
- multi_json (1.3.6)
80
- nokogiri (1.5.2)
81
- polyglot (0.3.3)
82
- rack (1.4.1)
83
- rack-cache (1.2)
84
- rack (>= 0.4)
85
- rack-ssl (1.3.2)
86
- rack
87
- rack-test (0.6.1)
88
- rack (>= 1.0)
89
- rails (3.2.3)
90
- actionmailer (= 3.2.3)
91
- actionpack (= 3.2.3)
92
- activerecord (= 3.2.3)
93
- activeresource (= 3.2.3)
94
- activesupport (= 3.2.3)
95
- bundler (~> 1.0)
96
- railties (= 3.2.3)
97
- railties (3.2.3)
98
- actionpack (= 3.2.3)
99
- activesupport (= 3.2.3)
100
- rack-ssl (~> 1.3.2)
101
- rake (>= 0.8.7)
102
- rdoc (~> 3.4)
103
- thor (~> 0.14.6)
104
- rake (10.0.4)
105
- rdoc (3.12)
106
- json (~> 1.4)
107
- rubyzip (0.9.8)
108
- selenium-webdriver (2.21.2)
109
- childprocess (>= 0.2.5)
110
- ffi (~> 1.0)
111
- libwebsocket (~> 0.1.3)
112
- multi_json (~> 1.0)
113
- rubyzip
114
- simplecov (0.6.4)
115
- multi_json (~> 1.0)
116
- simplecov-html (~> 0.5.3)
117
- simplecov-html (0.5.3)
118
- sprockets (2.1.3)
119
- hike (~> 1.2)
120
- rack (~> 1.0)
121
- tilt (~> 1.1, != 1.3.0)
122
- sqlite3 (1.3.6)
123
- thor (0.14.6)
124
- tilt (1.3.3)
125
- treetop (1.4.10)
126
- polyglot
127
- polyglot (>= 0.3.1)
128
- tzinfo (0.3.33)
129
- xpath (0.1.4)
130
- nokogiri (~> 1.3)
131
-
132
- PLATFORMS
133
- ruby
134
-
135
- DEPENDENCIES
136
- bson_ext
137
- capybara
138
- haml
139
- merit!
140
- minitest
141
- minitest-spec
142
- mocha (= 0.13.3)
143
- mongoid (~> 2.0.0)
144
- rails (~> 3.2.3)
145
- simplecov
146
- sqlite3