Class: RGhost::Grid::Rails
- Defined in:
- lib/rghost/grid/rails_grid.rb
Overview
Grid::Rails mapps grid columns to ActiveRecord attributes. For example, let's take a Calls class to represent telephone calls.
Example
rails_grid=Grid::Rails.new(:align => :center, :width => 3)
Mapping, first the attribute name then the already known optionsName
rails_grid.column :mobile, :title => "Mobile", :align => :right
rails_grid.column :duration, :title => "Duration",:width => 3
rails_grid.column :start, :title => "Start", :format => :eurodate
rails_grid.column :called, :title => "Number", :align => :right, :title_align => :center
Load data
calls = Call.find(:all, :limit => 5000)
rails_grid.data(calls)
Relationships
You can also use attributes of associated objects, via a block or string. For example consider Accounts and Customers relationship. Let's produce a grid od all Customers plus some data reffering to the first account the customer had created.
class Accounts < ActiveRecord::Base
belongs_to :customer
end
class Customers < ActiveRecord::Base
has_many :accounts
end
c = Customers.find(:all, :include => :accounts )
Using a block
g=Grid::Rails.new :width => 4
g.column :id, :title => "Customer id"
g.column :name, :title => "Name", :width => 6
g.column :created_on, :title => "Date ", :format => lambda{|d| d.strftime("%m/%d/%Y") }
g.column lambda { accounts.first.login }, :title => "Login"
g.column lambda { accounts.first.type }, :title => "Account Type"
g.data(c)
Or a String
g.column 'accounts.first.login', :title => Login"
Constant Summary
Constants included from RubyToPs
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
-
#col(field_name, options = {}) ⇒ Object
:nodoc:.
-
#column(field_name, options = {}) ⇒ Object
:nodoc:.
-
#data(_data) ⇒ Object
The parameter
_data
is an Array of ActiveRecord::Base objects.
Methods inherited from Base
#format_field, #initialize, #proc_line, #ps, #style, #width
Methods included from CallbackFacade
#after_column, #before_column, #before_row, #even_column, #even_row, #odd_column, #odd_row
Methods included from RubyToPs
#array_to_stack, #hash_to_array, #pack_string, #ps_escape, #string_eval, #to_array, #to_bool, #to_string, #to_string_array
Methods inherited from PsObject
#<<, #call, #graphic_scope, #initialize, #ps, #raw, #set, #to_s
Constructor Details
This class inherits a constructor from RGhost::Grid::Base
Instance Method Details
#col(field_name, options = {}) ⇒ Object
:nodoc:
60 61 62 63 64 65 66 67 68 |
# File 'lib/rghost/grid/rails_grid.rb', line 60 def col(field_name, ={}) #:nodoc: super([:title],) @rails_cols||=[] owf=.dup #from options with field owf[:field_name]||=field_name @rails_cols << owf end |
#column(field_name, options = {}) ⇒ Object
:nodoc:
70 71 72 |
# File 'lib/rghost/grid/rails_grid.rb', line 70 def column(field_name, ={}) #:nodoc: col(field_name,) end |
#data(_data) ⇒ Object
The parameter _data
is an Array of ActiveRecord::Base objects.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/rghost/grid/rails_grid.rb', line 44 def data(_data) _data.collect do |d| line=@rails_cols.collect do |c| case c[:field_name] when Symbol d[c[:field_name]] when String d.instance_eval c[:field_name] when Proc d.instance_eval(&c[:field_name]) end end proc_line(line) end end |