module ViewModel::ActiveRecord::ControllerBase

def access_control

def access_control
  unless instance_variable_defined?(:@access_control)
    raise ArgumentError.new("AccessControl instance not set for Controller '#{self.name}'")
  end
  @access_control
end

def access_control

def access_control
  self.class.access_control
end

def access_control=(access_control)

def access_control=(access_control)
  if instance_variable_defined?(:@access_control)
    raise ArgumentError.new("AccessControl class for Controller '#{self.name}' already set")
  end
  unless access_control.is_a?(Class) && access_control < ViewModel::AccessControl
    raise ArgumentError.new("'#{access_control.inspect}' is not a valid AccessControl")
  end
  @access_control = access_control
end

def model_class

def model_class
  viewmodel_class.model_class
end

def model_class

def model_class
  self.class.model_class
end

def new_deserialize_context(viewmodel_class: self.viewmodel_class, access_control: self.access_control.new, **args)

def new_deserialize_context(viewmodel_class: self.viewmodel_class, access_control: self.access_control.new, **args)
  viewmodel_class.new_deserialize_context(access_control: access_control, **args)
end

def new_serialize_context(viewmodel_class: self.viewmodel_class, access_control: self.access_control.new, **args)

def new_serialize_context(viewmodel_class: self.viewmodel_class, access_control: self.access_control.new, **args)
  viewmodel_class.new_serialize_context(access_control: access_control, **args)
end

def prerender_viewmodel(viewmodel, serialize_context: new_serialize_context, **args)

def prerender_viewmodel(viewmodel, serialize_context: new_serialize_context, **args)
  super(viewmodel, serialize_context: serialize_context, **args)
end

def render_viewmodel(viewmodel, serialize_context: new_serialize_context, **args)

Override (pre)render_viewmodel to use the default serialization context from this controller.
def render_viewmodel(viewmodel, serialize_context: new_serialize_context, **args)
  super(viewmodel, serialize_context: serialize_context, **args)
end

def viewmodel_class

def viewmodel_class
  unless instance_variable_defined?(:@viewmodel_class)
    # try to autodetect the viewmodel based on our name
    if (match = /(.*)Controller$/.match(self.name))
      self.viewmodel_name = match[1].singularize
    else
      raise ArgumentError.new("Could not auto-determine ViewModel from Controller name '#{self.name}'")
    end
  end
  @viewmodel_class
end

def viewmodel_class

def viewmodel_class
  self.class.viewmodel_class
end

def viewmodel_class=(type)

def viewmodel_class=(type)
  if instance_variable_defined?(:@viewmodel_class)
    raise ArgumentError.new("ViewModel class for Controller '#{self.name}' already set")
  end
  unless type < ViewModel
    raise ArgumentError.new("'#{type.inspect}' is not a valid ViewModel")
  end
  @viewmodel_class = type
end

def viewmodel_name=(name)

def viewmodel_name=(name)
  self.viewmodel_class = ViewModel::Registry.for_view_name(name)
end