module Caprese::Typing

Public Instance Methods

controller_record_class() click to toggle source

Gets the record class for the current controller

@return [Class] the record class for the current controller

# File lib/caprese/controller/concerns/typing.rb, line 33
def controller_record_class
  record_class(unnamespace(params[:controller]))
end
fail_on_type_mismatch(type) click to toggle source

Checks if a given type mismatches the controller type

@param [String] type the pluralized type to check ('products','orders',etc.)

# File lib/caprese/controller/concerns/typing.rb, line 40
def fail_on_type_mismatch(type)
  failed = false

  begin
    failed = record_class(type) != controller_record_class
  rescue NameError
    failed = true
  end

  if failed
    invalid_typed_record = controller_record_class.new
    invalid_typed_record.errors.add(:type)

    fail RecordInvalidError.new(invalid_typed_record, engaged_field_aliases)
  end
end
record_class(type) click to toggle source

Gets the class for a record type @note “record type” can be plural, singular, or classified

i.e. 'orders', 'order', or 'Order'

@example

record_class(:orders) # => Order

@param [Symbol] type the record type to get the class for @return [Class] the class for a given record type

# File lib/caprese/controller/concerns/typing.rb, line 18
def record_class(type)
  begin
    type.to_s.classify.constantize
  rescue NameError => e
    if resource_type_aliases[type.to_sym]
      record_class(resource_type_aliases[type.to_sym].to_sym)
    else
      raise e
    end
  end
end