Class: Collection::Base

Inherits:
Concept::Base show all
Defined in:
app/models/collection/base.rb

Overview

Copyright 2011-2013 innoQ Deutschland GmbH

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Direct Known Subclasses

SKOS::Base

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Concept::Base

#associated_objects_in_editing_mode, broader_tops, #concept_relations_by_id, #concept_relations_by_id=, #concept_relations_by_id_and_rank, edit_link_partial_name, for_dashboard, inline_partial_name, #invalid_with_full_validation?, #labelings_by_text, #labelings_by_text=, #labels_for_labeling_class_and_language, #matches_for_class, new_link_partial_name, #notations_for_class, #notes_for_class, #pref_label, #related_concepts_for_relation_class, #save_with_full_validation!, #to_s, #valid_with_full_validation?, with_associations, with_pref_labels

Methods included from Concept::Validations

#distinct_versions, #exclusive_top_term, #rooted_top_terms, #unique_pref_label, #unique_pref_label_language, #valid_rank_for_ranked_relations

Methods included from Iqvoc::Versioning

#branch, #editor_selectable?, #in_review?, #lock_by_user, #locked?, #publish, #published?, #state, #to_review, #unlock, #unpublish

Class Method Details

.by_label_value(val) ⇒ Object



49
50
51
# File 'app/models/collection/base.rb', line 49

def self.by_label_value(val)
  includes(:labels).merge(Label::Base.by_query_value(val))
end

.by_origin(origin) ⇒ Object

********** Scopes



45
46
47
# File 'app/models/collection/base.rb', line 45

def self.by_origin(origin)
  where(:origin => origin)
end

.by_parent_id(parent_id) ⇒ Object



58
59
60
61
# File 'app/models/collection/base.rb', line 58

def self.by_parent_id(parent_id)
  includes(:parent_collection_members).
      where(Collection::Member::Base.arel_table[:collection_id].eq(parent_id))
end

.topsObject



53
54
55
56
# File 'app/models/collection/base.rb', line 53

def self.tops
  includes(:parent_collection_members).
      where("#{Collection::Member::Base.table_name}.target_id IS NULL")
end

Instance Method Details

#additional_infoObject



77
78
79
# File 'app/models/collection/base.rb', line 77

def additional_info
  concepts.count
end

#build_rdf_subject(&block) ⇒ Object



89
90
91
# File 'app/models/collection/base.rb', line 89

def build_rdf_subject(&block)
  IqRdf::Coll::build_uri(self.origin, IqRdf::Skos::build_uri("Collection"), &block)
end

#circular_subcollectionsObject

This only prevent circles of length 2. TODO: This should be a real circle detector (but still performant) or be removed (seems to me like the better idea).



165
166
167
168
169
170
171
172
# File 'app/models/collection/base.rb', line 165

def circular_subcollections
  Iqvoc::Collection.base_class.by_origin(@member_collection_origins).includes(:members => :target).each do |subcollection|
    if subcollection.subcollections.include?(self)
      errors.add(:base,
        I18n.t("txt.controllers.collections.circular_error", :label => subcollection.pref_label))
    end
  end
end

#conceptsObject



73
74
75
# File 'app/models/collection/base.rb', line 73

def concepts
  members.map(&:target).select { |m| !m.is_a?(::Collection::Base) }
end

#inline_member_collection_originsObject



115
116
117
118
# File 'app/models/collection/base.rb', line 115

def inline_member_collection_origins
  @member_collection_origins || collections.
      map { |m| m.origin }.uniq
end

#inline_member_collection_origins=(origins) ⇒ Object



110
111
112
113
# File 'app/models/collection/base.rb', line 110

def inline_member_collection_origins=(origins)
  @member_collection_origins = origins.to_s.
      split(Iqvoc::InlineDataHelper::SPLITTER).map(&:strip)
end

#inline_member_collectionsObject



120
121
122
123
124
125
126
# File 'app/models/collection/base.rb', line 120

def inline_member_collections
  if @member_collection_origins
    Collection::Base.where(:origin => @member_collection_origins)
  else
    subcollections
  end
end

#inline_member_concept_originsObject



98
99
100
# File 'app/models/collection/base.rb', line 98

def inline_member_concept_origins
  @member_concept_origins || concepts.map { |m| m.origin }.uniq
end

#inline_member_concept_origins=(origins) ⇒ Object



93
94
95
96
# File 'app/models/collection/base.rb', line 93

def inline_member_concept_origins=(origins)
  @member_concept_origins = origins.to_s.
      split(Iqvoc::InlineDataHelper::SPLITTER).map(&:strip)
end

#inline_member_conceptsObject



102
103
104
105
106
107
108
# File 'app/models/collection/base.rb', line 102

def inline_member_concepts
  if @member_concept_origins
    Concept::Base.editor_selectable.where(:origin => @member_concept_origins)
  else
    concepts.select{|c| c.editor_selectable?}
  end
end

#labelObject



85
86
87
# File 'app/models/collection/base.rb', line 85

def label
  pref_label
end

#pref_label_in_primary_thesaurus_languageObject



174
175
176
177
178
179
180
181
# File 'app/models/collection/base.rb', line 174

def pref_label_in_primary_thesaurus_language
  labels = self.send(Iqvoc::Concept.pref_labeling_class_name.to_relation_name).map(&:target).select{|l| l.published?}
  if labels.count == 0
    errors.add :base, I18n.t("txt.models.concept.no_pref_label_error")
  elsif not labels.map(&:language).map(&:to_s).include?(Iqvoc::Concept.pref_labeling_languages.first.to_s)
    errors.add :base, I18n.t("txt.models.concept.main_pref_label_language_missing_error")
  end
end

#regenerate_collection_membersObject



156
157
158
# File 'app/models/collection/base.rb', line 156

def regenerate_collection_members
  regenerate_members(Collection::Base, @member_collection_origins)
end

#regenerate_concept_membersObject



152
153
154
# File 'app/models/collection/base.rb', line 152

def regenerate_concept_members
  regenerate_members(Concept::Base, @member_concept_origins)
end

#regenerate_members(target_class, target_origins) ⇒ Object

********** Hook methods



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'app/models/collection/base.rb', line 130

def regenerate_members(target_class, target_origins)
  return if target_origins.nil? # There is nothing to do
  existing = self.members.includes(:target)
  existing = if target_class <= Collection::Base
    existing.select { |m| m.target.is_a?(Collection::Base) }
  else
    existing.reject { |m| m.target.is_a?(Collection::Base) }
  end
  new = []
  target_origins.each do |new_origin|
    member = existing.find{ |m| m.target.origin == new_origin }
    unless member
      c = target_class.by_origin(new_origin).first
      member = Iqvoc::Collection.member_class.create(:collection => self, :target => c) if c
    end
    new << member if member
  end
  (existing - new).each do |m|
    m.destroy
  end
end

#subcollectionsObject

********** Methods



69
70
71
# File 'app/models/collection/base.rb', line 69

def subcollections
  members.map(&:target).select { |m| m.is_a?(::Collection::Base) }
end

#to_paramObject



81
82
83
# File 'app/models/collection/base.rb', line 81

def to_param
  origin
end