Sha256: c30b466f65eca0d7dc255dae40fa0c05d64c3105540ca872bf98f0f4891b56ea

Contents?: true

Size: 1.15 KB

Versions: 29

Compression:

Stored size: 1.15 KB

Contents

# frozen_string_literal: true

module PagesCore
  module PageModel
    module Sortable
      extend ActiveSupport::Concern

      included do
        acts_as_list scope: :parent_page
        after_save :check_list_position
      end

      module ClassMethods
        def order_by_tags(tags)
          joins(
            "LEFT JOIN taggings ON taggings.taggable_id = pages.id AND " \
            "taggings.tag_id IN (#{tags.map(&:id).join(',')}) AND " \
            "taggings.taggable_type = " \
            "#{ActiveRecord::Base.connection.quote('Page')}"
          )
            .group("pages.id, localizations.id")
            .reorder(Arel.sql("COUNT(taggings.id) DESC"))
        end
      end

      def reorderable_children?
        !news_page?
      end

      def reorderable?
        !parent || !parent.news_page?
      end

      def content_order
        if news_page?
          "pages.pinned DESC, published_at DESC"
        else
          "position ASC"
        end
      end

      private

      def check_list_position
        if deleted?
          remove_from_list
        elsif !position?
          assume_bottom_position
        end
      end
    end
  end
end

Version data entries

29 entries across 29 versions & 1 rubygems

Version Path
pages_core-3.10.1 app/models/concerns/pages_core/page_model/sortable.rb
pages_core-3.9.2 app/models/concerns/pages_core/page_model/sortable.rb
pages_core-3.9.1 app/models/concerns/pages_core/page_model/sortable.rb
pages_core-3.9.0 app/models/concerns/pages_core/page_model/sortable.rb
pages_core-3.8.3 app/models/concerns/pages_core/page_model/sortable.rb
pages_core-3.8.2 app/models/concerns/pages_core/page_model/sortable.rb
pages_core-3.8.1 app/models/concerns/pages_core/page_model/sortable.rb
pages_core-3.8.0 app/models/concerns/pages_core/page_model/sortable.rb
pages_core-3.7.0 app/models/concerns/pages_core/page_model/sortable.rb