require 'active_model/mass_assignment_security'

module ActiveModel
  module MassAssignmentSecurity
    
  
    def sanitize_for_mass_assignment_with_warning(attributes)
      warn_when_mass_assignment_is_not_whitelist unless mass_assignment_authorizer.is_a? WhiteList
      sanitize_for_mass_assignment_without_warning(attributes)
    end
    alias_method_chain :sanitize_for_mass_assignment, :warning
    

    def warn_when_mass_assignment_is_not_whitelist
      return if WhinyMassAssignment::Config.whitelist[:mode] == :none
      only = WhinyMassAssignment::Config.whitelist[:only]
      if only 
        return unless [only].flatten().include?( self.class )
      else
        except = WhinyMassAssignment::Config.whitelist[:except]
        return if except && [except].flatten().include?( self.class )
      end        
      
      self.logger.debug WhinyMassAssignment::bc :yellow, "Mass assignment whitelisting has not been specified for #{ WhinyMassAssignment::c [:bright, :red], self.class.name }" if self.logger
      raise "Mass assignment whitelisting has not been specified for `#{self.class.name}` it is `#{mass_assignment_authorizer}`." if WhinyMassAssignment::Config.whitelist[:mode] == :raise
    end  

  end
end
