Module: ActiveRecord::Userstamp::Utilities

Defined in:
lib/active_record/userstamp/utilities.rb

Class Method Summary collapse

Class Method Details

.assign_attribute(record, attribute) ⇒ Object

Assigns the given attribute to the record, based on the model’s stamper.

If the stamper is a record, then it is assigned to the attribute; if it is a number, then it is assigned to the _id attribute

Parameters:

  • record (ActiveRecord::Base)

    The record to assign.

  • attribute (Symbol)

    The attribute to assign.



48
49
50
51
52
53
54
# File 'lib/active_record/userstamp/utilities.rb', line 48

def self.assign_attribute(record, attribute)
  attribute = attribute.to_s
  stamp_value = record.class.stamper_class.stamper

  attribute = attribute[0..-4] if !stamp_value.is_a?(Fixnum) && attribute.end_with?('_id')
  record.send("#{attribute}=", stamp_value)
end

.available_association_columns(model) ⇒ nil|Array<(bool, bool, bool)>

Obtains the creator/updater/deleter columns which are present in the model.

Parameters:

  • model (Class)

    The model to query.

Returns:

  • (nil|Array<(bool, bool, bool)>)

    Nil if the model does not have a table defined. Otherwise, a tuple of booleans indicating the presence of the created, updated, and deleted columns.



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/active_record/userstamp/utilities.rb', line 29

def self.available_association_columns(model)
  return nil if model.table_name.empty?
  columns = Set[*model.column_names]
  config = ActiveRecord::Userstamp.config

  [config.creator_attribute.present? && columns.include?(config.creator_attribute.to_s),
   config.updater_attribute.present? && columns.include?(config.updater_attribute.to_s),
   config.deleter_attribute.present? && columns.include?(config.deleter_attribute.to_s)]
rescue ActiveRecord::StatementInvalid => _
  nil
end

.remove_association(model, association) ⇒ void

This method returns an undefined value.

Removes the association methods from the model.

Parameters:

  • model (Class)

    The model to remove methods from.

  • association (Symbol)

    The name of the association to remove.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/active_record/userstamp/utilities.rb', line 7

def self.remove_association(model, association)
  methods = [
    association,
    "#{association}=",
    "build_#{association}",
    "create_#{association}",
    "create_#{association}!"
  ]

  model.generated_association_methods.module_eval do
    methods.each do |method|
      remove_method(method) if method_defined?(method)
    end
  end
end