+# == Schema Information
+#
+# Table name: gpx_files
+#
+# id :bigint(8) not null, primary key
+# user_id :bigint(8) not null
+# visible :boolean default(TRUE), not null
+# name :string default(""), not null
+# size :bigint(8)
+# latitude :float
+# longitude :float
+# timestamp :datetime not null
+# description :string default(""), not null
+# inserted :boolean not null
+# visibility :enum default("public"), not null
+#
+# Indexes
+#
+# gpx_files_timestamp_idx (timestamp)
+# gpx_files_user_id_idx (user_id)
+# gpx_files_visible_visibility_idx (visible,visibility)
+#
+# Foreign Keys
+#
+# gpx_files_user_id_fkey (user_id => users.id)
+#
+
+class Trace < ApplicationRecord
+ require "open3"
+
+ self.table_name = "gpx_files"
+
+ belongs_to :user, :counter_cache => true
+ has_many :tags, :class_name => "Tracetag", :foreign_key => "gpx_id", :dependent => :delete_all, :inverse_of => :trace
+ has_many :points, :class_name => "Tracepoint", :foreign_key => "gpx_id", :dependent => :delete_all, :inverse_of => :trace
+
+ scope :visible, -> { where(:visible => true) }
+ scope :visible_to, ->(u) { visible.where(:visibility => %w[public identifiable]).or(visible.where(:user => u)) }
+ scope :visible_to_all, -> { where(:visibility => %w[public identifiable]) }
+ scope :tagged, ->(t) { joins(:tags).where(:gpx_file_tags => { :tag => t }) }
+
+ has_one_attached :file, :service => Settings.trace_file_storage
+ has_one_attached :image, :service => Settings.trace_image_storage
+ has_one_attached :icon, :service => Settings.trace_icon_storage
+
+ validates :user, :associated => true
+ validates :name, :presence => true, :length => 1..255, :characters => true
+ validates :description, :presence => { :on => :create }, :length => 1..255, :characters => true
+ validates :timestamp, :presence => true
+ validates :visibility, :inclusion => %w[private public trackable identifiable]
+
+ after_save :set_filename