Forum: Radiant CMS Extending the Search Extension

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
1d9161badbedac9cb60524a1c33620d5?d=identicon&s=25 James Thompson (Guest)
on 2007-05-29 20:32
(Received via mailing list)
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
jamesthompson@stewartaa.com
http://www.stewartaa.com/
653df32382f1dd1fc89754c4582858ae?d=identicon&s=25 Daniel Sheppard (Guest)
on 2007-05-30 03:01
(Received via mailing list)
> 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.
1d9161badbedac9cb60524a1c33620d5?d=identicon&s=25 James Thompson (Guest)
on 2007-05-30 15:31
(Received via mailing list)
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
jamesthompson@stewartaa.com
http://www.stewartaa.com/
This topic is locked and can not be replied to.