Module: HumanID::Extension::Persistence

Defined in:
lib/humanid/extension/persistence.rb

Class Method Summary collapse

Class Method Details

.assign(human_id, model) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/humanid/extension/persistence.rb', line 19

def assign(human_id, model)
  # Sometimes human id depends on record id or timestamps.
  # When record isn't persisted human id will be generated wrong.
  if Generation.ready_to_generate?(model)
    model.send("#{human_id}=", model.send("generate_#{human_id}"))
  end
end

.assign!(human_id, model) ⇒ Object



8
9
10
11
12
13
14
15
16
17
# File 'lib/humanid/extension/persistence.rb', line 8

def assign!(human_id, model)
  if Generation.ready_to_generate?(model)
    new_value = model.send("generate_#{human_id}")
    if model.send(human_id) != new_value
      model[human_id] = new_value
      model.update_column(human_id, new_value)
      model.save!
    end
  end
end

.need_to_update?(human_id, model) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
30
31
32
33
34
# File 'lib/humanid/extension/persistence.rb', line 27

def need_to_update?(human_id, model)
  options = model.send("options_for_#{human_id}")
  case options[:update]
    when :always        then true
    when nil, :if_blank then !model.send("#{human_id}?")
                        else false
  end
end