files.rb revision f2c5529a09f91b3f5d46ac41d008159448b863c6
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Fostermodule OntologyVersion::Files
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster extend ActiveSupport::Concern
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster included do
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster # virtual attribute for upload
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster attr_accessible :raw_file
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster before_create :commit_raw_file, unless: :commit_oid?
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster end
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster def raw_file=(value)
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster @raw_file = value
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster end
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster def commit_raw_file
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster return true if ontology.parent
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster raise "raw file missing" unless @raw_file
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster self.path = @raw_file.original_filename if self.path.nil?
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster # otherwise the file upload is broken (no implicit conversion of ActionDispatch::Http::UploadedFile into String):
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster tmp_file = if @raw_file.class == ActionDispatch::Http::UploadedFile
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster @raw_file.tempfile
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster else
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster @raw_file
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster end
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster repository.save_file(tmp_file, self.path, "message", user)
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster end
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster def tmp_dir
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster Ontohub::Application.config.commits_path.join(commit_oid)
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster end
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster # path to the raw file
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster def raw_path
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster tmp_dir.join("raw",self.path)
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster end
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster # path to the raw file, checks out the raw file if is missing
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster def raw_path!
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster checkout_raw!
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster raw_path
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster end
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster def xml_dir
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster tmp_dir.join('xml')
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster end
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster # path to xml file (hets output)
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster def xml_path
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster return nil unless xml_name
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster xml_dir.join(xml_name)
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster end
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster # path to xml file (hets output) with code positions
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster def code_reference_path
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster return nil unless pp_xml_name
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster xml_dir.join(pp_xml_name)
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster end
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster def xml_file?
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster xml_path.try(:exist?)
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster end
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster def raw_file?
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster File.exists? raw_path
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster end
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster # checks out the raw file
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster def checkout_raw!
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster unless raw_file?
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster FileUtils.mkdir_p raw_path.dirname
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster File.open(raw_path, "w"){|f| f.write raw_data }
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster end
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster end
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster # returns the raw data directly from the repository
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster def raw_data
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster repository.get_file(self.path, commit_oid).content.encoding_utf8
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster end
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Fosterend
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster