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

Commit 0862416

Browse files
committed
fixed javascript actions; inserted jquery/ajax; fixed callbacks
1 parent 1005f8e commit 0862416

File tree

13 files changed

+127
-98
lines changed

13 files changed

+127
-98
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ app:
3333
stdin_open: true
3434
```
3535
36-
This also makes it possible to after the containers are up do docker attach project_app_1 which seems to work fine.
36+
This also makes it possible to after the containers are up do `docker attach angorah_web_1` which seems to work fine.
3737

3838
https://github.com/docker/compose/issues/423#issuecomment-141995398
3939

@@ -54,3 +54,9 @@ Removing relations:
5454
MATCH (Raphael)-[r:FRIEND]->(It)
5555
DELETE r
5656
```
57+
58+
Removing constraint:
59+
60+
```
61+
DROP UserNeo4j
62+
```

app/controllers/users_controller.rb

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class UsersController < ApplicationController
22
ACTIVE_MODEL_CLASS = User # UserNeo4j
33

4-
before_action :set_user, only: [:show, :edit, :update, :destroy, :invite]
4+
before_action :set_user, only: [:show, :friends, :edit, :update, :destroy, :invite]
55

66
# GET /users
77
# GET /users.json
@@ -12,19 +12,24 @@ def index
1212
# GET /users/1
1313
# GET /users/1.json
1414
def show
15-
@user_neo4j = UserNeo4j.find(@user.neo4j_uuid) if @user.neo4j_uuid.present?
15+
@user_neo4j = UserNeo4j.find(@user.neo4j_uuid)
1616
@friends_path = {}
1717

18-
# TODO: optimize queries to locate Users
19-
@friends = begin
20-
if params[:q].present?
21-
byebug
22-
uuids = @user_neo4j.friendsBySearch(params[:q]).pluck(:uuid)
23-
elsif @user_neo4j.present?
24-
uuids = @user_neo4j.friends.pluck(:uuid)
25-
end
18+
if params[:q].present?
19+
@friends, @friends_path = @user_neo4j.friendsBySearch(params[:q])
20+
elsif @user_neo4j.present?
21+
@friends = @user_neo4j.friends
22+
end
23+
end
2624

27-
uuids.present? ? User.where(:neo4j_uuid.in => uuids) : []
25+
# GET /users/1/friends
26+
# GET /users/1/friends.json
27+
def friends
28+
show # load data
29+
30+
respond_to do |format|
31+
format.html { render partial: 'friends' }
32+
format.json { render json: { status: :ok, message: notice } }
2833
end
2934
end
3035

@@ -67,6 +72,8 @@ def update
6772
end
6873
end
6974

75+
# GET /users/1/invite
76+
# GET /users/1/invite.json
7077
def invite
7178
notice = 'User was successfully invited.'
7279
friend_a = UserNeo4j.find(@current_user.neo4j_uuid)

app/javascript/packs/application.js

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
require("@rails/ujs").start()
77
require("turbolinks").start()
88
require("channels")
9-
9+
global.$ = require('jquery')
1010

1111
// Uncomment to copy all static images under ../images to the output folder and reference
1212
// them with the image_pack_tag helper in views (e.g <%= image_pack_tag 'rails.png' %>)
@@ -15,25 +15,19 @@ require("channels")
1515
// const images = require.context('../images', true)
1616
// const imagePath = (name) => images(name, true)
1717

18-
handdlerHearts = function handdlerHearts() {
19-
var els = document.querySelectorAll('.btn-invite');
20-
21-
for (const el of els) {
22-
el.addEventListener('click', function (event) {
23-
event.preventDefault();
24-
25-
el.classList.toggle('is-active');
26-
// TOOD: Use ajax
27-
setTimeout(function() { document.location.href = el.href }, 450);
28-
});
29-
}
30-
}
31-
32-
33-
document.addEventListener("DOMContentLoaded", function() {
34-
handdlerHearts();
35-
});
36-
37-
document.addEventListener("turbolinks:load", function() {
38-
handdlerHearts();
18+
$(document).on('ready turbolinks:load', function () {
19+
$('.btn-invite').click(function (event) {
20+
event.preventDefault();
21+
22+
var el = $(this);
23+
24+
$.get(el.attr('href'))
25+
.done(function(data) {
26+
el.toggleClass('is-active');
27+
})
28+
.fail(function() {
29+
alert("error");
30+
})
31+
;
32+
})
3933
});

app/models/concerns/user_sync.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ def save_neoj4
1212
params = attributes.slice(*%w[
1313
first_name
1414
last_name
15+
slug
1516
website
1617
titles
1718
subtitles
@@ -34,6 +35,6 @@ def destroy_neoj4
3435

3536
UserNeo4j.find(neo4j_uuid).destroy rescue nil
3637

37-
update({ neo4j_uuid: nil })
38+
set({ neo4j_uuid: nil })
3839
end
3940
end

app/models/user.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ def references
3434
end
3535

3636
def reload_titles!
37+
return unless persisted?
38+
3739
# TODO: Use async_perform with Sidekiq
3840
WebsiteScrapperWorker.new.perform(id)
3941
end

app/models/user_neo4j.rb

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class UserNeo4j
1717

1818
attr_accessor :skip_get_website_titles
1919

20-
has_many :both, :friends, type: :FRIEND, model_class: self, labels: false, unique: true
20+
has_many :out, :friends, type: :FRIEND, model_class: self, labels: false, unique: true
2121

2222
validates :slug, exclusion: { in: ['admin', 'root'] + RouteRecognizer.initial_path_segments }, uniqueness: true
2323
validates :first_name, presence: true
@@ -40,37 +40,30 @@ def references
4040
end
4141

4242
def reload_titles!
43+
return unless persisted?
44+
4345
# TODO: Use async_perform with Sidekiq
4446
WebsiteScrapperWorker.new.perform(uuid, self.class)
4547
end
4648

4749
def friendsBySearch(term)
48-
byebug
4950
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)
5451

55-
uuids.each do |uuid|
52+
friends = as(:user).friends(:l, :r, rel_length: { max: 5 }).
53+
where('ANY(title IN l.titles WHERE toLower(title) CONTAINS toLower({title})) OR
54+
ANY(subtitle IN l.subtitles WHERE toLower(subtitle) CONTAINS toLower({title}))').
55+
where('l <> user').
56+
params({ title: term })
57+
58+
friends.each do |friend|
5659
friends_path[uuid] = Neo4j::ActiveBase.current_session.
5760
query("MATCH (r:UserNeo4j { uuid: '#{self.uuid}' }),
58-
(l:UserNeo4j { uuid: '#{uuid}' }),
61+
(l:UserNeo4j { uuid: '#{friend.uuid}' }),
5962
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] }) }
63+
RETURN p", limit: 1).first.p.nodes
6264
end
6365

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)
66+
return friends, friends_path
7467
end
7568

7669
protected

app/views/users/_friends.html.erb

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<div class="columns">
2+
<div class="column is-two-thirds ">
3+
<h2 class="title is-2">Friends</h2>
4+
</div>
5+
<div class="column">
6+
<form name="search">
7+
<div class="field has-addons is-pulled-right">
8+
<div class="control">
9+
<%= text_field_tag 'q', nil, placeholder: 'Enter search term...', value: params[:q], class: 'input' %>
10+
</div>
11+
<div class="control">
12+
<button type="submit" class="button is-link">
13+
Search
14+
</button>
15+
</div>
16+
</div>
17+
</form>
18+
</div>
19+
</div>
20+
21+
<% if @friends.present? %>
22+
<div class="columns is-multiline">
23+
<% @friends.each do |item| %>
24+
<%= render partial: 'user', locals: { user: item, path: @friends_path[item.uuid] } %>
25+
<% end %>
26+
</div>
27+
<% else %>
28+
<p><%= params[:q].present? ? 'No results.' : 'Nothing here.' %></p>
29+
<% end %>
30+
31+
<script type="text/javascript">
32+
$(function () {
33+
var form = $("#friend-list form");
34+
var el = form.find("input[name='q']");
35+
var button = form.find("button[type='submit']");
36+
37+
form.submit(function (event) {
38+
event.preventDefault();
39+
40+
button.prop('disabled', true).text('Searching...');
41+
42+
$.ajax({
43+
method: "GET",
44+
url: '<%= friends_user_path(@user) %>',
45+
data: { q: el.val() },
46+
})
47+
.done(function (data) {
48+
$("#friend-list").html(data);
49+
})
50+
.fail(function () {
51+
alert("error");
52+
})
53+
.always(function () {
54+
button.prop('disabled', false);
55+
})
56+
;
57+
})
58+
});
59+
</script>

app/views/users/_user.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<%= friends_path(path) if defined? path %>
2222
</p>
2323
<p class="has-text-right is-size-6 mt-6">
24-
<time datetime="<%= user.created_at %>">Created at <%= l(user.created_at, format: :short) %></time>
24+
<time class="is-italic has-text-grey-light" datetime="<%= user.created_at %>">Created at <%= l(user.created_at, format: :short) %></time>
2525
</p>
2626
</div>
2727
</div>

app/views/users/index.html.erb

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,3 @@
2323
<%= render partial: 'user', locals: { user: user } %>
2424
<% end %>
2525
</div>
26-
27-
<script type="text/javascript">
28-
var els = document.querySelectorAll('.btn-invite');
29-
30-
for (const el of els) {
31-
el.addEventListener('click', function (event) {
32-
event.preventDefault();
33-
34-
el.querySelector('.heart').classList.toggle('is-active');
35-
// TOOD: Use ajax
36-
setTimeout(function() { document.location.href = el.href }, 900);
37-
});
38-
}
39-
</script>

app/views/users/show.html.erb

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -58,32 +58,6 @@
5858
</article>
5959
</div>
6060

61-
<div class="columns">
62-
<div class="column is-two-thirds ">
63-
<h2 class="title is-2">Friends</h2>
64-
</div>
65-
<div class="column">
66-
<form>
67-
<div class="field has-addons is-pulled-right">
68-
<div class="control">
69-
<%= text_field_tag 'q', nil, placeholder: 'Enter search term...', value: params[:q], class: 'input' %>
70-
</div>
71-
<div class="control">
72-
<button type="submit" class="button is-link">
73-
Search
74-
</button>
75-
</div>
76-
</div>
77-
</form>
78-
</div>
61+
<div id="friend-list">
62+
<%= render partial: 'friends', locals: { friends: @friends, friends_path: @friends_path } %>
7963
</div>
80-
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)