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



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

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)
  else
    super
  end
end

Class Method Details

.authorized_associationsObject



238
239
240
# File 'app/models/graph_starter/asset.rb', line 238

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

.authorized_for(user) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'app/models/graph_starter/asset.rb', line 188

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



204
205
206
# File 'app/models/graph_starter/asset.rb', line 204

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

.authorized_properties_and_levels(user) ⇒ Object



208
209
210
# File 'app/models/graph_starter/asset.rb', line 208

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

.authorized_properties_query(user) ⇒ Object



212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'app/models/graph_starter/asset.rb', line 212

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_association(association_name = nil) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'app/models/graph_starter/asset.rb', line 46

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_name_propertyObject



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

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



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

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



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

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_imagesObject



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

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

.has_images?Boolean

Returns:



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

def self.has_images?
  !!@has_images
end

.icon_classObject



242
243
244
# File 'app/models/graph_starter/asset.rb', line 242

def self.icon_class
  'bookmark'
end

.model_slugObject



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

def self.model_slug
  name.tableize
end

.name_property(property_name = nil) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'app/models/graph_starter/asset.rb', line 75

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[name.to_s].nil?
    @name_property = name

    validates name, presence: true
    index name
  end
end

.propertiesObject



184
185
186
# File 'app/models/graph_starter/asset.rb', line 184

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

.property_name_and_uuid_and_ruby_type_queryObject



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

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

.ratedObject



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

def self.rated
  @rated = true
end

.rated?Boolean

Returns:



70
71
72
# File 'app/models/graph_starter/asset.rb', line 70

def self.rated?
  !!@rated
end

.search_properties(*array) ⇒ Object



121
122
123
124
125
126
127
# File 'app/models/graph_starter/asset.rb', line 121

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



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

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



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

def body
end

#categoriesObject



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

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

#first_image_source_urlObject



42
43
44
# File 'app/models/graph_starter/asset.rb', line 42

def first_image_source_url
  images.first && images.first.source_url
end

#nameObject



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

def name
  title
end

#rating_for(user) ⇒ Object



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

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

#rating_level_for(user) ⇒ Object



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

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

#secret_sauce_recommendationsObject



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'app/models/graph_starter/asset.rb', line 139

def secret_sauce_recommendations
  user_class = GraphStarter.configuration.user_class
  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