2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-01-09 18:25:14 -05:00
|
|
|
# Support for ensure_{blah}! methods.
|
|
|
|
module EnsureMagic
|
|
|
|
def method_missing(method, *args, &block)
|
2023-01-20 13:52:49 -05:00
|
|
|
if method.to_s =~ /\Aensure_(.*)\!\z/
|
2014-01-09 18:25:14 -05:00
|
|
|
can_method = :"#{Regexp.last_match[1]}?"
|
|
|
|
|
|
|
|
if respond_to?(can_method)
|
|
|
|
unless send(can_method, *args, &block)
|
|
|
|
raise Discourse::InvalidAccess.new("#{can_method} failed")
|
2023-01-09 07:10:19 -05:00
|
|
|
end
|
2014-01-09 18:25:14 -05:00
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
super.method_missing(method, *args, &block)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Make sure we can see the object. Will raise a NotFound if it's nil
|
|
|
|
def ensure_can_see!(obj)
|
|
|
|
raise Discourse::InvalidAccess.new("Can't see #{obj}") unless can_see?(obj)
|
|
|
|
end
|
2014-04-18 12:42:31 -04:00
|
|
|
end
|