Sha256: 19cb78fc338b8bd54ea39a1729f05acff851efeb76086bad112091352799b5e2

Contents?: true

Size: 1.8 KB

Versions: 8

Compression:

Stored size: 1.8 KB

Contents

# encoding: UTF-8

# Copyright 2011 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)
        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)
          scope.any? || scope.create!

          # 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!
        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).all.each do |relation|
            relation.destroy
          end

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

    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
iqvoc-3.5.6 app/models/concept/relation/reverse_relation_extension.rb
iqvoc-3.5.5 app/models/concept/relation/reverse_relation_extension.rb
iqvoc-3.5.4 app/models/concept/relation/reverse_relation_extension.rb
iqvoc-3.5.3 app/models/concept/relation/reverse_relation_extension.rb
iqvoc-3.5.2 app/models/concept/relation/reverse_relation_extension.rb
iqvoc-3.5.1 app/models/concept/relation/reverse_relation_extension.rb
iqvoc-3.5.0 app/models/concept/relation/reverse_relation_extension.rb
iqvoc-3.4.0 app/models/concept/relation/reverse_relation_extension.rb