Module: ActiveLdap::Validations

Defined in:
lib/active_ldap/validations.rb

Class Method Summary collapse

Class Method Details

.append_features(base) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/active_ldap/validations.rb', line 5

def self.append_features(base)
  super

  base.class_eval do
    alias_method :new_record?, :new_entry?
    class << self
      alias_method :human_attribute_name_active_ldap,
                   :human_attribute_name
    end
    include ActiveRecord::Validations
    class << self
      alias_method :human_attribute_name,
                   :human_attribute_name_active_ldap
      unless method_defined?(:human_attribute_name_with_gettext)
        def human_attribute_name_with_gettext(attribute_key_name)
          s_("#{self}|#{attribute_key_name.humanize}")
        end
      end
    end

    # Workaround for GetText's ugly implementation
    begin
      instance_method(:save_without_validation)
    rescue NameError
      alias_method_chain :save, :validation
      alias_method_chain :save!, :validation
      alias_method_chain :update_attribute, :validation_skipping
    end

    validate :validate_required_values

    class << self
      def evaluate_condition_with_active_ldap_support(condition, entry)
        evaluate_condition_without_active_ldap_support(condition, entry)
      rescue ActiveRecord::ActiveRecordError
        raise Error, $!.message
      end
      alias_method_chain :evaluate_condition, :active_ldap_support
    end

    def save_with_active_ldap_support!
      save_without_active_ldap_support!
    rescue ActiveRecord::RecordInvalid
      raise EntryInvalid, $!.message
    end
    alias_method_chain :save!, :active_ldap_support

    def valid?
      ensure_apply_object_class
      super
    end

    # validate_required_values
    #
    # Basic validation:
    # - Verify that every 'MUST' specified in the schema has a value defined
    def validate_required_values
      # Make sure all MUST attributes have a value
      @object_classes.each do |object_class|
        object_class.must.each do |required_attribute|
          # Normalize to ensure we catch schema problems
          # needed?
          real_name = to_real_attribute_name(required_attribute.name, true)
          raise UnknownAttribute.new(required_attribute) if real_name.nil?
          # # Set default if it wasn't yet set.
          # @data[real_name] ||= [] # need?
          value = @data[real_name] || []
          # Check for missing requirements.
          if value.empty?
            _schema = schema
            aliases = required_attribute.aliases.collect do |name|
              self.class.human_attribute_name(name)
            end
            args = [self.class.human_object_class_name(object_class)]
            if ActiveLdap.const_defined?(:GetTextFallback)
              if aliases.empty?
                format = "is required attribute by objectClass '%s'"
              else
                format = "is required attribute by objectClass '%s'" \
                         ": aliases: %s"
                args << aliases.join(', ')
              end
            else
              if aliases.empty?
                format = _("%{fn} is required attribute by objectClass '%s'")
              else
                format = _("%{fn} is required attribute by objectClass " \
                           "'%s': aliases: %s")
                args << aliases.join(', ')
              end
            end
            errors.add(real_name, format % args)
          end
        end
      end
    end

    private
    def run_validations_with_active_ldap_support(validation_method)
      run_validations_without_active_ldap_support(validation_method)
    rescue ActiveRecord::ActiveRecordError
      raise Error, $!.message
    end
    alias_method_chain :run_validations, :active_ldap_support
  end
end