Created
June 11, 2009 14:31
-
-
Save tarulalan/127922 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Active Record | |
require 'libglade2' | |
require 'rubygems' | |
require 'active_record' | |
ActiveRecord::Base.logger = Logger.new(STDERR) | |
ActiveRecord::Base.colorize_logging = false | |
ActiveRecord::Base.establish_connection( | |
:adapter => "mysql", | |
:encoding => "utf8", | |
:database => "country", | |
:username => "root", | |
:password =>"password", | |
:socket => "/var/run/mysqld/mysqld.sock", | |
:dbfile => ":memory:" | |
) | |
ActiveRecord::Schema.define do | |
create_table :countries do |table| | |
table.column :name, :string | |
end | |
create_table :states do |table| | |
table.column :country_id, :integer | |
table.column :name, :string | |
end | |
create_table :cities do |table| | |
table.column :state_id, :integer | |
table.column :name, :string | |
end | |
end | |
=begin | |
create database “ country” in your mysql by excuting this query | |
“mysql > create database country; “. | |
And set your mysql root user name “root” and “password” in the above code. | |
You can understand easily by seeing the above code, how active record creates tables and corresponding fields. | |
Create_table: 'table name' - - - > It will create table in db. | |
Column: name, :String ( Field Type) - - -> It will set column field by this type and name. | |
=end | |
# Relationships : | |
class Country < ActiveRecord::Base | |
has_many :state | |
end | |
class State < ActiveRecord::Base | |
belongs_to :country | |
has_many :cities | |
end | |
class City < ActiveRecord::Base | |
belongs_to :state | |
end | |
=begin | |
This tells about relationships among those tables... | |
“Country “ table record entries having relationship with so many “State” table record entries... | |
simply we can represent by one to many relationship, so in this case we have used “ has_many ”. This means a country “has_many” states. | |
“State” table record entries having relations under “Country” table record entries.... It can be represented by “ belongs_to “. Since every state belongs to a country hence we have used “belongs_to” in the state class to say that it belongs to a country. | |
=end | |
# Populating values into tables : | |
country=Country.new(:name=>'India') | |
country.save | |
# here we are inserting value 'India' into country table | |
country.state << State.new(:name => 'TamilNadu') | |
# it will insert value 'TamilNadu' into state table with country_id | |
country.state.find(:all,:conditions => {:name => 'TamilNadu'}).first.cities << City.new(:name =>'Chennai') | |
# it will append the new entries ' Chennai ' in “city” table with 'Country_id' and ' State_id' . | |
country=Country.new(:name=>'America') | |
country.save | |
country.state << State.new(:name => 'Ohio') | |
country.state.find(:all,:conditions => {:name => 'Ohio'}).first.cities << City.new(:name =>'Chicago') | |
# Like wise, you can insert values into three tables as your wish | |
# and insert more country, states and city names in corresponding tables . . . | |
class ComboCountryGlade | |
include GetText | |
attr :glade | |
def initialize(path_or_data, root = nil, domain = nil, localedir = nil, flag = GladeXML::FILE) | |
bindtextdomain(domain, localedir, nil, "UTF-8") | |
@glade = GladeXML.new(path_or_data, root, domain, localedir, flag) {|handler| method(handler)} | |
@glade['window1'].show_all | |
country_combo_method | |
end | |
# ComboBoxEntry Methods | |
# country method : | |
def country_combo_method | |
@glade["country_comboboxentry"].show_all | |
@glade["state_comboboxentry"].hide_all | |
@glade["State_label"].hide_all | |
@glade["city_comboboxentry"].hide_all | |
@glade["City_label"].hide_all | |
# here, we are hiding state and city comboboxentry and their lables | |
@country_combobox = @glade["country_comboboxentry"] | |
@country_list = Gtk::ListStore.new(String) | |
@country_cell = Gtk::CellRendererText.new() | |
# here, we are setting comboboxentry properties to box by calling inbuild Gtk methods | |
@country_combobox.pack_start(@country_cell, true) | |
@country_combobox.add_attribute(@country_cell, 'text',0) | |
@country_combobox.set_model(@country_list) | |
@country=Country.all | |
# here, we calling active record class. | |
# [Country.all] ---> it will return all countries entries | |
@country.each do |country_name| | |
@country_iter =@country_list.append | |
@country_iter[0] =country_name.name | |
end | |
end | |
=begin | |
here, c_name is iterative variable of @country array record. | |
From that, we need “ country name field alone”, so that we use [c_name.name] ----> it will return countries name. | |
[@c_list_cat.append] -----> it will helps to append strings in comboboxentry. | |
=end | |
# ComboBoxEntry change signal code : for country | |
def on_country_comboboxentry_changed(widget) | |
@country_click_iter= @glade['country_comboboxentry'].active_iter | |
@country_list.each do |model,path,@iter_country| | |
if path.to_s == @country_click_iter.to_s | |
@glade['country_comboboxentry-entry1'].text =@iter_country[0] | |
country_name=@glade['country_comboboxentry-entry1'].text | |
@state_combo = @glade["state_comboboxentry"] | |
@state_combo.clear | |
state_combo_method(country_name) | |
end | |
end | |
end | |
=begin | |
active_iter : return , pointer of comboboxentry list by corresponding to mouse or keyboard press on list. | |
path.to_s : to identify TreeView path of combobox entry. | |
state_combo(country_name) : Iside country change signal method, we are going to call “ State Comboboxentry and passing argument as country name, which is selected by user at run time. | |
same like this logic we have to write code for state and city comboboxentries also . | |
=end | |
# State mathod : | |
def state_combo_method(country_name) | |
@glade["state_comboboxentry"].show_all | |
@glade["State_label"].show_all | |
@glade["city_comboboxentry"].hide_all | |
@glade["City_label"].hide_all | |
@state_combobox = @glade["state_comboboxentry"] | |
@state_list = Gtk::ListStore.new(String) | |
@state_cell = Gtk::CellRendererText.new() | |
@glade['state_comboboxentry-entry2'].text="" | |
@state_combobox.pack_start(@state_cell, true) | |
@state_combobox.add_attribute(@state_cell, 'text',0) | |
@state_combobox.set_model(@state_list) | |
country1=Country.find(:all,:conditions=>{:name=>country_name}).first.state | |
country1.each do |state_name| | |
@state_iter =@state_list.append | |
@state_iter[0] =state_name.name | |
end | |
end | |
# ComboBoxEntry change signal code : for state | |
def on_state_comboboxentry_changed(widget) | |
state_click_iter= @glade['state_comboboxentry'].active_iter | |
@state_list.each do |model,path,@iter_state| | |
if path.to_s == state_click_iter.to_s | |
@glade['state_comboboxentry-entry2'].text =@iter_state[0] | |
state_name= @glade['state_comboboxentry-entry2'].text | |
@city_combo=@glade["city_comboboxentry"] | |
@city_combo.clear | |
city_combo_method( state_name) | |
end | |
end | |
end | |
end | |
# city method : | |
def city_combo_method(state_name) | |
@glade["city_comboboxentry"].show_all | |
@glade["City_label"].show_all | |
@city_combo = @glade["city_comboboxentry"] | |
@city_list = Gtk::ListStore.new(String) | |
@city_cell = Gtk::CellRendererText.new() | |
@glade['city_comboboxentry-entry3'].text="" | |
@city_combo.pack_start(@city_cell, true) | |
@city_combo.add_attribute(@city_cell, 'text',0) | |
@city_combo.set_model(@city_list) | |
city=State.all(:conditions=>{:name=>state_name}).first.cities | |
city.each do |city_name| | |
@city_iter =@city_list.append | |
@city_iter[0] =city_name.name | |
end | |
end | |
# ComboBoxEntry change signal code : for city | |
def on_city_comboboxentry_changed(widget) | |
@city_click_iter= @glade['city_comboboxentry'].active_iter | |
@city_list.each do |model,path,@iter_city| | |
if path.to_s == @city_click_iter.to_s | |
@glade['city_comboboxentry-entry3'].text =@iter_city[0] | |
end | |
end | |
end | |
# Main program | |
if __FILE__ == $0 | |
# Set values as your own application. | |
PROG_PATH = "combo_country.glade" | |
PROG_NAME = "YOUR_APPLICATION_NAME" | |
ComboCountryGlade.new(PROG_PATH, nil, PROG_NAME) | |
Gtk.main | |
end | |
# End of program | |
=begin | |
Save the whole program as combo.rb | |
And then run this program in terminal. | |
$ruby combo.rb | |
while running this combo.rb , combo.glade file must contain in the same folder. | |
=end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment