Skip to content

Instantly share code, notes, and snippets.

@luma
Created March 9, 2010 21:19
Show Gist options
  • Select an option

  • Save luma/327132 to your computer and use it in GitHub Desktop.

Select an option

Save luma/327132 to your computer and use it in GitHub Desktop.
require 'rubygems'
require "data_objects"
require "do_sqlite3"
require "dm-core"
require "dm-aggregates"
require "dm-migrations"
require "dm-timestamps"
require "dm-types"
require "dm-validations"
require "dm-serializer"
require "dm-observer"
# If you want the logs displayed you have to do this before the call to setup
DataMapper::Logger.new($stdout, :debug)
# An in-memory Sqlite3 connection:
DataMapper.setup(:default, 'sqlite3::memory:')
#DataMapper.setup(:default, 'sqlite3://db/budget.sqlite3')
class User
include DataMapper::Resource
property :id, Serial
property :email, String, :required => true, :unique => true, :format => :email_address, :unique_index => true
property :first_name, String, :required => true
property :last_name, String, :required => true
property :created_at, DateTime
property :updated_at, DateTime
validates_is_unique :email
belongs_to :supplier, :required => false
def name
[self.first_name, self.last_name].join(' ')
end
alias_method :to_s, :name
end
class Address
include DataMapper::Resource
property :id, Serial
property :type, Enum[:physical, :mailing], :required => true
property :recipient, String, :required => true
property :organisation, String, :required => true
property :unit, String
property :floor, String
property :building_name, String
property :street_number, String
property :street_name, String, :required => true
property :suburb, String
property :city, String
property :state, String
property :post_code, String
property :country, String, :required => true, :default => "New Zealand"
property :created_at, DateTime
property :updated_at, DateTime
belongs_to :supplier, :required => false
end
class Supplier
include ::DataMapper::Resource
property :id, Serial
property :permalink, String, :length => 3..100, :unique_index => true
property :trading_name, String, :unique_index => true
property :legal_name, String, :required => true, :unique_index => true
property :created_at, DateTime, :index => true
property :updated_at, DateTime
has 1, :key_contact, :model => 'User'
has 1, :physical_address, :model => 'Address', :type => :physical
has 1, :mailing_address, :model => 'Address', :type => :mailing
def self.create_valid(options = {})
key_contact = User.get(options.delete(:key_contact))
supplier = Supplier.new(options)
supplier.key_contact = key_contact
supplier.physical_address = Address.new({
:type => :physical,
:recipient => key_contact.to_s,
:organisation => supplier.trading_name,
:street_number => "TBA",
:street_name => "TBA",
:city => "TBA",
:post_code => 'TBA'
})
supplier.mailing_address = Address.new({
:type => :mailing,
:recipient => key_contact.to_s,
:organisation => supplier.trading_name,
:street_number => "TBA",
:street_name => "TBA",
:city => "TBA",
:post_code => 'TBA'
})
transaction do
supplier.save
end # transaction
puts "\n\n"
puts "SUPPLIER ERRORS: #{supplier.errors.full_messages.collect {|m| "\"#{m}\""}.join(', ')}"
puts "SUPPLIER KEY CONTACT ERRORS: #{supplier.key_contact.errors.full_messages.collect {|m| "\"#{m}\""}.join(', ')}"
puts "SUPPLIER PHYSICAL ADDRESS ERRORS: #{supplier.physical_address.errors.full_messages.collect {|m| "\"#{m}\""}.join(', ')}"
puts "SUPPLIER MAILING ADDRESS ERRORS: #{supplier.mailing_address.errors.full_messages.collect {|m| "\"#{m}\""}.join(', ')}"
supplier
end
end
Supplier.auto_migrate!
User.auto_migrate!
Address.auto_migrate!
user = User.create({
:email => "bob@luma.co.nz",
:first_name => "Admin",
:last_name => "Administrator"
})
supplier = Supplier.create_valid({
:permalink => 'foo-bar',
:trading_name => 'Foo Bar',
:legal_name => 'Foo Bar Ltd',
:key_contact => user.id
})
puts "\n\n"
supplier.reload
puts supplier.inspect
puts supplier.key_contact.inspect
puts supplier.physical_address.inspect
puts supplier.mailing_address.inspect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment