diff --git a/app/controllers/ntriple_controller.rb b/app/controllers/ntriple_controller.rb new file mode 100644 index 00000000..44e0380e --- /dev/null +++ b/app/controllers/ntriple_controller.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +# Controls the ntriple editing logic +class NtripleController < AdminController + + def edit + @term = params[:term_id] + @file = File.open("#{Settings.cache_dir}/ns/#{params[:term_id]}.nt", 'r').read + end + + def update + binding.pry + reader = RDF::Reader.for(:ntriples).new(params[params["id"]]["ntriples"]) + reader = reader.validate! + if reader.valid? + PreloadCache.write_triples(params[params["id"]]["ntriples"], params["id"]) + # Commit triples to file and to BG + flash[:success] = "NTriples for #{ params["id"] } has been updated successfully" + redirect_to '/' + else + # Redirect back to form + redirect_to "/ntriples/#{params["id"]}" + end + end +end diff --git a/app/services/preload_cache.rb b/app/services/preload_cache.rb index 82ce5865..24c64092 100644 --- a/app/services/preload_cache.rb +++ b/app/services/preload_cache.rb @@ -7,6 +7,11 @@ def self.preload(term) write("#{Settings.cache_dir}/ns/#{term.id}.jsonld", term.full_graph.dump(:jsonld, standard_prefixes: true)) end + def self.write_triples(triples, id) + FileUtils.mkdir_p("#{Settings.cache_dir}/ns/#{id}") + write("#{Settings.cache_dir}/ns/#{id}.nt", triples) + end + private def self.write(path, content) diff --git a/app/views/ntriple/edit.html.erb b/app/views/ntriple/edit.html.erb new file mode 100644 index 00000000..c634650b --- /dev/null +++ b/app/views/ntriple/edit.html.erb @@ -0,0 +1,5 @@ +<%= simple_form_for(@term, :url => update_ntriples_path(:id => @term)) do |f| %> + <%= f.input 'ntriples', as: :text, input_html: { cols: 150, rows: 30, value: @file } %> + + <%= f.submit %> +<% end %> \ No newline at end of file diff --git a/app/views/terms/show.html.erb b/app/views/terms/show.html.erb index 40ddb234..e5c03bed 100644 --- a/app/views/terms/show.html.erb +++ b/app/views/terms/show.html.erb @@ -22,6 +22,7 @@ <%= link_to "Edit", edit_term_path(@term), { :class => 'btn btn-default' } %> <% end %> <%= link_to "Manage cache", cache_term_path(@term), { :class => 'btn btn-default' } %> + <%= link_to "Edit N-Triples", edit_ntriples_path(:term_id => @term.id), { :class => 'btn btn-default' } %> <% if @term.term_id.hasParent? %> diff --git a/config/routes.rb b/config/routes.rb index 5cd7a84b..cfcecbeb 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -29,6 +29,9 @@ post 'predicates/*id/review_update', :to => "predicates#review_update", :as => "review_update_predicate" patch 'predicates/*id', :to => "predicates#update", :as => "update_predicate" + get 'ntriples/*term_id', :to => "ntriple#edit", :as => "edit_ntriples" + post 'ntriples/*term_id/update', :to => "ntriple#update", :as => "update_ntriples" + resources :vocabularies, :only => [:index, :new, :create, :edit] get '/vocabularies/*vocabulary_id/new', :to => "terms#new", :as => "new_term" resources :predicates, :only => [:index, :new, :create, :edit] diff --git a/spec/controllers/ntriple_controller_spec.rb b/spec/controllers/ntriple_controller_spec.rb new file mode 100644 index 00000000..66a31709 --- /dev/null +++ b/spec/controllers/ntriple_controller_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe NtripleController, type: :controller do + +end