Sha256: 3e48d24b1ff2d8b05e5e1dac7b9cba9c841c2e6197f3e9ed41d046f17374ee81

Contents?: true

Size: 1.16 KB

Versions: 4

Compression:

Stored size: 1.16 KB

Contents

require "dryopteris"

module Dryopteris
  module RailsExtension
    def self.included(base)
      base.extend(ClassMethods)
      
      # sets up default of stripping tags for all fields
      base.class_eval do
        before_save :sanitize_fields
        class_inheritable_reader :dryopteris_options
      end
    end

    module ClassMethods
      def sanitize_fields(options = {})
        write_inheritable_attribute(:dryopteris_options, {
          :except     => (options[:except] || []),
          :allow_tags => (options[:allow_tags] || [])
        })
      end
      
      alias_method :sanitize_field, :sanitize_fields
    end

      
    def sanitize_fields
      self.class.columns.each do |column|
        next unless (column.type == :string || column.type == :text)

        field = column.name.to_sym
        value = self[field]

        if dryopteris_options && dryopteris_options[:except].include?(field)
          next
        elsif dryopteris_options && dryopteris_options[:allow_tags].include?(field)
          self[field] = Dryopteris.sanitize(value)
        else
          self[field] = Dryopteris.strip_tags(value)
        end
      end
      
    end
    
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
jmcnevin-dryopteris-0.1.2 lib/dryopteris/rails_extension.rb
mdalessio-dryopteris-0.1.0 lib/dryopteris/rails_extension.rb
mdalessio-dryopteris-0.1.1 lib/dryopteris/rails_extension.rb
mdalessio-dryopteris-0.1.2 lib/dryopteris/rails_extension.rb