console

An article by Gaspard Bucher

Zena adds some useful methods when using “script/console” or for custom scripts:

login

You can login a user with:

login(:gaspard)

If you have multiple sites, you need to specify the host:

login(:gaspard, 'zenadmin.org')

find(query)

Use SQLiss to find nodes:

n = find('posts where created_at > 2011-03-24')

If plural/singular is not clear, you can explicitly say if you want one or many:

n = find(:all, 'posts where created_at > 2011-03-24')

You can also find a node from it’s zip (url id):

n = find(6)

count(query)

Use SQLiss to count nodes:

n = count('posts where created_at > 2011-03-24')

foreach(query)

Loop in all elements of a query. This is better then find(query).each because Zena takes care of loading elements by chunks.

foreach('posts where created_at > 2011-03-24') do |post|
  # ...
end

nodes(id_or_title)

Quick access to find a node from a given title or zip (this is similar to the “nodes” method in tests):

nodes(2)

rename_prop(list, old_key, new_key)

Changes a property on a list of elements containing the properties (Node, Version, Site, etc). If you specify a string instead of a list, the string will be used to find nodes.

list = find('posts')
rename_prop(list, 'name', 'last_name')
# Same as
rename_prop('posts', 'name', 'last_name')

This method does not create new versions but changes them in place.

field_to_prop(list, native_key, prop_key)

Changes a native field (as a normal SQL table column) to a property. The example below changes the value in Zena’s legacy “custom_a” column to a “hot” property. If you specify a string instead of a list, the string will be used to find nodes.

field_to_prop('pages', 'custom_a', 'hot')

set_prop(list, key, value)

Batch change a property without creating new versions (changes all versions). If you specify a string instead of a list, the string will be used to find nodes. Example to remove the “date” attribute:

set_prop('posts', 'date', nil)