Skip to content

Commit

Permalink
Refactored 2 methods to correct stderr output warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
yakschuss committed Mar 17, 2016
1 parent b0cf6a6 commit 454b0b5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
24 changes: 16 additions & 8 deletions lib/merit/controller_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/dummy/app/models/merit/point_rules.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 454b0b5

Please sign in to comment.