Extending the Search Extension


#1

Ok, I can’t find the solution to this problem. I have implemented the
suggestion from Oliver [http://lists.radiantcms.org/pipermail/radiant/
2007-May/004927.html] to control extension load order, but my
customized render call isn’t overwriting the default SearchPage one.
When I look at the debug log I only see the call for Page.find. I
went into Console and confirmed that CatalogSearchExtension is being
included via ‘SearchPage.include? CatalogSearchExtension’ so am I
missing something?

=== /catalog_extension.rb SNIPPET ===
[… SNIP …]

def activate
admin.tabs.add “Catalog”, “/admin/catalog”, :after =>
“Pages”, :visibility => [:all]

 CatalogPage

 require_dependency 'search_extension'

 SearchPage.send :include, CatalogSearchExtension

end

[… SNIP …]
=== END ===

=== /lib/catalog_search_extension.rb SNIPPET ===
module CatalogSearchExtension
include Radiant::Taggable

attr_accessor :product_results

[… SNIP …]

Customized render method that also searches products

def render
@query_result = []
@product_results = []
@query = “”
q = @request.parameters[:q]

 unless (@query = q.to_s.strip).blank?
   tokens = query.split.collect { |c| "%#{c.downcase}%"}

   pages = Page.find(:all, :include => [ :parts ],
       :conditions => [(["((LOWER(content) LIKE ?) OR (LOWER

(title) LIKE ?))"] * tokens.size).join(" AND "),
*tokens.collect { |token| [token] *
2 }.flatten])

   products = CatalogProduct.find(:all,
                                  :conditions => [(["((LOWER

(description) LIKE ?) OR (LOWER(title) LIKE ?))"] * tokens.size).join
(" AND "),
*tokens.collect
{ |token| [token] * 2 }.flatten])

   @query_result = pages.delete_if { |p| !p.published? }
   @product_results = products
 end

 lazy_initialize_parser_and_context

 if layout
   parse_object(layout)
 else
   render_page_part(:body)
 end

end
end
=== END ===

James Thompson
IT Director & Web Programmer
Stewart & Associates, Inc.
550 West Kentucky Street
Louisville, KY 40203
(502) 583.5502
removed_email_address@domain.invalid
http://www.stewartaa.com/


#2

Ok, I can’t find the solution to this problem. I have
implemented the
suggestion from Oliver
[http://lists.radiantcms.org/pipermail/radiant/
2007-May/004927.html] to control extension load order, but my
customized render call isn’t overwriting the default SearchPage one.
When I look at the debug log I only see the call for Page.find. I
went into Console and confirmed that CatalogSearchExtension is being
included via ‘SearchPage.include? CatalogSearchExtension’ so am I
missing something?

You can’t override an existing method when you include a module. Methods
in the concrete class override those in the included
modules.

What you need is to run a class_eval to include those methods instead of
a module inclusion.

Dan.


#3

Thanks for the assist!

For future reference here’s how I changed my code to make this work,
maybe there is a still cleaner way:

=== /catalog_extension.rb SNIPPET ===
[… SNIP …]
def activate
admin.tabs.add “Catalog”, “/admin/catalog”, :after =>
“Pages”, :visibility => [:all]

 CatalogPage

 require_dependency 'search_extension'

 # SearchPage.send :include, CatalogSearchExtension
 CatalogSearchExtension.extend_search

end
[… SNIP …]
=== end ===

=== /lib/catalog_search_extension.rb SNIPPET ===
module CatalogSearchExtension
def self.extend_search
SearchPage.class_eval do
# Custom tags and other modifications
[… SNIP …]

   # Customized render method that also searches products
   def render
     [... SNIP ...]
   end
 end

end
end
=== end ===

James Thompson
IT Director & Web Programmer
Stewart & Associates, Inc.
550 West Kentucky Street
Louisville, KY 40203
(502) 583.5502
removed_email_address@domain.invalid
http://www.stewartaa.com/