Module ActiveLdap::Associations::ClassMethods
In: lib/active_ldap/associations.rb
Error AttributeAssignmentError AdapterNotSpecified OperationNotPermitted RequiredObjectClassMissed ConnectionError RequiredAttributeMissed LdifInvalid DistinguishedNameNotSetError EntryNotFound LdapError SaveError StrongAuthenticationRequired NotImplemented AdapterNotFound TimeoutError AuthenticationError AttributeValueInvalid EntryNotSaved DistinguishedNameInputInvalid EntryAlreadyExist ObjectClassError UnknownAttribute EntryInvalid DeleteError ConfigurationError ConnectionNotSetup DistinguishedNameInvalid Schema\n[lib/active_ldap/schema.rb\nlib/active_ldap/schema/syntaxes.rb] Base DistinguishedName Reloadable::Deprecated Reloadable::Subclasses Enumerable Ldif Collection EntryAttribute StandardError Children HasManyWrap HasMany BelongsToMany Proxy BelongsTo Normalizable Common Find LDIF Delete Update ActiveRecord::Callbacks GetText Parser Base\n[lib/active_ldap/adapter/base.rb\nlib/active_ldap/adapter/jndi.rb\nlib/active_ldap/adapter/ldap.rb\nlib/active_ldap/adapter/net_ldap.rb] Jndi Ldap NetLdap GetTextSupport ActiveRecord::Validations Xml JndiConnection lib/active_ldap/distinguished_name.rb lib/active_ldap/base.rb lib/active_ldap/xml.rb lib/active_ldap/schema.rb lib/active_ldap/entry_attribute.rb lib/active_ldap/ldif.rb lib/active_ldap/ldap_error.rb Compatible ClassMethods Associations LdapBenchmarking ActionController Populate lib/active_ldap/association/has_many_wrap.rb lib/active_ldap/association/children.rb lib/active_ldap/association/collection.rb lib/active_ldap/association/proxy.rb lib/active_ldap/association/belongs_to_many.rb lib/active_ldap/association/belongs_to.rb lib/active_ldap/association/has_many.rb HasManyUtils Association ClassMethods Tree Acts Command ClassMethods Normalizable Attributes Update Common ModifyNameRecordLoadable AddOperationModifiable DeleteOperationModifiable ReplaceOperationModifiable ModifyRecordLoadable DeleteRecordLoadable AddRecordLoadable ContentRecordLoadable LDIF Delete Find Operations GetTextSupport Escape ClassMethods Configuration ClassMethods ObjectClass ClassMethods Callbacks lib/active_ldap/get_text/parser.rb GetText lib/active_ldap/adapter/jndi_connection.rb lib/active_ldap/adapter/net_ldap.rb lib/active_ldap/adapter/ldap.rb lib/active_ldap/adapter/base.rb lib/active_ldap/adapter/jndi.rb Adapter Validations GetTextFallback Helper ClassMethods HumanReadable Salt UserPassword ClassMethods Connection ActiveLdap dot/m_46_0.png

Methods

Constants

VALID_BELONGS_TO_OPTIONS = [:class, :class_name, :foreign_key, :primary_key, :many, :extend]
VALID_HAS_MANY_OPTIONS = [:class, :class_name, :foreign_key, :primary_key, :wrap, :extend]

Public Instance methods

[Source]

    # File lib/active_ldap/associations.rb, line 26
26:       def associated_class(name)
27:         @associated_classes[name.to_s]
28:       end

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
            # :primary_key => "uid" # User#uid
            ## deprecated since 1.1.0. Use :primary_key instead.
            ## :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

[Source]

    # File lib/active_ldap/associations.rb, line 48
48:       def belongs_to(association_id, options={})
49:         validate_belongs_to_options(options)
50:         klass = options[:class]
51:         klass ||= (options[:class_name] || association_id.to_s).classify
52:         foreign_key = options[:foreign_key]
53:         primary_key = options[:primary_key]
54:         many = options[:many]
55:         set_associated_class(association_id, klass)
56: 
57:         opts = {
58:           :association_id => association_id,
59:           :foreign_key_name => foreign_key,
60:           :primary_key_name => primary_key,
61:           :many => many,
62:           :extend => options[:extend],
63:         }
64:         if opts[:many]
65:           association_class = Association::BelongsToMany
66:           foreign_key_name = opts[:foreign_key_name]
67:           if foreign_key_name
68:             message = _(":foreign_key belongs_to(:many) option is " \
69:                         "deprecated since 1.1.0. Use :primary_key instead.")
70:             ActiveSupport::Deprecation.warn(message)
71:             opts[:primary_key_name] ||= foreign_key_name
72:           end
73:           opts[:primary_key_name] ||= dn_attribute
74:         else
75:           association_class = Association::BelongsTo
76:           opts[:foreign_key_name] ||= "#{association_id}_id"
77: 
78:           before_save("if defined?(@\#{association_id})\nassociation = @\#{association_id}\nif association and association.updated?\nself[association.__send__(:primary_key)] =\nassociation[\#{opts[:foreign_key_name].dump}]\nend\nend\n")
79:         end
80: 
81:         association_accessor(association_id) do |target|
82:           association_class.new(target, opts)
83:         end
84:       end

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", # Group#gidNumber
           :foreign_key => "gidNumber"  # User#gidNumber
           ## deprecated since 1.1.0. Those options
           ## are inverted.
           # :primary_key => "gidNumber", # User#gidNumber
           # :foreign_key => "gidNumber"  # Group#gidNumber
  has_many :members, :class_name => "User",
           :wrap => "memberUid" # Group#memberUid

[Source]

     # File lib/active_ldap/associations.rb, line 113
113:       def has_many(association_id, options = {})
114:         validate_has_many_options(options)
115:         klass = options[:class]
116:         klass ||= (options[:class_name] || association_id.to_s).classify
117:         foreign_key = options[:foreign_key]
118:         primary_key = options[:primary_key]
119:         set_associated_class(association_id, klass)
120: 
121:         opts = {
122:           :association_id => association_id,
123:           :foreign_key_name => foreign_key,
124:           :primary_key_name => primary_key,
125:           :wrap => options[:wrap],
126:           :extend => options[:extend],
127:         }
128:         if opts[:wrap]
129:           association_class = Association::HasManyWrap
130:         else
131:           association_class = Association::HasMany
132:           primary_key_name = opts[:primary_key_name]
133:           foreign_key_name = opts[:foreign_key_name]
134:           if primary_key_name != foreign_key_name and
135:               primary_key_name != "dn" and
136:               !new.have_attribute?(primary_key_name)
137:             message = _(":primary_key and :foreign_key has_many options are " \
138:                         "inverted their mean since 1.1.0. Please invert them.")
139:             ActiveSupport::Deprecation.warn(message)
140:             opts[:foreign_key_name] = primary_key_name
141:             opts[:primary_key_name] = foreign_key_name
142:           end
143:         end
144: 
145:         association_accessor(association_id) do |target|
146:           association_class.new(target, opts)
147:         end
148:       end

[Source]

    # File lib/active_ldap/associations.rb, line 21
21:       def set_associated_class(name, klass)
22:         @associated_classes ||= {}
23:         @associated_classes[name.to_s] = klass
24:       end

Private Instance methods

[Source]

     # File lib/active_ldap/associations.rb, line 151
151:       def association_accessor(name, &make_association)
152:         define_method("__make_#{name}") do
153:           make_association.call(self)
154:         end
155:         associations << name
156:         association_reader(name, &make_association)
157:         association_writer(name, &make_association)
158:       end

[Source]

     # File lib/active_ldap/associations.rb, line 160
160:       def association_reader(name, &make_association)
161:         class_eval("def \#{name}\n@\#{name} ||= __make_\#{name}\nend\n", __FILE__, __LINE__ + 1)
162:       end

[Source]

     # File lib/active_ldap/associations.rb, line 169
169:       def association_writer(name, &make_association)
170:         class_eval("def \#{name}=(new_value)\nassociation = defined?(@\#{name}) ? @\#{name} : nil\nassociation ||= __make_\#{name}\nassociation.replace(new_value)\n@\#{name} = new_value.nil? ? nil : association\n@\#{name}\nend\n", __FILE__, __LINE__ + 1)
171:       end

[Source]

     # File lib/active_ldap/associations.rb, line 185
185:       def validate_belongs_to_options(options)
186:         options.assert_valid_keys(VALID_BELONGS_TO_OPTIONS)
187:       end

[Source]

     # File lib/active_ldap/associations.rb, line 192
192:       def validate_has_many_options(options)
193:         options.assert_valid_keys(VALID_HAS_MANY_OPTIONS)
194:       end

[Validate]