Module: NCore::ActiveModel

Extended by:
ActiveSupport::Concern
Defined in:
lib/ncore/rails/active_model.rb

Instance Method Summary collapse

Instance Method Details

#cache_key(*_) ⇒ Object

compatible with ActiveRecord 5.2+ default settings

on <= 5.1, does not include version


29
30
31
32
33
34
35
# File 'lib/ncore/rails/active_model.rb', line 29

def cache_key(*_)
  if new_record?
    "#{model_name.cache_key}/new"
  else
    "#{model_name.cache_key}/#{id}"
  end
end

#cache_key_with_versionObject



43
44
45
46
47
48
49
# File 'lib/ncore/rails/active_model.rb', line 43

def cache_key_with_version
  if version = cache_version
    "#{cache_key}-#{version}"
  else
    cache_key
  end
end

#cache_versionObject



37
38
39
40
41
# File 'lib/ncore/rails/active_model.rb', line 37

def cache_version
  if timestamp = try(:updated_at)
    timestamp.utc.to_s(:usec)
  end
end

#destroy(*args) ⇒ Object



22
23
24
# File 'lib/ncore/rails/active_model.rb', line 22

def destroy(*args)
  delete(*args)
end

#new_record?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/ncore/rails/active_model.rb', line 14

def new_record?
  !id
end

#persisted?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/ncore/rails/active_model.rb', line 18

def persisted?
  !new_record?
end