Sha256: 6278f86e8b4fb715238d5ceab9079d952c6b8cf44260308cfc55cf599171f9af

Contents?: true

Size: 1.93 KB

Versions: 5

Compression:

Stored size: 1.93 KB

Contents

# encoding: UTF-8

# Copyright 2011-2013 innoQ Deutschland GmbH
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

module Concept
  module Relation
    module ReverseRelationExtension

      def create_with_reverse_relation(target_concept, attributes = {})
        relation_class = proxy_association.reflection.class_name.constantize
        ActiveRecord::Base.transaction do
          # The one direction
          scope = relation_class.where(:owner_id => proxy_association.owner.id, :target_id => target_concept.id)
          attributes = attributes.except(:rank) unless relation_class.rankable?
          scope.any? || scope.create!(attributes)

          # The reverse direction
          scope = relation_class.reverse_relation_class.where(:owner_id => target_concept.id, :target_id => proxy_association.owner.id)
          scope.any? || scope.create!(attributes)
        end
      end

      def destroy_with_reverse_relation(target_concept)
        relation_class = proxy_association.reflection.class_name.constantize
        ActiveRecord::Base.transaction do
          relation_class.where(:owner_id => proxy_association.owner.id, :target_id => target_concept.id).load.each do |relation|
            relation.destroy
          end

          relation_class.reverse_relation_class.where(:owner_id => target_concept.id, :target_id => proxy_association.owner.id).load.each do |relation|
            relation.destroy
          end
        end
      end

    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
iqvoc-4.3.3 app/models/concept/relation/reverse_relation_extension.rb
iqvoc-4.3.2 app/models/concept/relation/reverse_relation_extension.rb
iqvoc-4.3.1 app/models/concept/relation/reverse_relation_extension.rb
iqvoc-4.3.0 app/models/concept/relation/reverse_relation_extension.rb
iqvoc-4.2.0 app/models/concept/relation/reverse_relation_extension.rb