Sha256: f65298ac68ac6b534b9f27c5880803ed1f69ffddd19fc5ac6a3258500a323ff3

Contents?: true

Size: 1.05 KB

Versions: 15

Compression:

Stored size: 1.05 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: index, **attributes)
          end.select(&:id).sort_by(&:index)
        end

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

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
katalyst-navigation-1.4.1 app/models/katalyst/navigation/types/nodes_type.rb
katalyst-navigation-1.4.0 app/models/katalyst/navigation/types/nodes_type.rb
katalyst-navigation-1.3.4 app/models/katalyst/navigation/types/nodes_type.rb
katalyst-navigation-1.3.3 app/models/katalyst/navigation/types/nodes_type.rb
katalyst-navigation-1.3.2 app/models/katalyst/navigation/types/nodes_type.rb
katalyst-navigation-1.3.1 app/models/katalyst/navigation/types/nodes_type.rb
katalyst-navigation-1.3.0 app/models/katalyst/navigation/types/nodes_type.rb
katalyst-navigation-1.2.0 app/models/katalyst/navigation/types/nodes_type.rb
katalyst-navigation-1.1.2 app/models/katalyst/navigation/types/nodes_type.rb
katalyst-navigation-1.1.1 app/models/katalyst/navigation/types/nodes_type.rb
katalyst-navigation-1.1.0 app/models/katalyst/navigation/types/nodes_type.rb
katalyst-navigation-1.0.3 app/models/katalyst/navigation/types/nodes_type.rb
katalyst-navigation-1.0.2 app/models/katalyst/navigation/types/nodes_type.rb
katalyst-navigation-1.0.1 app/models/katalyst/navigation/types/nodes_type.rb
katalyst-navigation-1.0.0 app/models/katalyst/navigation/types/nodes_type.rb