module KittyPolicy::Helper

Public Instance Methods

method_name(action, subject = nil) click to toggle source
# File lib/kitty_policy/helper.rb, line 7
def method_name(action, subject = nil)
  if subject
    "can_#{action}_#{underscore(subject_to_string(subject)).tr('/', '_')}?"
  else
    "can_#{action}?"
  end
end

Private Instance Methods

subject_to_string(subject) click to toggle source
# File lib/kitty_policy/helper.rb, line 17
def subject_to_string(subject)
  case subject
  when Class, Symbol then subject.to_s
  when String then subject.gsub(/[^\w]/, '')
  else subject.class.to_s
  end
end
underscore(text) click to toggle source
# File lib/kitty_policy/helper.rb, line 25
def underscore(text)
  text.tr('::', '_')
      .gsub(/([A-Z]+)([A-Z][a-z])/) { "#{Regexp.last_match[1]}_#{Regexp.last_match[2]}" }
      .gsub(/([a-z\d])([A-Z])/) { "#{Regexp.last_match[1]}_#{Regexp.last_match[2]}" }
      .tr('-', '_')
      .downcase
end