Class: Label::SKOSXL::Base

Inherits:
Base
  • Object
show all
Includes:
FirstLevelObjectValidations, Validations, Versioning
Defined in:
app/models/label/skosxl/base.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Validations

#origin_has_to_be_escaped, #pref_label_language, #referenced_published_concepts_have_main_language_pref_label

Class Method Details

.by_origin(origin) ⇒ Object

********** Scopes



138
139
140
# File 'app/models/label/skosxl/base.rb', line 138

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

.dashboard_pathObject

********** Class Methods



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

def self.dashboard_path
  'label_dashboard_path'
end


262
263
264
# File 'app/models/label/skosxl/base.rb', line 262

def self.edit_link_partial_name
  'partials/label/skosxl/edit_link_base'
end

.for_dashboardObject



146
147
148
# File 'app/models/label/skosxl/base.rb', line 146

def self.for_dashboard
  unpublished_or_follow_up
end

.inline_partial_nameObject



254
255
256
# File 'app/models/label/skosxl/base.rb', line 254

def self.inline_partial_name
  'partials/label/skosxl/inline_base'
end


258
259
260
# File 'app/models/label/skosxl/base.rb', line 258

def self.new_link_partial_name
  'partials/label/skosxl/new_link_base'
end

.search_result_partial_nameObject



250
251
252
# File 'app/models/label/skosxl/base.rb', line 250

def self.search_result_partial_name
  'partials/label/skosxl/search_result'
end

.single_query(params = {}) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'app/models/label/skosxl/base.rb', line 178

def self.single_query(params = {})
  query_str = build_query_string(params)

  scope = self.by_query_value(query_str)
              .by_language(params[:languages].to_a)
              .includes(:concepts)
              .references(:concepts)
              .published
              .order(Arel.sql("LENGTH(#{Label::Base.table_name}.value)"))

  if params[:collection_origin].present?
    collection = Collection::Base.where(origin: params[:collection_origin]).last
    if collection
      scope = scope.includes(concepts: [ collections: { collection_members: :collection } ])
      scope = scope.where("#{Collection::Member::Base.table_name}.collection_id" => collection.id)
    else
      raise "Collection with Origin #{params[:collection_origin]} not found!"
    end
  end

  # apply search entity type
  case params[:for]
  when 'concept'
    scope = scope.includes(:concepts).merge(Iqvoc::Concept.base_class.published)
  when 'collection'
    scope = scope.includes(:concepts).merge(Iqvoc::Collection.base_class.published)
  end


  # change note filtering
  if params[:change_note_date_from].present? || params[:change_note_date_to].present?
    change_note_relation = Iqvoc.change_note_class_name.to_relation_name

    scope = scope.includes(change_note_relation.to_sym => :annotations)
                 .references(change_note_relation)
                 .references('note_annotations')


    # change note type filtering
    scope = case params[:change_note_type]
            when 'created'
              scope.where('note_annotations.predicate = ?', 'created')
            when 'modified'
              scope.where('note_annotations.predicate = ?', 'modified')
            else
              scope.where('note_annotations.predicate = ? OR note_annotations.predicate = ?', 'created', 'modified')
            end

    if params[:change_note_date_from].present?
      begin
        DateTime.parse(params[:change_note_date_from])
        date_from = params[:change_note_date_from]
        scope = scope.where('note_annotations.value >= ?', date_from)
      rescue ArgumentError
        Rails.logger.error "Invalid date was entered for search"
      end
    end

    if params[:change_note_date_to].present?
      begin
        date_to = DateTime.parse(params[:change_note_date_to]).end_of_day.to_s
        scope = scope.where('note_annotations.value <= ?', date_to)
      rescue ArgumentError
        Rails.logger.error "Invalid date was entered for search"
      end
    end
  end

  scope = yield(scope) if block_given?
  scope.map { |result| SearchResult.new(result) }
end

.with_associationsObject



142
143
144
# File 'app/models/label/skosxl/base.rb', line 142

def self.with_associations
  includes(labelings: :owner)
end

Instance Method Details

#<=>(other) ⇒ Object

********** Methods



162
163
164
# File 'app/models/label/skosxl/base.rb', line 162

def <=>(other)
  self.to_s.downcase <=> other.to_s.downcase
end

#==(other) ⇒ Object



166
167
168
# File 'app/models/label/skosxl/base.rb', line 166

def ==(other)
  language == other.try(:language) && value == other.try(:value) && rev == other.try(:rev)
end

#associated_objects_in_editing_modeObject

Responsible for displaying a warning message about associated objects which are currently edited y another user.



324
325
326
327
328
# File 'app/models/label/skosxl/base.rb', line 324

def associated_objects_in_editing_mode
  {
    label_relations: Label::Relation::Base.by_domain(id).range_in_edit_mode
  }
end

#build_initial_change_note(user) ⇒ Object

initial created-ChangeNote creation



347
348
349
350
351
352
353
354
355
356
357
# File 'app/models/label/skosxl/base.rb', line 347

def build_initial_change_note(user)
  send(Iqvoc::change_note_class_name.to_relation_name).new do |change_note|
    change_note.value = I18n.t('txt.views.versioning.initial_version')
    change_note.language = I18n.locale.to_s
    change_note.position = 1
    change_note.annotations_attributes = [
      { namespace: 'dct', predicate: 'creator', value: user.name },
      { namespace: 'dct', predicate: 'created', value: DateTime.now.to_s }
    ]
  end
end

#build_notesObject



359
360
361
362
363
# File 'app/models/label/skosxl/base.rb', line 359

def build_notes
  Iqvoc::XLLabel.note_class_names.each do |note_class_name|
    send(note_class_name.to_relation_name).build if send(note_class_name.to_relation_name).empty?
  end
end

#build_rdf_subject(&block) ⇒ Object



276
277
278
279
280
# File 'app/models/label/skosxl/base.rb', line 276

def build_rdf_subject(&block)
  ns = IqRdf::Namespace.find_namespace_class(self.rdf_namespace.to_sym)
  raise "Namespace '#{rdf_namespace}' is not defined in IqRdf document." unless ns
  IqRdf.build_uri(self.origin, ns.build_uri(self.rdf_class), &block)
end

#class_pathObject



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

def class_path
  'label_path'
end

#concepts_for_labeling_class(labeling_class) ⇒ Object



282
283
284
285
# File 'app/models/label/skosxl/base.rb', line 282

def concepts_for_labeling_class(labeling_class)
  labeling_class = labeling_class.name if labeling_class < ApplicationRecord # Use the class name string
  labelings.select{ |l| l.class.name == labeling_class.to_s }.map(&:owner)
end

#duplicate(user) ⇒ Object



330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# File 'app/models/label/skosxl/base.rb', line 330

def duplicate(user)
  clone = deep_clone(except: [:origin, :rev, :published_version_id, :published_at, :expired_at, :to_review], include: [:labelings])

  clone.origin = Origin.new.to_s
  clone.value += " [#{I18n.t('txt.models.label.copy')}]"

  clone.labelings.select { |l| l.type == "Labeling::SKOSXL::PrefLabel" }.each do |l|
    clone.labelings.delete(l)
    clone.labelings.append Labeling::SKOSXL::AltLabel.new(owner_id: l.owner_id)
  end

  clone.build_initial_change_note(user)
  clone.save!
  clone
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


170
171
172
# File 'app/models/label/skosxl/base.rb', line 170

def eql?(other)
  self == other
end

#from_rdf(str) ⇒ Object



299
300
301
302
303
304
# File 'app/models/label/skosxl/base.rb', line 299

def from_rdf(str)
  raise 'invalid rdf literal' unless str =~ /^"(.+)"(@(.+))$/
  self.value = $1
  self.language = $3
  self
end

#from_rdf!(str) ⇒ Object



306
307
308
309
# File 'app/models/label/skosxl/base.rb', line 306

def from_rdf!(str)
  from_rdf(str)
  save(validate: false)
end

#has_concept_or_label_relations?Boolean

Returns:

  • (Boolean)


315
316
317
318
319
320
# File 'app/models/label/skosxl/base.rb', line 315

def has_concept_or_label_relations?
  # Check if one of the additional association methods return elements
  Iqvoc::XLLabel.additional_association_classes.each do |association_class, foreign_key|
    return true if send(association_class.name.to_relation_name).count > 0
  end
end

#hashObject



174
175
176
# File 'app/models/label/skosxl/base.rb', line 174

def hash
  [value, language, rev].hash
end

#notes_for_class(note_class) ⇒ Object



266
267
268
269
# File 'app/models/label/skosxl/base.rb', line 266

def notes_for_class(note_class)
  note_class = note_class.name if note_class < ApplicationRecord # Use the class name string
  notes.select{ |note| note.class.name == note_class }
end


287
288
289
290
291
292
# File 'app/models/label/skosxl/base.rb', line 287

def related_labels_for_relation_class(relation_class, only_published = true)
  relation_class = relation_class.name if relation_class < ApplicationRecord # Use the class name string
  relations.select { |rel| rel.class.name == relation_class }
           .map(&:range)
           .select { |l| l.published? || !only_published }
end

#relations_for_class(relation_class) ⇒ Object



271
272
273
274
# File 'app/models/label/skosxl/base.rb', line 271

def relations_for_class(relation_class)
  relation_class = relation_class.name if relation_class < ApplicationRecord # Use the class name string
  relations.select{ |rel| rel.class.name == relation_class }
end

#to_paramObject



311
312
313
# File 'app/models/label/skosxl/base.rb', line 311

def to_param
  origin
end