Class: DBListWidget

Inherits:
BaseList show all
Defined in:
lib/cuca/stdlib/listwidget/dblist.rb

Overview

List with Active Record model as data source

DBList(‘dblistname’, User, :columns => [ { :id => ‘id’, :query => ‘user.id’, :display=>‘ID’ },

{ :id => 'name',  :display=>'Name', :searchable=>false } ])

Options: :joins => ActiveRecord find :joins options.

:columns: Columns possible flags:

:id         => [required] name. Likly this is the database column name
:query      => [optional] how to query the id (eg: users.name) . If false then treated as virtual column
:display    => [optional] title for the column
:searchable => [optional] true/false if this column can be searched (default autodetect)
:sortable = => [optional] true/false if columns can be sorted (default autodetect)

DBLIST specific is only :query

Constant Summary collapse

@@like_expression =
'LIKE'

Instance Attribute Summary

Attributes inherited from BaseList

#data, #rewrite_hooks

Instance Method Summary collapse

Methods inherited from BaseList

#add_rewrite_hook, #check_query_def, #list_size_links, #load_query_definition, #rewrite_field, #row2hash

Methods included from Cuca::Generator::Markaby

#mab, #mabtext

Methods included from Cuca::Generator::View

#view, #view_p, #viewtext, #viewtext_p

Methods inherited from Cuca::Widget

#app, #cgi, #clear, clear_hints, #content, #content=, #controller, define_attr_method, #escape, #escapeHTML, #get_assigns, #hints, #initialize, #log, #params, #query_parameters, #request_method, #request_parameters, run_attr_method, #session, #to_s, #unescape, #unescapeHTML

Constructor Details

This class inherits a constructor from Cuca::Widget

Instance Method Details

#columnsObject



22
23
24
25
# File 'lib/cuca/stdlib/listwidget/dblist.rb', line 22

def columns
#   $stderr.puts " Getting Columns: #{@columns}"
  @columns.delete_if { |c| !c[:display] }
end

#fixup_columnsObject

this will fix/add searchable/sortable and query flag



112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/cuca/stdlib/listwidget/dblist.rb', line 112

def fixup_columns
   @columns.each_index do |idx| 

     if @columns[idx][:searchable].nil? then
         @columns[idx][:searchable] = @model_class.column_methods_hash[@columns[idx][:id].intern] ? true : false
     end
     @columns[idx][:query] = @columns[idx][:id] if @columns[idx][:query].nil?
     
     if @columns[idx][:sortable].nil? then
         @columns[idx][:sortable] = @columns[idx][:query] == false ? false : true
     end
     
   end
end

#normalize_result(ar_res) ⇒ Object

transform a active record result to an [[]]- array



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/cuca/stdlib/listwidget/dblist.rb', line 60

def normalize_result(ar_res)
  res = []
  ar_res.each do |r|
     c = []
     columns.each do |col| 
        if r.attributes[col[:id]] then
           c << r.send(col[:id].intern)
        else
           c << ''
        end
     end
     res << c
  end
  res
end

#output(list_name, model_class = nil, data_setup = {}) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/cuca/stdlib/listwidget/dblist.rb', line 127

def output(list_name, model_class = nil, data_setup = {})
  @columns           = data_setup[:columns] || [] 
  @extra_conditions = data_setup[:conditons] || ""
  @joins	      = data_setup[:joins] || ""
  @group_by	     =  data_setup[:group_by] || ""
  @options	      ||= data_setup[:options] 		# can be used by 'setup'
  @model_class = model_class || nil
  setup
  fixup_columns
#   $stderr.puts @columns.inspect
#   @columns.freeze
  @extra_conditions.freeze
  @query_columns = @columns.dup
  @columns = @columns.delete_if { |c| !c[:display] } # don't display columns without a 'display'
  super(list_name)
end

#query(query_def) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/cuca/stdlib/listwidget/dblist.rb', line 77

def query(query_def)
  findstuff = {:conditions => where_clause(query_def) }
  findstuff[:order]  = "#{query_def.order_by} #{query_def.order}" unless (query_def.order_by.nil? || query_def.order_by == '')
  findstuff[:offset] = query_def.range.first
  findstuff[:limit]  = query_def.range.last-query_def.range.first+1
  findstuff[:joins]  = @joins || nil
  findstuff[:group]  = @group_by if @group_by.to_s != '' 
  sel = @query_columns.collect do |c| 
     ret = c.has_key?(:query) ? "#{c[:query]} as #{c[:id]}" : c[:id] 
     ret = nil if c[:query] == false
     ret
  end
  findstuff[:select] = sel.compact.join(',')
  @data = @model_class.find(:all, findstuff)
  @additional_data = @data.dup

  rowcount_findstuff = findstuff.dup
  rowcount_findstuff.delete(:limit)
  rowcount_findstuff.delete(:offset)
  rowcount_findstuff.delete(:order)
        
  @data = normalize_result(@data)   
  
  @total_rows= @model_class.count(rowcount_findstuff)
  if @total_rows.kind_of?(Hash) then   # if group_by is defined we'll get this
    @total_rows = @total_rows.size
  end
end

#quote(input) ⇒ Object



37
38
39
# File 'lib/cuca/stdlib/listwidget/dblist.rb', line 37

def quote(input)
  input.gsub(/\\/, '\&\&').gsub(/'/, "''") 
end

#setupObject



106
107
108
# File 'lib/cuca/stdlib/listwidget/dblist.rb', line 106

def setup
  super
end

#wc_query_field(field_id) ⇒ Object

returns :query field by :id (only :id is defined in the QueryDef)



28
29
30
31
32
33
34
35
# File 'lib/cuca/stdlib/listwidget/dblist.rb', line 28

def wc_query_field(field_id)
   @columns.each do |c|
         if c[:id] == field_id then 
            return c[:query]
         end
   end
   field_id
end

#where_clause(query_def) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/cuca/stdlib/listwidget/dblist.rb', line 41

def where_clause(query_def)
  like = @@like_expression || 'LIKE'		# this allows to overwrite to ILIKE for example
  res = []
  query_def.filters.each_pair do |k,v|
      next if (v.nil? || v == '')
      if @model_class.columns_hash.include?(k) && @model_class.columns_hash[k].number? then
        res << "#{wc_query_field(k)} = #{v.to_i}"
      else
        res << "#{wc_query_field(k)} #{like} '%#{quote(v)}%'"
      end
  end
  wc =  "true"
  res.collect { |e| "(#{e})" }.each { |c| wc+=" AND #{c}" }
  wc+= " AND #{@extra_conditions}" if @extra_conditions != ''
#   $stderr.puts "WHERE clause is #{wc}"
  return wc
end