Module: Sanitizable::Macro
- Defined in:
- lib/sanitizable/macro.rb
Instance Method Summary collapse
- #acts_as_sanitizable(*attribute_names, &block) ⇒ Object
- #acts_as_sanitizable_on(*attribute_names, &block) ⇒ Object
- #reset_sanitization ⇒ Object
-
#sanitizes(*attribute_names, &block) ⇒ Object
Defines one or multiple columns that should be sanitized.
- #skip_sanitization_of(*attribute_names) ⇒ Object
- #skip_sanitization_on(*attribute_names) ⇒ Object
Instance Method Details
#acts_as_sanitizable(*attribute_names, &block) ⇒ Object
35 36 37 |
# File 'lib/sanitizable/macro.rb', line 35 def acts_as_sanitizable(*attribute_names, &block) sanitizes(*attribute_names, &block) end |
#acts_as_sanitizable_on(*attribute_names, &block) ⇒ Object
39 40 41 |
# File 'lib/sanitizable/macro.rb', line 39 def acts_as_sanitizable_on(*attribute_names, &block) sanitizes(*attribute_names, &block) end |
#reset_sanitization ⇒ Object
58 59 60 |
# File 'lib/sanitizable/macro.rb', line 58 def reset_sanitization sanitizable_attributes.clear if self.include?(Model) end |
#sanitizes(*attribute_names, &block) ⇒ Object
Defines one or multiple columns that should be sanitized. The method used for sanitizing can be defined via the :with key and can either contain the name of any method that is implemented on the particular class or a custom Proc. Additionally, an Array of Symbols or Procs can be speficied. Alternatively a block can be supplied as sanitizer.
Example:
class User < ActiveRecord::Base
sanitizes :first_name, :last_name, with: :strip
sanitizes :email, with: [:strip, :downcase]
sanitizes :biography, with: ->(biography) { biography.squish }
sanitizes :username do |username|
# strip leading "@" characters as used in Twitter usernames
username.strip.downcase.sub(/\A@/, '')
end
end
21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/sanitizable/macro.rb', line 21 def sanitizes(*attribute_names, &block) include Model unless self.include?(Model) = attribute_names..assert_valid_keys(:with, :on) [:with] = block || [:with] raise ArgumentError, "#{self.name}#sanitizes must define a :with option or have a block" unless [:with] raise ArgumentError, "#{self.name}#sanitizes must define at least one attribute" if attribute_names.empty? self.sanitizable_attributes += attribute_names.map do |attribute_name| AttributeSanitizer.new(attribute_name.to_s, .dup) end self.sanitizable_attributes.uniq! end |
#skip_sanitization_of(*attribute_names) ⇒ Object
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/sanitizable/macro.rb', line 43 def skip_sanitization_of(*attribute_names) = attribute_names..assert_valid_keys(:on) if self.include?(Model) attribute_names = attribute_names.map(&:to_s) context = [:on] sanitizable_attributes.delete_if do |attribute| attribute_names.include?(attribute.name) and context == attribute.context end end end |
#skip_sanitization_on(*attribute_names) ⇒ Object
54 55 56 |
# File 'lib/sanitizable/macro.rb', line 54 def skip_sanitization_on(*attribute_names) skip_sanitization_of(*attribute_names) end |