Sign In
Start Page | Recent Changes | All Pages | Orphan Pages | Junebug Help

Comparing version 10 and version 9

h3. Simple examples

h4. Query all records of a given model



  
    , 
    , 
     
h4. Simple search with conditions


  
    
      , 
      , 
       
Number of records returned:
h3. Summary of the tags shown h4. model tag Attribute: * *name*: (required) specify model name here h4. 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. h3. Functions that may be called on the returned data (or on the Model itself) 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. h4. Class functions First you need to prepare your model like this:

class Product < ActiveRecord::Base

  def self.myfunction(params=nil,conditions=nil)
    "returned value"
  end

  def self.otherfunction(params=nil,conditions=nil)
    ["value1","value2"]
  end

  def self.thirdfunction(params=nil,conditions=nil)
    { 'key' => 'value', 'key2' => 'value2' } 
  end

  def self.fourthfunction(params=nil,conditions=nil)
    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


  
    
    Great! Returned a real value...
  

* *otherfunction* returns an aray that may be displayed this way


  
    , 
    Number of returned items: 
  

please note the difference bw *each:value* and *each,value* * *thirdfunction* returns a hash, that may be displayed this way:


  
    
    
  

* *fourthfunction* returns an array of Products:


  
    
        , 
Number of returned products:
h4. Instance functions Instance functions may be created like class functions. They may return a single string, an array of values, a hash, an array of hashes or model instances. Same game. Prepare your model almost like above:

class Product < ActiveRecord::Base

  def myfunction(params=nil,conditions=nil)
    ["value1","value2"]
  end

  def function_is_allowed_to?(fun,user)
    ['myfunction'].include?(fun)
  end

end
* *myfunction* returns an aray that may be displayed this way


  
    
      
        , 
        Number of returned items: 
      
    
  

The important point here is that *function* tag is placed inside *model:find:each* instead of *model*. In the latter case the function is called on the Model class, in the former case on the returned Model instance. *(params=nil,conditions=nil)**params=nil,conditions=nil* parameters may be used to pass parameters and conditions to the function. * *params* is an array that is the value of the attribute *params* split on commas like this: params="1,2,3" => ["1","2","3"] * *conditions* is the combined conditions given to the *find* tag and represented by the Filter url.

Back to Page