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


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

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

#cache_key_with_versionObject



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

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

#cache_versionObject



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

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

#destroy(*args) ⇒ Object



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

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

#new_record?Boolean

Returns:

  • (Boolean)


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

def new_record?
  !id
end

#persisted?Boolean

Returns:

  • (Boolean)


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

def persisted?
  !new_record?
end