Class: DBListWidget
- Inherits:
-
BaseList
- Object
- Cuca::Widget
- BaseList
- DBListWidget
- 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
Instance Attribute Summary
Attributes inherited from BaseList
Instance Method Summary collapse
- #columns ⇒ Object
-
#fixup_columns ⇒ Object
this will fix/add searchable/sortable and query flag.
-
#normalize_result(ar_res) ⇒ Object
transform a active record result to an [[]]- array.
- #output(list_name, model_class = nil, data_setup = {}) ⇒ Object
- #query(query_def) ⇒ Object
- #setup ⇒ Object
-
#wc_query_field(field_id) ⇒ Object
returns :query field by :id (only :id is defined in the QueryDef).
- #where_clause(query_def) ⇒ Object
Methods inherited from BaseList
#add_rewrite_hook, #check_query_def, #load_query_definition, #rewrite_field
Methods included from Cuca::Generator::Markaby
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
#columns ⇒ Object
20 21 22 23 |
# File 'lib/cuca/stdlib/listwidget/dblist.rb', line 20 def columns # $stderr.puts " Getting Columns: #{@columns}" @columns end |
#fixup_columns ⇒ Object
this will fix/add searchable/sortable and query flag
96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/cuca/stdlib/listwidget/dblist.rb', line 96 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
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/cuca/stdlib/listwidget/dblist.rb', line 53 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
111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/cuca/stdlib/listwidget/dblist.rb', line 111 def output(list_name, model_class = nil, data_setup = {}) @columns = data_setup[:columns] || [] @extra_conditions = data_setup[:conditons] || "" @joins = data_setup[:joins] || "" @model_class = model_class || nil setup fixup_columns # $stderr.puts @columns.inspect @columns.freeze @extra_conditions.freeze super(list_name) end |
#query(query_def) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/cuca/stdlib/listwidget/dblist.rb', line 69 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 sel = @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(',') $stderr.puts "Find-Stuff: #{findstuff.inspect}" @data = @model_class.find(:all, findstuff) @data = normalize_result(@data) @total_rows= @model_class.count(:conditions => where_clause(query_def), :joins => @joins) # $stderr.puts "Query: #{@data.inspect} - #{query_def.order_by.inspect}" end |
#setup ⇒ Object
90 91 92 |
# File 'lib/cuca/stdlib/listwidget/dblist.rb', line 90 def setup super end |
#wc_query_field(field_id) ⇒ Object
returns :query field by :id (only :id is defined in the QueryDef)
26 27 28 29 30 31 32 33 |
# File 'lib/cuca/stdlib/listwidget/dblist.rb', line 26 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
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/cuca/stdlib/listwidget/dblist.rb', line 35 def where_clause(query_def) 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}" else res << "#{wc_query_field(k)} LIKE '%#{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 |