Sha256: cfb21a74162f5e0baf859adef6b7cea3fd02bd0d5790ea3e46aca1e49da5e824

Contents?: true

Size: 1.02 KB

Versions: 6

Compression:

Stored size: 1.02 KB

Contents

# frozen_string_literal: true

module Mongoid
module Orderable
module Mixins
  module Helpers
    def orderable_keys
      Array(orderable_inherited_class.orderable_configs.try(:keys))
    end

    def default_orderable_field
      self.class.orderable_configs.detect {|_c, conf| conf[:default] }.try(:first) || orderable_keys.first
    end

    def orderable_scope(field = nil)
      field ||= default_orderable_field

      if embedded?
        _parent.send(_association.name).send("orderable_#{field}_scope", self)
      else
        orderable_inherited_class.send("orderable_#{field}_scope", self)
      end
    end

    def orderable_scope_changed?(field)
      !orderable_scope(field).where(_id: _id).exists?
    end

    def orderable_bottom(field = nil, in_list = true)
      field ||= default_orderable_field
      f = orderable_field(field)
      max = orderable_scope(field).ne(f => nil).max(f)
      return orderable_top(field) unless max
      in_list ? max : max.next
    end
  end
end
end
end

Version data entries

6 entries across 6 versions & 1 rubygems

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