Sha256: 5f16c32128c369ba35916623c80fefa18782bafcea86dc5c846cc42d4cecc3af

Contents?: true

Size: 1.04 KB

Versions: 9

Compression:

Stored size: 1.04 KB

Contents

# frozen_string_literal: true

module Katalyst
  module Navigation
    module Types
      # Data serialization/deserialization for Katalyst::Navigation::Menu::Version structural data
      class NodesType < ActiveRecord::Type::Json
        def serialize(value)
          super(value.as_json)
        end

        def deserialize(value)
          case value
          when nil
            nil
          when String
            deserialize(super)
          when Hash
            deserialize_params(value)
          when Array
            deserialize_array(value)
          end
        end

        private

        # Deserialize a params-style array, e.g. "0" => { ... }
        def deserialize_params(value)
          value.map do |index, attributes|
            Node.new(index:, **attributes)
          end.select(&:id).sort_by(&:index)
        end

        def deserialize_array(value)
          value.map.with_index do |attributes, index|
            Node.new(index:, **attributes)
          end.select(&:id).sort_by(&:index)
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
katalyst-navigation-1.8.4 app/models/katalyst/navigation/types/nodes_type.rb
katalyst-navigation-1.8.3 app/models/katalyst/navigation/types/nodes_type.rb
katalyst-navigation-1.8.2 app/models/katalyst/navigation/types/nodes_type.rb
katalyst-navigation-1.8.1 app/models/katalyst/navigation/types/nodes_type.rb
katalyst-navigation-1.8.0 app/models/katalyst/navigation/types/nodes_type.rb
katalyst-navigation-1.6.0 app/models/katalyst/navigation/types/nodes_type.rb
katalyst-navigation-1.5.2 app/models/katalyst/navigation/types/nodes_type.rb
katalyst-navigation-1.5.1 app/models/katalyst/navigation/types/nodes_type.rb
katalyst-navigation-1.5.0 app/models/katalyst/navigation/types/nodes_type.rb