module LinkedRails::Policy::ClassMethods

Public Instance Methods

condition_for(attr, pass, shape_opts = {}) click to toggle source
# File lib/linked_rails/policy.rb, line 121
def condition_for(attr, pass, shape_opts = {})
  raise("#{attr} not permitted by #{self}") if attribute_options(attr).blank? && pass.permission_required?

  alternatives = node_shapes_for(attr, **shape_opts)
  if alternatives.count == 1
    Condition.new(shape: alternatives.first, pass: pass)
  elsif alternatives.count.positive?
    Condition.new(shape: SHACL::NodeShape.new(or: alternatives), pass: pass)
  else
    pass
  end
end
permitted_attributes() click to toggle source
# File lib/linked_rails/policy.rb, line 134
def permitted_attributes
  initialize_permitted_attributes

  _permitted_attributes
end

Private Instance Methods

attribute_options(attr) click to toggle source
# File lib/linked_rails/policy.rb, line 142
def attribute_options(attr)
  permitted_attributes.select { |opts| opts[:attributes].include?(attr) }
end
condition_alternatives(attr) click to toggle source
# File lib/linked_rails/policy.rb, line 146
def condition_alternatives(attr)
  attribute_options(attr)
    .select { |opts| opts[:conditions].present? }
    .map { |opts| opts[:conditions] }
end
initialize_permitted_attributes() click to toggle source
# File lib/linked_rails/policy.rb, line 162
def initialize_permitted_attributes
  return if _permitted_attributes && method(:_permitted_attributes).owner == singleton_class

  self._permitted_attributes = superclass.try(:_permitted_attributes)&.dup || []
end
node_shapes_for(attr, property: [], sh_not: []) click to toggle source
# File lib/linked_rails/policy.rb, line 152
def node_shapes_for(attr, property: [], sh_not: [])
  alternatives = condition_alternatives(attr)
  alternatives = [[]] if alternatives.empty? && (property.any? || sh_not.any?)

  alternatives.map do |props|
    properties = property_shapes(props) + property
    SHACL::NodeShape.new(property: properties, sh_not: sh_not)
  end
end
permit_array_attributes(attrs, conditions = {}) click to toggle source
# File lib/linked_rails/policy.rb, line 172
def permit_array_attributes(attrs, conditions = {})
  permitted_attributes << {attributes: attrs, conditions: conditions, options: {array: true}}
end
permit_attributes(attrs, conditions = {}) click to toggle source
# File lib/linked_rails/policy.rb, line 168
def permit_attributes(attrs, conditions = {})
  permitted_attributes << {attributes: attrs, conditions: conditions, options: {}}
end
permit_nested_attributes(attrs, conditions = {}) click to toggle source
# File lib/linked_rails/policy.rb, line 176
def permit_nested_attributes(attrs, conditions = {})
  permitted_attributes << {attributes: attrs, conditions: conditions, options: {nested: true}}
end
policy_class() click to toggle source
# File lib/linked_rails/policy.rb, line 180
def policy_class
  @policy_class ||= name.sub(/Policy/, '').classify.safe_constantize
end
property_shapes(conditions) click to toggle source
# File lib/linked_rails/policy.rb, line 184
def property_shapes(conditions)
  conditions.map { |key, options| send("#{key}_shapes", options) }.flatten.compact
end