mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
Refactor guardian as dissused in this topic https://meta.discourse.org/t/so-you-want-to-help-out-with-discourse/3823/41?u=hunter
Creates a mixin for the ensure_* functions and creates seperate mixins for functions dealing with posts, categories, and topics.
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
# Support for ensure_{blah}! methods.
|
||||
module EnsureMagic
|
||||
|
||||
def method_missing(method, *args, &block)
|
||||
if method.to_s =~ /^ensure_(.*)\!$/
|
||||
can_method = :"#{Regexp.last_match[1]}?"
|
||||
|
||||
if respond_to?(can_method)
|
||||
raise Discourse::InvalidAccess.new("#{can_method} failed") unless send(can_method, *args, &block)
|
||||
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
|
||||
|
||||
end
|
||||
Reference in New Issue
Block a user