Has the boss just discovered wikis? Does he now want to be able to create links between records in your intranet applications? Need to quickly add wiki functionality to your models and views so you can get back on with your life?
That’s what happened to me recently. Since the wiki code was almost exactly the same for all of the applications that needed changing, I’ve put the code into a plugin. Hopefully someone else will find it useful.
Installing the plugin
The plugin is installed in the same manner as any other. Fire up your favourite terminal and navigate to the root of your rails application. Then run the following command:
script/plugin install http://hazy.stupor.org/svn/has_wiki_field/trunk/has_wiki_field/
That should be all that is required.
Using the plugin
The following example should get you up and running quickly but make sure you read the efficiency note below.
Lets say you have a Person model that has name and bio attributes. We would like to be able to link to other people from within a bio. So in Allan’s record we might want the bio to read “This man is friends with [[Betty]]” and have [[Betty]] automatically change into a link to her record when it is displayed.
We, start by adding has_wiki_field to the Person model:
class Person < ActiveRecord::Base
has_wiki_field :field => "name", :sql_match => "LIKE"
end
The :field option specifies what field should be matched by the text in [[links]]. The :sql_match option specifies how the records should be matched. :sql_match defaults to “=” but using “LIKE” will allow case insensitive links.
Now we need to change show.html.erb for the people controller so that links are generated from the bio.
Where previously we might have displayed a bio using:
We will now use the wikify method, passing it the name of the attribute to wikify and a code block which generates the html for links. A code block is used to allow flexibility in link generation. The following block simply calls link_to but you could, for example, write a block which looks up an image associated with your model and displays that instead.
Anyway, for our Person model we add the following to the view:
<%= @person.wikify(:bio) do |name, object|
if object.nil?
h name
else
link_to name, object
end
end
%>
In a real application this should be put into a method in people_helper to help keep things tidy.
That’s it! Where [[Some Name]] appears in a Person’s bio it will be turned into a link to the person with the name ‘Some Name’.
Remember to read the important efficiency note section below before using this in a production environment.
An important note about efficiency
The default method used for finding which object a [[link]] points to is not efficient. For sites that receive more than a tiny amount of traffic it should be replaced by your own model specific method.
The default method takes the contents of each [[link]] and queries the database for the first record that has ‘link’ as the value for the field specified in the model, I.e.
find(:first, :conditions => ["somefieldname = ?", key])
This will obviously require a database look-up for every link which could slow your site down tremendously for pages containing many links.
To fix this you must provide your own method to match objects with the keys extracted from [[links]]. The class method to override for this is object_for_key(key).
For example, if we have a class called Person we might replace the object_for_key method like so:
class Person < ActiveRecord::Base
has_wiki_field
def self.object_for_key( key )
# some super fast method for matching the key
# from a link to a person object, goes here...
#
# return the Person object that matches or nil if there is no match
end
end
Naturally, it’s up to you to manage the data used by your object_for_key method. wiki_options[:field] and wiki_options[:sql_match] will be available to you in this method if they were set as options to has_wiki_field.
Future plans
The plugin doesn’t do much currently. If I’d only needed it for a single application there’s a good chance it wouldn’t have made it into a plugin at all. It has provided me with a reasonable starting point for my applications and hopefully it’ll get better as time goes on.
Some ideas for future versions:
- Add a controller action which takes a key and redirects to the appropriate record or creates a new record with the needed field already filled.
- Using the new controller method above, allow links of the form [[Some Key#Model]] where “Model” specifies what type of thing links are pointing to. Ta-dah! Now we can link between all the models in an application.
- Implement some basic object_for_key methods for common situations. The plugin should attempt to gently direct programers towards these methods.
- Implement a basic link caching mechanism. When a has_wiki_field model is saved links should be extracted and stored in a table. Then all current links for a model can be loaded in a single database transaction.