Sha256: 3c439e7508d2a4fc5c23791a148bab48566914e669d227c9178fcb4a67011548

Contents?: true

Size: 1.36 KB

Versions: 5

Compression:

Stored size: 1.36 KB

Contents

# frozen_string_literal: true

module Mongoid
module Orderable
module Mixins
  module Movable
    def move_to!(target_position, options = {})
      move_field_to target_position, options
      save
    end
    alias insert_at! move_to!

    def move_to(target_position, options = {})
      move_field_to target_position, options
    end
    alias insert_at move_to

    def move_to=(target_position, options = {})
      move_field_to target_position, options
    end
    alias insert_at= move_to=

    %i[top bottom].each do |symbol|
      class_eval <<~KLASS, __FILE__, __LINE__ + 1
        def move_to_#{symbol}(options = {})
          move_to :#{symbol}, options
        end

        def move_to_#{symbol}!(options = {})
          move_to! :#{symbol}, options
        end
      KLASS
    end

    %i[higher lower].each do |symbol|
      class_eval <<~KLASS, __FILE__, __LINE__ + 1
        def move_#{symbol}(options = {})
          move_to :#{symbol}, options
        end

        def move_#{symbol}!(options = {})
          move_to! :#{symbol}, options
        end
      KLASS
    end

    protected

    def move_all
      @move_all || {}
    end

    def move_field_to(position, options)
      field = options[:field] || default_orderable_field
      @move_all = move_all.merge(field => position)
    end
  end
end
end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
mongoid_orderable-6.0.4 lib/mongoid/orderable/mixins/movable.rb
mongoid_orderable-6.0.3 lib/mongoid/orderable/mixins/movable.rb
mongoid_orderable-6.0.2 lib/mongoid/orderable/mixins/movable.rb
mongoid_orderable-6.0.1 lib/mongoid/orderable/mixins/movable.rb
mongoid_orderable-6.0.0 lib/mongoid/orderable/mixins/movable.rb