diff --git a/lib/data_cleanup/rules.rb b/lib/data_cleanup/rules.rb index 155b86b..a85db52 100644 --- a/lib/data_cleanup/rules.rb +++ b/lib/data_cleanup/rules.rb @@ -1,4 +1,8 @@ -require_relative "rules/base" +# frozen_string_literal: true + +if File.exist?(Rails.root.join('lib', "data_cleanup", "rules", "base.rb")) + require_relative "rules/base" +end module DataCleanup module Rules diff --git a/lib/generators/data_cleanup_rule/data_cleanup_rule_generator.rb b/lib/generators/data_cleanup_rule/data_cleanup_rule_generator.rb index 6e48031..91ca8b8 100644 --- a/lib/generators/data_cleanup_rule/data_cleanup_rule_generator.rb +++ b/lib/generators/data_cleanup_rule/data_cleanup_rule_generator.rb @@ -6,11 +6,19 @@ # Copy the Rule template and create a new Rule file def add_rule_file + empty_directory "lib/data_cleanup/rules" + unless File.exist?(base_class_path) + template "base.rb", base_class_path + end template "rule.rb.erb", rule_path.to_s end private + def base_class_path + Rails.root.join("lib", "data_cleanup", "rules", "base.rb") + end + # The name of the model we're fixing (e.g. 'User') # # Returns String diff --git a/lib/generators/data_cleanup_rule/templates/base.rb b/lib/generators/data_cleanup_rule/templates/base.rb new file mode 100644 index 0000000..30c918e --- /dev/null +++ b/lib/generators/data_cleanup_rule/templates/base.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +module DataCleanup + module Rules + + # Base class for rules to clean invalid database records + class Base + + def log(message) + DataCleanup.logger.info(message) + end + + # Description of the rule and how it's fixing the data + def description + self.class.name.humanize + end + + # Run this rule and fix data in the database. + def call + raise NotImplementedError, "Please define call() in #{self}" + end + + end + + end +end