scopes.rb revision 7f12123144c64e1114c5fc62158230fa96706746
bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Boschmodule Ontology::Scopes
0cc566d5e68a650d12fd73631d53a467fad72e1eTimo Sirainen extend ActiveSupport::Concern
0cc566d5e68a650d12fd73631d53a467fad72e1eTimo Sirainen
0cc566d5e68a650d12fd73631d53a467fad72e1eTimo Sirainen included do
0cc566d5e68a650d12fd73631d53a467fad72e1eTimo Sirainen equal_scope 'repository_id'
0cc566d5e68a650d12fd73631d53a467fad72e1eTimo Sirainen
0cc566d5e68a650d12fd73631d53a467fad72e1eTimo Sirainen scope :without_parent, where('ontologies.parent_id' => nil)
0cc566d5e68a650d12fd73631d53a467fad72e1eTimo Sirainen
0cc566d5e68a650d12fd73631d53a467fad72e1eTimo Sirainen scope :basepath, ->(path) do
0cc566d5e68a650d12fd73631d53a467fad72e1eTimo Sirainen joins(:ontology_version).where('ontology_versions.basepath' => path)
0cc566d5e68a650d12fd73631d53a467fad72e1eTimo Sirainen end
0cc566d5e68a650d12fd73631d53a467fad72e1eTimo Sirainen
0cc566d5e68a650d12fd73631d53a467fad72e1eTimo Sirainen scope :list, -> do
0cc566d5e68a650d12fd73631d53a467fad72e1eTimo Sirainen includes(:logic).
0cc566d5e68a650d12fd73631d53a467fad72e1eTimo Sirainen order('ontologies.state asc, ontologies.symbols_count desc')
efe78d3ba24fc866af1c79b9223dc0809ba26cadStephan Bosch end
efe78d3ba24fc866af1c79b9223dc0809ba26cadStephan Bosch
0cc566d5e68a650d12fd73631d53a467fad72e1eTimo Sirainen scope :with_path, ->(path) do
0cc566d5e68a650d12fd73631d53a467fad72e1eTimo Sirainen condition = <<-CONDITION
0cc566d5e68a650d12fd73631d53a467fad72e1eTimo Sirainen ("ontology_versions"."file_extension" = :extname)
0cc566d5e68a650d12fd73631d53a467fad72e1eTimo Sirainen OR (("ontology_versions"."file_extension" IS NULL)
0cc566d5e68a650d12fd73631d53a467fad72e1eTimo Sirainen AND ("ontologies"."file_extension" = :extname))
0cc566d5e68a650d12fd73631d53a467fad72e1eTimo Sirainen CONDITION
0cc566d5e68a650d12fd73631d53a467fad72e1eTimo Sirainen
0cc566d5e68a650d12fd73631d53a467fad72e1eTimo Sirainen with_basepath(File.basepath(path)).
0cc566d5e68a650d12fd73631d53a467fad72e1eTimo Sirainen where(condition, extname: File.extname(path)).
0cc566d5e68a650d12fd73631d53a467fad72e1eTimo Sirainen readonly(false)
0cc566d5e68a650d12fd73631d53a467fad72e1eTimo Sirainen end
0cc566d5e68a650d12fd73631d53a467fad72e1eTimo Sirainen
0cc566d5e68a650d12fd73631d53a467fad72e1eTimo Sirainen scope :with_basepath, ->(path) do
0cc566d5e68a650d12fd73631d53a467fad72e1eTimo Sirainen join = <<-JOIN
0cc566d5e68a650d12fd73631d53a467fad72e1eTimo Sirainen LEFT JOIN "ontology_versions"
0cc566d5e68a650d12fd73631d53a467fad72e1eTimo Sirainen ON "ontologies"."ontology_version_id" = "ontology_versions"."id"
0cc566d5e68a650d12fd73631d53a467fad72e1eTimo Sirainen JOIN
0cc566d5e68a650d12fd73631d53a467fad72e1eTimo Sirainen
0cc566d5e68a650d12fd73631d53a467fad72e1eTimo Sirainen condition = <<-CONDITION
96ca70b91de9f2b07728812bbec222e18f121cbdTimo Sirainen ("ontology_versions"."basepath" = :path)
0cc566d5e68a650d12fd73631d53a467fad72e1eTimo Sirainen OR (("ontology_versions"."basepath" IS NULL)
0cc566d5e68a650d12fd73631d53a467fad72e1eTimo Sirainen AND ("ontologies"."basepath" = :path))
0cc566d5e68a650d12fd73631d53a467fad72e1eTimo Sirainen CONDITION
0cc566d5e68a650d12fd73631d53a467fad72e1eTimo Sirainen
0cc566d5e68a650d12fd73631d53a467fad72e1eTimo Sirainen joins(join).where(condition, path: path).readonly(false)
0cc566d5e68a650d12fd73631d53a467fad72e1eTimo Sirainen end
0cc566d5e68a650d12fd73631d53a467fad72e1eTimo Sirainen
0cc566d5e68a650d12fd73631d53a467fad72e1eTimo Sirainen scope :parents_first, -> do
0cc566d5e68a650d12fd73631d53a467fad72e1eTimo Sirainen order('(CASE WHEN ontologies.parent_id IS NULL THEN 1 ELSE 0 END) DESC,'\
0cc566d5e68a650d12fd73631d53a467fad72e1eTimo Sirainen ' ontologies.parent_id asc')
0cc566d5e68a650d12fd73631d53a467fad72e1eTimo Sirainen end
0cc566d5e68a650d12fd73631d53a467fad72e1eTimo Sirainen
0cc566d5e68a650d12fd73631d53a467fad72e1eTimo Sirainen # searching scopes
0cc566d5e68a650d12fd73631d53a467fad72e1eTimo Sirainen scope :filter_by_ontology_type, ->(type_id) do
baf3e87e186453fda13bd21f7cbcb2efc8492e8bTimo Sirainen where(ontology_type_id: type_id)
0cc566d5e68a650d12fd73631d53a467fad72e1eTimo Sirainen end
0cc566d5e68a650d12fd73631d53a467fad72e1eTimo Sirainen
0cc566d5e68a650d12fd73631d53a467fad72e1eTimo Sirainen scope :filter_by_project, ->(project_id) do
0cc566d5e68a650d12fd73631d53a467fad72e1eTimo Sirainen joins(:projects).where("projects.id = #{project_id}")
0cc566d5e68a650d12fd73631d53a467fad72e1eTimo Sirainen end
scope :filter_by_formality, ->(formality_id) do
where(formality_level_id: formality_id)
end
scope :filter_by_license, ->(license_id) do
joins(:license_models).where("license_models.id = #{license_id}")
end
scope :filter_by_task, ->(task_id) do
joins(:tasks).where("tasks.id = #{task_id}")
end
# state scopes
scope :state, ->(*states) do
where state: states.map(&:to_s)
end
end
end