2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-01-10 07:25:14 +08:00
|
|
|
# Support for ensure_{blah}! methods.
|
|
|
|
module EnsureMagic
|
|
|
|
def method_missing(method, *args, &block)
|
2023-01-21 02:52:49 +08:00
|
|
|
if method.to_s =~ /\Aensure_(.*)\!\z/
|
2014-01-10 07:25:14 +08: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 20:10:19 +08:00
|
|
|
end
|
2014-01-10 07:25:14 +08: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-19 00:42:31 +08:00
|
|
|
end
|