Skip to content

Commit

Permalink
1620 deletion of child ontologies (#1748)
Browse files Browse the repository at this point in the history
* Make removed child ontologies "not present".

* Scope default children association of acts_as_tree to present ontologies.

* Show only present ontologies of this file.

* Fix ontology factory.

* Add cucumber features.

* Fix restoring children.

* Fix restoring a child ontology.

* Add specs for restoring children.

* Really fix restoring a child ontology.

* Only save the ontology if it has changed or if it is a new record.
  • Loading branch information
eugenk committed Aug 10, 2016
1 parent cca5e2f commit 22f8827
Show file tree
Hide file tree
Showing 21 changed files with 317 additions and 23 deletions.
6 changes: 6 additions & 0 deletions app/models/ontology/associations_and_attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ module Ontology::AssociationsAndAttributes
extend ActiveSupport::Concern

included do
acts_as_tree
# Add the scope "present: true" to the children association by acts_as_tree.
Ontology.reflect_on_association(:children).
options[:conditions] = {present: true}
has_many :all_children, class_name: Ontology.to_s, foreign_key: 'parent_id'

belongs_to :language
belongs_to :logic, counter_cache: true
belongs_to :ontology_type
Expand Down
4 changes: 4 additions & 0 deletions app/models/ontology/class_methods_and_scopes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ module Ontology::ClassMethodsAndScopes
scope :state, ->(*states) do
where state: states.map(&:to_s)
end

scope :present, ->() do
where(present: true)
end
end

module ClassMethods
Expand Down
2 changes: 0 additions & 2 deletions app/models/ontology/distributed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ module Ontology::Distributed
extend ActiveSupport::Concern

included do
acts_as_tree

def self.homogeneous
select_with_character_selector(HOMOGENEOUS_SELECTOR)
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/ontology_version/parsing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def retrieve_available_provers

def update_states_for_self_and_children(state)
update_state!(state)
ontology.children.each do |child|
ontology.reload.children.each do |child|
child.versions.where(commit_oid: commit_oid).first.update_state!(state)
child.versions.where(commit_oid: commit_oid).first.save!
end
Expand Down
4 changes: 2 additions & 2 deletions app/views/files/_show_dir.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
%tr{"data-id" => entry.index}
- if i == 0
%td.ontology{rowspan: entries.size}
- unless entry.dir? || entry.ontologies.empty?
- unless entry.dir? || entry.ontologies.present.empty?
%ul.ontology-list
- entry.ontologies.each do |ontology|
- entry.ontologies.present.each do |ontology|
%li.ontology-list-item
= fancy_link ontology
- if ontology.state=='failed'
Expand Down
6 changes: 3 additions & 3 deletions app/views/files/_show_file.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@
= f.button t('files.discard'), type: 'cancel', id: 'codemirror-btn-discard', class: 'btn btn-danger'
= render partial: 'modal_discard'
- unless resource.ontologies.empty?
%h4
%h4#ontology-list-headline
Defining
= pluralize resource.ontologies.size, Settings.OMS
= pluralize resource.ontologies.present.count, Settings.OMS
%ul.ontology-list
- resource.ontologies.each do |ontology|
- resource.ontologies.present.each do |ontology|
%li= fancy_link ontology
24 changes: 24 additions & 0 deletions features/RepositoryEditFile.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Feature: RepositoryEditFile

Scenario: View the Ontologies of the file (SingleOntology)
Given I have uploaded an ontology
When I visit the file view of the ontology
Then I should see all the file's ontologies

Scenario: View the Ontologies of the file (DistributedOntology)
Given I have uploaded a distributed ontology
When I visit the file view of the ontology
Then I should see all the file's ontologies

Scenario: View the Ontologies of the file (DistributedOntology with deleted children)
Given I have uploaded a distributed ontology
And Children of the ontology have been deleted
When I visit the file view of the ontology
Then I should see all the file's ontologies

Scenario: View the Ontologies of the file (DistributedOntology with restored children)
Given I have uploaded a distributed ontology
And Children of the ontology have been deleted
And Children of the ontology have been restored
When I visit the file view of the ontology
Then I should see all the file's ontologies
23 changes: 23 additions & 0 deletions features/RepositoryFileBrowser.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Feature: RepositoryFileBrowser

Scenario: SingleOntology Display
Given I have uploaded an ontology
When I visit the file browser of the ontology's repository
Then I should see the ontology's file
And I should see the ontlogy next to the ontology's file
And I should see no other ontologies next to the ontology's file

Scenario: DistributedOntology Display
Given I have uploaded a distributed ontology
When I visit the file browser of the ontology's repository
Then I should see the ontology's file
And I should see the ontlogy and its children next to the ontology's file
And I should see no other ontologies next to the ontology's file

Scenario: DistributedOntology with Deleted Children Display
Given I have uploaded a distributed ontology
And Children of the ontology have been deleted
When I visit the file browser of the ontology's repository
Then I should see the ontology's file
And I should see the ontlogy and its children next to the ontology's file
And I should see no other ontologies next to the ontology's file
4 changes: 0 additions & 4 deletions features/step_definitions/ontology_search_steps.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
Given(/^there is an ontology$/) do
@ontology = FactoryGirl.create :ontology
end

When(/^I open the ontologies overview page$/) do
visit ontologies_path
end
Expand Down
3 changes: 2 additions & 1 deletion features/step_definitions/ontology_state_steps.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Given(/^There is an ontology$/) do
Given(/^There is an ontology$/i) do
@ontology = FactoryGirl.create :ontology
@ontology_version = @ontology.current_version
end

Given(/^there is an ontology with a "([^"]+)" version$/) do |state|
Expand Down
5 changes: 3 additions & 2 deletions features/step_definitions/ontology_versions_steps.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
Given(/^there is a distributed ontology$/) do
Given(/^there is a distributed ontology$/i) do
@distributed_ontology = FactoryGirl.create :distributed_ontology, :with_versioned_children
@ontology = @distributed_ontology
@ontology_version = @ontology.current_version
end

Given(/^there is an ontology file$/) do
Given(/^there is an ontology file$/i) do
repository = @ontology.repository
user = repository.permissions.first.subject
Tempfile.create('testfile') do |f|
Expand Down
58 changes: 58 additions & 0 deletions features/step_definitions/repository_edit_file_steps.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
def build_version_for(ontology)
commit_oid = '1'*40
ontology.versions.build(
{commit_oid: commit_oid,
commit: FactoryGirl.create(:commit, commit_oid: commit_oid),
basepath: File.basepath(ontology.path),
file_extension: File.extname(ontology.path),
fast_parse: false},
{without_protection: true})
end

Given(/^I have uploaded a(.*) ontology$/) do |type|
steps %Q{
Given I have an account
And I am logged in
And there is a#{type} ontology
And I have permissions to edit the ontology
And there is an ontology file
}
end

Given(/^Children of the ontology have been deleted$/i) do
@deleted_child = @ontology.children.last
@deleted_child.present = false
@deleted_child.save!

build_version_for(@ontology).save!
@ontology.children.each do |child|
build_version_for(child).save!
end
end

Given(/^Children of the ontology have been restored$/i) do
@deleted_child.present =true
@deleted_child.save!

build_version_for(@ontology).save!
@ontology.children.each do |child|
build_version_for(child).save!
end
end

When(/^I visit the file view of the ontology$/i) do
# The request object does not contain a requested mime type, so we need to
# stub this.
allow_any_instance_of(FilesController).
to receive(:existing_file_requested_as_html?).
and_return(true)
visit repository_tree_path(@ontology.repository, path: @ontology.path)
end

Then(/^I should see all the file's ontologies$/i) do
# The file defines the DistributedOntolgy and its children.
number = 1 + @ontology.children.count

expect(page.find('#ontology-list-headline').text).to match(/\s+#{number}\s+/)
expect(page).to have_selector('ul.ontology-list > li', count: number)
end
38 changes: 38 additions & 0 deletions features/step_definitions/repository_file_browser_steps.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
When(/^I visit the file browser of the ontology's repository$/i) do
visit repository_tree_path(@ontology.repository)
end

Then(/^I should see the ontology's file$/i) do
file_cell_selector = 'table.file-table td.name'
expect(page).to have_selector(file_cell_selector, count: 1)
expect(find(file_cell_selector).
has_link?(@ontology.path, polymorphic_path(@ontology))).
to be(true)
end

Then(/^I should see the ontlogy next to the ontology's file$/i) do
ontology_cell_selector = 'table.file-table td.ontology ul.ontology-list'
expect(page).to have_selector(ontology_cell_selector, count: 1)
expect(find(ontology_cell_selector).
has_link?(@ontology.name, polymorphic_path(@ontology))).
to be(true)
end

Then(/^I should see no other ontologies next to the ontology's file$/i) do
ontology_cell_selector = 'table.file-table td.ontology ul.ontology-list'
expect(find(ontology_cell_selector)).
to have_selector('li', count: 1 + @ontology.children.count)
end

Then(/^I should see the ontlogy and its children next to the ontology's file$/i) do
ontology_cell_selector = 'table.file-table td.ontology ul.ontology-list'
expect(page).to have_selector(ontology_cell_selector, count: 1)
expect(find(ontology_cell_selector).
has_link?(@ontology.name, polymorphic_path(@ontology))).
to be(true)
@ontology.children.each do |child|
expect(find(ontology_cell_selector).
has_link?(child.name, polymorphic_path(child))).
to be(true)
end
end
1 change: 1 addition & 0 deletions features/support/env.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
require 'sidekiq/testing'
require 'cucumber/rails'
require 'capybara/poltergeist'
require 'cucumber/rspec/doubles'
require 'webmock'
require 'vcr'

Expand Down
16 changes: 13 additions & 3 deletions lib/hets/dg/node_evaluation_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,19 @@ def procure_child_ontology(internal_iri)
child_locid = parent_ontology.locid_for_child(internal_iri)

# find or create child-ontology by IRI
ontology = parent_ontology.children.find_with_locid(child_locid)
ontology = parent_ontology.all_children.find_with_locid(child_locid)
if ontology.nil?
options = {
name: internal_iri,
basepath: parent_ontology.basepath,
file_extension: parent_ontology.file_extension,
repository_id: parent_ontology.repository_id,
present: true,
}
ontology = SingleOntology.new(options, without_protection: true)
parent_ontology.children << ontology
ontology.save!
end
ontology.present = true
ontology.save! if ontology.changed? || !ontology.persisted?

version = ontology.versions.build
version.basepath = ontology.basepath
Expand Down Expand Up @@ -149,6 +149,16 @@ def update_ontologies_per_logic_count!(ontologies)
Logic.reset_counters(logic_id, :ontologies)
end
end

def set_all_children_inactive_except(present_ontologies)
return if parent_ontology.nil?
parent_ontology.all_children.each do |child|
unless present_ontologies.include?(child.name)
child.present = false
child.save!
end
end
end
end
end
end
7 changes: 7 additions & 0 deletions lib/hets/dg/node_evaluator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class NodeEvaluator < ConcurrentEvaluator
attr_accessor :current_element
attr_accessor :internal_iri

register :all, :start, to: :all_start
register :all, :end, to: :all_end
register :root, :start, to: :dgraph
register :ontology, :start, to: :ontology_start
Expand All @@ -22,6 +23,10 @@ def dgraph(current_element)
importer.dgnode_count = current_element['dgnodes'].to_i
end

def all_start
@node_names = []
end

def all_end
importer.versions.compact.each do |version|
version.save!
Expand All @@ -33,9 +38,11 @@ def all_end
ontology.__elasticsearch__.index_document
ontology.__elasticsearch__.update_document
end
set_all_children_inactive_except(@node_names)
end

def ontology_start(current_element)
@node_names << current_element['name']
self.internal_iri = child_name = current_element['name']

ontohub_iri = generate_ontology_iri(internal_iri, current_element)
Expand Down
9 changes: 7 additions & 2 deletions spec/factories/ontology_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
description { Faker::Lorem.paragraph }
logic { FactoryGirl.create :logic }
state { 'pending' }
present { true }

factory :done_ontology do
state { 'done' }
Expand Down Expand Up @@ -119,11 +120,17 @@

logic = FactoryGirl.create(:logic)
child_one = FactoryGirl.build(:ontology, :with_version,
parent: ontology,
basepath: ontology.basepath,
file_extension: ontology.file_extension,
logic: logic,
repository: ontology.repository)
child_one.versions.first.parent = version

child_two = FactoryGirl.build(:ontology, :with_version,
parent: ontology,
basepath: ontology.basepath,
file_extension: ontology.file_extension,
logic: logic,
repository: ontology.repository)
child_two.versions.first.parent = version
Expand All @@ -132,8 +139,6 @@
source: child_one,
target: child_two,
ontology: ontology)

ontology.children.push(child_one, child_two)
end
end

Expand Down
8 changes: 8 additions & 0 deletions spec/fixtures/ontologies/casl/test2_child_deleted.casl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
spec sp =
sort s
op f:s->s
. forall x:s . f(x) = x
then
. exists x:s . f(x) = x
with s |-> s
end
12 changes: 12 additions & 0 deletions spec/fixtures/ontologies/casl/test2_child_renamed.casl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
spec sp =
sort s
op f:s->s
. forall x:s . f(x) = x
then
. exists x:s . f(x) = x
with s |-> s
end

spec sp1_renamed =
sp hide f
end

0 comments on commit 22f8827

Please sign in to comment.