Skip to content

Commit

Permalink
Merge pull request #203 from iknow/migration-controller-concern
Browse files Browse the repository at this point in the history
Extract migration parsing to a controller concern
  • Loading branch information
chrisandreae authored Apr 23, 2024
2 parents a651321 + d1a4c5b commit 3c95c63
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 48 deletions.
2 changes: 1 addition & 1 deletion lib/iknow_view_models/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module IknowViewModels
VERSION = '3.9.2'
VERSION = '3.10.0'
end
49 changes: 2 additions & 47 deletions lib/view_model/active_record/controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'view_model/active_record/controller_base'
require 'view_model/active_record/collection_nested_controller'
require 'view_model/active_record/singular_nested_controller'
require 'view_model/controller/migration_versions'

# Controller for accessing an ViewModel::ActiveRecord
# Provides for the following routes:
Expand All @@ -16,8 +17,7 @@ module ViewModel::ActiveRecord::Controller
include ViewModel::ActiveRecord::ControllerBase
include ViewModel::ActiveRecord::CollectionNestedController
include ViewModel::ActiveRecord::SingularNestedController

MIGRATION_VERSION_HEADER = 'X-ViewModel-Versions'
include ViewModel::Controller::MigrationVersions

def show(scope: nil, viewmodel_class: self.viewmodel_class, serialize_context: new_serialize_context(viewmodel_class: viewmodel_class))
view = nil
Expand Down Expand Up @@ -95,51 +95,6 @@ def viewmodel_id
parse_param(:id)
end

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

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

def migrated_deep_schema_version
ViewModel::Migrator.migrated_deep_schema_version(viewmodel_class, migration_versions, include_referenced: true)
end
Expand Down
52 changes: 52 additions & 0 deletions lib/view_model/controller/migration_versions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# frozen_string_literal: true

module ViewModel::Controller::MigrationVersions
extend ActiveSupport::Concern

MIGRATION_VERSION_HEADER = 'X-ViewModel-Versions'

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

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
end

0 comments on commit 3c95c63

Please sign in to comment.