@@ -6,6 +6,7 @@ class Page < ApplicationRecord
66 include Identifier
77 include Protected
88 include Privacy
9+ include Searchable
910
1011 PAGE_LAYOUTS = [
1112 'layouts/better_together/page' ,
@@ -14,12 +15,33 @@ class Page < ApplicationRecord
1415
1516 has_many :page_blocks , -> { positioned } , dependent : :destroy , class_name : 'BetterTogether::Content::PageBlock'
1617 has_many :blocks , through : :page_blocks
18+ has_many :image_blocks , -> { where ( type : 'BetterTogether::Content::Image' ) } , through : :page_blocks , source : :block
19+ has_many :rich_text_blocks , -> { where ( type : 'BetterTogether::Content::RichText' ) } , through : :page_blocks , source : :block
1720
1821 accepts_nested_attributes_for :page_blocks , allow_destroy : true
1922
2023 translates :title , type : :string
2124 translates :content , backend : :action_text
2225
26+ settings index : { number_of_shards : 1 } do
27+ mappings dynamic : 'false' do
28+ indexes :title , as : 'title'
29+ indexes :content , as : 'content'
30+ indexes :rich_text_content , type : 'nested' do
31+ indexes :body , type : 'text'
32+ end
33+
34+ indexes :blocks , type : 'nested' do
35+ indexes :rich_text_content , type : 'nested' do
36+ indexes :body , type : 'text'
37+ end
38+ indexes :rich_text_translations , type : 'nested' do
39+ indexes :body , type : 'text'
40+ end
41+ end
42+ end
43+ end
44+
2345 slugged :title , min_length : 1
2446
2547 # Validations
@@ -30,6 +52,25 @@ class Page < ApplicationRecord
3052 scope :published , -> { where . not ( published_at : nil ) }
3153 scope :by_publication_date , -> { order ( published_at : :desc ) }
3254
55+ # Customize the data sent to Elasticsearch for indexing
56+ def as_indexed_json ( options = { } )
57+ self . as_json (
58+ methods : [ :title , :content , *self . class . localized_attribute_list ] ,
59+
60+ include : {
61+ rich_text_content : { only : :body } ,
62+ image_blocks : {
63+
64+ } ,
65+ rich_text_blocks : {
66+ include : {
67+ rich_text_content : { only : :body }
68+ }
69+ }
70+ }
71+ )
72+ end
73+
3374 def published?
3475 published_at . present? && published_at < Time . zone . now
3576 end
0 commit comments