Sha256: b0047260afad4980cb9cd42faa51e4f97dfa71a24c21a365782ccd2066418650

Contents?: true

Size: 817 Bytes

Versions: 4

Compression:

Stored size: 817 Bytes

Contents

module RailsLiveDashboard
  module Types
    class QueryContent < ActiveRecord::Type::Value
      PAYLOAD =
        %i[
          name
          sql
          parameters
          duration
          kind
          cached
        ].freeze

      CONTENT_STRUCT = Struct.new(*PAYLOAD)

      def type
        :jsonb
      end

      def cast(value)
        sanitized = sanitize_input(value)
        CONTENT_STRUCT.new(**sanitized)
      end

      def deserialize(value)
        decoded = ActiveSupport::JSON.decode(value)&.symbolize_keys
        sanitized = sanitize_input(decoded)
        CONTENT_STRUCT.new(**sanitized)
      end

      def serialize(value)
        ActiveSupport::JSON.encode(value)
      end

      private

      def sanitize_input(input)
        input.slice(*PAYLOAD)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rails_live_dashboard-0.2.0 app/models/rails_live_dashboard/types/query_content.rb
rails_live_dashboard-0.1.2 app/models/rails_live_dashboard/types/query_content.rb
rails_live_dashboard-0.1.1 app/models/rails_live_dashboard/types/query_content.rb
rails_live_dashboard-0.1.0 app/models/rails_live_dashboard/types/query_content.rb