<commit_before>class KojacBasePolicy
  attr_reader :user, :record, :op

  def initialize(user, record, op=nil)
	  raise Pundit::NotAuthorizedError, "must be logged in" unless user
    @user = user
    @record = record
	  @op = op
  end

  def unauthorized!(aMessage=nil)
	  raise Pundit::NotAuthorizedError, aMessage||"You are not authorized to perform this action"
  end

  #def self.write_op_filter(aCurrentUser,aSafeFields,aSourceFields)
		#	ring = aCurrentUser.ring
		#	has_owner = !!self.column_names.include?('owner_id')
		#	has_dealership = !!self.column_names.include?('dealership_id')
		#	# default to user
		#	if ring <= SALES_RING
		#		aSafeFields['owner_id'] ||= aCurrentUser.owner_id if has_owner
		#		aSafeFields['dealership_id'] ||= aCurrentUser.dealership_id if has_dealership
		#	end
		#	if ring > SYSADMIN_RING
		#		unauthorized! if aSafeFields['owner_id'] != aCurrentUser.owner_id if has_owner
		#	end
		#	if ring > OWNER_ADMIN_RING
		#		unauthorized! if aSafeFields['dealership_id'] != aCurrentUser.dealership_id if has_dealership
		#	end
		#end

  def query_ring
	  user.ring
  end

  # kojac methods
  def read?
	  record.class.ring_can?(query_ring,:read)
  end

  def write?
	  record.class.ring_can?(query_ring,:write)
  end

  # rails methods
  def index?
	  record.class.ring_can?(query_ring,:read)
  end

  def show?
	  record.class.ring_can?(query_ring,:read)
  end

  def create?
	  record.class.ring_can?(query_ring,:create)
  end

  def new?
	  record.class.ring_can?(query_ring,:create)
  end

  def update?
	  record.class.ring_can?(query_ring,:write)
  end

  def edit?
	  record.class.ring_can?(query_ring,:write)
  end

  def destroy?
	  record.class.ring_can?(query_ring,:destroy)
  end

  def scope
    Pundit.policy_scope!(user, record.class)
  end

	def permitted_attributes(aAbility=nil)
		raise "ability not given" unless (@op && @op[:verb]) || aAbility
		if !aAbility && @op
			aAbility = case @op[:verb]
				when 'CREATE'
				when 'UPDATE'
					:write
				when 'READ'
					:read
				when 'ADD'
					:add
				when 'REMOVE'
					:remove
				when 'CREATE_ON'
					:create_on
			end
		end
		cls = record.is_a?(Class) ? record : record.class
		cls.permitted(query_ring,aAbility)
	end

  def permitted_fields(aAbility=nil)
	  result = permitted_attributes(aAbility)
	  cls = record.is_a?(Class) ? record : record.class
		result.delete_if { |f| cls.reflections.has_key? f }
		result
	end

	def permitted_associations(aAbility=nil)
	  result = permitted_attributes(aAbility)
	  cls = record.is_a?(Class) ? record : record.class
		result.delete_if { |f| !cls.reflections.has_key? f }
		result
	end

end
<commit_msg>refactor<commit_after>class KojacBasePolicy
  attr_reader :user, :record, :op

  def initialize(user, record, op=nil)
	  raise Pundit::NotAuthorizedError, "must be logged in" unless user
    @user = user
    @record = record
	  @op = op
  end

  def unauthorized!(aMessage=nil)
	  raise Pundit::NotAuthorizedError, aMessage||"You are not authorized to perform this action"
  end

  #def self.write_op_filter(aCurrentUser,aSafeFields,aSourceFields)
		#	ring = aCurrentUser.ring
		#	has_owner = !!self.column_names.include?('owner_id')
		#	has_dealership = !!self.column_names.include?('dealership_id')
		#	# default to user
		#	if ring <= SALES_RING
		#		aSafeFields['owner_id'] ||= aCurrentUser.owner_id if has_owner
		#		aSafeFields['dealership_id'] ||= aCurrentUser.dealership_id if has_dealership
		#	end
		#	if ring > SYSADMIN_RING
		#		unauthorized! if aSafeFields['owner_id'] != aCurrentUser.owner_id if has_owner
		#	end
		#	if ring > OWNER_ADMIN_RING
		#		unauthorized! if aSafeFields['dealership_id'] != aCurrentUser.dealership_id if has_dealership
		#	end
		#end

  def query_ring
	  user.ring
  end

  # kojac methods
  def create?
	  record.class.ring_can?(query_ring,:create)
  end

  def read?
	  record.class.ring_can?(query_ring,:read)
  end

  def write?
	  record.class.ring_can?(query_ring,:write)
  end

  def destroy?
	  record.class.ring_can?(query_ring,:destroy)
  end

  # rails methods
  def index?
	  record.class.ring_can?(query_ring,:read)
  end

  def show?
	  record.class.ring_can?(query_ring,:read)
  end

  def new?
	  record.class.ring_can?(query_ring,:create)
  end

  def update?
	  record.class.ring_can?(query_ring,:write)
  end

  def edit?
	  record.class.ring_can?(query_ring,:write)
  end

  def scope
    Pundit.policy_scope!(user, record.class)
  end

  def self.ability_from_op(aOp)
	  return nil unless aOp
	  case aOp[:verb]
			when 'CREATE'
			when 'UPDATE'
				:write
			when 'READ'
				:read
			when 'ADD'
				:add
			when 'REMOVE'
				:remove
			when 'CREATE_ON'
				:create_on
		end
  end

	def permitted_attributes(aAbility=nil)
		#raise "Ability from op no longer supported" if !aAbility && @op && @op[:verb]
		aAbility ||= self.class.ability_from_op(@op)
		raise "ability not given" unless aAbility
		cls = record.is_a?(Class) ? record : record.class
		cls.permitted(query_ring,aAbility)
	end

  def permitted_fields(aAbility=nil)
	  result = permitted_attributes(aAbility)
	  cls = record.is_a?(Class) ? record : record.class
		result.delete_if { |f| cls.reflections.has_key? f }
		result
	end

	def permitted_associations(aAbility=nil)
	  result = permitted_attributes(aAbility)
	  cls = record.is_a?(Class) ? record : record.class
		result.delete_if { |f| !cls.reflections.has_key? f }
		result
	end

end

{'fraction_non_alphanumeric': 0.11029004249030112, 'fraction_numerical': 0.0, 'mean_word_length': 3.8774774774774774, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6794024', 'n_tokens_mistral': 2189, 'n_tokens_neox': 2001, 'n_words': 415}