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



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'app/models/graph_starter/asset.rb', line 231

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
    if self.class.body_property
      self.class.send(:define_method, method_name) do
        read_attribute(self.class.body_property)
      end

      send(method_name)
    end
  else
    super
  end
end

Class Method Details

.authorized_associationsObject



398
399
400
# File 'app/models/graph_starter/asset.rb', line 398

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

.authorized_for(user) ⇒ Object



336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# File 'app/models/graph_starter/asset.rb', line 336

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

  query, associations = if category_associations.size > 0
                          query = all(:asset).query

                          category_associations

                          relationship_types_cypher = category_associations.map {|name| self.associations[name].relationship_type }.join('|:')

                          query = query.optional_match("(asset)-[:#{relationship_types_cypher}]-(category:Asset)")

                          [query,
                           [:asset, :category]]
                        else
                          [all(:asset),
                           :asset]
                        end

  ::GraphStarter::QueryAuthorizer.new(query, asset: GraphStarter.configuration.scope_filters[self.name.to_sym])
    .authorized_query(associations, user)
    .with('DISTINCT asset AS asset, level')
    .break
    .proxy_as(self, :asset)
end

.authorized_properties(user) ⇒ Object



362
363
364
# File 'app/models/graph_starter/asset.rb', line 362

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

.authorized_properties_and_levels(user) ⇒ Object



366
367
368
# File 'app/models/graph_starter/asset.rb', line 366

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

.authorized_properties_query(user) ⇒ Object



370
371
372
373
374
375
376
377
378
379
380
381
382
# File 'app/models/graph_starter/asset.rb', line 370

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



185
186
187
188
189
190
191
192
193
194
# File 'app/models/graph_starter/asset.rb', line 185

def self.body_property(property_name = nil)
  if property_name.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:

  • (Boolean)


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

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

.category_associations(*association_names) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
# File 'app/models/graph_starter/asset.rb', line 94

def self.category_associations(*association_names)
  if association_names.empty?
    @category_associations || []
  else
    fail "Cannot declare category_associations twice" if @category_associations.present?
    names = association_names.map(&:to_sym)
    bad_names = names.select {|name| associations[name].nil? }
    fail ArgumentError, "Associations #{bad_names.join(', ')} is not defined" if !bad_names.empty?

    @category_associations = names
  end
end

.default_body_propertyObject



200
201
202
203
204
205
206
# File 'app/models/graph_starter/asset.rb', line 200

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

  @body_property
end

.default_name_propertyObject



166
167
168
169
170
171
172
# File 'app/models/graph_starter/asset.rb', line 166

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



323
324
325
326
# File 'app/models/graph_starter/asset.rb', line 323

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



209
210
211
212
213
214
215
# File 'app/models/graph_starter/asset.rb', line 209

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:

  • (Boolean)


217
218
219
# File 'app/models/graph_starter/asset.rb', line 217

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

.enumerable_property(property_name, values) ⇒ Object



118
119
120
121
122
123
124
125
# File 'app/models/graph_starter/asset.rb', line 118

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

  validates property_name.to_sym, 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



132
133
134
# File 'app/models/graph_starter/asset.rb', line 132

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

.enumerable_property_values_for(property_name) ⇒ Object



127
128
129
130
# File 'app/models/graph_starter/asset.rb', line 127

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



260
261
262
263
264
265
266
267
268
# File 'app/models/graph_starter/asset.rb', line 260

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



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

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

.has_image?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'app/models/graph_starter/asset.rb', line 62

def self.has_image?
  !!@has_image
end

.has_imagesObject



48
49
50
51
# File 'app/models/graph_starter/asset.rb', line 48

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

.has_images?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'app/models/graph_starter/asset.rb', line 58

def self.has_images?
  !!@has_images
end

.icon_classObject



402
403
404
# File 'app/models/graph_starter/asset.rb', line 402

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

.image_associationObject



66
67
68
69
70
71
72
# File 'app/models/graph_starter/asset.rb', line 66

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

.model_slugObject



328
329
330
# File 'app/models/graph_starter/asset.rb', line 328

def self.model_slug
  name.tableize
end

.name_property(property_name = nil) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'app/models/graph_starter/asset.rb', line 146

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:

  • (Boolean)


162
163
164
# File 'app/models/graph_starter/asset.rb', line 162

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

.propertiesObject



332
333
334
# File 'app/models/graph_starter/asset.rb', line 332

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

.property_name_and_uuid_and_ruby_type_queryObject



384
385
386
387
388
389
390
391
392
393
394
395
396
# File 'app/models/graph_starter/asset.rb', line 384

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



137
138
139
# File 'app/models/graph_starter/asset.rb', line 137

def self.rated
  @rated = true
end

.rated?Boolean

Returns:

  • (Boolean)


141
142
143
# File 'app/models/graph_starter/asset.rb', line 141

def self.rated?
  !!@rated
end

.sanitize_title(title) ⇒ Object



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

def self.sanitize_title(title)
  sanitizer = Rails::Html::WhiteListSanitizer.new

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

.search_properties(*array) ⇒ Object



252
253
254
255
256
257
258
# File 'app/models/graph_starter/asset.rb', line 252

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

.unique_slug_from(string) ⇒ Object



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

def self.unique_slug_from(string)
  base = string.to_slug.normalize.to_s
end

Instance Method Details

#as_json(options = {}) ⇒ Object



294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'app/models/graph_starter/asset.rb', line 294

def as_json(options = {})
  data = {
    id: id,
    title: title,
    name: title,
    model_slug: self.class.model_slug,
    summary: summary,
    categories: categories
  }.tap do |result|
    result[:image_urls] = image_array.map(&:source_url) if image_array
    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

  options[:root] ? {self.class.model_slug.singularize => data} : data
end

#categoriesObject



107
108
109
110
111
112
113
114
115
# File 'app/models/graph_starter/asset.rb', line 107

def categories
  if self.class.category_associations
    self.class.category_associations.flat_map do |category_association|
      send(category_association)
    end.compact
  else
    []
  end
end

#first_imageObject



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

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

#first_image_source_urlObject



90
91
92
# File 'app/models/graph_starter/asset.rb', line 90

def first_image_source_url
  first_image && first_image.source_url
end

#image_arrayObject



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

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

#place_slugObject

This is doing something strange, commenting out for now… def self.inherited(subclass)

subclass.property :slug
subclass.before_validation :place_slug
subclass.validates :slug, presence: true
subclass.constraint :slug, type: :unique

end



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

def place_slug
  return if self.slug.present?

  name_value = read_attribute(self.class.name_property)
  self.slug = self.class.unique_slug_from(name_value)
    name_value.to_slug.normalize.to_s if name_value
end

#rating_for(user) ⇒ Object



227
228
229
# File 'app/models/graph_starter/asset.rb', line 227

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

#rating_level_for(user) ⇒ Object



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

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

#safe_titleObject



174
175
176
# File 'app/models/graph_starter/asset.rb', line 174

def safe_title
  self.class.sanitize_title(title)
end

#secret_sauce_recommendationsObject



270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'app/models/graph_starter/asset.rb', line 270

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



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

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

#unique_view_countObject



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

def unique_view_count
  views.size
end

#viewsObject



311
312
313
# File 'app/models/graph_starter/asset.rb', line 311

def views
  @views ||= viewer_sessions.rels
end