ActiveScaffold – Improved Search

As I noted in my last post, I have changed projects. In my current project, we are using a combination of Ruby and Java. A small group of developers (including me) on the project were tasked with creating a set of automation tools. One of the tools we needed was a small data gathering and reporting application to help our project managers keep track of our progress. The logical choice, for us, was to implement our reporting tool in Ruby on Rails (ROR).Why ROR? One word, “Scaffolding”. If you are not a Ruby or ROR fan, you may not be familiar with Scaffolding (screencast). Scaffolding gives a developer “declarative” control over the user interface.The original scaffolding is really not meant to be a full fledge declarative user interface. It was really intended to be a starting point. The original scaffolding does not support relationships very well (as in not at all). Given this information we knew that we needed to find a rails plugin that could do more than the original. We had two choices.

  1. Streamlined – This was the first plugin that we evaluated. We really liked the interface that it generated and its configuration syntax, but we were not happy with how it allowed users to create related objects. Creating a new relationship in Streamlined is a heavy three step process.
    1. Goto related object’s page and create the new related object.
    2. Return to the parent page.
    3. Apply the relationship to the desired parent.
  2. ActiveScaffold – This was the second plugin that we evaluated. ActiveScaffold allows users to create anything and everything about a record from a single page. This was really attractive to us. ActiveScaffold allowed us to quickly add relationships without have to move to multiple parts of the application.

ActiveScaffold won the race in the end. Armed with ActiveScaffold, we were able to quickly to implement our reporting application. One criticism from our users was the somewhat weak search that ActiveScaffold exposes.The default search is a simple ‘%like%’ search on all columns in a given record. Our users wanted to be able to negate a search. Give me all records except those that contain X. What can we do? ActiveScaffold does not support this functionality. We can go hacking 😉 The module in ActiveScaffold that implements this functionality is ActiveScaffold::Finder; specifically the create_conditions_for_columns method.This is my hacked up version of create_conditions_for_columns. Negation is triggered by placing a ~ in front of a term. Example: ~adam. Fill free to take this code and do with it as you wish. Hopefully someone else can make use of it. As always, questions and comments are welcome.

def self.create_conditions_for_columns(tokens, columns,    like_pattern = '%?%')  # if there aren't any columns, then just return a nil condition  return unless columns.length > 0  tokens = [tokens] if tokens.is_a? String  phrases = []  tokens.each do |token|    where_clauses = []    not_token = /^~/.match(token)    not_clause = not_token ? 'NOT' : ''    columns.each do |column|      where_clauses <<        "LOWER(COALESCE(#{column.search_sql}, '')) REGEXP ?"    end    phrases << "(#{not_clause} (#{where_clauses.join(' OR ')}))"  end  sql = phrases.join(' AND ')  tokens = tokens.collect do |value|    value = /^~/.match(value) ? value[1..-1] : value    value = '.' if value.empty?    [value.downcase] * columns.length  end.flatten  [sql, *tokens]end

2 Responses

  1. hi
    i like your post
    i would like to know,how can we give the relationship between role table and user table
    we could not do this in activescaffold
    can you please give me the answer to my email id

  2. @hussain,

    If I understand you correctly, all you should need to do is define the relationship between the user and role in the model. After the relationship is defined, I think you only need to define the controller for both the user and the role and it should just work. You should check ActiveScaffold documentation for more information.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: