Module: PagesCore::PageModel::Sortable::ClassMethods

Defined in:
app/models/concerns/pages_core/page_model/sortable.rb

Instance Method Summary collapse

Instance Method Details

#order_by_tags(tags) ⇒ Object


12
13
14
15
16
17
18
19
20
21
22
# File 'app/models/concerns/pages_core/page_model/sortable.rb', line 12

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