Skip to content

Commit

Permalink
Rails Scaffold sample
Browse files Browse the repository at this point in the history
  • Loading branch information
jschementi committed May 4, 2009
1 parent 3caf7a2 commit 8dce742
Show file tree
Hide file tree
Showing 71 changed files with 9,327 additions and 0 deletions.
2 changes: 2 additions & 0 deletions ardb/activerecord.log
@@ -0,0 +1,2 @@
# Logfile created on Mon May 04 02:09:40 -07:00 2009 by /
Migrating to CreatePeople (20090423112611)
47 changes: 47 additions & 0 deletions ardb/app.rb
@@ -0,0 +1,47 @@
require 'person'

puts "Loading .NET Libraries #{Time.now}"
require 'mscorlib'
require 'system'
require 'System.Drawing'
require 'System.Windows.Forms'

include System
include System::Drawing
include System::Windows::Forms
include System::ComponentModel

puts "Starting app #{Time.now}"

require 'c:/dev/repl'

class MyForm < Form # System::Windows::Forms
def initialize
@binding_src = BindingSource.new
@datagrid = DataGridView.new
self.controls.add @datagrid
@datagrid.dock = DockStyle.top;
self.size = Size.new 800, 200
self.load do |s,e|
@customer_list = BindingList.of(Person).new

# Make sure all fields are loaded by calling
# one field on each record
Person.all.each{|pe| pe.age }

Person.all.each do |pe|
@customer_list.add pe
end rescue nil # Note sure why rescue is needed

@binding_src.data_source = @customer_list
@datagrid.data_source = @binding_src
end
end
end

t = Thread.new do
Application.enable_visual_styles
Application.run MyForm.new
end

repl binding
40 changes: 40 additions & 0 deletions ardb/ardb.rb
@@ -0,0 +1,40 @@
require 'event' # defines RubyEvent

require 'mscorlib'
require 'system'

include System::ComponentModel

# For use in a ActiveRecord model, to databind it's attributes.
module ActiveRecord::AttributeChangeNotifier
include INotifyPropertyChanged

# required by INotifyPropertyChange
def add_PropertyChanged handler
@attr_change ||= RubyEvent.new
repl binding, 'AddPropertyChanged'
@attr_change.add handler
end

# required by INotifyPropertyChange
def remove_PropertyChanged handler
@attr_change.remove handler if @attr_change
end

# Executed before ActiveRecord::Base#save
def before_save
if self.changed?
@model_changes = self.changes.clone
end
true
end

# Executed after ActiveRecord::Base#save
def after_save
repl binding, 'after_save'
@model_changes.each do |field, (oldval, newval)|
@attr_change.caller.call(field, oldval, newval) if @attr_change
end
@model_changes = []
end
end
15 changes: 15 additions & 0 deletions ardb/arinit.rb
@@ -0,0 +1,15 @@
puts "Loading rubygems #{Time.now}"
require 'rubygems'

puts "Loading activerecord #{Time.now}"
require 'activerecord'

puts "Connecting to database #{Time.now}"
ActiveRecord::Base.establish_connection(
:adapter => "mssql",
:host => "JIMMYSCH-GEMINI\\SQLEXPRESS",
:database => "ardb2",
:integrated_security => true
)

ActiveRecord::Base.logger = Logger.new("activerecord.log")
3 changes: 3 additions & 0 deletions ardb/console.rb
@@ -0,0 +1,3 @@
require 'person'
require 'irb'
IRB.start
40 changes: 40 additions & 0 deletions ardb/event.rb
@@ -0,0 +1,40 @@
# CLR event-like functionality for Ruby.
#
# >>> foo_event, on_foo_event = RubyEvent.build
# >>> handler = foo.add { puts "foo event fired" }
# >>> # or
# >>> def another
# >>> puts "foo event fired again"
# >>> end
# >>> foo.add method(:another)
# >>> on_foo_event.call
# foo event fired
# foo event fired again
# => nil
class RubyEvent

# List of event handlers of type Proc
attr_reader :handlers

# Triggers the event; Proc which calls all handlers
attr_reader :caller

def initialize
@handlers = []
@caller ||= lambda do |*args|
@handlers.each { |h| h.call(*args) }
nil
end
end

def add(event = nil, &block)
event = block if event.nil? && block_given?
if event.kind_of?(RubyEvent)
@handlers.concat event.handlers
else
raise TypeError, "event handler must respond to call" unless event.respond_to?(:call)
@handlers << event
end
event
end
end
3 changes: 3 additions & 0 deletions ardb/migrate.rb
@@ -0,0 +1,3 @@
require 'arinit'
puts "Migrating database #{Time.now}"
ActiveRecord::Migrator.migrate "migrations"
16 changes: 16 additions & 0 deletions ardb/migrations/20090423112611_create_people.rb
@@ -0,0 +1,16 @@
class CreatePeople < ActiveRecord::Migration
def self.up
create_table :people do |t|
t.string :first_name
t.string :last_name

t.integer :age

t.timestamps
end
end

def self.down
drop_table :people
end
end
7 changes: 7 additions & 0 deletions ardb/person.rb
@@ -0,0 +1,7 @@
require 'arinit'
require 'ardb'

# ActiveRecord Person model, to be databinded with
class Person < ActiveRecord::Base
include ActiveRecord::AttributeChangeNotifier
end

0 comments on commit 8dce742

Please sign in to comment.