merit 3.0.3 → 4.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (79) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +1 -9
  3. data/NEWS.md +25 -0
  4. data/README.md +29 -10
  5. data/app/models/merit/badge.rb +1 -9
  6. data/lib/merit/{rules_badge_methods.rb → badge_rules_methods.rb} +0 -0
  7. data/lib/merit/base_target_finder.rb +1 -6
  8. data/lib/merit/{model_additions.rb → class_methods.rb} +0 -11
  9. data/lib/merit/controller_extensions.rb +4 -22
  10. data/lib/{generators → merit/generators}/active_record/install_generator.rb +8 -8
  11. data/lib/{generators → merit/generators}/active_record/merit_generator.rb +4 -4
  12. data/lib/{generators → merit/generators}/active_record/remove_generator.rb +5 -5
  13. data/lib/{generators/active_record/templates/add_merit_fields_to_model.rb → merit/generators/active_record/templates/add_merit_fields_to_model.erb} +0 -0
  14. data/lib/{generators/active_record/templates/create_badges_sashes.rb → merit/generators/active_record/templates/create_badges_sashes.erb} +0 -0
  15. data/lib/{generators/active_record/templates/create_merit_actions.rb → merit/generators/active_record/templates/create_merit_actions.erb} +2 -0
  16. data/lib/{generators/active_record/templates/create_merit_activity_logs.rb → merit/generators/active_record/templates/create_merit_activity_logs.erb} +0 -0
  17. data/lib/{generators/active_record/templates/create_sashes.rb → merit/generators/active_record/templates/create_sashes.erb} +0 -0
  18. data/lib/{generators/active_record/templates/create_scores_and_points.rb → merit/generators/active_record/templates/create_scores_and_points.erb} +1 -1
  19. data/lib/{generators/active_record/templates/remove_merit_fields_from_model.rb → merit/generators/active_record/templates/remove_merit_fields_from_model.erb} +0 -0
  20. data/lib/{generators/active_record/templates/remove_merit_tables.rb → merit/generators/active_record/templates/remove_merit_tables.erb} +0 -0
  21. data/lib/merit/generators/install_generator.rb +21 -0
  22. data/lib/{generators/merit → merit/generators}/merit_generator.rb +7 -2
  23. data/lib/{generators/merit → merit/generators}/remove_generator.rb +7 -2
  24. data/lib/{generators/merit/templates/merit.rb → merit/generators/templates/merit.erb} +13 -14
  25. data/lib/{generators/merit/templates/merit_badge_rules.rb → merit/generators/templates/merit_badge_rules.erb} +0 -0
  26. data/lib/{generators/merit/templates/merit_point_rules.rb → merit/generators/templates/merit_point_rules.erb} +0 -0
  27. data/lib/{generators/merit/templates/merit_rank_rules.rb → merit/generators/templates/merit_rank_rules.erb} +0 -0
  28. data/lib/merit/judge.rb +6 -5
  29. data/{app/models/merit/action.rb → lib/merit/models/action_concern.rb} +12 -10
  30. data/lib/merit/models/active_record/{merit/action.rb → action.rb} +5 -1
  31. data/lib/merit/models/active_record/{merit/activity_log.rb → activity_log.rb} +3 -1
  32. data/lib/merit/models/active_record/{merit/badges_sash.rb → badges_sash.rb} +4 -2
  33. data/lib/merit/models/active_record/{merit/sash.rb → sash.rb} +5 -2
  34. data/lib/merit/models/active_record/{merit/score.rb → score.rb} +4 -1
  35. data/lib/merit/models/badges_sash_concern.rb +13 -0
  36. data/lib/merit/models/base/badges_sash.rb +1 -1
  37. data/lib/merit/models/base/sash.rb +1 -1
  38. data/lib/merit/models/sash_concern.rb +53 -0
  39. data/lib/merit/{rules_points_methods.rb → point_rules_methods.rb} +0 -0
  40. data/lib/merit/{rules_rank_methods.rb → rank_rules_methods.rb} +3 -18
  41. data/lib/merit.rb +29 -56
  42. data/merit.gemspec +5 -2
  43. data/test/dummy/app/models/address.rb +1 -12
  44. data/test/dummy/app/models/comment.rb +3 -21
  45. data/test/dummy/app/models/user.rb +1 -14
  46. data/test/dummy/config/application.rb +5 -1
  47. data/test/dummy/config/application_api_only.rb +1 -1
  48. data/test/dummy/db/migrate/20130329224406_create_merit_actions.rb +2 -0
  49. data/test/dummy/db/schema.rb +4 -8
  50. data/test/integration/navigation_test.rb +3 -3
  51. data/test/test_helper.rb +0 -2
  52. data/test/unit/base_target_finder_test.rb +7 -26
  53. data/test/unit/merit_unit_test.rb +6 -8
  54. data/test/unit/rule_unit_test.rb +8 -8
  55. data/test/unit/rules_matcher_test.rb +9 -6
  56. data/test/unit/sash_finder_test.rb +5 -5
  57. data/test/unit/sash_test.rb +16 -0
  58. data/test/unit/score_test.rb +1 -1
  59. data/test/unit/target_finder_test.rb +13 -13
  60. metadata +64 -62
  61. data/UPGRADING.md +0 -256
  62. data/lib/generators/active_record/templates/add_target_data_to_merit_actions.rb +0 -5
  63. data/lib/generators/active_record/upgrade_generator.rb +0 -40
  64. data/lib/generators/merit/install_generator.rb +0 -16
  65. data/lib/generators/merit/upgrade_generator.rb +0 -7
  66. data/lib/merit/models/mongoid/merit/action.rb +0 -18
  67. data/lib/merit/models/mongoid/merit/activity_log.rb +0 -11
  68. data/lib/merit/models/mongoid/merit/badges_sash.rb +0 -20
  69. data/lib/merit/models/mongoid/merit/sash.rb +0 -30
  70. data/lib/merit/models/mongoid/merit/score.rb +0 -45
  71. data/test/dummy/db/migrate/20140906225844_create_players.rb +0 -8
  72. data/test/dummy/public/javascripts/application.js +0 -2
  73. data/test/dummy/public/javascripts/controls.js +0 -965
  74. data/test/dummy/public/javascripts/dragdrop.js +0 -974
  75. data/test/dummy/public/javascripts/effects.js +0 -1123
  76. data/test/dummy/public/javascripts/prototype.js +0 -6001
  77. data/test/dummy/public/javascripts/rails.js +0 -191
  78. data/test/orm_models/active_record.rb +0 -11
  79. data/test/orm_models/mongoid.rb +0 -15
@@ -0,0 +1,53 @@
1
+ module Merit::Models
2
+ module SashConcern
3
+ def badges
4
+ badge_ids.map { |id| Merit::Badge.find id }
5
+ end
6
+
7
+ def badge_ids
8
+ badges_sashes.map(&:badge_id)
9
+ end
10
+
11
+ def add_badge(badge_id)
12
+ bs = Merit::BadgesSash.new(badge_id: badge_id.to_i)
13
+ badges_sashes << bs
14
+ bs
15
+ end
16
+
17
+ def rm_badge(badge_id)
18
+ badges_sashes.where(badge_id: badge_id.to_i).first.try(:destroy)
19
+ end
20
+
21
+ # Retrieve the number of points from a category
22
+ # By default all points are summed up
23
+ # @param category [String] The category
24
+ # @return [Integer] The number of points
25
+ def points(options = {})
26
+ if (category = options[:category])
27
+ scores.where(category: category).first.try(:points) || 0
28
+ else
29
+ scores.reduce(0) { |sum, score| sum + score.points }
30
+ end
31
+ end
32
+
33
+ def add_points(num_points, options = {})
34
+ point = Merit::Score::Point.new
35
+ point.num_points = num_points
36
+ scores
37
+ .where(category: options[:category] || 'default')
38
+ .first_or_create
39
+ .score_points << point
40
+ point
41
+ end
42
+
43
+ def subtract_points(num_points, options = {})
44
+ add_points(-num_points, options)
45
+ end
46
+
47
+ private
48
+
49
+ def create_scores
50
+ scores << Merit::Score.create
51
+ end
52
+ end
53
+ end
@@ -44,30 +44,15 @@ module Merit
44
44
  private
45
45
 
46
46
  def grant_when_applies(scoped_model, rule, level)
47
- scope_to_promote(scoped_model, rule.level_name, level).each do |object|
47
+ scoped_model.where("#{rule.level_name} < #{level}").each do |object|
48
48
  next unless rule.applies?(object)
49
+
49
50
  object.update_attribute rule.level_name, level
50
51
  end
51
- rescue rank_exception
52
+ rescue ActiveRecord::StatementInvalid
52
53
  str = "Error while granting rankings. Probably you need to add
53
54
  #{rule.level_name} column to #{scoped_model.class.name}."
54
55
  raise RankAttributeNotDefined, str
55
56
  end
56
-
57
- def rank_exception
58
- if defined? ActiveRecord
59
- ActiveRecord::StatementInvalid
60
- else
61
- Exception
62
- end
63
- end
64
-
65
- def scope_to_promote(scope, level_name, level)
66
- if Merit.orm == :mongoid
67
- scope.where(:"#{level_name}".lt => level)
68
- else
69
- scope.where("#{level_name} < #{level}")
70
- end
71
- end
72
57
  end
73
58
  end
data/lib/merit.rb CHANGED
@@ -1,17 +1,4 @@
1
- require 'merit/rule'
2
- require 'merit/rules_badge_methods'
3
- require 'merit/rules_points_methods'
4
- require 'merit/rules_rank_methods'
5
- require 'merit/rules_matcher'
6
- require 'merit/controller_extensions'
7
- require 'merit/model_additions'
8
- require 'merit/judge'
9
- require 'merit/reputation_change_observer'
10
- require 'merit/sash_finder'
11
- require 'merit/base_target_finder'
12
- require 'merit/target_finder'
13
- require 'merit/models/base/sash'
14
- require 'merit/models/base/badges_sash'
1
+ require 'zeitwerk'
15
2
 
16
3
  module Merit
17
4
  def self.setup
@@ -24,7 +11,6 @@ module Merit
24
11
  @config.checks_on_each_request
25
12
  end
26
13
 
27
- # # Define ORM
28
14
  def self.orm
29
15
  @config.orm || :active_record
30
16
  end
@@ -49,9 +35,14 @@ module Merit
49
35
  @config.add_observer(class_name)
50
36
  end
51
37
 
52
- def self.upgrade_target_data_warning
53
- Rails.logger.warn '[merit] Missing column: target_data. Run `rails ' \
54
- 'generate merit:upgrade` and `rake db:migrate` to add it.'
38
+ # If the app is reloaded, avoid printing `warning: previous definition of AppBadgeRules was here`
39
+ def self.remove_badge_rules
40
+ remove_const(:AppBadgeRules) if self.const_defined?('AppBadgeRules')
41
+ end
42
+
43
+ # If the app is reloaded, avoid printing `warning: previous definition of AppPointRules was here`
44
+ def self.remove_point_rules
45
+ remove_const(:AppPointRules) if self.const_defined?('AppPointRules')
55
46
  end
56
47
 
57
48
  class Configuration
@@ -80,46 +71,28 @@ module Merit
80
71
  config.app_generators.orm Merit.orm
81
72
 
82
73
  initializer 'merit.controller' do |app|
83
- extend_orm_with_has_merit
84
- require_models
85
- ActiveSupport.on_load(action_controller_hook) do
86
- begin
87
- # Load app rules on boot up
88
- Merit::AppBadgeRules = Merit::BadgeRules.new.defined_rules
89
- Merit::AppPointRules = Merit::PointRules.new.defined_rules
90
- include Merit::ControllerExtensions
91
- rescue NameError => e
92
- # Trap NameError if installing/generating files
93
- raise e unless
94
- e.to_s =~ /uninitialized constant Merit::(BadgeRules|PointRules)/
74
+ config.to_prepare do
75
+ ActiveSupport.on_load(:active_record) { include Merit }
76
+ ActiveSupport.on_load(app.config.api_only ? :action_controller_api : :action_controller_base) do
77
+ begin
78
+ # Remove previous definitions of constant if they are defined when app reloads
79
+ Merit.remove_badge_rules
80
+ Merit.remove_point_rules
81
+ # Load app rules on boot up
82
+ Merit::AppBadgeRules = Merit::BadgeRules.new.defined_rules
83
+ Merit::AppPointRules = Merit::PointRules.new.defined_rules
84
+ include Merit::ControllerExtensions
85
+ rescue NameError => e
86
+ # Trap NameError if installing/generating files
87
+ raise e unless
88
+ e.to_s =~ /uninitialized constant Merit::(BadgeRules|PointRules)/
89
+ end
95
90
  end
96
91
  end
97
92
  end
98
-
99
- def require_models
100
- require 'merit/models/base/sash'
101
- require 'merit/models/base/badges_sash'
102
- require "merit/models/#{Merit.orm}/merit/activity_log"
103
- require "merit/models/#{Merit.orm}/merit/badges_sash"
104
- require "merit/models/#{Merit.orm}/merit/sash"
105
- require "merit/models/#{Merit.orm}/merit/score"
106
- end
107
-
108
- def extend_orm_with_has_merit
109
- if Object.const_defined?('ActiveRecord')
110
- ActiveRecord::Base.send :include, Merit
111
- end
112
- if Object.const_defined?('Mongoid')
113
- Mongoid::Document.send :include, Merit
114
- end
115
- end
116
-
117
- def action_controller_hook
118
- if Rails.application.config.api_only
119
- :action_controller_api
120
- else
121
- :action_controller_base
122
- end
123
- end
124
93
  end
125
94
  end
95
+
96
+ loader = Zeitwerk::Loader.for_gem
97
+ loader.setup
98
+ loader.eager_load
data/merit.gemspec CHANGED
@@ -6,17 +6,20 @@ Gem::Specification.new do |s|
6
6
  s.files = `git ls-files`.split("\n").reject{|f| f =~ /^\./ }
7
7
  s.test_files = `git ls-files -- test/*`.split("\n")
8
8
  s.license = 'MIT'
9
- s.version = '3.0.3'
9
+ s.version = '4.0.3'
10
10
  s.authors = ["Tute Costa"]
11
11
  s.email = 'tutecosta@gmail.com'
12
12
 
13
13
  s.required_ruby_version = '>= 2.3.0'
14
14
 
15
- s.add_dependency 'ambry', '~> 1.0.0'
15
+ s.add_runtime_dependency 'ambry', '~> 1.0.0'
16
+ s.add_runtime_dependency 'zeitwerk'
17
+
16
18
  s.add_development_dependency 'rails', '>= 5.1.6'
17
19
  s.add_development_dependency 'capybara'
18
20
  s.add_development_dependency 'simplecov'
19
21
  s.add_development_dependency 'rubocop'
20
22
  s.add_development_dependency 'minitest-rails'
21
23
  s.add_development_dependency 'mocha'
24
+ s.add_development_dependency 'webrick'
22
25
  end
@@ -1,14 +1,3 @@
1
- case Merit.orm
2
- when :active_record
3
- class Address < ActiveRecord::Base
4
- end
5
- when :mongoid
6
- class Address
7
- include Mongoid::Document
8
- include Mongoid::Timestamps
9
- end
10
- end
11
-
12
- class Address
1
+ class Address < ActiveRecord::Base
13
2
  belongs_to :user
14
3
  end
@@ -1,26 +1,8 @@
1
- case Merit.orm
2
- when :active_record
3
- class Comment < ActiveRecord::Base
4
- def friend
5
- User.find_by_name('friend')
6
- end
1
+ class Comment < ActiveRecord::Base
2
+ def friend
3
+ User.find_by_name('friend')
7
4
  end
8
- when :mongoid
9
- class Comment
10
- include Mongoid::Document
11
- include Mongoid::Timestamps
12
5
 
13
- field :name, :type => String
14
- field :comment, :type => String
15
- field :votes, :type => Integer, :default => 0
16
-
17
- def friend
18
- User.find_by(name: 'friend')
19
- end
20
- end
21
- end
22
-
23
- class Comment
24
6
  has_merit
25
7
 
26
8
  belongs_to :user
@@ -1,17 +1,4 @@
1
- case Merit.orm
2
- when :active_record
3
- class User < ActiveRecord::Base
4
- end
5
- when :mongoid
6
- class User
7
- include Mongoid::Document
8
- include Mongoid::Timestamps
9
-
10
- field :name, :type => String
11
- end
12
- end
13
-
14
- class User
1
+ class User < ActiveRecord::Base
15
2
  has_merit
16
3
 
17
4
  has_many :addresses
@@ -10,11 +10,15 @@ Bundler.require
10
10
  require "merit"
11
11
 
12
12
  module Dummy
13
- class Application < Rails::Application
13
+ class Application < ::Rails::Application
14
14
  if Rails.version.match? "5.2.+"
15
15
  config.active_record.sqlite3.represent_boolean_as_integer = true
16
16
  end
17
17
 
18
+ if Rails.version.match? "6.0.+"
19
+ config.load_defaults "6.0"
20
+ end
21
+
18
22
  config.i18n.enforce_available_locales = true
19
23
  config.encoding = "utf-8"
20
24
  config.filter_parameters += [:password]
@@ -19,7 +19,7 @@ Bundler.require
19
19
  require "merit"
20
20
 
21
21
  module Dummy
22
- class Application < Rails::Application
22
+ class Application < ::Rails::Application
23
23
  config.load_defaults 5.2 if ENV["RAILS_VERSION"] =~ /^5.2/
24
24
  config.api_only = true
25
25
  config.i18n.enforce_available_locales = true
@@ -10,6 +10,8 @@ class CreateMeritActions < ActiveRecord::Migration[5.0]
10
10
  t.boolean :processed, :default => false
11
11
  t.timestamps null: false
12
12
  end
13
+
14
+ add_index :merit_actions, :processed
13
15
  end
14
16
 
15
17
  def self.down
@@ -2,15 +2,15 @@
2
2
  # of editing this file, please use the migrations feature of Active Record to
3
3
  # incrementally modify your database, and then regenerate this schema definition.
4
4
  #
5
- # This file is the source Rails uses to define your schema when running `rails
6
- # db:schema:load`. When creating a new database, `rails db:schema:load` tends to
5
+ # This file is the source Rails uses to define your schema when running `bin/rails
6
+ # db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to
7
7
  # be faster and is potentially less error prone than running all of your
8
8
  # migrations from scratch. Old migrations may fail to apply correctly if those
9
9
  # migrations use external dependencies or application code.
10
10
  #
11
11
  # It's strongly recommended that you check this file into your version control system.
12
12
 
13
- ActiveRecord::Schema.define(version: 2014_09_06_225844) do
13
+ ActiveRecord::Schema.define(version: 2014_08_19_133931) do
14
14
 
15
15
  create_table "addresses", force: :cascade do |t|
16
16
  t.integer "user_id"
@@ -49,6 +49,7 @@ ActiveRecord::Schema.define(version: 2014_09_06_225844) do
49
49
  t.datetime "created_at", null: false
50
50
  t.datetime "updated_at", null: false
51
51
  t.text "target_data"
52
+ t.index ["processed"], name: "index_merit_actions_on_processed"
52
53
  end
53
54
 
54
55
  create_table "merit_activity_logs", force: :cascade do |t|
@@ -73,11 +74,6 @@ ActiveRecord::Schema.define(version: 2014_09_06_225844) do
73
74
  t.index ["sash_id"], name: "index_merit_scores_on_sash_id"
74
75
  end
75
76
 
76
- create_table "players", force: :cascade do |t|
77
- t.integer "sash_id"
78
- t.integer "level", default: 0
79
- end
80
-
81
77
  create_table "sashes", force: :cascade do |t|
82
78
  t.datetime "created_at", null: false
83
79
  t.datetime "updated_at", null: false
@@ -180,7 +180,7 @@ class NavigationTest < ActionDispatch::IntegrationTest
180
180
 
181
181
  visit "/users/#{user.id}/edit"
182
182
  fill_in 'Name', with: 'a'
183
- assert_difference('Merit::ActivityLog.count', 2) do
183
+ assert_difference('Merit::ActivityLog.count', 1) do
184
184
  click_button('Update User')
185
185
  end
186
186
 
@@ -305,8 +305,8 @@ class NavigationTest < ActionDispatch::IntegrationTest
305
305
  end
306
306
  end
307
307
 
308
- comment_1.reload.points.must_be :==, 2
309
- comment_2.reload.points.must_be :==, 2
308
+ _(comment_1.reload.points).must_be :==, 2
309
+ _(comment_2.reload.points).must_be :==, 2
310
310
  end
311
311
 
312
312
  test 'api/comments#show should grant 1 point to user' do
data/test/test_helper.rb CHANGED
@@ -38,5 +38,3 @@ Capybara.default_selector = :css
38
38
 
39
39
  # Load support files
40
40
  Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
41
-
42
- Merit.orm = :active_record if Merit.orm.nil?
@@ -14,7 +14,7 @@ describe Merit::BaseTargetFinder do
14
14
 
15
15
  finder = Merit::BaseTargetFinder.new(rule, action)
16
16
  collection = finder.find
17
- collection.must_be :==, comment
17
+ _(collection).must_be :==, comment
18
18
  end
19
19
  end
20
20
 
@@ -22,13 +22,13 @@ describe Merit::BaseTargetFinder do
22
22
  it 'should fall back to the action#target_model' do
23
23
  rule = Merit::Rule.new
24
24
  rule.to = :itself
25
- action = Merit::Action.new(target_model: 'players', target_id: 3)
26
- user = Player.new(id: 3)
25
+ action = Merit::Action.new(target_model: 'users', target_id: 3)
26
+ user = User.new(id: 3)
27
27
 
28
- Player.stubs(:find_by_id).with(3).returns(user)
28
+ User.stubs(:find_by_id).with(3).returns(user)
29
29
 
30
30
  finder = Merit::BaseTargetFinder.new(rule, action)
31
- finder.find.must_be :==, user
31
+ _(finder.find).must_be :==, user
32
32
  end
33
33
  end
34
34
 
@@ -41,7 +41,7 @@ describe Merit::BaseTargetFinder do
41
41
 
42
42
  finder = Merit::BaseTargetFinder.new(rule, action)
43
43
  Rails.logger.expects(:warn)
44
- finder.find.must_be_nil
44
+ _(finder.find).must_be_nil
45
45
  end
46
46
  end
47
47
 
@@ -57,26 +57,7 @@ describe Merit::BaseTargetFinder do
57
57
  target_data: comment.to_yaml)
58
58
 
59
59
  finder = Merit::BaseTargetFinder.new(rule, action)
60
- finder.find.name.must_be :==, 'the comment name'
61
- end
62
- end
63
-
64
- describe 'warns when the target_data column has not been created' do
65
- it 'sends a message to the logger' do
66
- comment = Comment.new(name: 'the comment name')
67
-
68
- rule = Merit::Rule.new
69
- rule.to = :itself
70
- rule.model_name = 'comment'
71
- action = Merit::Action.new(target_model: 'comment',
72
- target_id: 2,
73
- target_data: comment.to_yaml)
74
- action.stubs(:respond_to?).with(:target_data).returns(false)
75
-
76
- Rails.logger.expects(:warn)
77
-
78
- finder = Merit::BaseTargetFinder.new(rule, action)
79
- finder.reanimate_target_from_action
60
+ _(finder.find.name).must_be :==, 'the comment name'
80
61
  end
81
62
  end
82
63
  end
@@ -1,24 +1,22 @@
1
1
  require 'test_helper'
2
2
 
3
- # TODO: Split different objects tests in it's own files
4
3
  class MeritUnitTest < ActiveSupport::TestCase
5
- require "orm_models/#{Merit.orm}"
6
-
7
4
  test 'extends only meritable models' do
8
- assert Player.method_defined?(:points), 'has_merit adds methods'
9
- assert !Fruit.method_defined?(:points), 'other models aren\'t extended'
5
+ assert User.method_defined?(:points), 'has_merit adds methods'
6
+ assert !Address.method_defined?(:points), 'other models aren\'t extended'
10
7
  end
11
8
 
12
9
  test 'Badges get "related_models" methods' do
13
- assert Merit::Badge.method_defined?(:soldiers), 'Badge#soldiers should be defined'
14
- assert Merit::Badge.method_defined?(:players), 'Badge#players should be defined'
10
+ Comment.new; User.new # load meritable classes
11
+ assert Merit::Badge.method_defined?(:comments), 'Badge#comments should be defined'
12
+ assert Merit::Badge.method_defined?(:users), 'Badge#users should be defined'
15
13
  end
16
14
 
17
15
  test 'unknown ranking raises exception' do
18
16
  class WeirdRankRules
19
17
  include Merit::RankRulesMethods
20
18
  def initialize
21
- set_rank level: 1, to: Player, level_name: :clown
19
+ set_rank level: 1, to: User, level_name: :clown
22
20
  end
23
21
  end
24
22
  assert_raises Merit::RankAttributeNotDefined do
@@ -11,47 +11,47 @@ describe Merit::Rule do
11
11
  end
12
12
 
13
13
  it 'truthy block should make rule apply' do
14
- @rule.applies?('str').must_be :==, true
14
+ _(@rule.applies?('str')).must_be :==, true
15
15
  end
16
16
 
17
17
  it 'falsy block should make rule fail' do
18
- @rule.applies?('string').must_be :==, false
18
+ _(@rule.applies?('string')).must_be :==, false
19
19
  end
20
20
 
21
21
  it 'block needs parameter for rule to pass' do
22
- @rule.applies?.must_be :==, false
22
+ _(@rule.applies?).must_be :==, false
23
23
  end
24
24
  end
25
25
 
26
26
  describe '#applies (without block)' do
27
27
  it 'empty condition should make rule apply' do
28
- @rule.applies?.must_be :==, true
28
+ _(@rule.applies?).must_be :==, true
29
29
  end
30
30
  end
31
31
 
32
32
  describe '#badge' do
33
33
  it 'raises exception on inexistent badge' do
34
34
  @rule.badge_name = 'inexistent'
35
- -> { @rule.badge }.must_raise Merit::BadgeNotFound
35
+ _(-> { @rule.badge }).must_raise Merit::BadgeNotFound
36
36
  end
37
37
 
38
38
  it 'finds related badge by name, when the name is a string' do
39
39
  Merit::Badge.create(id: 98, name: 'test-badge-98')
40
40
  @rule.badge_name = "test-badge-98"
41
- @rule.badge.must_be :==, Merit::Badge.find(98)
41
+ _(@rule.badge).must_be :==, Merit::Badge.find(98)
42
42
  end
43
43
 
44
44
  it 'finds related badge by name, when the name is a symbol' do
45
45
  Merit::Badge.create(id: 100, name: :testbadge)
46
46
  @rule.badge_name = 'testbadge'
47
- @rule.badge.must_be :==, Merit::Badge.find(100)
47
+ _(@rule.badge).must_be :==, Merit::Badge.find(100)
48
48
  @rule.badge
49
49
  end
50
50
 
51
51
  it 'finds related badge by name' do
52
52
  Merit::Badge.create(id: 98, name: 'test-badge-98')
53
53
  @rule.badge_id = 98
54
- @rule.badge.must_be :==, Merit::Badge.find(98)
54
+ _(@rule.badge).must_be :==, Merit::Badge.find(98)
55
55
  end
56
56
  end
57
57
  end
@@ -5,29 +5,32 @@ describe Merit::RulesMatcher do
5
5
  describe 'rules actions are treated as a regexp' do
6
6
  it 'selects matching rules (suffix)' do
7
7
  matcher = Merit::RulesMatcher.new('comments', 'update')
8
- matcher.select_from(
8
+ selected = matcher.select_from(
9
9
  'comments#update' => 'comments#update',
10
10
  'comments#up' => 'comments#up',
11
11
  'comments#up$' => 'comments#up$',
12
12
  'comments#up.+$' => 'comments#up.+$',
13
- ).must_be :==, ['comments#update', 'comments#up.+$']
13
+ )
14
+ _(selected).must_be :==, ['comments#update', 'comments#up.+$']
14
15
 
15
16
  matcher = Merit::RulesMatcher.new('comments', 'up')
16
- matcher.select_from(
17
+ selected = matcher.select_from(
17
18
  'comments#update' => 'comments#update',
18
19
  'comments#up' => 'comments#up',
19
20
  'comments#up$' => 'comments#up$',
20
21
  'comments#up.+$' => 'comments#up.+$',
21
- ).must_be :==, ['comments#up', 'comments#up$']
22
+ )
23
+ _(selected).must_be :==, ['comments#up', 'comments#up$']
22
24
  end
23
25
 
24
26
  it 'selects matching rules (prefix)' do
25
27
  matcher = Merit::RulesMatcher.new('/posts/1/comments', 'create')
26
- matcher.select_from(
28
+ selected = matcher.select_from(
27
29
  'comments#create' => 'comments#create',
28
30
  '^comments#create' => '^comments#create',
29
31
  '^.*/comments#create' => '^.*/comments#create',
30
- ).must_be :==, ['^.*/comments#create']
32
+ )
33
+ _(selected).must_be :==, ['^.*/comments#create']
31
34
  end
32
35
  end
33
36
 
@@ -3,11 +3,11 @@ require 'test_helper'
3
3
  describe Merit::SashFinder do
4
4
  it 'should return an array of sashes of the target objects' do
5
5
  sash_1 = Merit::Sash.new
6
- user_1 = Player.new
6
+ user_1 = User.new
7
7
  user_1.stubs(:_sash).returns(sash_1)
8
8
 
9
9
  sash_2 = Merit::Sash.new
10
- user_2 = Player.new
10
+ user_2 = User.new
11
11
  user_2.stubs(:_sash).returns(sash_2)
12
12
 
13
13
  # TODO: With stub we are not exercising compact
@@ -20,8 +20,8 @@ describe Merit::SashFinder do
20
20
 
21
21
  Merit::SashFinder.stubs(:targets).returns(users)
22
22
  sashes = Merit::SashFinder.find(rule, action)
23
- sashes.count.must_be :==, 2
24
- sashes.must_include sash_1
25
- sashes.must_include sash_2
23
+ _(sashes.count).must_be :==, 2
24
+ _(sashes).must_include sash_1
25
+ _(sashes).must_include sash_2
26
26
  end
27
27
  end
@@ -6,6 +6,22 @@ class SashTest < ActiveSupport::TestCase
6
6
  @sash = Merit::Sash.create
7
7
  end
8
8
 
9
+ describe "#rm_badge" do
10
+ describe "when has badge" do
11
+ it "returns truthy" do
12
+ @sash.badges_sashes.create!(badge_id: 1)
13
+
14
+ assert_equal !!@sash.rm_badge(1), true
15
+ end
16
+ end
17
+
18
+ describe "when does NOT have badge" do
19
+ it "returns falsey" do
20
+ assert_equal !!@sash.rm_badge(0), false
21
+ end
22
+ end
23
+ end
24
+
9
25
  describe "#add_points" do
10
26
  describe "when category specified" do
11
27
  it "should create a new Point with specified category" do
@@ -8,6 +8,6 @@ describe Merit::Score do
8
8
  point = Merit::Score::Point.new
9
9
  point.score = score
10
10
 
11
- point.sash_id.must_be :==, score.sash_id
11
+ _(point.sash_id).must_be :==, score.sash_id
12
12
  end
13
13
  end