Intercepting the WordPress Loop with Pre_get_posts

#deprecated (see http://codex.wordpress.org/Function_Reference/query_posts#Post_.26_Page_Parameters)

Using the “pre_get_posts” filter you can intercept the WordPress loop and override or set attributes in the “$query” object. This is useful because you can select just the posts you want, say for instance on your homepage you want to show posts from just one category.

add_filter('pre_get_posts', 'filter_homepage_posts');
function filter_homepage_posts($query) {
    $limit_number_of_posts = 5;
    $featured_category_id = get_cat_id('Reviews'); // by cat name...
    if ($query->is_home) {
        $query->set('cat', $featured_category_id);
        $query->set('showposts', $limit_number_of_posts);
    }
  return $query;
}

Thematic Function Reference

I could not find a listing of all thematic theme functions for WordPress online. So the following is just a recursive grep of the thematic directory.

grep -rh "function thematic_" *

Each function needs to prepended with “thematic_” when adding as a action or filter eto a WordPress childtheme. Example

function my_function_says_hello(){ print "Hello!"; }
add_filter('thematic_abovepagebottom', 'my_function_says_hello');

Here is the list

remove_generators()
abovecomments()
abovecommentslist()
belowcommentslist()
abovetrackbackslist()
belowtrackbackslist()
abovecommentsform()
show_subscription_checkbox()
belowcommentsform()
show_manual_subscription_form()
belowcomments()
singlecomment_text()
multiplecomments_text()
postcomment_text()
postreply_text()
commentbox_text()
commentbutton_text()
commenter_link()
comments_template()
include_comments()
abovecontainer()
archives()
navigation_above()
navigation_below()
above_indexloop()
archiveloop()
authorloop()
categoryloop()
indexloop()
searchloop()
singlepost()
tagloop()
below_indexloop()
above_categoryloop()
below_categoryloop()
above_searchloop()
below_searchloop()
above_tagloop()
below_tagloop()
belowcontainer()
page_title()
nav_above()
archive_loop()
author_loop()
category_loop()
index_loop()
single_post()
search_loop()
tag_loop()
time_title()
time_display()
postheader()
postheader_posteditlink()
postheader_posttitle()
postheader_postmeta()
postmeta_authorlink()
postmeta_entrydate()
postmeta_editlink()
content()
archivesopen()
category_archives()
monthly_archives()
archivesclose()
404()
404_content()
postfooter()
postfooter_posteditlink()
postfooter_postcategory()
postfooter_posttags()
postfooter_postcomments()
postfooter_postconnect()
nav_below()
previous_post_link()
next_post_link()
author_info_avatar()
cats_meow($glue)
tag_ur_it($glue)
comments($comment, $args, $depth)
pings($comment, $args, $depth)
body_class( $print = true )
post_class( $print = true )
comment_class( $print = true )
date_classes( $t, &$c, $p = '' )
abovefooter()
footer()
footertext($thm_footertext)
belowfooter()
after()
subsidiaries()
siteinfoopen()
siteinfo()
siteinfoclose()
create_doctype()
head_profile()
doctitle()
create_contenttype()
seo()
canonical_url()
use_excerpt()
use_autoexcerpt()
create_description()
show_description()
create_robots()
show_robots()
create_stylesheet()
show_rss()
show_commentsrss()
show_pingback()
show_commentreply()
head_scripts()
add_menuclass($ulclass)
before()
aboveheader()
header()
brandingopen()
blogtitle()
blogdescription()
brandingclose()
access()
belowheader()
trim_excerpt($text)
the_excerpt($deprecated = '')
excerpt_rss()
tag_query()
sidebar()
abovemainasides()
betweenmainasides()
belowmainasides()
aboveindextop()
belowindextop()
aboveindexinsert()
belowindexinsert()
aboveindexbottom()
belowindexbottom()
abovesingletop()
belowsingletop()
abovesingleinsert()
belowsingleinsert()
abovesinglebottom()
belowsinglebottom()
abovepagetop()
belowpagetop()
abovepagebottom()
belowpagebottom()
abovesubasides()
belowsubasides()
subsidiaryopen()
before_first_sub()
between_firstsecond_sub()
between_secondthird_sub()
after_third_sub()
subsidiaryclose()
search_form()
widgets_init()
sort_widgetized_areas($content)
primary_aside()
secondary_aside()
1st_subsidiary_aside()
2nd_subsidiary_aside()
3rd_subsidiary_aside()
index_top()
index_insert()
index_bottom()
single_top()
single_insert()
single_bottom()
page_top()
page_bottom()
before_widget_area($hook)
after_widget_area($hook)
before_widget()
after_widget()
before_title()
after_title()

Thematic is a nice theme framework for WordPress. More info is available at the developers website ThemeShaper. Using the above filters it is possible to create a highly customized child theme quickly.

Very Simple Breadcrumb Navigation in Rails with Before_filter

This may not be the ideal solution. This just manages request.referers in a session variable, then looping over each unique path prints a link to the screen. It’s more of a “history” than a hierarchy for resources in an application. It is however, pretty straight forward to implement.

First create a before_filter in your application controller.Call it on every action. In the definition we’ll parse the request.referer variable with the URI::parse method so that we’re only dealing with the relative, not absolute, resource. We’ll make sure that we’re only storing unique paths and if the user arrives at an index action, we set the session[:breadcrumbs] variable to nil. This indicates that they are at the top level.

#app/controllers/application_contoller.rb
#...
before_filter :setup_breadcrumb_navigation
protected
def setup_breadcrumb_navigation
    if params[:action] == "index"
      session[:breadcrumbs] = nil
    else
      url = URI::parse request.referer
      if session[:breadcrumbs].nil?
        session[:breadcrumbs] = url.path.strip
      else
        session[:breadcrumbs] = session[:breadcrumbs] + ", "+url.path if session[:breadcrumb]
      end
      session[:breadcrumbs] = session[:breadcrumbs].split(", ").uniq.join(",")
    end
  end
end

The helper function just splits apart the string stored in the session[:breadcrumbs] variable, looping over the relative paths. We link to them, but first replace the ugly “/” and preface the display with some form of delimiter. I chose to use the “::” which looks nice to me.

#app/helpers/application_helper.rb
  def breadcrumbs
    content_tag :span, :id=>"breadcrumbs" do
      if not session[:breadcrumbs].blank?
        session[:breadcrumbs].split(",").uniq.map{ |breadcrumb|
            link_to(" :: "+breadcrumb.to_s.gsub("/"," ")+"",breadcrumb)
        }
      end
    end
  end

This is definitely a hack. You won’t necessarily find a hierarchical relationship between links in the breadcrumb navigation. However, it will display the users history letting them quickly access a resource they just visited a page or two before. It’s perhaps 80% of the functionality for only 5% of the work.

Oh yeah, putting this in a view

# app/views/layouts/application.html.erb
<%= breadcrumbs %>

Yield a Block within Rails Helper Method with Multiple Content_tags Using Concat

To clean up some repetitive html coding in views, pass a block of text to a helper function which will wrap it for you the same way, each and every time. For example, I have a ‘help’ link which will toggle the display of a block of text if a link is clicked. I use this “+/- help” link throughout my application in various views. I could create a partial but this gets messy. Instead this is what I want to write in my views…

<% help do %>
  here is my help text...
<% end %>

which will render HTML similar to this…

<a href='#' 'class='help' id='help_link_123456' onclick='some_func_to_toggle_state'>+/- help</a>
<div id="help_123456">
  here is my help text...
</div>

In order to accomplish this you need to create a helper method. Assigning a unique id, just a random number, will help avoid collisions with the state of the toggled div if you use this more than once per page. Placing other HTML helper methods inside the concat() method allows multiple tags as well as rendering the block passed to the function in the appropriate place. It looks a little unwieldy, but works nicely.

# app/helpers/application_helper.rb
module ApplicationHelper
  def help(&block)
    uniqid = rand; concat( link_to_function("+/- help") do |page|
      page["help_#{uniqid}"].toggle
      page.visual_effect :highlight, "help_#{uniqid}"
    end + content_tag(:div,:class=>"help",:id=>"help_#{uniqid}", :style=>"display:none") do
      yield
    end )
  end
end

Nesting Resources in Rails Routes.Rb with Namespaces

When I have a controller that takes more than one has_many argument, I think about creating a namespace. This way I may still use my forums, pages controllers w/out needing any conditional logic, testing for the presence of :course_id in the params hash.

#config/routes.rb
map.namespace :courses, :path_prefix=>'/courses/:course_id' do |courses|
  courses.resources :forums
  courses.resources :pages
end

map.resources :courses
map.resources :forums
map.resources :pages
# some view.html.erb
<%= courses_forums_path(1) %> /courses/1/forums
<%= courses_pages_path(2) %> /courses/2/pages
# w/ use in a form don't forget to pass an array instead of just the resource (will map to forums_controller)
<%=form_for [:courses, Forum.new] do |f| %><%end%>

in the namespaced controller you can extend the parent controller (if you like) to have access to methods coursescontroller defines.

# app/controllers/courses/forums_controller.rb
class Courses::ForumsController < CoursesController
 before_filter :require_course_login #defined in parent ../courses_controller.rb
 def index
  end
end

to use the generator to create namespaced controller

./script/generate controller 'courses/forums' 

Rake DB Everything, Dump, Destroy, Create, Load

I’m a big fan of the yaml_db plugin. But I don’t like running rake db:data:load, only to find that my db columns mismatch my model attributes, thus aborting the data import task. To quickly add/remove columns/attributes from a model and rebuild the database with previous db info, I wrote this simple rake task. It saves model records as yaml in db/seandb.yml, and reloads them in the same task but with the db rebuilt. I nest the save method in begin/rescue so that if there are any conflicts the task will continue.

This biggest challenge was to get a list of all the active record models in the Rails app. This problem has been posted a lot and I didn’t find an easy solution. For now, I just look in the app/models directory and ‘classify’ and ‘constantize’ the file name. This will load the model so that I can iterate over all the active record subclasses and call the appropriate Model.find(:all) method. I could maybe do the same w/ all files in the Rails.configuration.load_paths, but my models are, at least for now, under app/models. Plugin models are loaded and available from the rake task if you require the :environment.

It’s as simple as running

rake db:everything
SAVEDB = "#{RAILS_ROOT}/db/seandb.yml"
namespace :db do
   task :everything => [:environment, :spit, :drop, :create, :migrate, :populate] do
     desc "spit out model records as yaml, rebuild the database and repopulate the db"
   end

   task :spit do
    Dir["#{RAILS_ROOT}/app/models/*"].each {|file| (File.basename(file,".*").classify.constantize)}
    File.open(SAVEDB, "w") do |f|
      ActiveRecord::Base.send(:subclasses).each do |model|
        begin
          model.find(:all).each do |record|
              f.write(record.to_yaml)
              print "\twrote #{record.class}\n"
          end
        rescue
        end
      end
    end
   end

   task :populate do
    File.open( SAVEDB ) do |yf|
      YAML.each_document( yf ) do |ydoc|
        begin
          m = ydoc.clone
          m.save
          p "...saved #{m.id}"
        rescue
        end
      end
    end
   end
end