2020-08-26 20:29:57 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2023-03-22 17:29:08 -04:00
|
|
|
module ::DiscourseDataExplorer
|
2024-09-08 15:22:27 -04:00
|
|
|
class QueryFinder
|
|
|
|
def self.find(id)
|
|
|
|
default_query = Queries.default[id.to_s]
|
|
|
|
return raise ActiveRecord::RecordNotFound unless default_query
|
|
|
|
|
|
|
|
query = Query.find_by(id: id) || Query.new
|
|
|
|
query.attributes = default_query
|
|
|
|
query.user_id = Discourse::SYSTEM_USER_ID.to_s
|
|
|
|
query
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-26 20:29:57 -04:00
|
|
|
class Query < ActiveRecord::Base
|
2022-12-29 07:31:29 -05:00
|
|
|
self.table_name = "data_explorer_queries"
|
2023-03-22 17:29:08 -04:00
|
|
|
|
2020-08-26 20:29:57 -04:00
|
|
|
has_many :query_groups
|
|
|
|
has_many :groups, through: :query_groups
|
|
|
|
belongs_to :user
|
2022-11-17 15:09:50 -05:00
|
|
|
validates :name, presence: true
|
2020-08-26 20:29:57 -04:00
|
|
|
|
2022-12-29 07:31:29 -05:00
|
|
|
scope :for_group,
|
2023-12-01 10:47:54 -05:00
|
|
|
->(group) do
|
2022-12-29 07:31:29 -05:00
|
|
|
where(hidden: false).joins(
|
|
|
|
"INNER JOIN data_explorer_query_groups
|
2021-04-08 12:47:44 -04:00
|
|
|
ON data_explorer_query_groups.query_id = data_explorer_queries.id
|
2022-12-29 07:31:29 -05:00
|
|
|
AND data_explorer_query_groups.group_id = #{group.id}",
|
|
|
|
)
|
2023-12-01 10:47:54 -05:00
|
|
|
end
|
2021-04-08 12:47:44 -04:00
|
|
|
|
2020-08-26 20:29:57 -04:00
|
|
|
def params
|
2023-03-22 17:29:08 -04:00
|
|
|
@params ||= Parameter.create_from_sql(sql)
|
2020-08-26 20:29:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def cast_params(input_params)
|
|
|
|
result = {}.with_indifferent_access
|
|
|
|
self.params.each do |pobj|
|
|
|
|
result[pobj.identifier] = pobj.cast_to_ruby input_params[pobj.identifier]
|
|
|
|
end
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
def slug
|
|
|
|
Slug.for(name).presence || "query-#{id}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.find(id)
|
2024-09-08 15:22:27 -04:00
|
|
|
return super if id.to_i >= 0
|
|
|
|
QueryFinder.find(id)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# for `Query.unscoped.find`
|
|
|
|
class ActiveRecord_Relation
|
|
|
|
def find(id)
|
|
|
|
return super if id.to_i >= 0
|
|
|
|
QueryFinder.find(id)
|
2020-08-26 20:29:57 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|