module SetAsPrimary

Constants

VERSION

Public Instance Methods

_handle_setup_errors(primary_flag_attribute, configuration) click to toggle source
# File lib/set_as_primary.rb, line 36
def _handle_setup_errors(primary_flag_attribute, configuration)
  if !primary_flag_attribute.is_a?(Symbol)
    raise SetAsPrimary::Error, "wrong argument type (expected Symbol)"
  end

  owner_key = configuration[:owner_key]

  if owner_key.present? && reflect_on_association(owner_key).nil?
    raise ActiveRecord::AssociationNotFoundError.new(self, owner_key)
  end
end
set_as_primary(primary_flag_attribute = :primary, options = {}) click to toggle source
# File lib/set_as_primary.rb, line 19
def set_as_primary(primary_flag_attribute = :primary, options = {})
  if primary_flag_attribute.is_a?(Hash)
    options = primary_flag_attribute; primary_flag_attribute = :primary
  end

  configuration = { owner_key: nil, force_primary: true }

  configuration.update(options) if options.is_a?(Hash)

  _handle_setup_errors(primary_flag_attribute, configuration)

  self._primary_flag_attribute = primary_flag_attribute
  self._owner_key = configuration[:owner_key]
  self._force_primary = configuration[:force_primary]
end

Private Instance Methods

_klass() click to toggle source
# File lib/set_as_primary.rb, line 94
def _klass
  self.class
end
_polymorphic_condition_options() click to toggle source
# File lib/set_as_primary.rb, line 85
def _polymorphic_condition_options
  owner = self.public_send(self.class._owner_key)

  {
    "#{_klass._owner_key}_id".to_sym =>  owner.id,
    "#{_klass._owner_key}_type".to_sym => owner.class.name
  }
end
_scope_options() click to toggle source
# File lib/set_as_primary.rb, line 74
def _scope_options
  return nil if _klass._owner_key.nil?

  @_scope_options ||= if _klass.reflect_on_association(_klass._owner_key).options[:polymorphic]
    _polymorphic_condition_options
  else
    owner_id = "#{_klass._owner_key}_id".to_sym
    { owner_id => public_send(owner_id) }
  end
end
force_primary() click to toggle source
# File lib/set_as_primary.rb, line 61
def force_primary
  scope = _klass
  scope = scope.where(_scope_options) if _scope_options.present?
  count = scope.count

  if count == 1 && destroyed?
    object = scope.first
    object.update_columns(_klass._primary_flag_attribute => true)
  elsif (count == 1 && !new_record?) || (count == 0 && new_record?)
    public_send("#{_klass._primary_flag_attribute}=", true)
  end
end
unset_old_primary() click to toggle source
# File lib/set_as_primary.rb, line 51
def unset_old_primary
  return unless public_send(_klass._primary_flag_attribute)

  scope = _klass
  scope = scope.where(_scope_options) if _scope_options.present?
  scope = scope.where("id != ?", id) unless new_record?

  scope.update_all(_klass._primary_flag_attribute => false)
end