merit 3.0.3 → 4.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (76) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +1 -9
  3. data/NEWS.md +7 -0
  4. data/README.md +14 -2
  5. data/app/models/merit/badge.rb +1 -9
  6. data/lib/merit.rb +6 -36
  7. data/lib/merit/{rules_badge_methods.rb → badge_rules_methods.rb} +0 -0
  8. data/lib/merit/base_target_finder.rb +1 -6
  9. data/lib/merit/{model_additions.rb → class_methods.rb} +0 -11
  10. data/lib/merit/controller_extensions.rb +4 -22
  11. data/lib/{generators → merit/generators}/active_record/install_generator.rb +8 -8
  12. data/lib/{generators → merit/generators}/active_record/merit_generator.rb +3 -3
  13. data/lib/{generators → merit/generators}/active_record/remove_generator.rb +5 -5
  14. 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
  15. data/lib/{generators/active_record/templates/create_badges_sashes.rb → merit/generators/active_record/templates/create_badges_sashes.erb} +0 -0
  16. data/lib/{generators/active_record/templates/create_merit_actions.rb → merit/generators/active_record/templates/create_merit_actions.erb} +0 -0
  17. data/lib/{generators/active_record/templates/create_merit_activity_logs.rb → merit/generators/active_record/templates/create_merit_activity_logs.erb} +0 -0
  18. data/lib/{generators/active_record/templates/create_sashes.rb → merit/generators/active_record/templates/create_sashes.erb} +0 -0
  19. data/lib/{generators/active_record/templates/create_scores_and_points.rb → merit/generators/active_record/templates/create_scores_and_points.erb} +1 -1
  20. 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
  21. data/lib/{generators/active_record/templates/remove_merit_tables.rb → merit/generators/active_record/templates/remove_merit_tables.erb} +0 -0
  22. data/lib/merit/generators/install_generator.rb +18 -0
  23. data/lib/{generators/merit → merit/generators}/merit_generator.rb +3 -1
  24. data/lib/{generators/merit → merit/generators}/remove_generator.rb +3 -1
  25. data/lib/{generators/merit/templates/merit.rb → merit/generators/templates/merit.erb} +0 -3
  26. data/lib/{generators/merit/templates/merit_badge_rules.rb → merit/generators/templates/merit_badge_rules.erb} +0 -0
  27. data/lib/{generators/merit/templates/merit_point_rules.rb → merit/generators/templates/merit_point_rules.erb} +0 -0
  28. data/lib/{generators/merit/templates/merit_rank_rules.rb → merit/generators/templates/merit_rank_rules.erb} +0 -0
  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/merit.gemspec +4 -2
  42. data/test/dummy/app/models/address.rb +1 -12
  43. data/test/dummy/app/models/comment.rb +3 -21
  44. data/test/dummy/app/models/user.rb +1 -14
  45. data/test/dummy/config/application.rb +5 -1
  46. data/test/dummy/config/application_api_only.rb +1 -1
  47. data/test/dummy/db/schema.rb +1 -6
  48. data/test/integration/navigation_test.rb +2 -2
  49. data/test/test_helper.rb +0 -2
  50. data/test/unit/base_target_finder_test.rb +7 -26
  51. data/test/unit/merit_unit_test.rb +6 -8
  52. data/test/unit/rule_unit_test.rb +8 -8
  53. data/test/unit/rules_matcher_test.rb +9 -6
  54. data/test/unit/sash_finder_test.rb +5 -5
  55. data/test/unit/score_test.rb +1 -1
  56. data/test/unit/target_finder_test.rb +13 -13
  57. metadata +47 -59
  58. data/UPGRADING.md +0 -256
  59. data/lib/generators/active_record/templates/add_target_data_to_merit_actions.rb +0 -5
  60. data/lib/generators/active_record/upgrade_generator.rb +0 -40
  61. data/lib/generators/merit/install_generator.rb +0 -16
  62. data/lib/generators/merit/upgrade_generator.rb +0 -7
  63. data/lib/merit/models/mongoid/merit/action.rb +0 -18
  64. data/lib/merit/models/mongoid/merit/activity_log.rb +0 -11
  65. data/lib/merit/models/mongoid/merit/badges_sash.rb +0 -20
  66. data/lib/merit/models/mongoid/merit/sash.rb +0 -30
  67. data/lib/merit/models/mongoid/merit/score.rb +0 -45
  68. data/test/dummy/db/migrate/20140906225844_create_players.rb +0 -8
  69. data/test/dummy/public/javascripts/application.js +0 -2
  70. data/test/dummy/public/javascripts/controls.js +0 -965
  71. data/test/dummy/public/javascripts/dragdrop.js +0 -974
  72. data/test/dummy/public/javascripts/effects.js +0 -1123
  73. data/test/dummy/public/javascripts/prototype.js +0 -6001
  74. data/test/dummy/public/javascripts/rails.js +0 -191
  75. data/test/orm_models/active_record.rb +0 -11
  76. data/test/orm_models/mongoid.rb +0 -15
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a847a60ecad264aa85e3118fa9e26a974de2e08142577fe134c12e48c4989faf
4
- data.tar.gz: 7f219282c0e6c4f33e10f2b77040ef19dbec9874e11140c47cc216fb17c9665b
3
+ metadata.gz: f0ee557f98d45fe9a83d5de23d407e2b36f85f6a5d4204b1ed89fec8bf5c90fb
4
+ data.tar.gz: ae393531c2a569a8aece0d533523aaa6fa6e407c4fe4aa3efe96f49d349b3d37
5
5
  SHA512:
6
- metadata.gz: 8b2714cb9b1c672ba79a1d1034ac6f53bf66c544d568d358c2fe98f3a42801589db9a1213c7eb36abe03a51f8392b4929d52db06549d8786d6b7bfe125e48486
7
- data.tar.gz: 2c062006a8352ad9bc7bce01db34851a30e4b7b8fce7de19c6dc4377366e743832ad0507e88b498ba1ed9f6f498e787ebddbb6f2c27577d1870c2dd08d0e6679
6
+ metadata.gz: c4b48c33d57774076e00d9732ccac73dbb71b1ee1efe87329ba9ee542593e1a8c3823bef1c5c783a45ed46b28cc3eda4626ddefe9683682341ccc8d51bcf9889
7
+ data.tar.gz: 0e886c1d895343875d06c537015d4ae14b0723f96d8ddac2a264892b7a3aee0d821f7e6ccb404f68371ac9ca9d5adad78f211b1943747a9bc6dc53361efd6cb6
data/Gemfile CHANGED
@@ -4,16 +4,8 @@ gemspec
4
4
 
5
5
  gem 'rails', "~> #{ENV.fetch('RAILS_VERSION', 6.0)}"
6
6
 
7
- case ENV['ORM']
8
- when 'active_record'
9
- gem 'activerecord'
10
- when 'mongoid'
11
- gem 'mongoid'
12
- end
13
-
14
7
  group :development, :test do
15
- gem 'activerecord-jdbcsqlite3-adapter', :platforms => [:jruby]
16
8
  gem 'sqlite3', '~> 1.4'
17
9
  end
18
10
 
19
- gem 'coveralls', require: false
11
+ gem 'coveralls', '~> 0.8.23', require: false
data/NEWS.md CHANGED
@@ -2,6 +2,13 @@
2
2
 
3
3
  User-visible changes worth mentioning.
4
4
 
5
+ ## 4.0.0
6
+
7
+ - Stop testing on Rails 5.1
8
+ - Drop support for mongoid
9
+ - Drop v2 upgrade generators
10
+ - Enable Zeitwerk gem mode (and renames internal merit constants)
11
+
5
12
  ## 3.0.3
6
13
 
7
14
  - Test against Ruby 2.6 and Rails 6
data/README.md CHANGED
@@ -38,8 +38,7 @@ and Rankings.
38
38
  2. Run `rails g merit:install`. This creates several migrations.
39
39
  3. Run `rails g merit MODEL_NAME` (e.g. `user`). This creates a migration and adds `has_merit` to MODEL_NAME.
40
40
  4. Run `rake db:migrate`
41
- 5. Define badges in `config/initializers/merit.rb`. You can also define ORM:
42
- `:active_record` (default) or `:mongoid`.
41
+ 5. Define badges in `config/initializers/merit.rb`
43
42
  6. Configure reputation rules for your application in `app/models/merit/*`
44
43
 
45
44
 
@@ -114,6 +113,19 @@ grant_on ['users#create', 'users#update'], badge: 'autobiographer', temporary: t
114
113
  end
115
114
  ```
116
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
+
117
129
  ## Other Actions
118
130
 
119
131
  ```ruby
@@ -26,14 +26,6 @@ module Merit
26
26
  end
27
27
  end
28
28
 
29
- def _mongoid_sash_in(sashes)
30
- {:sash_id.in => sashes}
31
- end
32
-
33
- def _active_record_sash_in(sashes)
34
- {sash_id: sashes}
35
- end
36
-
37
29
  class << self
38
30
  def find_by_name_and_level(name, level)
39
31
  badges = Merit::Badge.by_name(name)
@@ -51,7 +43,7 @@ module Merit
51
43
  def _define_related_entries_method(meritable_class_name)
52
44
  define_method(:"#{meritable_class_name.underscore.pluralize}") do
53
45
  sashes = BadgesSash.where(badge_id: id).pluck(:sash_id)
54
- meritable_class_name.constantize.where(send "_#{Merit.orm}_sash_in", sashes)
46
+ meritable_class_name.constantize.where(sash_id: sashes)
55
47
  end
56
48
  end
57
49
  end
@@ -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,11 +35,6 @@ 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.'
55
- end
56
-
57
38
  class Configuration
58
39
  attr_accessor :checks_on_each_request, :orm, :user_model_name, :observers,
59
40
  :current_user_method
@@ -81,7 +62,6 @@ module Merit
81
62
 
82
63
  initializer 'merit.controller' do |app|
83
64
  extend_orm_with_has_merit
84
- require_models
85
65
  ActiveSupport.on_load(action_controller_hook) do
86
66
  begin
87
67
  # Load app rules on boot up
@@ -96,22 +76,8 @@ module Merit
96
76
  end
97
77
  end
98
78
 
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
79
  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
80
+ ActiveRecord::Base.include(Merit)
115
81
  end
116
82
 
117
83
  def action_controller_hook
@@ -123,3 +89,7 @@ module Merit
123
89
  end
124
90
  end
125
91
  end
92
+
93
+ loader = Zeitwerk::Loader.for_gem
94
+ loader.setup
95
+ loader.eager_load
@@ -25,12 +25,7 @@ module Merit
25
25
  end
26
26
 
27
27
  def reanimate_target_from_action
28
- if @action.respond_to? :target_data
29
- YAML.load(@action.target_data)
30
- else
31
- Merit.upgrade_target_data_warning
32
- nil
33
- end
28
+ YAML.load(@action.target_data)
34
29
  end
35
30
  end
36
31
  end
@@ -9,7 +9,6 @@ module Merit
9
9
  # https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/1079-belongs_to-dependent-destroy-should-destroy-self-before-assocation
10
10
  belongs_to :sash, class_name: 'Merit::Sash', inverse_of: nil, optional: true
11
11
 
12
- send :"_merit_#{Merit.orm}_specific_config"
13
12
  _merit_delegate_methods_to_sash
14
13
  _merit_define_badge_related_entries_method
15
14
  _merit_sash_initializer
@@ -22,16 +21,6 @@ module Merit
22
21
  methods.each { |method| delegate method, to: :_sash }
23
22
  end
24
23
 
25
- def _merit_active_record_specific_config
26
- end
27
-
28
- def _merit_mongoid_specific_config
29
- field :level, type: Integer, default: 0
30
- def find_by_id(id)
31
- where(_id: id).first
32
- end
33
- end
34
-
35
24
  def _merit_define_badge_related_entries_method
36
25
  meritable_class_name = name.demodulize
37
26
  Merit::Badge._define_related_entries_method(meritable_class_name)
@@ -27,16 +27,8 @@ module Merit
27
27
  had_errors: had_errors?,
28
28
  target_model: controller_path,
29
29
  target_id: target_id,
30
- }.merge(target_data_hash)
31
- end
32
-
33
- def target_data_hash
34
- if Merit::Action.new.respond_to?(:target_data)
35
- { target_data: target_object.to_yaml }
36
- else
37
- Merit.upgrade_target_data_warning
38
- {}
39
- end
30
+ target_data: target_object.to_yaml,
31
+ }
40
32
  end
41
33
 
42
34
  def rules_defined?
@@ -69,21 +61,11 @@ module Merit
69
61
 
70
62
  def target_id
71
63
  target_id = target_object.try(:id)
72
- # If target_id is nil
73
- # then use params[:id].
74
- if target_id.nil? && send("check_#{Merit.orm}_id", params[:id])
64
+ # If target_id is nil use (only digits of) params[:id]
65
+ if target_id.nil? && params[:id].to_s =~ /^[0-9]+$/
75
66
  target_id = params[:id]
76
67
  end
77
68
  target_id
78
69
  end
79
-
80
- # This check avoids trying to set a slug as integer FK
81
- def check_active_record_id(id)
82
- id.to_s =~ /^[0-9]+$/
83
- end
84
-
85
- def check_mongoid_id(id)
86
- id.to_s =~ /^[0-9a-fA-F]{24}$/
87
- end
88
70
  end
89
71
  end
@@ -1,8 +1,8 @@
1
1
  require 'rails/generators/active_record'
2
2
 
3
- module ActiveRecord
4
- module Generators
5
- class InstallGenerator < Rails::Generators::Base
3
+ module Merit
4
+ module Generators::ActiveRecord
5
+ class InstallGenerator < ::Rails::Generators::Base
6
6
  include Rails::Generators::Migration
7
7
 
8
8
  source_root File.expand_path('../templates', __FILE__)
@@ -13,19 +13,19 @@ module ActiveRecord
13
13
  end
14
14
 
15
15
  def copy_migrations_and_model
16
- migration_template 'create_merit_actions.rb',
16
+ migration_template 'create_merit_actions.erb',
17
17
  'db/migrate/create_merit_actions.rb'
18
18
 
19
- migration_template 'create_merit_activity_logs.rb',
19
+ migration_template 'create_merit_activity_logs.erb',
20
20
  'db/migrate/create_merit_activity_logs.rb'
21
21
 
22
- migration_template 'create_sashes.rb',
22
+ migration_template 'create_sashes.erb',
23
23
  'db/migrate/create_sashes.rb'
24
24
 
25
- migration_template 'create_badges_sashes.rb',
25
+ migration_template 'create_badges_sashes.erb',
26
26
  'db/migrate/create_badges_sashes.rb'
27
27
 
28
- migration_template 'create_scores_and_points.rb',
28
+ migration_template 'create_scores_and_points.erb',
29
29
  'db/migrate/create_scores_and_points.rb'
30
30
  end
31
31
 
@@ -1,8 +1,8 @@
1
1
  require 'rails/generators/active_record'
2
2
 
3
- module ActiveRecord
4
- module Generators
5
- class MeritGenerator < ActiveRecord::Generators::Base
3
+ module Merit
4
+ module Generators::ActiveRecord
5
+ class MeritGenerator < ::ActiveRecord::Generators::Base
6
6
  include Rails::Generators::Migration
7
7
 
8
8
  source_root File.expand_path('../templates', __FILE__)
@@ -1,8 +1,8 @@
1
1
  require 'rails/generators/active_record'
2
2
 
3
- module ActiveRecord
4
- module Generators
5
- class RemoveGenerator < ActiveRecord::Generators::Base
3
+ module Merit
4
+ module Generators::ActiveRecord
5
+ class RemoveGenerator < ::ActiveRecord::Generators::Base
6
6
  include Rails::Generators::Migration
7
7
 
8
8
  source_root File.expand_path('../templates', __FILE__)
@@ -13,11 +13,11 @@ module ActiveRecord
13
13
  end
14
14
 
15
15
  def copy_migrations_and_model
16
- migration_template 'remove_merit_tables.rb',
16
+ migration_template 'remove_merit_tables.erb',
17
17
  'db/migrate/remove_merit_tables.rb'
18
18
 
19
19
  migration_template(
20
- 'remove_merit_fields_from_model.rb',
20
+ 'remove_merit_fields_from_model.erb',
21
21
  "db/migrate/remove_merit_fields_from_#{table_name}.rb"
22
22
  )
23
23
  end
@@ -7,7 +7,7 @@ class CreateScoresAndPoints < ActiveRecord::Migration<%= migration_version %>
7
7
 
8
8
  create_table :merit_score_points do |t|
9
9
  t.references :score
10
- t.integer :num_points, default: 0
10
+ t.bigint :num_points, default: 0
11
11
  t.string :log
12
12
  t.datetime :created_at
13
13
  end
@@ -0,0 +1,18 @@
1
+ require "rails/generators"
2
+
3
+ module Merit
4
+ module Generators
5
+ class InstallGenerator < ::Rails::Generators::Base
6
+ source_root File.expand_path('../templates', __FILE__)
7
+ hook_for :orm
8
+
9
+ desc 'Copy config and rules files'
10
+ def copy_migrations_and_model
11
+ template 'merit.erb', 'config/initializers/merit.rb'
12
+ template 'merit_badge_rules.erb', 'app/models/merit/badge_rules.rb'
13
+ template 'merit_point_rules.erb', 'app/models/merit/point_rules.rb'
14
+ template 'merit_rank_rules.erb', 'app/models/merit/rank_rules.rb'
15
+ end
16
+ end
17
+ end
18
+ end
@@ -1,6 +1,8 @@
1
+ require "rails/generators"
2
+
1
3
  module Merit
2
4
  module Generators
3
- class MeritGenerator < Rails::Generators::NamedBase
5
+ class MeritGenerator < ::Rails::Generators::NamedBase
4
6
  source_root File.expand_path('../templates', __FILE__)
5
7
  hook_for :orm
6
8
 
@@ -1,6 +1,8 @@
1
+ require "rails/generators"
2
+
1
3
  module Merit
2
4
  module Generators
3
- class RemoveGenerator < Rails::Generators::NamedBase
5
+ class RemoveGenerator < ::Rails::Generators::NamedBase
4
6
  source_root File.expand_path('../templates', __FILE__)
5
7
  hook_for :orm
6
8
 
@@ -3,9 +3,6 @@ Merit.setup do |config|
3
3
  # Check rules on each request or in background
4
4
  # config.checks_on_each_request = true
5
5
 
6
- # Define ORM. Could be :active_record (default) and :mongoid
7
- # config.orm = :active_record
8
-
9
6
  # Add application observers to get notifications when reputation changes.
10
7
  # config.add_observer 'MyObserverClassName'
11
8
 
@@ -1,5 +1,3 @@
1
- require_dependency "merit/models/#{Merit.orm}/merit/action"
2
-
3
1
  # Merit::Action general schema
4
2
  # ______________________________________________________________
5
3
  # source | action | target
@@ -12,10 +10,14 @@ require_dependency "merit/models/#{Merit.orm}/merit/action"
12
10
  # ______________________________________________________________
13
11
  #
14
12
  # Rules relate to merit_actions by action name ('controller#action' string)
15
- module Merit
16
- class Action
17
- def self.check_unprocessed
18
- where(processed: false).find_each(&:check_all_rules)
13
+ module Merit::Models
14
+ module ActionConcern
15
+ extend ActiveSupport::Concern
16
+
17
+ class_methods do
18
+ def check_unprocessed
19
+ where(processed: false).find_each(&:check_all_rules)
20
+ end
19
21
  end
20
22
 
21
23
  # Check rules defined for a merit_action
@@ -23,16 +25,16 @@ module Merit
23
25
  mark_as_processed!
24
26
  return if had_errors
25
27
 
26
- check_rules rules_matcher.select_from(AppBadgeRules), :badges
27
- check_rules rules_matcher.select_from(AppPointRules), :points
28
+ check_rules rules_matcher.select_from(Merit::AppBadgeRules), :badges
29
+ check_rules rules_matcher.select_from(Merit::AppPointRules), :points
28
30
  end
29
31
 
30
32
  private
31
33
 
32
34
  def check_rules(rules_array, badges_or_points)
33
35
  rules_array.each do |rule|
34
- judge = Judge.new rule, action: self
35
- judge.send :"apply_#{badges_or_points}"
36
+ judge = Merit::Judge.new(rule, action: self)
37
+ judge.public_send(:"apply_#{badges_or_points}")
36
38
  end
37
39
  end
38
40