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

Constant Summary collapse

IMAGE_MODELS =
[]

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Authorizable

#set_access_levels

Class Method Details

.authorized_associationsObject



153
154
155
# File 'app/models/graph_starter/asset.rb', line 153

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

.authorized_for(user) ⇒ Object



110
111
112
113
114
115
116
117
# File 'app/models/graph_starter/asset.rb', line 110

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

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

.authorized_properties(user) ⇒ Object



119
120
121
# File 'app/models/graph_starter/asset.rb', line 119

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

.authorized_properties_and_levels(user) ⇒ Object



123
124
125
# File 'app/models/graph_starter/asset.rb', line 123

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

.authorized_properties_query(user) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'app/models/graph_starter/asset.rb', line 127

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

.category_associationObject



44
45
46
# File 'app/models/graph_starter/asset.rb', line 44

def self.category_association
  @category_association
end

.category_association=(association_name) ⇒ Object



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

def self.category_association=(association_name)
  @category_association = association_name
end

.descendantsObject



97
98
99
100
# File 'app/models/graph_starter/asset.rb', line 97

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

.for_query(query) ⇒ Object



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

def self.for_query(query)
  all.where(title: /.*#{query}.*/i)
end

.has_imagesObject



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

def self.has_images
  GraphStarter::Asset::IMAGE_MODELS << self
  has_many :out, :images, type: :HAS_IMAGE, model_class: '::GraphStarter::Image'
end

.has_images?Boolean

Returns:

  • (Boolean)


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

def self.has_images?
  GraphStarter::Asset::IMAGE_MODELS.include?(self)
end

.icon_classObject



157
158
159
# File 'app/models/graph_starter/asset.rb', line 157

def self.icon_class
  'bookmark'
end

.model_slugObject



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

def self.model_slug
  name.tableize
end

.propertiesObject



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

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

.property_name_and_uuid_and_ruby_type_queryObject



141
142
143
144
145
146
147
148
149
150
151
# File 'app/models/graph_starter/asset.rb', line 141

def self.property_name_and_uuid_and_ruby_type_query
  properties_and_uuids_and_ruby_types = properties.map do |property|
    [property, SecureRandom.uuid, self.attributes[property][: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

Instance Method Details

#as_json(_options = {}) ⇒ Object



87
88
89
90
91
92
93
94
95
# File 'app/models/graph_starter/asset.rb', line 87

def as_json(_options = {})
  {self.class.model_slug =>
    {id: id,
     title: title,
     name: title,
     images: images.map {|image| image.source.url },
     model_slug: self.class.model_slug}
   }
end

#bodyObject



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

def body
end

#categoriesObject



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

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

#first_image_sourceObject



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

def first_image_source
  images.first && images.first.source
end

#nameObject



83
84
85
# File 'app/models/graph_starter/asset.rb', line 83

def name
  title
end

#secret_sauce_recommendationsObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/models/graph_starter/asset.rb', line 65

def secret_sauce_recommendations
  query_as(:source)
    .match('source-[:HAS_CATEGORY]->(category:Category)<-[:HAS_CATEGORY]-(asset:Asset)')
    .break
    .optional_match('source<-[:CREATED]-(creator:User)-[:CREATED]->asset')
    .break
    .optional_match('source<-[:VIEWED]-(viewer:User)-[: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