Sha256: a6d67c590e7e9cbc14b604bf345152167ce861080d3776c44cddfc99af27d050

Contents?: true

Size: 1.58 KB

Versions: 8

Compression:

Stored size: 1.58 KB

Contents

# encoding: utf-8
module Mongoid
  module Relations
    module Touchable
      extend ActiveSupport::Concern

      module ClassMethods

        # Add the metadata to the touchable relations if the touch option was
        # provided.
        #
        # @example Add the touchable.
        #   Model.touchable(meta)
        #
        # @param [ Metadata ] metadata The relation metadata.
        #
        # @return [ Class ] The model class.
        #
        # @since 3.0.0
        def touchable(metadata)
          if metadata.touchable?
            name = metadata.name
            method_name = define_relation_touch_method(name)
            after_create method_name
            after_destroy method_name
            after_touch method_name
          end
          self
        end

        private

        # Define the method that will get called for touching belongs_to
        # relations.
        #
        # @api private
        #
        # @example Define the touch relation.
        #   Model.define_relation_touch_method(:band)
        #
        # @param [ Symbol ] name The name of the relation.
        #
        # @since 3.1.0
        #
        # @return [ Symbol ] The method name.
        def define_relation_touch_method(name)
          method_name = "touch_#{name}_after_create_or_destroy"
          class_eval <<-TOUCH
            def #{method_name}
              without_autobuild do
                relation = __send__(:#{name})
                relation.touch if relation
              end
            end
          TOUCH
          method_name.to_sym
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
mongoid-3.1.7 lib/mongoid/relations/touchable.rb
mongoid-3.1.6 lib/mongoid/relations/touchable.rb
mongoid-3.1.5 lib/mongoid/relations/touchable.rb
mongoid-3.1.4 lib/mongoid/relations/touchable.rb
mongoid-3.1.3 lib/mongoid/relations/touchable.rb
mongoid-3.1.2 lib/mongoid/relations/touchable.rb
mongoid-3.1.1 lib/mongoid/relations/touchable.rb
mongoid-3.1.0 lib/mongoid/relations/touchable.rb