Module: Caprese::Record::Aliasing::ClassMethods

Defined in:
lib/caprese/record/aliasing.rb

Instance Method Summary collapse

Instance Method Details

#caprese_alias_field(field) ⇒ Symbol

Note:

The reason this is useful is because ActiveRecord validations must validate the actual field name of a model, but when we add errors they should always have aliased fields

Given an actual field, convert to its appropriate field alias for the class

Parameters:

  • field (String, Symbol)

    the actual field name to alias

Returns:

  • (Symbol)

    the aliased field name, or the original name symbolized



30
31
32
# File 'lib/caprese/record/aliasing.rb', line 30

def caprese_alias_field(field)
  caprese_field_aliases.invert[field = field.to_sym] || field
end

#caprese_field_aliasesObject

Provides the ability to display an aliased field name to the consumer of the API, and then map that name to its real name on the server

Examples:

{
  alias: :actual
}


48
49
50
# File 'lib/caprese/record/aliasing.rb', line 48

def caprese_field_aliases
  {}
end

#caprese_typeObject

The type that is serialized and responded with for this class



53
54
55
# File 'lib/caprese/record/aliasing.rb', line 53

def caprese_type
  self.name.underscore
end

#caprese_unalias_field(field) ⇒ Symbol

Given an aliased field, convert to its actual field for the class

Parameters:

  • field (String, Symbol)

    the actual field name to alias

Returns:

  • (Symbol)

    the aliased field name, or the original name symbolized



38
39
40
# File 'lib/caprese/record/aliasing.rb', line 38

def caprese_unalias_field(field)
  caprese_field_aliases[field = field.to_sym] || field
end