Class: Note::Base

Inherits:
ApplicationRecord
  • Object
show all
Defined in:
app/models/note/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

Class Method Details

.by_language(lang_code) ⇒ Object



65
66
67
68
69
70
71
72
73
# File 'app/models/note/base.rb', line 65

def self.by_language(lang_code)
  lang_code = Array.wrap(lang_code).flatten.compact

  if lang_code.none? || lang_code.include?(nil)
    where(arel_table[:language].eq(nil).or(arel_table[:language].in(lang_code)))
  else
    where(language: lang_code)
  end
end

.by_owner(owner) ⇒ Object



91
92
93
94
95
96
97
98
99
# File 'app/models/note/base.rb', line 91

def self.by_owner(owner)
  if owner.is_a?(Label::Base)
    for_labels.where(owner_id: owner.id)
  elsif owner.is_a?(Concept::Base)
    for_concepts.where(owner_id: owner.id)
  else
    raise "Note::Base.by_owner: Unknown owner (#{owner.inspect})"
  end
end

.by_owner_type(klass) ⇒ Object



79
80
81
# File 'app/models/note/base.rb', line 79

def self.by_owner_type(klass)
  where(owner_type: klass.is_a?(ActiveRecord::Base) ? klass.name : klass)
end

.by_query_value(query) ⇒ Object



75
76
77
# File 'app/models/note/base.rb', line 75

def self.by_query_value(query)
  where(["#{table_name}.value ILIKE ?", query.mb_chars.downcase.to_s])
end

.edit_partial_name(obj) ⇒ Object



133
134
135
# File 'app/models/note/base.rb', line 133

def self.edit_partial_name(obj)
  'partials/note/edit_base'
end

.for_conceptsObject



83
84
85
# File 'app/models/note/base.rb', line 83

def self.for_concepts
  where(owner_type: 'Concept::Base')
end

.for_labelsObject



87
88
89
# File 'app/models/note/base.rb', line 87

def self.for_labels
  where(owner_type: 'Label::Base')
end

.partial_name(obj) ⇒ Object



129
130
131
# File 'app/models/note/base.rb', line 129

def self.partial_name(obj)
  'partials/note/base'
end

.search_result_partial_nameObject



179
180
181
# File 'app/models/note/base.rb', line 179

def self.search_result_partial_name
  'partials/note/search_result'
end

.single_query(params = {}) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'app/models/note/base.rb', line 137

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

  scope = by_query_value(query_str).
          by_language(params[:languages].to_a)

  case params[:for]
  when 'concept'
    scope = scope.where('concepts.type' => Iqvoc::Concept.base_class_name)
                 .includes(:concept)
                 .references(:concepts)
    owner = :concept
  when 'collection'
    scope = scope.where('concepts.type' => Iqvoc::Collection.base_class_name)
                 .includes(:concept)
                 .references(:concepts)
    owner = :collection
  else
    # no additional conditions
    scope
  end

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

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

.view_section(obj) ⇒ Object



121
122
123
# File 'app/models/note/base.rb', line 121

def self.view_section(obj)
  'notes'
end

.view_section_sort_key(obj) ⇒ Object



125
126
127
# File 'app/models/note/base.rb', line 125

def self.view_section_sort_key(obj)
  100
end

Instance Method Details

#<=>(other) ⇒ Object

********** Methods



103
104
105
# File 'app/models/note/base.rb', line 103

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

#build_search_result_rdf(document, result) ⇒ Object



183
184
185
186
# File 'app/models/note/base.rb', line 183

def build_search_result_rdf(document, result)
  result.Sdc::link(IqRdf.build_uri(owner.origin))
  build_rdf(document, result)
end

#from_annotation_list!(str) ⇒ Object

TODO: This should move to umt because the “list” is more or less proprietary



108
109
110
111
112
113
114
115
# File 'app/models/note/base.rb', line 108

def from_annotation_list!(str)
  str.gsub(/\[|\]/, '').split('; ').map { |a| a.split(' ') }.each do |annotation|
    namespace, predicate = annotation.first.split(':', 2)
    annotations << Note::Annotated::Base.new(value: annotation.second,
        namespace: namespace, predicate: predicate)
  end
  self
end

#to_sObject



117
118
119
# File 'app/models/note/base.rb', line 117

def to_s
  "#{self.value}"
end