module LinkedRails::Policy
Public Class Methods
new(user_context, record)
click to toggle source
# File lib/linked_rails/policy.rb, line 20 def initialize(user_context, record) @user_context = user_context @record = record end
Public Instance Methods
create?()
click to toggle source
# File lib/linked_rails/policy.rb, line 49 def create? false end
create_child?(klass, opts = {})
click to toggle source
# File lib/linked_rails/policy.rb, line 26 def create_child?(klass, opts = {}) child_policy(klass, opts).create? end
destroy?()
click to toggle source
# File lib/linked_rails/policy.rb, line 53 def destroy? false end
index_children?(klass, opts = {})
click to toggle source
# File lib/linked_rails/policy.rb, line 30 def index_children?(klass, opts = {}) child_policy(klass, opts).show? end
permitted_attributes()
click to toggle source
# File lib/linked_rails/policy.rb, line 34 def permitted_attributes self.class.permitted_attributes .select { |opts| attribute_permitted?(opts[:conditions]) } .map { |opts| sanitized_attributes(opts[:attributes], opts[:options] || {}) } .flatten end
show?()
click to toggle source
# File lib/linked_rails/policy.rb, line 41 def show? false end
update?()
click to toggle source
# File lib/linked_rails/policy.rb, line 45 def update? false end
Private Instance Methods
attribute_permitted?(conditions)
click to toggle source
# File lib/linked_rails/policy.rb, line 59 def attribute_permitted?(conditions) conditions.all? do |key, opts| raise "Unknown attribute condition #{key}" unless respond_to?("check_#{key}", true) send(:"check_#{key}", opts) end end
child_policy(klass, opts = {})
click to toggle source
# File lib/linked_rails/policy.rb, line 67 def child_policy(klass, opts = {}) Pundit.policy(user_context, record.build_child(klass, opts.merge(user_context: user_context))) end
forbid_with_message(message)
click to toggle source
# File lib/linked_rails/policy.rb, line 71 def forbid_with_message(message) @message = message false end
parent_policy()
click to toggle source
# File lib/linked_rails/policy.rb, line 76 def parent_policy return if record.try(:parent).blank? @parent_policy ||= Pundit.policy(user_context, record.parent) end
policy_class()
click to toggle source
# File lib/linked_rails/policy.rb, line 82 def policy_class self.class.policy_class end
sanitize_array_attribute(attr)
click to toggle source
# File lib/linked_rails/policy.rb, line 86 def sanitize_array_attribute(attr) [attr, attr => []] end
sanitize_attribute(attr)
click to toggle source
# File lib/linked_rails/policy.rb, line 90 def sanitize_attribute(attr) attr end
sanitize_nested_attribute(key)
click to toggle source
# File lib/linked_rails/policy.rb, line 104 def sanitize_nested_attribute(key) # rubocop:disable Metrics/AbcSize association = record.class.reflect_on_association(key) return nil if association.blank? || (!association.polymorphic? && !association.klass) nested_attributes = if association.polymorphic? Pundit.policy(user_context, record).try("#{association.name}_attributes") || [] else child = record.build_child(association.klass, user_context: user_context) Pundit.policy(user_context, child).permitted_attributes end {"#{key}_attributes" => nested_attributes + %i[id _destroy]} end
sanitized_attributes(attributes, opts)
click to toggle source
# File lib/linked_rails/policy.rb, line 94 def sanitized_attributes(attributes, opts) if opts[:nested] attributes.map(&method(:sanitize_nested_attribute)) elsif opts[:array] attributes.map(&method(:sanitize_array_attribute)) else attributes.map(&method(:sanitize_attribute)) end end