Skip to content

Commit

Permalink
Fix: warning: previous definition messages (#363)
Browse files Browse the repository at this point in the history
When app reload is triggered, following warnings were spamming the console, especially running DelayedJob which reloads app every 5 seconds when running:

Fix:
If the constants are already defined, remove them first before setting again
  • Loading branch information
natanio authored Mar 5, 2022
1 parent bd48a4d commit 2dae5f0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
13 changes: 13 additions & 0 deletions lib/merit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ def self.add_observer(class_name)
@config.add_observer(class_name)
end

# If the app is reloaded, avoid printing `warning: previous definition of AppBadgeRules was here`
def self.remove_badge_rules
remove_const(:AppBadgeRules) if self.const_defined?('AppBadgeRules')
end

# If the app is reloaded, avoid printing `warning: previous definition of AppPointRules was here`
def self.remove_point_rules
remove_const(:AppPointRules) if self.const_defined?('AppPointRules')
end

class Configuration
attr_accessor :checks_on_each_request, :orm, :user_model_name, :observers,
:current_user_method
Expand Down Expand Up @@ -65,6 +75,9 @@ class Engine < Rails::Engine
ActiveSupport.on_load(:active_record) { include Merit }
ActiveSupport.on_load(app.config.api_only ? :action_controller_api : :action_controller_base) do
begin
# Remove previous definitions of constant if they are defined when app reloads
Merit.remove_badge_rules
Merit.remove_point_rules
# Load app rules on boot up
Merit::AppBadgeRules = Merit::BadgeRules.new.defined_rules
Merit::AppPointRules = Merit::PointRules.new.defined_rules
Expand Down
4 changes: 2 additions & 2 deletions test/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# This file is the source Rails uses to define your schema when running `rails
# db:schema:load`. When creating a new database, `rails db:schema:load` tends to
# This file is the source Rails uses to define your schema when running `bin/rails
# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to
# be faster and is potentially less error prone than running all of your
# migrations from scratch. Old migrations may fail to apply correctly if those
# migrations use external dependencies or application code.
Expand Down

0 comments on commit 2dae5f0

Please sign in to comment.