graph_starter 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 39794eb0d0df1788618e78fefa3b3ec2fa41e80d
4
- data.tar.gz: 2b8f7dd5e79a3de4cf86929a5203c17377d9ee19
3
+ metadata.gz: 4537a9fa443ead4fb371c58a6353c7b11b61b776
4
+ data.tar.gz: bba04ede1062f46677f6a5feeb91dc601e2c0f1f
5
5
  SHA512:
6
- metadata.gz: 81a7742da6c3cc7586c6754ff7cad43ed49fe14800d0b777984eb3a42c96365f52007f3fc5f0b18080f2491a0a65cba4c7f44898ec70181a919857d5801e4bf7
7
- data.tar.gz: ed50b31f0eb1b7a1a0b93f1458670c670332b52409027ac85499dd676462085f0800ffccf6bfbdfe2238d9566313afc5d13b219d46fa7ad27a06100355bf107b
6
+ metadata.gz: 166ca5a40abc2b1664ce121f50c930d047976440f8810831bb399d615e9e29ec24b2ea60e1ee475bb21041c179f1d01bf69203ebbea7167d5e91356a96db64b2
7
+ data.tar.gz: d2576210af4ee32f4c9714d6195b0fd0583f349a1add17e5a809319c1877cd6f6e530a8e27bac405f58d7c9af3321aad95df95e5b5545ec277bd051050323b74
@@ -57,6 +57,19 @@ module GraphStarter
57
57
  redirect_to action: :edit
58
58
  end
59
59
 
60
+ def rate
61
+ if current_user
62
+ rating = asset.rating_for(current_user)
63
+ rating ||= Rating.find(from_node: current_user, to_node: asset)
64
+
65
+ rating.update_attribute(:level, params[:new_rating])
66
+
67
+ render json: rating
68
+ else
69
+ render json: {}
70
+ end
71
+ end
72
+
60
73
  def asset
61
74
  model_class_scope.find(params[:id])
62
75
  end
@@ -9,44 +9,49 @@ module GraphStarter
9
9
 
10
10
  include Authorizable
11
11
 
12
- property :title
13
- validates :title, presence: true
14
-
15
12
  property :summary
16
13
 
17
14
  property :view_count, type: Integer
18
15
 
19
16
  property :private, type: Boolean, default: false
20
17
 
21
- #has_many :in, :creators, type: :CREATED, model_class: :User
22
18
 
23
- has_many :in, :viewers, rel_class: :View, model_class: :User
19
+ if GraphStarter.configuration.user_class
20
+ #has_many :in, :creators, type: :CREATED, model_class: GraphStarter.configuration.user_class
21
+
22
+ has_many :in, :viewers, rel_class: :View, model_class: GraphStarter.configuration.user_class
23
+
24
+ has_many :in, :rated_by_user, rel_class: :'GraphStarter::Rating', model_class: GraphStarter.configuration.user_class
25
+ end
26
+
24
27
 
25
28
  SecretSauceRecommendation = Struct.new(:asset, :score)
26
29
 
27
30
  def body
28
31
  end
29
32
 
30
- IMAGE_MODELS = []
31
33
  def self.has_images
32
- GraphStarter::Asset::IMAGE_MODELS << self
34
+ @has_images = true
33
35
  has_many :out, :images, type: :HAS_IMAGE, model_class: '::GraphStarter::Image'
34
36
  end
35
37
 
36
38
  def self.has_images?
37
- GraphStarter::Asset::IMAGE_MODELS.include?(self)
39
+ !!@has_images
38
40
  end
39
41
 
40
42
  def first_image_source_url
41
43
  images.first && images.first.source_url
42
44
  end
43
45
 
44
- def self.category_association
45
- @category_association
46
- end
47
-
48
- def self.category_association=(association_name)
49
- @category_association = association_name
46
+ def self.category_association(association_name = nil)
47
+ if association_name.nil?
48
+ @category_association
49
+ else
50
+ fail "Cannot declare category_association twice" if @category_association.present?
51
+ name = association_name.to_sym
52
+ fail ArgumentError, "Association #{name} is not defined" if associations[name].nil?
53
+ @category_association = name
54
+ end
50
55
  end
51
56
 
52
57
  def categories
@@ -58,17 +63,75 @@ module GraphStarter
58
63
  end
59
64
 
60
65
 
66
+ def self.rated
67
+ @rated = true
68
+ end
69
+
70
+ def self.rated?
71
+ !!@rated
72
+ end
73
+
74
+
75
+ def self.name_property(property_name = nil)
76
+ if property_name.nil?
77
+ name_property(default_name_property) if @name_property.nil?
78
+
79
+ @name_property
80
+ else
81
+ fail "Cannot declare name_property twice" if @name_property.present?
82
+ name = property_name.to_sym
83
+ fail ArgumentError, "Property #{name} is not defined" if attributes[name.to_s].nil?
84
+ @name_property = name
85
+
86
+ validates name, presence: true
87
+ end
88
+ end
89
+
90
+ def self.default_name_property
91
+ (%w(name title) & attributes.keys)[0].tap do |property|
92
+ if property.nil?
93
+ fail "No name_property defined for #{self.name}!"
94
+ end
95
+ end
96
+ end
97
+
98
+ def rating_level_for(user)
99
+ rating = rating_for(user)
100
+ rating && rating.level
101
+ end
102
+
103
+ def rating_for(user)
104
+ rated_by_user(nil, :rating).where(uuid: user.uuid).pluck(:rating)[0]
105
+ end
106
+
107
+ def method_missing(method_name, *args, &block)
108
+ if [:name, :title].include?(method_name.to_sym)
109
+ self.class.send(:define_method, method_name) do
110
+ read_attribute(self.class.name_property)
111
+ end
112
+
113
+ send(method_name)
114
+ else
115
+ super
116
+ end
117
+ end
118
+
119
+
61
120
  def self.for_query(query)
62
121
  all.where(title: /.*#{query}.*/i)
63
122
  end
64
123
 
65
124
  def secret_sauce_recommendations
125
+ user_class = GraphStarter.configuration.user_class
126
+ user_class = (user_class.is_a?(Class) ? user_class : user_class.to_s.constantize)
127
+ user_label = user_class.mapped_label_name
128
+
66
129
  query_as(:source)
67
130
  .match('source-[:HAS_CATEGORY]->(category:Category)<-[:HAS_CATEGORY]-(asset:Asset)')
68
131
  .break
69
- .optional_match('source<-[:CREATED]-(creator:User)-[:CREATED]->asset')
132
+ .optional_match("source<-[:CREATED]-(creator:#{user_label})-[:CREATED]->asset")
70
133
  .break
71
- .optional_match('source<-[:VIEWED]-(viewer:User)-[:VIEWED]->asset')
134
+ .optional_match("source<-[:VIEWED]-(viewer:#{user_label})-[:VIEWED]->asset")
72
135
  .limit(5)
73
136
  .order('score DESC')
74
137
  .pluck(
@@ -110,10 +173,17 @@ module GraphStarter
110
173
  def self.authorized_for(user)
111
174
  require 'graph_starter/query_authorizer'
112
175
 
113
- ::GraphStarter::QueryAuthorizer.new(all(:asset).categories(:category, nil, optional: true))
114
- .authorized_query([:asset, :category], user)
115
- .with('DISTINCT asset AS asset')
116
- .proxy_as(self, :asset)
176
+ if category_association
177
+ ::GraphStarter::QueryAuthorizer.new(all(:asset).send(category_association, :category, nil, optional: true))
178
+ .authorized_query([:asset, :category], user)
179
+ .with('DISTINCT asset AS asset')
180
+ .proxy_as(self, :asset)
181
+ else
182
+ ::GraphStarter::QueryAuthorizer.new(all(:asset))
183
+ .authorized_query(:asset, user)
184
+ .with('DISTINCT asset AS asset')
185
+ .proxy_as(self, :asset)
186
+ end
117
187
  end
118
188
 
119
189
  def self.authorized_properties(user)
@@ -0,0 +1,24 @@
1
+ require 'graph_starter/ip_address_validator'
2
+
3
+ module GraphStarter
4
+ class Rating
5
+ include Neo4j::ActiveRel
6
+
7
+ from_class :User
8
+ to_class :'GraphStarter::Asset'
9
+ type :RATES
10
+ creates_unique
11
+
12
+ property :level, type: Integer
13
+ validates :level, inclusion: {in: 1..5}
14
+
15
+ property :rated_at
16
+
17
+ before_create :set_rated_at
18
+
19
+ def set_rated_at
20
+ self.rated_at ||= Time.now
21
+ end
22
+ end
23
+ end
24
+
@@ -2,23 +2,21 @@
2
2
  = stylesheet_link_tag 'jquery.datetimepicker', media: 'all', 'data-turbolinks-track' => true
3
3
 
4
4
 
5
- = form_for @asset, url: url_for(action: :update), html: {class: 'ui form'} do |f|
6
- .field
7
- label Title
5
+ h1 = @asset.name
8
6
 
9
- = f.text_field :title
7
+ = form_for @asset, url: url_for(action: :update), html: {class: 'ui form'} do |f|
10
8
 
11
- = image_tag @asset.image.url, class: 'ui medium image'
12
- .field
13
- label Image
9
+ = image_tag @asset.first_image_source_url, class: 'ui medium image'
10
+ /.field
11
+ / label Image
14
12
 
15
- = f.file_field :image
13
+ / = f.file_field :image
16
14
 
17
15
  - @asset.class.authorized_properties_and_levels(current_user).each do |property, level|
18
16
  .field
19
17
  label = property.name.humanize
20
18
 
21
- = render partial: 'properties/property', locals: {property: property, asset: @asset, level: level, form: f}
19
+ = render partial: 'graph_starter/properties/property', locals: {property: property, asset: @asset, level: level, form: f}
22
20
 
23
21
 
24
22
  = f.submit 'Update', class: 'ui button'
@@ -1,4 +1,8 @@
1
1
 
2
+ javascript:
3
+ var asset_id = '#{@asset.id}';
4
+ var model_slug = '#{@asset.class.model_slug}';
5
+
2
6
  - main_column_exists = @asset.class.has_images? && @asset.images.present? || @asset.body.present?
3
7
  - side_column_width = (main_column_exists ? 'three' : 'eight')
4
8
  - column_count = (main_column_exists ? 'three' : 'two')
@@ -66,7 +70,7 @@
66
70
  a.ui.large.green.button href="#{image.source_url}" Full Size
67
71
 
68
72
 
69
- div class="#{side_column_width} wide column"
73
+ div class="#{side_column_width} wide column" id="right-column"
70
74
  - if @current_user_is_admin
71
75
  a.ui.labeled.icon.button.right.floated data-authorizable="#{@asset.to_json}"
72
76
  | Edit Permissions
@@ -76,6 +80,20 @@
76
80
  | Edit
77
81
 
78
82
  .ui.items
83
+ - if @asset.class.rated? && defined?(current_user) && current_user
84
+ .item
85
+ .content
86
+ .ui.horizontal.divider Rating
87
+ .description
88
+ .ui.star.rating data-max-rating="5" data-rating="#{@asset.rating_level_for(current_user)}"
89
+ coffee:
90
+ $('#right-column .ui.rating').rating
91
+ onRate: (new_rating) ->
92
+ $.ajax
93
+ method: 'PUT'
94
+ url: "/#{model_slug}/#{asset_id}/rate/#{new_rating}"
95
+
96
+
79
97
  - @asset.class.authorized_properties(defined?(current_user) && current_user).each do |property|
80
98
  - if @asset.read_attribute(property.name).present?
81
99
  .item
@@ -1,7 +1,7 @@
1
1
  #menu.ui.secondary.pointing.menu
2
+ = link_to 'Home', '/', class: "item #{'active' if request.path == '/'}"
2
3
  - GraphStarter::Asset.descendants.each do |model_class|
3
4
  - path = graph_starter.assets_path(model_slug: model_class.model_slug)
4
5
  = link_to model_class.name.pluralize.humanize, path, class: "item #{'active' if request.path.match(/^#{path}/)}"
5
6
 
6
- - engine_view do
7
- = render 'layouts/graph_starter/custom_menu'
7
+ = render 'layouts/graph_starter/custom_menu'
@@ -17,7 +17,7 @@ html
17
17
  - controller = params[:controller].to_sym
18
18
 
19
19
  - if ![:users, :groups].include?(controller)
20
- = javascript_include_tag 'ember_apps/permissions_modal', 'data-turbolinks-track' => true
20
+ = javascript_include_tag 'graph_starter/ember_apps/permissions_modal', 'data-turbolinks-track' => true
21
21
 
22
22
  = csrf_meta_tags
23
23
 
data/config/routes.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  GraphStarter::Engine.routes.draw do
2
2
  resources :groups
3
+
3
4
  root 'assets#home'
4
5
 
5
6
  resources :categories
@@ -19,6 +20,7 @@ GraphStarter::Engine.routes.draw do
19
20
  get ':model_slug' => 'assets#index', as: :assets
20
21
  get ':model_slug/:id' => 'assets#show', as: :asset
21
22
  get ':model_slug/:id/edit' => 'assets#edit', as: :edit_asset
23
+ put ':model_slug/:id/rate/:new_rating' => 'assets#rate', as: :rate_asset
22
24
  get ':model_slug/search/:query.json' => 'assets#search', as: :search_assets
23
- patch ':model_slug/:id' => 'assets#update'
25
+ patch ':model_slug/:id' => 'assets#update'
24
26
  end
data/lib/graph_starter.rb CHANGED
@@ -1,3 +1,4 @@
1
+ require "graph_starter/configuration"
1
2
  require "graph_starter/engine"
2
3
 
3
4
  module GraphStarter
@@ -0,0 +1,28 @@
1
+ module GraphStarter
2
+ class Configuration
3
+ attr_writer :user_class
4
+
5
+ def user_class
6
+ @user_class || (:User if defined?(::User))
7
+ end
8
+ end
9
+ end
10
+
11
+ module GraphStarter
12
+ CONFIG = Configuration.new
13
+
14
+ def self.configure(config_hash = nil)
15
+ if config_hash.nil?
16
+ yield Configuration.new
17
+ else
18
+ config_hash.each do |key, value|
19
+ CONFIG.send("#{key}=", value)
20
+ end
21
+ end
22
+ end
23
+
24
+ def self.configuration
25
+ CONFIG
26
+ end
27
+ end
28
+
@@ -18,8 +18,8 @@ module GraphStarter
18
18
  config.assets.precompile += %w(
19
19
  missing.png
20
20
 
21
- ember_apps/permissions_modal.js
22
- ember_apps/user_list_dropdown.js
21
+ graph_starter/ember_apps/permissions_modal.js
22
+ graph_starter/ember_apps/user_list_dropdown.js
23
23
  )
24
24
  end
25
25
  end
@@ -0,0 +1,8 @@
1
+ module GraphStarter
2
+ class Railtie < Rails::Railtie
3
+ initializer 'neo4j.start', after: :load_config_initializers do |app|
4
+ GraphStarter.configure(app.config.graph_starter.to_hash)
5
+ end
6
+ end
7
+ end
8
+
@@ -1,3 +1,3 @@
1
1
  module GraphStarter
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: graph_starter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian Underwood
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-10-22 00:00:00.000000000 Z
11
+ date: 2015-10-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -102,7 +102,6 @@ extensions: []
102
102
  extra_rdoc_files: []
103
103
  files:
104
104
  - MIT-LICENSE
105
- - README.rdoc
106
105
  - Rakefile
107
106
  - app/assets/images/missing.png
108
107
  - app/assets/javascripts/graph_starter/application.coffee.erb
@@ -129,6 +128,7 @@ files:
129
128
  - app/models/graph_starter/image.rb
130
129
  - app/models/graph_starter/model.rb
131
130
  - app/models/graph_starter/property.rb
131
+ - app/models/graph_starter/rating.rb
132
132
  - app/models/graph_starter/view.rb
133
133
  - app/views/graph_starter/assets/TODO.md
134
134
  - app/views/graph_starter/assets/_cards.html.slim
@@ -154,12 +154,13 @@ files:
154
154
  - app/views/layouts/graph_starter/_menu.html.slim
155
155
  - app/views/layouts/graph_starter/_twitter_meta_tags.html.slim
156
156
  - app/views/layouts/graph_starter/application.html.slim
157
- - app/views/layouts/graph_starter/custom_menu.html.slim
158
157
  - config/routes.rb
159
158
  - lib/graph_starter.rb
159
+ - lib/graph_starter/configuration.rb
160
160
  - lib/graph_starter/engine.rb
161
161
  - lib/graph_starter/ip_address_validator.rb
162
162
  - lib/graph_starter/query_authorizer.rb
163
+ - lib/graph_starter/railtie.rb
163
164
  - lib/graph_starter/version.rb
164
165
  - lib/tasks/graph_starter_tasks.rake
165
166
  - test/dummy/README.rdoc
data/README.rdoc DELETED
@@ -1,3 +0,0 @@
1
- = GraphStarter
2
-
3
- This project rocks and uses MIT-LICENSE.