This article shows how to convert a Rails application to a standard Ruby application, and it shows how to present the data via GTK2. The idea is that a web application and a desktop application can have similar needs, and going back and forth via a common framework has some advantages. We are taking this rails controller and using just the People section from this view and adapting it to GTK2 using Ruby-GNOME2. We are doing this development work on the MCJ GNU/Linux Reference OS version 3.2.6a with the extra GTK2 packages compiled in as documented in the Reference OS.
This is how the Rails gems and ActiveRecord is set up:
require 'rubygems' require 'active_support/all' require 'active_record' require 'gtk2' ActiveRecord::Base.establish_connection( :adapter => "sqlite3", :database => "/sources/mcj.rsd" ) class People < ActiveRecord::Base set_table_name "peoples" end class Realm < ActiveRecord::Base; end class Art < ActiveRecord::Base; end class Setting < ActiveRecord::Base; end |
This is code that is pretty much right out of the Rails application:
$rname = 'l1g' @realmrow = Realm.find(:first, :conditions=> ["name='"+$rname+"'"]) # People ****************************** @people_distinct = People.find(:all, :conditions=> ["( realm='l1g' OR realm='mcjbuildtab1')"], :select=> "DISTINCT person") @people_with_person = [] for x in @people_distinct @people_with_arts = People.find :all, :conditions=> ["( person= '"+x.person+"' AND ( realm='mcjbuildtab1' OR realm='l1g'))"] for y in @people_with_arts @arts_matching_people = Art.find :all, :conditions=> ["( artnum= "+y.artnum.to_s+" AND realm='"+y.realm+"')"] for z in @arts_matching_people z.classification=x.person @people_with_person << z end end end @people_with_person= @people_with_person.sort_by { |a| [ a.classification, a.date] } $lastperson="wukkawukka" @people_with_person_m=[] for zz in @people_with_person if zz.classification == $lastperson zz.classification = "" else $lastperson=zz.classification end @people_with_person_m << zz end |
First we take the @people_with_person_m array and create a GTK list to render:
keyword_list = Gtk::ListStore.new(String, String, String, String) window = Gtk::Window.new("People Keywords") window.signal_connect("destroy"){Gtk.main_quit} person='person' for ruk in @people_with_person_m iter=keyword_list.append iter[0]=person iter[1]=ruk.classification iter[2]=ruk.artnum.to_s iter[3]=ruk.title person='' end |
Now let’s populate a GTK::TreeView:
view = Gtk::TreeView.new(keyword_list) renderer = Gtk::CellRendererText.new renderer.background="gray" col = Gtk::TreeViewColumn.new("Keyword Type", renderer, :text => 0) view.append_column(col) col = Gtk::TreeViewColumn.new("Keyword", renderer, :text => 1) view.append_column(col) col = Gtk::TreeViewColumn.new("Article Number", renderer, :text => 2) view.append_column(col) col = Gtk::TreeViewColumn.new("Title", renderer, :text => 3) view.append_column(col) vbox = Gtk::VBox.new vbox.add(view) window.add(vbox).show_all Gtk.main |
The above code can be run by Ruby directly. This is the output: