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
16
17
18
19
20
21
22
23
24
25
# File 'lib/view_model/controller/migration_versions.rb', line 8

def migration_versions
  @migration_versions ||=
    begin
      versions = specified_migration_versions.dup

      unless ViewModel::Config.strict_migration_versions
        # Ignore the current version. This allows skipping migrations if only
        # current versions are requested. Can't be ignored when strict
        # migrations are enabled, since we need to walk the tree to assert
        # that all visited types are mentioned.
        versions.reject! do |viewmodel_class, required_version|
          viewmodel_class.schema_version == required_version
        end
      end

      versions.freeze
    end
end

#specified_migration_versionsObject



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
52
53
54
55
56
57
58
59
60
61
# File 'lib/view_model/controller/migration_versions.rb', line 27

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