Sha256: bb04bb5e201b4a0fa6cece92e3343bb10d2b6aaf35760ce8743e8239564ce2d9

Contents?: true

Size: 1.08 KB

Versions: 17

Compression:

Stored size: 1.08 KB

Contents

# frozen_string_literal: true

# rubocop:disable Style/ClassAndModuleChildren
module Maglev::Page::SearchConcern
  extend ActiveSupport::Concern

  class_methods do
    def search(keyword, locale)
      return [] if keyword.blank?

      title = search_title_node(locale)
      query = all.select(Maglev::Page.arel_table[Arel.star], title).distinct.joins(:paths).order(title.asc)
      matching = "%#{keyword}%"

      query.where(
        search_title_clause(matching, locale).or(
          search_path_clause(matching, locale)
        )
      )
    end

    protected

    def search_title_clause(query, locale)
      search_title_node(locale).matches(query)
    end

    def search_path_clause(query, locale)
      path = Maglev::PagePath.arel_table[:value]
      path.matches(query).and(path.eq(locale))
    end

    def search_title_node(locale)
      Arel::Nodes::InfixOperation.new('->>',
                                      arel_table[:title_translations],
                                      Arel::Nodes.build_quoted(locale))
    end
  end
end
# rubocop:enable Style/ClassAndModuleChildren

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
maglevcms-1.4.0 app/models/maglev/page/search_concern.rb
maglevcms-1.3.0 app/models/maglev/page/search_concern.rb
maglevcms-1.2.2 app/models/maglev/page/search_concern.rb
maglevcms-1.2.1 app/models/maglev/page/search_concern.rb
maglevcms-1.2.0 app/models/maglev/page/search_concern.rb
maglevcms-1.1.7 app/models/maglev/page/search_concern.rb
maglevcms-1.1.6 app/models/maglev/page/search_concern.rb
maglevcms-1.1.5 app/models/maglev/page/search_concern.rb
maglevcms-1.1.4 app/models/maglev/page/search_concern.rb
maglevcms-1.1.3 app/models/maglev/page/search_concern.rb
maglevcms-1.1.2 app/models/maglev/page/search_concern.rb
maglevcms-1.1.1 app/models/maglev/page/search_concern.rb
maglevcms-1.1.0 app/models/maglev/page/search_concern.rb
maglevcms-1.0.0 app/models/maglev/page/search_concern.rb
maglevcms-1.0.0.rc3 app/models/maglev/page/search_concern.rb
maglevcms-1.0.0.rc2 app/models/maglev/page/search_concern.rb
maglevcms-1.0.0.rc1 app/models/maglev/page/search_concern.rb