Sha256: e9386485f46b2d101db03ca4c14832df3168116e71a1395e995dc2afc08bcf8b

Contents?: true

Size: 1.59 KB

Versions: 5

Compression:

Stored size: 1.59 KB

Contents

module Sanitize::Rails

  # Adds the +sanitizes+ method to ActiveRecord children classes
  #
  module ActiveRecord
    # Generates before_save/before_create filters that implement
    # sanitization on the given fields, in the given callback
    # point.
    #
    # Usage:
    #
    #   sanitizes :some_field, :some_other_field #, :on => :save
    #
    # Valid callback points are :save and :create, callbacks are installed "before_"
    # by default. Generated callbacks are named with the "sanitize_" prefix follwed
    # by the field names separated by an underscore.
    #
    def sanitizes(*fields)
      options   = fields.extract_options!
      callback  = Engine.callback_for(options)
      sanitizer = Engine.method_for(fields)

      define_method(sanitizer) do                  # # Unrolled version
        fields.each do |field|                     #
          value = read_attribute(field)
          unless value.blank?                      # def sanitize_fieldA_fieldB
            sanitized = Engine.clean(value)        #   write_attribute(fieldA, Engine.clean(read_attribute(fieldA))) unless fieldA.blank?
            write_attribute(field, sanitized)      #   write_attribute(fieldB, Engine.clean(read_attribute(fieldB))) unless fieldB.blank?
          end                                      # end
        end                                        #
      end                                          # end

      protected sanitizer                          # protected :sanitize_fieldA_fieldB
      send callback, sanitizer                     # before_save :sanitize_fieldA_fieldB
    end
  end

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
sanitize-rails-1.2.0 lib/sanitize/rails/active_record.rb
sanitize-rails-1.1.1 lib/sanitize/rails/active_record.rb
sanitize-rails-1.1.0 lib/sanitize/rails/active_record.rb
sanitize-rails-1.0.1 lib/sanitize/rails/active_record.rb
sanitize-rails-1.0.0 lib/sanitize/rails/active_record.rb