Skip to content
This repository was archived by the owner on Nov 25, 2024. It is now read-only.

Commit 1005f8e

Browse files
committed
created models to clear outheres classests.
1 parent 3c04fa7 commit 1005f8e

13 files changed

Lines changed: 128 additions & 69 deletions

File tree

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,20 @@ Things you may want to cover:
2323

2424
* ...
2525

26+
## Debugging with `byebug`
27+
28+
## Docker compos config
29+
30+
```yaml
31+
app:
32+
tty: true
33+
stdin_open: true
34+
```
35+
36+
This also makes it possible to after the containers are up do docker attach project_app_1 which seems to work fine.
37+
38+
https://github.com/docker/compose/issues/423#issuecomment-141995398
39+
2640
## Neo4j
2741
2842
### Removing data

app/controllers/application_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ def set_session_user_id
88

99
@current_user = User.first if @current_user.nil?
1010

11-
session[:session_user_id] = @current_user.id.to_s
11+
session[:session_user_id] = @current_user&.id&.to_s
1212
end
1313
end

app/controllers/users_controller.rb

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,20 @@ def index
1212
# GET /users/1
1313
# GET /users/1.json
1414
def show
15-
@user_neo4j = UserNeo4j.find(@user.neo4j_uuid)
15+
@user_neo4j = UserNeo4j.find(@user.neo4j_uuid) if @user.neo4j_uuid.present?
1616
@friends_path = {}
1717

1818
# TODO: optimize queries to locate Users
19-
@friends =
19+
@friends = begin
2020
if params[:q].present?
21-
uuids = @user_neo4j.
22-
friends(:l, :r, rel_length: 5).
23-
where("ANY(title IN l.titles WHERE toLower(title) CONTAINS toLower({title}))").
24-
params({ title: params[:q] }).
25-
pluck(:uuid)
26-
27-
uuids.each do |uuid|
28-
@friends_path[uuid] = Neo4j::ActiveBase.current_session.
29-
query("MATCH (r:UserNeo4j { uuid: '#{@user.neo4j_uuid}' }),
30-
(l:UserNeo4j { uuid: '#{uuid}' }),
31-
p = shortestPath((r)-[FRIEND*..15]->(l))
32-
RETURN p", limit: 1).first.p.nodes.
33-
map { |node| User.find_by({ neo4j_uuid: node.properties[:uuid] }) }
34-
end
35-
36-
User.where(:neo4j_uuid.in => uuids)
37-
else
21+
byebug
22+
uuids = @user_neo4j.friendsBySearch(params[:q]).pluck(:uuid)
23+
elsif @user_neo4j.present?
3824
uuids = @user_neo4j.friends.pluck(:uuid)
39-
40-
User.where(:neo4j_uuid.in => uuids)
4125
end
26+
27+
uuids.present? ? User.where(:neo4j_uuid.in => uuids) : []
28+
end
4229
end
4330

4431
# GET /users/new

app/helpers/users_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ def invite_link_to(user)
66
link_to invite_user_path(user), class: "card-footer-item btn-invite #{'is-active' if invited}" do
77
content_tag(:div, nil, class: 'heart') + ' Invite'
88
end
9+
rescue
10+
nil
911
end
1012

1113
def friends_path(path)

app/models/concerns/user_sync.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
module UserSync
2+
extend ActiveSupport::Concern
3+
4+
included do
5+
after_save :save_neoj4
6+
before_destroy :destroy_neoj4
7+
end
8+
9+
def save_neoj4
10+
return unless (attributes.keys - ['updated_at']).any?
11+
12+
params = attributes.slice(*%w[
13+
first_name
14+
last_name
15+
website
16+
titles
17+
subtitles
18+
introduction
19+
]).merge({ 'skip_get_website_titles' => true })
20+
21+
if neo4j_uuid.present?
22+
user = UserNeo4j.find(neo4j_uuid)
23+
user.update(params)
24+
elsif
25+
user = UserNeo4j.create(params)
26+
27+
# update without callbacks
28+
set({ neo4j_uuid: user.uuid })
29+
end
30+
end
31+
32+
def destroy_neoj4
33+
return unless neo4j_uuid.present?
34+
35+
UserNeo4j.find(neo4j_uuid).destroy rescue nil
36+
37+
update({ neo4j_uuid: nil })
38+
end
39+
end

app/models/user.rb

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ class User
55
include Mongoid::Timestamps
66
include Mongoid::Slug
77

8+
include UserSync
9+
810
field :first_name, type: String
911
field :last_name, type: String
1012
field :website, type: String
@@ -21,9 +23,7 @@ class User
2123
validates :website, uniqueness: true, presence: true, website: true, unless: :skip_get_website_titles
2224

2325
after_save :get_website_titles, unless: :skip_get_website_titles
24-
after_save :sync_neoj4
2526
after_create :clear_database
26-
before_destroy :destroy_neoj4
2727

2828
def full_name
2929
[first_name, last_name].join(' ')
@@ -46,35 +46,6 @@ def get_website_titles
4646
reload_titles!
4747
end
4848

49-
def sync_neoj4
50-
return unless (changed_attributes.keys - ['updated_at']).any?
51-
52-
params = attributes.slice(*%w[
53-
first_name
54-
last_name
55-
website
56-
titles
57-
subtitles
58-
introduction
59-
]).merge({ 'skip_get_website_titles' => true })
60-
61-
if neo4j_uuid.present?
62-
user = UserNeo4j.find(neo4j_uuid)
63-
user.update(params)
64-
else
65-
user = UserNeo4j.create(params)
66-
set({ neo4j_uuid: user.uuid })
67-
end
68-
end
69-
70-
def destroy_neoj4
71-
return unless neo4j_uuid.present?
72-
73-
UserNeo4j.find(neo4j_uuid).destroy rescue nil
74-
75-
update({ neo4j_uuid: nil })
76-
end
77-
7849
# preventing misuse of the application in an open environment
7950
def clear_database
8051
return if User.count <= 25

app/models/user_neo4j.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,35 @@ def reload_titles!
4444
WebsiteScrapperWorker.new.perform(uuid, self.class)
4545
end
4646

47+
def friendsBySearch(term)
48+
byebug
49+
friends_path = {}
50+
uuids = friends(:l, :r, rel_length: { max: 5 }).
51+
where("ANY(title IN l.titles WHERE toLower(title) CONTAINS toLower({title}))").
52+
params({ title: term }).
53+
pluck(:uuid)
54+
55+
uuids.each do |uuid|
56+
friends_path[uuid] = Neo4j::ActiveBase.current_session.
57+
query("MATCH (r:UserNeo4j { uuid: '#{self.uuid}' }),
58+
(l:UserNeo4j { uuid: '#{uuid}' }),
59+
p = shortestPath((r)-[FRIEND*..5]->(l))
60+
RETURN p", limit: 1).first.p.nodes.
61+
map { |node| User.find_by({ neo4j_uuid: node.properties[:uuid] }) }
62+
end
63+
64+
return uuids, friends_path
65+
66+
# FIXME: May it works
67+
# as(:user).
68+
# friends(:friends).
69+
# query.
70+
# with('shortestPath((user)-[:FRIEND*..5]->(friends)) AS shortest_path').
71+
# where("ANY(title IN friends.titles WHERE toLower(title) CONTAINS toLower({title}))").
72+
# params({ title: term }).
73+
# pluck(:shortest_path)
74+
end
75+
4776
protected
4877

4978
def get_website_titles

app/validators/website_validator.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class WebsiteValidator < ActiveModel::EachValidator
22
def validate_each(record, attribute, value)
3-
HTTParty.get(value, { timeout: 10 })
3+
HTTParty.get(value, { timeout: 5 })
44
rescue => e
55
record.errors[:attribute] << e.message
66
end

app/views/users/_user.html.erb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515
</div>
1616
<% end %>
1717

18-
<div class="content">
19-
<p><%= user.introduction %></p>
18+
<div>
19+
<p><br /><%= user.introduction %></p>
2020
<p class="is-size-7">
2121
<%= friends_path(path) if defined? path %>
2222
</p>
23-
<p class="has-text-right is-size-6">
23+
<p class="has-text-right is-size-6 mt-6">
2424
<time datetime="<%= user.created_at %>">Created at <%= l(user.created_at, format: :short) %></time>
2525
</p>
2626
</div>

app/views/users/show.html.erb

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,12 @@
7878
</div>
7979
</div>
8080

81-
82-
<div class="columns is-multiline">
83-
<% @friends.each do |item| %>
84-
<%= render partial: 'user', locals: { user: item, path: @friends_path[item.neo4j_uuid] } %>
85-
<% end %>
86-
</div>
81+
<% if @friends.present? %>
82+
<div class="columns is-multiline">
83+
<% @friends.each do |item| %>
84+
<%= render partial: 'user', locals: { user: item, path: @friends_path[item.neo4j_uuid] } %>
85+
<% end %>
86+
</div>
87+
<% else %>
88+
<p><%= params[:q].present? ? 'No results.' : 'Nothing here.' %></p>
89+
<% end %>

0 commit comments

Comments
 (0)