Sha256: 305f1152ea8a553c68b9893351e42faccda45434fbf9463f7e057bb5b1022533

Contents?: true

Size: 1.76 KB

Versions: 32

Compression:

Stored size: 1.76 KB

Contents

module Para
  module Orderable
    extend ActiveSupport::Concern

    included do
      scope :ordered, -> { order("#{ table_name }.position ASC") }

      before_create :orderable_assign_position
      after_commit  :reprocess_ordering, on: :create
      after_destroy :reprocess_ordering
    end

    private

    def orderable_assign_position
      return if attribute_present?(:position)

      last_resource = orderable_scope
        .where.not(position: nil)
        .select(:position)
        .first

      self.position = if last_resource && last_resource.position
        last_resource.position + 1
      else
        0
      end
    end

    # Unfragment existing resources positions
    #
    def reprocess_ordering
      orderable_scope.each_with_index do |resource, index|
        resource.update_column(:position, index)
      end
    end

    def orderable_scope
      if (parent = _orderable_options[:parent]) && (as = _orderable_options[:as])
        try(parent).try(as).try(:ordered) || self.class.none
      else
        self.class.unscoped.ordered
      end
    end
  end

  module ActiveRecordOrderableMixin
    extend ActiveSupport::Concern

    included do
      class_attribute :orderable, :_orderable_options
    end

    module ClassMethods
      def acts_as_orderable(options = {})
        return if orderable?

        unless (
          ( options[:parent] &&  options[:as]) ||
          (!options[:parent] && !options[:as])
        )
          raise "You need to either pass :parent and :as options to the " \
                "acts_as_orderable macro, or no options at all."
        end

        self.orderable = true
        self._orderable_options = options
        include Para::Orderable
      end

      def orderable?
        !!orderable
      end
    end
  end
end

Version data entries

32 entries across 32 versions & 1 rubygems

Version Path
para-0.12.4 lib/para/orderable.rb
para-0.12.3 lib/para/orderable.rb
para-0.12.2 lib/para/orderable.rb
para-0.12.1 lib/para/orderable.rb
para-0.12.0 lib/para/orderable.rb
para-0.11.4 lib/para/orderable.rb
para-0.11.3 lib/para/orderable.rb
para-0.11.2 lib/para/orderable.rb
para-0.11.1 lib/para/orderable.rb
para-0.11.0 lib/para/orderable.rb
para-0.10.0 lib/para/orderable.rb
para-0.9.4 lib/para/orderable.rb
para-0.9.3.3 lib/para/orderable.rb
para-0.9.3.2 lib/para/orderable.rb
para-0.9.3.1 lib/para/orderable.rb
para-0.9.2 lib/para/orderable.rb
para-0.9.0 lib/para/orderable.rb
para-0.8.15 lib/para/orderable.rb
para-0.8.14 lib/para/orderable.rb
para-0.8.13 lib/para/orderable.rb