Module: Normatron::Extensions::ActiveRecord::ClassMethods

Defined in:
lib/normatron/extensions/active_record.rb

Instance Method Summary collapse

Instance Method Details

#normalize(*args) ⇒ Object


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/normatron/extensions/active_record.rb', line 24

def normalize(*args)
  # Check the existence of all attributes
  options = args.extract_options!
  filters, columns = args.map(&:to_s), column_names
  raise UnknownAttributeError if (columns & filters).size != filters.size

  # Need to use default filters?
  if options[:with].nil? || options[:with].blank?
    new_filters = Normatron.config.default_filters
  else
    new_filters = Normatron.build_hash(options[:with])
  end

  @normalize_rules ||= {}

  # Append new filters to rules
  @normalize_rules =
  args.reduce(@normalize_rules) do |hash, att|
    filters = (@normalize_rules[att] || {}).merge(new_filters)
    hash.merge({att => filters})
  end
end