Sha256: 8a0489896d8b4a7f4f9c796defe1535c98e9e715722ba588f872f5507cbaf9b9

Contents?: true

Size: 1.73 KB

Versions: 14

Compression:

Stored size: 1.73 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_reflection.class_name.constantize
        ActiveRecord::Base.transaction do
          # The one direction
          scope = relation_class.where(:owner_id => proxy_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_owner.id)
          scope.any? || scope.create!
        end
      end

      def destroy_with_reverse_relation(target_concept)
        relation_class = proxy_reflection.class_name.constantize
        ActiveRecord::Base.transaction do
          relation_class.where(:owner_id => proxy_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_owner.id).all.each do |relation|
            relation.destroy
          end
        end
      end

    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
iqvoc-3.2.11 app/models/concept/relation/reverse_relation_extension.rb
iqvoc-3.2.10 app/models/concept/relation/reverse_relation_extension.rb
iqvoc-3.2.9 app/models/concept/relation/reverse_relation_extension.rb
iqvoc-3.2.8 app/models/concept/relation/reverse_relation_extension.rb
iqvoc-3.2.7 app/models/concept/relation/reverse_relation_extension.rb
iqvoc-3.3.4 app/models/concept/relation/reverse_relation_extension.rb
iqvoc-3.3.3 app/models/concept/relation/reverse_relation_extension.rb
iqvoc-3.3.2 app/models/concept/relation/reverse_relation_extension.rb
iqvoc-3.3.1 app/models/concept/relation/reverse_relation_extension.rb
iqvoc-3.3.0 app/models/concept/relation/reverse_relation_extension.rb
iqvoc-3.2.6 app/models/concept/relation/reverse_relation_extension.rb
iqvoc-3.2.5 app/models/concept/relation/reverse_relation_extension.rb
iqvoc-3.2.4 app/models/concept/relation/reverse_relation_extension.rb
iqvoc-3.2.3 app/models/concept/relation/reverse_relation_extension.rb