class Mongoid::Alize::ToCallback

Public Instance Methods

alias_destroy_callback() click to toggle source
# File lib/mongoid/alize/to_callback.rb, line 145
def alias_destroy_callback
  unless callback_defined?(aliased_destroy_callback_name)
    klass.send(:alias_method, aliased_destroy_callback_name, destroy_callback_name)
    klass.send(:public, aliased_destroy_callback_name)
  end
end
aliased_destroy_callback_name() click to toggle source
# File lib/mongoid/alize/to_callback.rb, line 152
def aliased_destroy_callback_name
  :"denormalize_destroy_#{direction}_#{relation}"
end
attach() click to toggle source
# File lib/mongoid/alize/to_callback.rb, line 7
def attach
  define_denorm_attrs

  define_callback
  alias_callback
  set_callback

  define_destroy_callback
  alias_destroy_callback
  set_destroy_callback
end
define_callback() click to toggle source
# File lib/mongoid/alize/to_callback.rb, line 19
      def define_callback
        klass.class_eval <<-CALLBACK, __FILE__, __LINE__ + 1

          def #{callback_name}#{force_param}

            #{iterable_relation}.each do |relation|
              next if relation.attributes.frozen?

              is_one = #{is_one?}
              if is_one
                field_values = #{field_values("self")}
              else
                field_values = #{field_values("self", :id => true)}
              end

              prefixed_name = #{prefixed_name}
              if is_one
                #{relation_set('prefixed_name', 'field_values')}
              else
                #{pull_from_inverse}
                #{relation_push('prefixed_name', 'field_values')}
              end

            end

            #{debug ? "puts \"#{callback_name}\"": ""}
            true
          end
          protected :#{callback_name}
        CALLBACK
      end
define_destroy_callback() click to toggle source
# File lib/mongoid/alize/to_callback.rb, line 51
      def define_destroy_callback
        klass.class_eval <<-CALLBACK, __FILE__, __LINE__ + 1

          def #{destroy_callback_name}
            #{iterable_relation}.each do |relation|
              next if relation.attributes.frozen?

              is_one = #{is_one?}
              prefixed_name = #{prefixed_name}
              if is_one
                #{relation_unset('prefixed_name')}
              else
                #{pull_from_inverse}
              end
            end

            #{debug ? "puts \"#{destroy_callback_name}\"": ""}
            true
          end
          protected :#{destroy_callback_name}
        CALLBACK
      end
destroy_callback_name() click to toggle source
# File lib/mongoid/alize/to_callback.rb, line 156
def destroy_callback_name
  :"_#{aliased_destroy_callback_name}"
end
direction() click to toggle source
# File lib/mongoid/alize/to_callback.rb, line 160
def direction
  "to"
end
find_relation() click to toggle source
# File lib/mongoid/alize/to_callback.rb, line 125
def find_relation
  "relation.class.relations.values.find { |metadata| metadata.inverse(self) == :#{relation} && metadata.class_name == self.class.name }"
end
is_one?() click to toggle source
# File lib/mongoid/alize/to_callback.rb, line 111
      def is_one?
        if inverse_relation
          if self.inverse_metadata.relation.superclass == Mongoid::Relations::One
            "true"
          else
            "false"
          end
        else
          <<-RUBIES
            (#{find_relation}.relation.superclass == Mongoid::Relations::One)
          RUBIES
        end
      end
iterable_relation() click to toggle source
# File lib/mongoid/alize/to_callback.rb, line 129
def iterable_relation
  "[self.#{relation}].flatten.compact"
end
prefixed_name() click to toggle source
# File lib/mongoid/alize/to_callback.rb, line 85
      def prefixed_name
        if inverse_relation
          ":#{inverse_relation}_fields"
        else
          <<-RUBIES
            (#{find_relation}.name.to_s + '_fields')
          RUBIES
        end
      end
pull_from_inverse() click to toggle source
# File lib/mongoid/alize/to_callback.rb, line 74
      def pull_from_inverse
        <<-RUBIES
          #{relation_pull('prefixed_name', '{ "_id" => self.id }')}
          if _f = relation.send(prefixed_name)
            _f.reject! do |hash|
              hash["_id"] == self.id
            end
          end
        RUBIES
      end
relation_pull(field, value) click to toggle source
# File lib/mongoid/alize/to_callback.rb, line 103
def relation_pull(field, value)
  Mongoid::Compatibility::Version.mongoid4_or_newer? ? "relation.pull(#{field}.to_sym => #{value})" : "relation.pull(#{field}, #{value})"
end
relation_push(field, value) click to toggle source
# File lib/mongoid/alize/to_callback.rb, line 107
def relation_push(field, value)
  Mongoid::Compatibility::Version.mongoid4_or_newer? ? "relation.push(#{field}.to_sym => #{value})" : "relation.push(#{field}, #{value})"
end
relation_set(field, value) click to toggle source
# File lib/mongoid/alize/to_callback.rb, line 95
def relation_set(field, value)
  Mongoid::Compatibility::Version.mongoid4_or_newer? ? "relation.set(#{field}.to_sym => #{value})" : "relation.set(#{field}, #{value})"
end
relation_unset(field) click to toggle source
# File lib/mongoid/alize/to_callback.rb, line 99
def relation_unset(field)
  "relation.unset(#{field}.to_sym)"
end
set_callback() click to toggle source
# File lib/mongoid/alize/to_callback.rb, line 133
def set_callback
  unless callback_attached?("save", aliased_callback_name)
    klass.set_callback(:save, :after, aliased_callback_name)
  end
end
set_destroy_callback() click to toggle source
# File lib/mongoid/alize/to_callback.rb, line 139
def set_destroy_callback
  unless callback_attached?("destroy", aliased_destroy_callback_name)
    klass.set_callback(:destroy, :after, aliased_destroy_callback_name)
  end
end