Module: ViewModel::Controller::MigrationVersions

Extended by:
ActiveSupport::Concern
Included in:
ActiveRecord::Controller
Defined in:
lib/view_model/controller/migration_versions.rb

Constant Summary collapse

MIGRATION_VERSION_HEADER =
'X-ViewModel-Versions'

Instance Method Summary collapse

Instance Method Details

#migration_versionsObject



8
9
10
11
12
13
14
15
# File 'lib/view_model/controller/migration_versions.rb', line 8

def migration_versions
  @migration_versions ||=
    begin
      specified_migration_versions.reject do |viewmodel_class, required_version|
        viewmodel_class.schema_version == required_version
      end.freeze
    end
end

#specified_migration_versionsObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/view_model/controller/migration_versions.rb', line 17

def specified_migration_versions
  @specified_migration_versions ||=
    begin
      version_spec =
        if params.include?(:versions)
          params[:versions]
        elsif request.headers.include?(MIGRATION_VERSION_HEADER)
          begin
            JSON.parse(request.headers[MIGRATION_VERSION_HEADER])
          rescue JSON::ParserError
            raise ViewModel::Error.new(status: 400, detail: "Invalid JSON in #{MIGRATION_VERSION_HEADER}")
          end
        else
          {}
        end

      versions =
        IknowParams::Parser.parse_value(
          version_spec,
          with: IknowParams::Serializer::HashOf.new(
            IknowParams::Serializer::String, IknowParams::Serializer::Integer))

      migration_versions = {}

      versions.each do |view_name, required_version|
        viewmodel_class = ViewModel::Registry.for_view_name(view_name)
        migration_versions[viewmodel_class] = required_version
      rescue ViewModel::DeserializationError::UnknownView
        # Ignore requests to migrate types that no longer exist
        next
      end

      migration_versions.freeze
    end
end