Module: Caprese::Aliasing

Extended by:
ActiveSupport::Concern
Included in:
Controller
Defined in:
lib/caprese/controller/concerns/aliasing.rb

Instance Method Summary collapse

Instance Method Details

#actual_field(field, klass = controller_record_class) ⇒ Symbol

Gets the actual field name for the controller_record_class for any given field requested

Parameters:

  • field (Symbol, String)

    the field that was requested

  • klass (Class) (defaults to: controller_record_class)

    the klass to get field aliases for

Returns:

  • (Symbol)

    the actual field name for the field requested



56
57
58
# File 'lib/caprese/controller/concerns/aliasing.rb', line 56

def actual_field(field, klass = controller_record_class)
  klass.caprese_unalias_field(field.to_sym)
end

#actual_fields(fields) ⇒ Hash<Array>

Takes in a set of possibly aliased fields with types and converts them to their actual types and fields

Parameters:

  • the (Hash<Array>)

    hash of possibly aliased resource types with their possibly aliased field specifier arrays

Returns:

  • (Hash<Array>)

    the actual resource type and fields



84
85
86
87
88
# File 'lib/caprese/controller/concerns/aliasing.rb', line 84

def actual_fields(fields)
  Hash[*fields.each do |type, fields|
    [actual_type(type), fields.map { |f| actual_field(f, record_class(type)) }]
  end]
end

#actual_includes(includes) ⇒ Array<String>

Takes in a set of possibly aliased includes and converts them to their actual names

Parameters:

  • the (String)

    CSV string of possibly aliased includes

Returns:

  • (Array<String>)

    the actual includes



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/caprese/controller/concerns/aliasing.rb', line 64

def actual_includes(includes)
  includes.split(',').map do |i|
    if(i = i.split('.')).size > 1
      klass = nil
      i.map do |i2|
        actual = actual_field(i2, klass)
        klass = klass.reflections[actual].klass
        actual
      end.join('.')
    else
      actual_field(i)
    end
  end
end

#actual_type(type) ⇒ Symbol

Checks resource_type_aliases for a type alias, or returns the type already stated

Parameters:

  • type (Symbol)

    the type to search for an alias for

Returns:

  • (Symbol)

    the actual type for the type alias



47
48
49
# File 'lib/caprese/controller/concerns/aliasing.rb', line 47

def actual_type(type)
  resource_type_aliases[type] || type
end

#engaged_field_aliasesObject

Records all of the field aliases engaged by the API request (called in ‘assign_changes_from_document` using comparison) so that when the response is returned, the appropriate alias is used in reference to fields

Success: @todo Errors: @see ErrorSerializer

{

aliased_attribute: true, # used aliased attribute name instead of unaliased one

aliased_relationship: { # used aliased relationship name
  aliased_attribute: true # and aliased attribute names
  aliased_attribute_2: true
},

aliased_relationship: {}, # used aliased relationship name but no aliased attribute names

unaliased_relationship: { # used unaliased relationship name
  aliased_attribute: true # and aliased attribute name for that relationship
},

}



29
30
31
# File 'lib/caprese/controller/concerns/aliasing.rb', line 29

def engaged_field_aliases
  @__engaged_field_aliases ||= {}
end

#resource_type_aliasesObject

Note:

The ‘caprese_type` class variable of the model should also be set to the new type

Specifies specific resource models that have types that are aliased.

Examples:

{
  questions: attributes
}


39
40
41
# File 'lib/caprese/controller/concerns/aliasing.rb', line 39

def resource_type_aliases
  {}
end