Module: Castkit::ActiveRecord::Extensions::ClassMethods

Defined in:
lib/castkit/active_record/extensions.rb

Overview

Class-level methods to extend ActiveRecord models.

Instance Method Summary collapse

Instance Method Details

#castkit_ignored_on_update(*attributes) ⇒ Array<Symbol>

Defines or returns the list of attributes that should not be assigned when calling ‘update_from_dataobject!` on an ActiveRecord model.

This method merges any custom-defined attributes with the model’s ‘readonly_attributes`, unless explicitly overridden.

Examples:

Mark ‘:id` and `:status` as unassignable

class User < ApplicationRecord
  castkit_ignored_on_update :id, :status
end

Raises:

  • (NotImplementedError)

    if the model doesn’t support ‘.readonly_attributes`



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/castkit/active_record/extensions.rb', line 34

def castkit_ignored_on_update(*attributes)
  unless respond_to?(:readonly_attributes)
    raise NotImplementedError, "Expected class to respond to `readonly_attributes`"
  end

  @__castkit_ignored_attributes ||= []

  unless attributes.empty?
    @__castkit_ignored_attributes = attributes.map(&:to_sym)
    @__castkit_cached_ignored_attributes = nil
  end

  unassignable_attributes = (@__castkit_ignored_attributes + readonly_attributes.to_a)
  @__castkit_cached_ignored_attributes ||= unassignable_attributes.map(&:to_sym).uniq.freeze
end