Skip to content

Instantly share code, notes, and snippets.

@mkristian
Created May 26, 2009 04:04
Show Gist options
  • Save mkristian/117884 to your computer and use it in GitHub Desktop.
Save mkristian/117884 to your computer and use it in GitHub Desktop.
require 'pathname'
require 'rubygems'
require 'slf4r/ruby_logger'
gem 'data_objects' , "0.9.11"
require 'dm-core'
require 'ldap_resource'
dummy = true #uncomment this to use dummy, i.e. a database instead of ldap
dummy = false # uncomment this to use ldap
unless dummy
require 'ldap_resource'
require 'adapters/ldap_adapter'
DataMapper.setup(:ldap, {
:adapter => 'ldap',
:host => 'localhost',
:port => '389',
:base => ENV['LDAP_BASE'] || "dc=example,dc=com",
:bind_name => "cn=admin," + (ENV['LDAP_BASE'] || "dc=example,dc=com"),
:password => ENV['LDAP_PWD'] || "behappy"
})
else
require 'dummy_ldap_resource'
DataMapper.setup(:ldap, 'sqlite3:./ldap.sqlite3')
end
DataMapper.setup(:default, 'sqlite3:./db.sqlite3')
class Customer
include DataMapper::Resource
property :id, Serial, :field => "gidnumber" #, :field => "cn"
property :name, String, :field => "cn" #, :field => "o"
# property :unit, String, :field => "ou"
has n, :positions
#has n, :bills
dn_prefix { |customer| "cn=#{customer.name}"}
# treebase "ou=Customers"
treebase "ou=groups"
ldap_properties do |customer|
# properties = { :objectclass => ["groupOfNames"] }
properties = { :objectclass => ["posixGroup"] }
properties
end
def self.auto_upgrade!(args = nil)
DataMapper.logger.warn("Skipping #{self.name}.auto_upgrade!")
end
def self.default_repository_name
:ldap
end
end
class Position
include DataMapper::Resource
# include DataMapper::Stamped
property :id, Serial
property :name, String, :length => 100, :nullable => false
property :category, String, :length => 40, :nullable => false
property :text, Text, :nullable => false, :nullable => false
property :note, String, :length => 255
property :amount, BigDecimal, :nullable => false
property :price, BigDecimal, :nullable => false
property :currency, String, :limit => 3..3, :nullable =>
false, :default => 'EUR'
property :tax, BigDecimal, :nullable => false, :default => 19
property :paynow, String, :limit => 10
property :outbound, Boolean, :nullable => false, :default => false
property :sorting, Integer
belongs_to :customer
# property :customer_id, Integer#, :nullable => false
def customer
return nil unless self.customer_id
DataMapper.repository(:ldap) do
Customer.get(self.customer_id)
end
end
def customer=(c)
self.customer_id = c.id if c
end
#belongs_to :bill
end
#DataMapper.auto_migrate!
c = Customer.create(:name => "name1") #, :unit => "unit1")
p = Position.create(:name => "name1", :category => "cat1", :text => "text1", :amount => 1, :price => 1)
c.positions << p
c.save
p c.positions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment