Guest User

Untitled

a guest
Mar 8th, 2018
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.45 KB | None | 0 0
  1. # Active Record
  2.  
  3.  
  4. require 'libglade2'
  5. require 'rubygems'
  6. require 'active_record'
  7. ActiveRecord::Base.logger = Logger.new(STDERR)
  8. ActiveRecord::Base.colorize_logging = false
  9.  
  10. ActiveRecord::Base.establish_connection(
  11. :adapter => "mysql",
  12. :encoding => "utf8",
  13. :database => "country",
  14. :username => "root",
  15. :password =>"password",
  16. :socket => "/var/run/mysqld/mysqld.sock",
  17. :dbfile => ":memory:"
  18. )
  19.  
  20. ActiveRecord::Schema.define do
  21. create_table :countries do |table|
  22. table.column :name, :string
  23. end
  24.  
  25. create_table :states do |table|
  26. table.column :country_id, :integer
  27. table.column :name, :string
  28. end
  29.  
  30. create_table :cities do |table|
  31. table.column :state_id, :integer
  32. table.column :name, :string
  33.  
  34. end
  35.  
  36. end
  37.  
  38.  
  39.  
  40. =begin
  41.  
  42. create database “ country” in your mysql by excuting this query
  43. “mysql > create database country; “.
  44.  
  45. And set your mysql root user name “root” and “password” in the above code.
  46.  
  47.  
  48. You can understand easily by seeing the above code, how active record creates tables and corresponding fields.
  49.  
  50. Create_table: 'table name' - - - > It will create table in db.
  51. Column: name, :String ( Field Type) - - -> It will set column field by this type and name.
  52.  
  53. =end
  54.  
  55. # Relationships :
  56.  
  57.  
  58. class Country < ActiveRecord::Base
  59. has_many :state
  60. end
  61.  
  62. class State < ActiveRecord::Base
  63. belongs_to :country
  64. has_many :cities
  65. end
  66.  
  67. class City < ActiveRecord::Base
  68. belongs_to :state
  69. end
  70.  
  71.  
  72. =begin
  73.  
  74. This tells about relationships among those tables...
  75.  
  76. “Country “ table record entries having relationship with so many “State” table record entries...
  77. 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.
  78.  
  79. “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.
  80.  
  81. =end
  82.  
  83. # Populating values into tables :
  84.  
  85. country=Country.new(:name=>'India')
  86. country.save
  87.  
  88. # here we are inserting value 'India' into country table
  89.  
  90.  
  91. country.state << State.new(:name => 'TamilNadu')
  92.  
  93. # it will insert value 'TamilNadu' into state table with country_id
  94.  
  95.  
  96. country.state.find(:all,:conditions => {:name => 'TamilNadu'}).first.cities << City.new(:name =>'Chennai')
  97.  
  98. # it will append the new entries ' Chennai ' in “city” table with 'Country_id' and ' State_id' .
  99.  
  100.  
  101.  
  102. country=Country.new(:name=>'America')
  103.  
  104. country.save
  105.  
  106. country.state << State.new(:name => 'Ohio')
  107. country.state.find(:all,:conditions => {:name => 'Ohio'}).first.cities << City.new(:name =>'Chicago')
  108.  
  109. # Like wise, you can insert values into three tables as your wish
  110. # and insert more country, states and city names in corresponding tables . . .
  111.  
  112.  
  113.  
  114.  
  115. class ComboCountryGlade
  116.  
  117. include GetText
  118.  
  119.  
  120.  
  121. attr :glade
  122.  
  123.  
  124.  
  125. def initialize(path_or_data, root = nil, domain = nil, localedir = nil, flag = GladeXML::FILE)
  126.  
  127. bindtextdomain(domain, localedir, nil, "UTF-8")
  128.  
  129. @glade = GladeXML.new(path_or_data, root, domain, localedir, flag) {|handler| method(handler)}
  130.  
  131. @glade['window1'].show_all
  132.  
  133.  
  134.  
  135. country_combo_method
  136.  
  137.  
  138.  
  139. end
  140.  
  141. # ComboBoxEntry Methods
  142.  
  143.  
  144. # country method :
  145.  
  146. def country_combo_method
  147. @glade["country_comboboxentry"].show_all
  148. @glade["state_comboboxentry"].hide_all
  149. @glade["State_label"].hide_all
  150. @glade["city_comboboxentry"].hide_all
  151. @glade["City_label"].hide_all
  152.  
  153. # here, we are hiding state and city comboboxentry and their lables
  154.  
  155. @country_combobox = @glade["country_comboboxentry"]
  156. @country_list = Gtk::ListStore.new(String)
  157. @country_cell = Gtk::CellRendererText.new()
  158.  
  159. # here, we are setting comboboxentry properties to box by calling inbuild Gtk methods
  160.  
  161. @country_combobox.pack_start(@country_cell, true)
  162. @country_combobox.add_attribute(@country_cell, 'text',0)
  163. @country_combobox.set_model(@country_list)
  164.  
  165. @country=Country.all
  166.  
  167. # here, we calling active record class.
  168. # [Country.all] ---> it will return all countries entries
  169.  
  170.  
  171. @country.each do |country_name|
  172. @country_iter =@country_list.append
  173. @country_iter[0] =country_name.name
  174. end
  175. end
  176.  
  177.  
  178. =begin
  179. here, c_name is iterative variable of @country array record.
  180. From that, we need “ country name field alone”, so that we use [c_name.name] ----> it will return countries name.
  181.  
  182. [@c_list_cat.append] -----> it will helps to append strings in comboboxentry.
  183. =end
  184.  
  185.  
  186.  
  187.  
  188.  
  189. # ComboBoxEntry change signal code : for country
  190.  
  191.  
  192. def on_country_comboboxentry_changed(widget)
  193. @country_click_iter= @glade['country_comboboxentry'].active_iter
  194. @country_list.each do |model,path,@iter_country|
  195. if path.to_s == @country_click_iter.to_s
  196. @glade['country_comboboxentry-entry1'].text =@iter_country[0]
  197. country_name=@glade['country_comboboxentry-entry1'].text
  198. @state_combo = @glade["state_comboboxentry"]
  199. @state_combo.clear
  200. state_combo_method(country_name)
  201. end
  202. end
  203.  
  204. end
  205.  
  206.  
  207. =begin
  208.  
  209. active_iter : return , pointer of comboboxentry list by corresponding to mouse or keyboard press on list.
  210.  
  211. path.to_s : to identify TreeView path of combobox entry.
  212.  
  213.  
  214. 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.
  215.  
  216. same like this logic we have to write code for state and city comboboxentries also .
  217.  
  218. =end
  219.  
  220.  
  221.  
  222. # State mathod :
  223.  
  224.  
  225. def state_combo_method(country_name)
  226. @glade["state_comboboxentry"].show_all
  227. @glade["State_label"].show_all
  228. @glade["city_comboboxentry"].hide_all
  229. @glade["City_label"].hide_all
  230.  
  231. @state_combobox = @glade["state_comboboxentry"]
  232. @state_list = Gtk::ListStore.new(String)
  233. @state_cell = Gtk::CellRendererText.new()
  234. @glade['state_comboboxentry-entry2'].text=""
  235. @state_combobox.pack_start(@state_cell, true)
  236. @state_combobox.add_attribute(@state_cell, 'text',0)
  237. @state_combobox.set_model(@state_list)
  238.  
  239.  
  240. country1=Country.find(:all,:conditions=>{:name=>country_name}).first.state
  241.  
  242.  
  243. country1.each do |state_name|
  244. @state_iter =@state_list.append
  245. @state_iter[0] =state_name.name
  246. end
  247.  
  248. end
  249.  
  250.  
  251.  
  252.  
  253.  
  254. # ComboBoxEntry change signal code : for state
  255.  
  256.  
  257. def on_state_comboboxentry_changed(widget)
  258.  
  259. state_click_iter= @glade['state_comboboxentry'].active_iter
  260. @state_list.each do |model,path,@iter_state|
  261. if path.to_s == state_click_iter.to_s
  262. @glade['state_comboboxentry-entry2'].text =@iter_state[0]
  263. state_name= @glade['state_comboboxentry-entry2'].text
  264. @city_combo=@glade["city_comboboxentry"]
  265. @city_combo.clear
  266. city_combo_method( state_name)
  267. end
  268. end
  269. end
  270.  
  271. end
  272.  
  273.  
  274.  
  275. # city method :
  276.  
  277.  
  278. def city_combo_method(state_name)
  279. @glade["city_comboboxentry"].show_all
  280. @glade["City_label"].show_all
  281.  
  282. @city_combo = @glade["city_comboboxentry"]
  283. @city_list = Gtk::ListStore.new(String)
  284. @city_cell = Gtk::CellRendererText.new()
  285. @glade['city_comboboxentry-entry3'].text=""
  286. @city_combo.pack_start(@city_cell, true)
  287.  
  288. @city_combo.add_attribute(@city_cell, 'text',0)
  289. @city_combo.set_model(@city_list)
  290.  
  291. city=State.all(:conditions=>{:name=>state_name}).first.cities
  292.  
  293. city.each do |city_name|
  294. @city_iter =@city_list.append
  295. @city_iter[0] =city_name.name
  296. end
  297. end
  298.  
  299.  
  300. # ComboBoxEntry change signal code : for city
  301.  
  302.  
  303. def on_city_comboboxentry_changed(widget)
  304. @city_click_iter= @glade['city_comboboxentry'].active_iter
  305. @city_list.each do |model,path,@iter_city|
  306. if path.to_s == @city_click_iter.to_s
  307. @glade['city_comboboxentry-entry3'].text =@iter_city[0]
  308. end
  309. end
  310. end
  311.  
  312.  
  313.  
  314. # Main program
  315.  
  316. if __FILE__ == $0
  317.  
  318. # Set values as your own application.
  319.  
  320. PROG_PATH = "combo_country.glade"
  321.  
  322. PROG_NAME = "YOUR_APPLICATION_NAME"
  323.  
  324. ComboCountryGlade.new(PROG_PATH, nil, PROG_NAME)
  325.  
  326. Gtk.main
  327.  
  328. end
  329.  
  330. # End of program
  331.  
  332.  
  333.  
  334. =begin
  335.  
  336. Save the whole program as combo.rb
  337.  
  338.  
  339. And then run this program in terminal.
  340.  
  341. $ruby combo.rb
  342.  
  343. while running this combo.rb , combo.glade file must contain in the same folder.
  344.  
  345. =end
Add Comment
Please, Sign In to add comment