lib/hanami/model/associations/many_to_many.rb in hanami-model-1.3.2 vs lib/hanami/model/associations/many_to_many.rb in hanami-model-1.3.3

- old
+ new

@@ -1,15 +1,17 @@ +# frozen_string_literal: true + require "hanami/utils/hash" module Hanami module Model module Associations # Many-To-Many association # # @since 0.7.0 # @api private - class ManyToMany # rubocop:disable Metrics/ClassLength + class ManyToMany # @since 0.7.0 # @api private def self.schema_type(entity) type = Sql::Types::Schema::AssociationType.new(entity) Types::Strict::Array.member(type) @@ -74,34 +76,32 @@ # @since 1.1.0 # @api private def add(*data) command(:create, relation(through), use: [:timestamps]) .call(associate(serialize(data))) - rescue => e - raise Hanami::Model::Error.for(e) + rescue => exception + raise Hanami::Model::Error.for(exception) end # @since 1.1.0 # @api private def delete relation(through).where(source_foreign_key => subject.fetch(source_primary_key)).delete end # @since 1.1.0 # @api private - # rubocop:disable Metrics/AbcSize def remove(target_id) association_record = relation(through) - .where(target_foreign_key => target_id, source_foreign_key => subject.fetch(source_primary_key)) - .one + .where(target_foreign_key => target_id, source_foreign_key => subject.fetch(source_primary_key)) + .one return if association_record.nil? ar_id = association_record.public_send relation(through).primary_key command(:delete, relation(through)).by_pk(ar_id).call end - # rubocop:enable Metrics/AbcSize private # @since 1.1.0 # @api private @@ -170,20 +170,18 @@ end # @since 1.1.0 # # @api private - # rubocop:disable Metrics/AbcSize def _build_scope result = relation(association.target.to_sym).qualified unless subject.nil? result = result - .join(through, target_foreign_key => target_primary_key) - .where(source_foreign_key => subject.fetch(source_primary_key)) + .join(through, target_foreign_key => target_primary_key) + .where(source_foreign_key => subject.fetch(source_primary_key)) end result.as(Model::MappedRelation.mapper_name) end - # rubocop:enable Metrics/AbcSize # @since 1.1.0 # @api private def __new__(new_scope) self.class.new(repository, source, target, subject, new_scope)