Extension - Model Tags
Simple examples
Query all records of a given model
<r:model name="Product">
<r:find:each>
<r:value name="id" />,
<r:value name="price" />,
<r:value name="name" /> <br>
</r:find:each>
</r:model>
Simple search with conditions
<r:model name="Product">
<r:find conditions="price>1">
<r:each>
<r:value name="id" />,
<r:value name="price" />,
<r:value name="name" /> <br>
</r:each>
Number of records returned: <r:count />
</r:find>
</r:model>
Summary of the tags shown
model tag
Attribute:- name: (required) specify model name here
model:find tag
There are two main operation modes for model:find:
- if id attribute was given that it returns the record with the given id
- if no id was specified than a group of records will be returned
Either modes support the same tags.
- limit, order, conditions attributes will be passed to Model.find(...)
If distinct attribute was given than the following query will run: SELECT DISTINCT(distinct) FROM model WHERE conditions etc…
Furthermore, there is a special page type called Filter that may give further filtering conditions. More on that later.
Functions that may be called on the returned data
What described above is very basic, but real life may require more complex calculations. To support that model_tags gives you several possibilities:
- You may create class funcions (methods) that may be called using a special tag function
- You may create entity functions (methods) that may be called on the result records
This leads to potential secutity issues, that will be addressed in a very basic fashion. Now lets see this in action.
Class functions
First you need to prepare your model like this:
class Product < ActiveRecord::Base
def self.myfunction
"returned value"
end
def self.otherfunction
["value1","value2"]
end
def self.thirdfunction
{ 'key' => 'value', 'key2' => 'value2' }
end
def self.fourthfunction
Product.find(...whatever...)
end
def self.function_is_allowed_to?(fun,user)
['myfunction','otherfunction','thirdfunction', 'fourthfunction'].include?(fun)
end
end
- myfunction returns a string that maybe displayed this way
<r:model name="Product">
<r:function name="myfunction">
<r:each:value />
<r:if_returned>Great! Returned a real value...</r:if_returned>
</r:function>
</r:model>
- otherfunction returns an aray that may be displayed this way
<r:model name="Product">
<r:function name="otherfunction">
<r:each><r:value />, </r:each>
Number of returned items: <r:count />
</r:function>
</r:model>
please note the difference bw each:value and each,value
- thirdfunction returns a hash, that may be displayed this way:
<r:model name="Product">
<r:function name="thirdfunction">
<r:each:value name="key" />
<r:each:value name="key2" />
</r:function>
</r:model>
- fourthfunction returns an array of Products:
<r:model name="Product">
<r:function name="fourthfunction">
<r:each>
<r:value name="id"/> <r:value name="price"/>, <br />
</r:each>
Number of returned products: <r:count />
</r:function>
</r:model>
