diff --git a/lib/merit/controller_extensions.rb b/lib/merit/controller_extensions.rb index d8539aa8..e27cdd51 100644 --- a/lib/merit/controller_extensions.rb +++ b/lib/merit/controller_extensions.rb @@ -47,15 +47,23 @@ def had_errors? end def target_object - target_obj = instance_variable_get(:"@#{controller_name.singularize}") - if target_obj.nil? - str = '[merit] No object found, you might need a ' \ - "'@#{controller_name.singularize}' variable in " \ - "'#{controller_path}_controller' if no reputation is applied. " \ - 'If you are using `model_name` option in the rule this is ok.' - Rails.logger.warn str + variable_name = :"@#{controller_name.singularize}" + if instance_variable_defined?(variable_name) + if target_obj = instance_variable_get(variable_name) + target_obj + else + warn_no_object_found + end end - target_obj + end + + def warn_no_object_found + str = '[merit] No object found, you might need a ' \ + "'@#{controller_name.singularize}' variable in " \ + "'#{controller_path}_controller' if no reputation is applied. " \ + 'If you are using `model_name` option in the rule this is ok.' + Rails.logger.warn str + nil end def target_id diff --git a/test/dummy/app/models/merit/point_rules.rb b/test/dummy/app/models/merit/point_rules.rb index 4b49e935..932654bd 100644 --- a/test/dummy/app/models/merit/point_rules.rb +++ b/test/dummy/app/models/merit/point_rules.rb @@ -36,7 +36,7 @@ def initialize object.comment.to_i > 0 end - score -5, to: :user, on: 'comments#destroy' do |comment| + score (-5), to: :user, on: 'comments#destroy' do |comment| comment.present? end end