Extras

At this point our application is complete and tested. At this point you can dive right in to the User’s Guide, but there are a few extra things we can add to further demonstrate some more of Ferris’ capabilities.

Behaviors and Searching

Similar to components, Ferris uses Behaviors as a way of organizing commonly used functionality for models. A useful behavior is the Searchable behavior.

First, we need to modify our model:

from ferris.behaviors.searchable import Searchable

def Post(BasicModel):
    class Meta:
        behaviors = (Searchable,)

Note

Any posts created before you made this change will not be searchable until you edit and resave them.

Now we’ll use the Search component in our controller to use the behavior:

from ferris.components.search import Search

class Posts(Controller):
    components = (scaffold.Scaffolding, Pagination, Search)

Now let’s the ability to search to our list action:

def list(self):
    if 'query' in self.request.params:
        self.context['posts'] = self.components.search()
    elif 'mine' in self.request.params:
        self.context['posts'] = self.Model.all_posts_by_user()
    else:
        self.context['posts'] = self.Model.all_posts()

Import the search macros into templates/posts/list.html:

{% import 'macros/search.html' as search with context %}

Finally, add these somewhere at the top of the inside of the layout_content block:

{{search.filter(uri())}}
{{search.info()}}

Now when we visit http://localhost:8080/posts there should be a search box from where we can search through all posts.