Module: ActiveLdap::Associations::ClassMethods
- Defined in:
- lib/active_ldap/associations.rb
Instance Method Summary collapse
- #associated_class(name) ⇒ Object
-
#belongs_to(association_id, options = {}) ⇒ Object
belongs_to.
-
#has_many(association_id, options = {}) ⇒ Object
has_many.
- #set_associated_class(name, klass) ⇒ Object
Instance Method Details
#associated_class(name) ⇒ Object
26 27 28 |
# File 'lib/active_ldap/associations.rb', line 26 def associated_class(name) @associated_classes[name.to_s] end |
#belongs_to(association_id, options = {}) ⇒ Object
belongs_to
This defines a method for an extension class map its DN key attribute value on to multiple items which reference it by |:foreign_key| in the other LDAP entry covered by class |:class_name|.
Example:
belongs_to :groups, :class_name => "Group",
:many => "memberUid" # Group#memberUid
# :foreign_key => "uid" # User#uid
# dn attribute value is used by default
belongs_to :primary_group, :class_name => "Group",
:foreign_key => "gidNumber", # User#gidNumber
:primary_key => "gidNumber" # Group#gidNumber
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/active_ldap/associations.rb', line 45 def belongs_to(association_id, ={}) () klass = [:class] || Inflector.classify(association_id) foreign_key = [:foreign_key] primary_key = [:primary_key] many = [:many] set_associated_class(association_id, klass) opts = { :association_id => association_id, :foreign_key_name => foreign_key, :primary_key_name => primary_key, :many => many, :extend => [:extend], } if opts[:many] association_class = Association::BelongsToMany opts[:foreign_key_name] ||= dn_attribute else association_class = Association::BelongsTo opts[:foreign_key_name] ||= "#{association_id}_id" before_save(<<-EOC) if defined?(@#{association_id}) association = @#{association_id} if association and association.updated? self[association.__send__(:primary_key)] = association[#{opts[:foreign_key_name].dump}] end end EOC end association_accessor(association_id) do |target| association_class.new(target, opts) end end |
#has_many(association_id, options = {}) ⇒ Object
has_many
This defines a method for an extension class expand an existing multi-element attribute into ActiveLdap objects. This discards any calls which result in entries that don’t exist in LDAP!
Example:
has_many :primary_members, :class_name => "User",
:primary_key => "gidNumber", # User#gidNumber
:foreign_key => "gidNumber" # Group#gidNumber
has_many :members, :class_name => "User",
:wrap => "memberUid" # Group#memberUid
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/active_ldap/associations.rb', line 97 def has_many(association_id, = {}) () klass = [:class] || Inflector.classify(association_id) foreign_key = [:foreign_key] || association_id.to_s + "_id" primary_key = [:primary_key] set_associated_class(association_id, klass) opts = { :association_id => association_id, :foreign_key_name => foreign_key, :primary_key_name => primary_key, :wrap => [:wrap], :extend => [:extend], } if opts[:wrap] association_class = Association::HasManyWrap else association_class = Association::HasMany end association_accessor(association_id) do |target| association_class.new(target, opts) end end |
#set_associated_class(name, klass) ⇒ Object
21 22 23 24 |
# File 'lib/active_ldap/associations.rb', line 21 def set_associated_class(name, klass) @associated_classes ||= {} @associated_classes[name.to_s] = klass end |