Class: GraphStarter::Asset

Inherits:
Object
  • Object
show all
Includes:
Authorizable, Neo4j::ActiveNode, Neo4j::Timestamps
Defined in:
app/models/graph_starter/asset.rb

Defined Under Namespace

Classes: SecretSauceRecommendation

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Authorizable

#set_access_levels

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'app/models/graph_starter/asset.rb', line 201

def method_missing(method_name, *args, &block)
  if [:name, :title].include?(method_name.to_sym)
    self.class.send(:define_method, method_name) do
      read_attribute(self.class.name_property)
    end

    send(method_name)
  elsif method_name.to_sym == :body
    self.class.send(:define_method, method_name) do
      read_attribute(self.class.body_property)
    end

    send(method_name)
  else
    super
  end
end

Class Method Details

.authorized_associationsObject



351
352
353
# File 'app/models/graph_starter/asset.rb', line 351

def self.authorized_associations
  associations.except(*Asset.associations.keys + [:images, :image])
end

.authorized_for(user) ⇒ Object



299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# File 'app/models/graph_starter/asset.rb', line 299

def self.authorized_for(user)
  require 'graph_starter/query_authorizer'

  if category_association
    ::GraphStarter::QueryAuthorizer.new(all(:asset).send(category_association, :category, nil, optional: true))
      .authorized_query([:asset, :category], user)
      .with('DISTINCT asset AS asset')
      .proxy_as(self, :asset)
  else
    ::GraphStarter::QueryAuthorizer.new(all(:asset))
      .authorized_query(:asset, user)
      .with('DISTINCT asset AS asset')
      .proxy_as(self, :asset)
  end
end

.authorized_properties(user) ⇒ Object



315
316
317
# File 'app/models/graph_starter/asset.rb', line 315

def self.authorized_properties(user)
  authorized_properties_query(user).pluck(:property)
end

.authorized_properties_and_levels(user) ⇒ Object



319
320
321
# File 'app/models/graph_starter/asset.rb', line 319

def self.authorized_properties_and_levels(user)
  authorized_properties_query(user).pluck(:property, :level)
end

.authorized_properties_query(user) ⇒ Object



323
324
325
326
327
328
329
330
331
332
333
334
335
# File 'app/models/graph_starter/asset.rb', line 323

def self.authorized_properties_query(user)
  query = property_name_and_uuid_and_ruby_type_query
          .merge(model: {Model: {name: name}})
          .on_create_set(model: {private: false})
          .break
          .merge('model-[:HAS_PROPERTY]->(property:Property {name: property_name})')
          .on_create_set(property: {private: false})
          .on_create_set('property.uuid = uuid, property.ruby_type = ruby_type')
          .with(:property)

  ::GraphStarter::Property # rubocop:disable Lint/Void
  QueryAuthorizer.new(query).authorized_query(:property, user)
end

.body_property(property_name = nil) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
# File 'app/models/graph_starter/asset.rb', line 153

def self.body_property(property_name = nil)
  if property_name.nil?
    body_property(default_name_property) if @body_property.nil?

    @body_property
  else
    fail "Cannot declare body_property twice" if @body_property.present?
    name = property_name.to_sym
    fail ArgumentError, "Property #{name} is not defined" if !attributes.key?(name.to_s)
    @body_property = name
  end
end

.body_property?(property_name) ⇒ Boolean

Returns:



166
167
168
# File 'app/models/graph_starter/asset.rb', line 166

def self.body_property?(property_name)
  @body_property && @body_property.to_sym == property_name.to_sym
end

.category_association(association_name = nil) ⇒ Object



71
72
73
74
75
76
77
78
79
80
# File 'app/models/graph_starter/asset.rb', line 71

def self.category_association(association_name = nil)
  if association_name.nil?
    @category_association
  else
    fail "Cannot declare category_association twice" if @category_association.present?
    name = association_name.to_sym
    fail ArgumentError, "Association #{name} is not defined" if associations[name].nil?
    @category_association = name
  end
end

.default_body_propertyObject



170
171
172
173
174
175
176
# File 'app/models/graph_starter/asset.rb', line 170

def self.default_body_property
  if @body_property.nil? && !attributes.key?('body')
    fail "No body_property defined for #{self.name}!"
  end

  body_property || 'body'
end

.default_name_propertyObject



139
140
141
142
143
144
145
# File 'app/models/graph_starter/asset.rb', line 139

def self.default_name_property
  (%w(name title) & attributes.keys)[0].tap do |property|
    if property.nil?
      fail "No name_property defined for #{self.name}!"
    end
  end
end

.descendantsObject



286
287
288
289
# File 'app/models/graph_starter/asset.rb', line 286

def self.descendants
  Rails.application.eager_load! if Rails.env == 'development'
  Neo4j::ActiveNode::Labels._wrapped_classes.select { |klass| klass < self }
end

.display_properties(*property_names) ⇒ Object



179
180
181
182
183
184
185
# File 'app/models/graph_starter/asset.rb', line 179

def self.display_properties(*property_names)
  if property_names.empty?
    @display_properties
  else
    @display_properties = property_names.map(&:to_sym)
  end
end

.display_property?(property_name) ⇒ Boolean

Returns:



187
188
189
# File 'app/models/graph_starter/asset.rb', line 187

def self.display_property?(property_name)
  display_properties.nil? || display_properties.include?(property_name.to_sym)
end

.enumerable_property(property_name, values) ⇒ Object



91
92
93
94
95
96
97
98
# File 'app/models/graph_starter/asset.rb', line 91

def self.enumerable_property(property_name, values)
  fail "values needs to be an Array, was #{values.inspect}" if !values.is_a?(Array)

  validates :status, inclusion: {in: values}

  enumerable_property_values[self.name.to_sym] ||= {}
  enumerable_property_values[self.name.to_sym][property_name.to_sym] ||= values
end

.enumerable_property_valuesObject



105
106
107
# File 'app/models/graph_starter/asset.rb', line 105

def self.enumerable_property_values
  @enumerable_property_values ||= {}
end

.enumerable_property_values_for(property_name) ⇒ Object



100
101
102
103
# File 'app/models/graph_starter/asset.rb', line 100

def self.enumerable_property_values_for(property_name)
  enumerable_property_values[self.name.to_sym] && 
    enumerable_property_values[self.name.to_sym][property_name.to_sym]
end

.for_query(query) ⇒ Object



228
229
230
231
232
233
234
235
236
# File 'app/models/graph_starter/asset.rb', line 228

def self.for_query(query)
  where_clause = self.search_properties.map do |property|
    fail "Invalid property: #{property}" if attributes[property].nil?
    "asset.#{property} =~ {query}"
  end.join(' OR ')

  query_string = query.strip.gsub(/\s+/, '.*')
  all(:asset).where(where_clause).params(query: "(?i).*#{query_string}.*")
end

.has_imageObject



30
31
32
33
# File 'app/models/graph_starter/asset.rb', line 30

def self.has_image
  @has_image = true
  has_one :out, :image, type: :HAS_IMAGE, model_class: '::GraphStarter::Image'
end

.has_image?Boolean

Returns:



39
40
41
# File 'app/models/graph_starter/asset.rb', line 39

def self.has_image?
  !!@has_image
end

.has_imagesObject



25
26
27
28
# File 'app/models/graph_starter/asset.rb', line 25

def self.has_images
  @has_images = true
  has_many :out, :images, type: :HAS_IMAGE, model_class: '::GraphStarter::Image'
end

.has_images?Boolean

Returns:



35
36
37
# File 'app/models/graph_starter/asset.rb', line 35

def self.has_images?
  !!@has_images
end

.icon_classObject



355
356
357
# File 'app/models/graph_starter/asset.rb', line 355

def self.icon_class
  GraphStarter.configuration.icon_classes[self.name.to_sym]
end

.image_associationObject



43
44
45
46
47
48
49
# File 'app/models/graph_starter/asset.rb', line 43

def self.image_association
  if has_images?
    :images
  elsif has_image?
    :image
  end
end

.model_slugObject



291
292
293
# File 'app/models/graph_starter/asset.rb', line 291

def self.model_slug
  name.tableize
end

.name_property(property_name = nil) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'app/models/graph_starter/asset.rb', line 119

def self.name_property(property_name = nil)
  if property_name.nil?
    name_property(default_name_property) if @name_property.nil?

    @name_property
  else
    fail "Cannot declare name_property twice" if @name_property.present?
    name = property_name.to_sym
    fail ArgumentError, "Property #{name} is not defined" if !attributes.key?(name.to_s)
    @name_property = name

    validates name, presence: true
    index name
  end
end

.name_property?(property_name) ⇒ Boolean

Returns:



135
136
137
# File 'app/models/graph_starter/asset.rb', line 135

def self.name_property?(property_name)
  @name_property && @name_property.to_sym == property_name.to_sym
end

.propertiesObject



295
296
297
# File 'app/models/graph_starter/asset.rb', line 295

def self.properties
  attributes.keys - Asset.attributes.keys
end

.property_name_and_uuid_and_ruby_type_queryObject



337
338
339
340
341
342
343
344
345
346
347
348
349
# File 'app/models/graph_starter/asset.rb', line 337

def self.property_name_and_uuid_and_ruby_type_query
  properties_and_uuids_and_ruby_types = properties.map do |property|
    type = self.attributes[property][:type]
    type = type.name if type.is_a?(Class)
    [property, SecureRandom.uuid, type]
  end

  Neo4j::Session.current.query
    .with('{array} AS array')
    .unwind('array AS row')
    .params(array: properties_and_uuids_and_ruby_types)
    .with('row[0] AS property_name, row[1] AS uuid, row[2] AS ruby_type')
end

.ratedObject



110
111
112
# File 'app/models/graph_starter/asset.rb', line 110

def self.rated
  @rated = true
end

.rated?Boolean

Returns:



114
115
116
# File 'app/models/graph_starter/asset.rb', line 114

def self.rated?
  !!@rated
end

.search_properties(*array) ⇒ Object



220
221
222
223
224
225
226
# File 'app/models/graph_starter/asset.rb', line 220

def self.search_properties(*array)
  if array.empty?
    @search_properties || [name_property]
  else
    @search_properties = array
  end
end

Instance Method Details

#as_json(_options = {}) ⇒ Object



262
263
264
265
266
267
268
269
270
271
272
# File 'app/models/graph_starter/asset.rb', line 262

def as_json(_options = {})
  {self.class.model_slug =>
    {id: id,
     title: title,
     name: title,
     model_slug: self.class.model_slug}
   }.tap do |result|
     result[:images] = images.map {|image| image.source.url } if self.class.has_images?
     result[:image] = image.source_url if self.class.has_image? && image
   end
end

#categoriesObject



82
83
84
85
86
87
88
# File 'app/models/graph_starter/asset.rb', line 82

def categories
  if self.class.category_association
    send(self.class.category_association)
  else
    []
  end
end

#first_imageObject



51
52
53
54
55
56
57
# File 'app/models/graph_starter/asset.rb', line 51

def first_image
  if self.class.has_images?
    images.first
  elsif self.class.has_image?
    image
  end
end

#first_image_source_urlObject



67
68
69
# File 'app/models/graph_starter/asset.rb', line 67

def first_image_source_url
  first_image && first_image.source_url
end

#image_arrayObject



59
60
61
62
63
64
65
# File 'app/models/graph_starter/asset.rb', line 59

def image_array
  if self.class.has_images?
    images.to_a
  elsif self.class.has_image?
    [image].compact
  end
end

#rating_for(user) ⇒ Object



197
198
199
# File 'app/models/graph_starter/asset.rb', line 197

def rating_for(user)
  rated_by_user(nil, :rating).where(uuid: user.uuid).pluck(:rating)[0]
end

#rating_level_for(user) ⇒ Object



192
193
194
195
# File 'app/models/graph_starter/asset.rb', line 192

def rating_level_for(user)
  rating = rating_for(user)
  rating && rating.level
end

#safe_titleObject



147
148
149
150
151
# File 'app/models/graph_starter/asset.rb', line 147

def safe_title
  sanitizer = Rails::Html::WhiteListSanitizer.new

  sanitizer.sanitize(title, tags: %w(b em i strong))
end

#secret_sauce_recommendationsObject



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'app/models/graph_starter/asset.rb', line 238

def secret_sauce_recommendations
  user_class = GraphStarter.configuration.user_class
  return [] if user_class.nil? # Should fix this later

  user_class = (user_class.is_a?(Class) ? user_class : user_class.to_s.constantize)
  user_label = user_class.mapped_label_name

  query_as(:source)
    .match('source-[:HAS_CATEGORY]->(category:Category)<-[:HAS_CATEGORY]-(asset:Asset)')
    .break
    .optional_match("source<-[:CREATED]-(creator:#{user_label})-[:CREATED]->asset")
    .break
    .optional_match("source<-[:VIEWED]-(viewer:#{user_label})-[:VIEWED]->asset")
    .limit(5)
    .order('score DESC')
    .pluck(
      :asset,
      '(count(category) * 2) +
       (count(creator) * 4) +
       (count(viewer) * 0.1) AS score').map do |other_asset, score|
    SecretSauceRecommendation.new(other_asset, score)
  end
end

#total_view_countObject



278
279
280
# File 'app/models/graph_starter/asset.rb', line 278

def total_view_count
  views.map(&:count).sum
end

#unique_view_countObject



282
283
284
# File 'app/models/graph_starter/asset.rb', line 282

def unique_view_count
  views.size
end

#viewsObject



274
275
276
# File 'app/models/graph_starter/asset.rb', line 274

def views
  @views ||= viewer_sessions.rels
end