Module: Userstamper::Utilities

Defined in:
lib/userstamper/utilities.rb

Class Method Summary collapse

Class Method Details

.assign_stamper(record, association) ⇒ Object

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 foreign key.

Parameters:

  • record (ActiveRecord::Base)

    The record to assign.

  • association (ActiveRecord::Reflection)

    The association to assign



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/userstamper/utilities.rb', line 46

def self.assign_stamper(record, association)
  stamp_value = record.class.stamper_class.stamper
  attribute =
    if stamp_value.is_a?(ActiveRecord::Base)
      association.name
    else
      association.foreign_key
    end

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

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

Obtains the creator/updater 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 columns.



28
29
30
31
32
33
34
35
36
37
# File 'lib/userstamper/utilities.rb', line 28

def self.available_association_columns(model)
  return nil if model.name.nil? || model.table_name.empty?
  columns = Set[*model.column_names]
  config = Userstamper.config

  [config.creator_attribute.present? && columns.include?(config.creator_attribute.to_s),
   config.updater_attribute.present? && columns.include?(config.updater_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/userstamper/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_possible_method(method)
    end
  end
end