module DefaultValueFor::InstanceMethods

Public Class Methods

new(attributes = nil, options = {}) click to toggle source
Calls superclass method
# File lib/default_value_for.rb, line 126
def initialize(attributes = nil, options = {})
  attributes = attributes.to_h if attributes.respond_to?(:to_h)
  @initialization_attributes = attributes.is_a?(Hash) ? attributes.stringify_keys : {}

  unless options[:without_protection]
    if respond_to?(:mass_assignment_options, true) && options.has_key?(:as)
      @initialization_attributes = sanitize_for_mass_assignment(@initialization_attributes, options[:as])
    elsif respond_to?(:sanitize_for_mass_assignment, true)
      @initialization_attributes = sanitize_for_mass_assignment(@initialization_attributes)
    else
      @initialization_attributes = remove_attributes_protected_from_mass_assignment(@initialization_attributes)
    end
  end

  if self.class.respond_to? :protected_attributes
    super(attributes, options)
  else
    super(attributes)
  end
end

Public Instance Methods

attributes_for_create(attribute_names) click to toggle source
Calls superclass method
# File lib/default_value_for.rb, line 147
def attributes_for_create(attribute_names)
  attribute_names += self.class._all_default_attribute_values.keys.map(&:to_s).find_all { |name|
    self.class.columns_hash.key?(name)
  }
  super
end
set_default_values() click to toggle source
# File lib/default_value_for.rb, line 154
def set_default_values
  self.class._all_default_attribute_values.each do |attribute, container|
    next unless new_record? || self.class._all_default_attribute_values_not_allowing_nil.include?(attribute)

    connection_default_value_defined = new_record? && respond_to?("#{attribute}_changed?") && !__send__("#{attribute}_changed?")

    attribute_blank = if attributes.has_key?(attribute)
                        column = self.class.columns_hash[attribute]
                        if column && column.type == :boolean
                          attributes[attribute].nil?
                        else
                          attributes[attribute].blank?
                        end
                      elsif respond_to?(attribute)
                        send(attribute).nil?
                      else
                        instance_variable_get("@#{attribute}").nil?
                      end
    next unless connection_default_value_defined || attribute_blank

    # allow explicitly setting nil through allow nil option
    next if @initialization_attributes.is_a?(Hash) &&
            (
              @initialization_attributes.has_key?(attribute) ||
              (
                @initialization_attributes.has_key?("#{attribute}_attributes") &&
                nested_attributes_options.stringify_keys[attribute]
              )
            ) &&
            !self.class._all_default_attribute_values_not_allowing_nil.include?(attribute)

    __send__("#{attribute}=", container.evaluate(self))
    if respond_to?(:clear_attribute_changes, true)
      clear_attribute_changes [attribute] if has_attribute?(attribute)
    else
      changed_attributes.delete(attribute)
    end
  end
end