Sha256: e2724e90bd28b7a633e4c57381f1f558674e9ea6eb0ee6a20be1c8bd4966f947

Contents?: true

Size: 771 Bytes

Versions: 3

Compression:

Stored size: 771 Bytes

Contents

# frozen_string_literal: true

module Motor
  class Query < ::Motor::ApplicationRecord
    audited

    belongs_to :author, polymorphic: true, optional: true

    has_many :taggable_tags, as: :taggable, dependent: :destroy
    has_many :tags, through: :taggable_tags, class_name: 'Motor::Tag'
    has_many :alerts, dependent: :destroy

    attribute :preferences, default: -> { HashWithIndifferentAccess.new }
    serialize :preferences, HashSerializer

    scope :active, -> { where(deleted_at: nil) }

    def result(variables_hash = {})
      result = Motor::Queries::RunQuery.call(self, variables_hash: variables_hash)
      column_names = result.columns.pluck(:name)

      result.data.map { |row| column_names.zip(row).to_h }
    end
    alias run result
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
motor-admin-0.1.52 app/models/motor/query.rb
motor-admin-0.1.51 app/models/motor/query.rb
motor-admin-0.1.50 app/models/motor/query.rb