Skip to content

Commit

Permalink
Simplify the attribute assignment logic
Browse files Browse the repository at this point in the history
This also fixes model-only configuration for the stamp columns
  • Loading branch information
lowjoel committed Feb 9, 2017
1 parent 85faa46 commit e69a260
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
27 changes: 15 additions & 12 deletions lib/active_record/userstamp/stampable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,29 +96,32 @@ def has_stamper?
end

def set_creator_attribute
attribute = ActiveRecord::Userstamp.config.creator_attribute
return if !has_stamper? || attribute.nil? || !has_attribute?(attribute)
return unless has_stamper?

current_attribute_value = send(attribute)
return if current_attribute_value.present?
creator_association = self.class.reflect_on_association(:creator)
return unless creator_association
return if creator.present?

ActiveRecord::Userstamp::Utilities.assign_stamper(self, :creator, attribute)
ActiveRecord::Userstamp::Utilities.assign_stamper(self, creator_association)
end

def set_updater_attribute
attribute = ActiveRecord::Userstamp.config.updater_attribute
return if !has_stamper? || attribute.nil? || !has_attribute?(attribute)
return unless has_stamper?

return if !self.new_record? && !self.changed?
updater_association = self.class.reflect_on_association(:updater)
return unless updater_association
return if !new_record? && !changed?

ActiveRecord::Userstamp::Utilities.assign_stamper(self, :updater, attribute)
ActiveRecord::Userstamp::Utilities.assign_stamper(self, updater_association)
end

def set_deleter_attribute
attribute = ActiveRecord::Userstamp.config.deleter_attribute
return if !has_stamper? || attribute.nil? || !has_attribute?(attribute)
return unless has_stamper?

ActiveRecord::Userstamp::Utilities.assign_stamper(self, :deleter, attribute)
deleter_association = self.class.reflect_on_association(:deleter)
return unless deleter_association

ActiveRecord::Userstamp::Utilities.assign_stamper(self, deleter_association)
save
end
end
17 changes: 11 additions & 6 deletions lib/active_record/userstamp/utilities.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,22 @@ def self.available_association_columns(model)
nil
end

# Assigns the stamper to the given association/attribute in the record.
# Assigns the stamper to the given association reflection in the record.
#
# If the stamper is a record, then it is assigned to the association; if it is a number, then it
# is assigned to the configured attribute.
# is assigned to the foreign key.
#
# @param [ActiveRecord::Base] record The record to assign.
# @param [Symbol] association The association to assign, if the stamper is a record.
# @param [Symbol] attribute The attribute to assign, if the stamper is an ID.
def self.assign_stamper(record, association, attribute)
# @param [ActiveRecord::Reflection] association The association to assign
def self.assign_stamper(record, association)
stamp_value = record.class.stamper_class.stamper
attribute = association if !stamp_value.is_a?(Fixnum) && !stamp_value.is_a?(Integer)
attribute =
if stamp_value.is_a?(ActiveRecord::Base)
association.name
else
association.foreign_key
end

record.send("#{attribute}=", stamp_value)
end
end

0 comments on commit e69a260

Please sign in to comment.