Module: RoleFu::Adapters::Pundit

Defined in:
lib/role_fu/adapters/pundit.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

Mixin for Pundit Policies (e.g. ApplicationPolicy)



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/role_fu/adapters/pundit.rb', line 7

def method_missing(method, *args, &block)
  if method.to_s.end_with?("?")
    action = method.to_s.delete_suffix("?")

    # Infer resource from policy name: PostPolicy -> "posts"
    resource_name = self.class.to_s.gsub("Policy", "").underscore.pluralize

    # Check "posts.update"
    permission = "#{resource_name}.#{action}"

    return true if user.role_fu_can?(permission)
  end

  super
end

Instance Method Details

#respond_to_missing?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/role_fu/adapters/pundit.rb', line 23

def respond_to_missing?(method, include_private = false)
  method.to_s.end_with?("?") || super
end