From 5c704f956e3c85b73784eed3e9f7f7912aeef191 Mon Sep 17 00:00:00 2001 From: straleyb Date: Wed, 17 Jul 2019 10:15:34 -0700 Subject: [PATCH 1/5] set up initial framwork for ntriples editing --- app/controllers/ntriple_controller.rb | 12 ++++++++++++ app/views/ntriple/edit.html.erb | 1 + app/views/terms/show.html.erb | 1 + config/routes.rb | 2 ++ spec/controllers/ntriple_controller_spec.rb | 5 +++++ 5 files changed, 21 insertions(+) create mode 100644 app/controllers/ntriple_controller.rb create mode 100644 app/views/ntriple/edit.html.erb create mode 100644 spec/controllers/ntriple_controller_spec.rb diff --git a/app/controllers/ntriple_controller.rb b/app/controllers/ntriple_controller.rb new file mode 100644 index 00000000..f0a6a10a --- /dev/null +++ b/app/controllers/ntriple_controller.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +# Controls the ntriple editing logic +class NtripleController < AdminController + + def edit + end + + def update + + end +end diff --git a/app/views/ntriple/edit.html.erb b/app/views/ntriple/edit.html.erb new file mode 100644 index 00000000..19404c76 --- /dev/null +++ b/app/views/ntriple/edit.html.erb @@ -0,0 +1 @@ +YO DAWG \ 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..5aa3cbf7 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -29,6 +29,8 @@ 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" + 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 From a3fa786040f537b69068ac4f7c4c5667cb788c18 Mon Sep 17 00:00:00 2001 From: straleyb Date: Wed, 17 Jul 2019 12:55:31 -0700 Subject: [PATCH 2/5] populates form field with ntriples value --- app/controllers/ntriple_controller.rb | 2 ++ app/views/ntriple/edit.html.erb | 4 +++- config/routes.rb | 1 + 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/app/controllers/ntriple_controller.rb b/app/controllers/ntriple_controller.rb index f0a6a10a..2646d13f 100644 --- a/app/controllers/ntriple_controller.rb +++ b/app/controllers/ntriple_controller.rb @@ -4,6 +4,8 @@ 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 diff --git a/app/views/ntriple/edit.html.erb b/app/views/ntriple/edit.html.erb index 19404c76..96975ca6 100644 --- a/app/views/ntriple/edit.html.erb +++ b/app/views/ntriple/edit.html.erb @@ -1 +1,3 @@ -YO DAWG \ No newline at end of file +<%= 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 } %> +<% end %> \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 5aa3cbf7..738eda6a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -30,6 +30,7 @@ patch 'predicates/*id', :to => "predicates#update", :as => "update_predicate" get 'ntriples/*term_id', :to => "ntriple#edit", :as => "edit_ntriples" + patch '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" From 7559ab3c71cd740b057d4a3948a9a6aa0ad77192 Mon Sep 17 00:00:00 2001 From: straleyb Date: Wed, 17 Jul 2019 13:31:13 -0700 Subject: [PATCH 3/5] entrypoint for ntriples commit/validation --- app/controllers/ntriple_controller.rb | 3 ++- app/views/ntriple/edit.html.erb | 2 ++ config/routes.rb | 2 +- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/app/controllers/ntriple_controller.rb b/app/controllers/ntriple_controller.rb index 2646d13f..14246dfe 100644 --- a/app/controllers/ntriple_controller.rb +++ b/app/controllers/ntriple_controller.rb @@ -9,6 +9,7 @@ def edit end def update - + binding.pry + redirect_to '/' end end diff --git a/app/views/ntriple/edit.html.erb b/app/views/ntriple/edit.html.erb index 96975ca6..c634650b 100644 --- a/app/views/ntriple/edit.html.erb +++ b/app/views/ntriple/edit.html.erb @@ -1,3 +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/config/routes.rb b/config/routes.rb index 738eda6a..cfcecbeb 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -30,7 +30,7 @@ patch 'predicates/*id', :to => "predicates#update", :as => "update_predicate" get 'ntriples/*term_id', :to => "ntriple#edit", :as => "edit_ntriples" - patch 'ntriples/*term_id/update', :to => "ntriple#update", :as => "update_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" From a859c61d54df62f94603e86c35ce7b1d0a7b3fc1 Mon Sep 17 00:00:00 2001 From: straleyb Date: Thu, 18 Jul 2019 13:16:17 -0700 Subject: [PATCH 4/5] sets up framework for validation --- app/controllers/ntriple_controller.rb | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/app/controllers/ntriple_controller.rb b/app/controllers/ntriple_controller.rb index 14246dfe..793418f1 100644 --- a/app/controllers/ntriple_controller.rb +++ b/app/controllers/ntriple_controller.rb @@ -9,7 +9,13 @@ def edit end def update - binding.pry - redirect_to '/' + reader = RDF::Reader.for(:ntriples).new(params[params["id"]]["ntriples"]) + reader = reader.validate! + if reader.valid? + # Commit triples to file and to BG + redirect_to '/' + else + # Redirect back to form + end end end From 00fa74b0e294da4783635ba785934bada77ddd08 Mon Sep 17 00:00:00 2001 From: straleyb Date: Fri, 19 Jul 2019 07:25:41 -0700 Subject: [PATCH 5/5] Redirect and commit to ntriples file --- app/controllers/ntriple_controller.rb | 4 ++++ app/services/preload_cache.rb | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/app/controllers/ntriple_controller.rb b/app/controllers/ntriple_controller.rb index 793418f1..44e0380e 100644 --- a/app/controllers/ntriple_controller.rb +++ b/app/controllers/ntriple_controller.rb @@ -9,13 +9,17 @@ def edit 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)