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

Commit

Permalink
inserted rubocop; fixed tests to neo4j
Browse files Browse the repository at this point in the history
  • Loading branch information
raphox committed Jun 15, 2020
1 parent 0862416 commit 16b3b9e
Show file tree
Hide file tree
Showing 20 changed files with 284 additions and 131 deletions.
72 changes: 72 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
AllCops:
TargetRubyVersion: 2.7.1
Exclude:
- "Gemfile"
- "Gemfile.lock"
- "db/schema.rb"

Layout/ParameterAlignment:
EnforcedStyle: with_fixed_indentation

Layout/DotPosition:
EnforcedStyle: trailing

Layout/FirstHashElementIndentation:
EnforcedStyle: consistent

Layout/MultilineMethodCallIndentation:
EnforcedStyle: indented

Layout/EmptyLinesAroundAttributeAccessor:
Enabled: true

Layout/SpaceAroundMethodCallOperator:
Enabled: true

Lint/DeprecatedOpenSSLConstant:
Enabled: true

Lint/MixedRegexpCaptureTypes:
Enabled: true

Lint/RaiseException:
Enabled: true

Lint/StructNewOverride:
Enabled: true

Style/Documentation:
Enabled: false

Style/FrozenStringLiteralComment:
Enabled: false

Style/AsciiComments:
Enabled: false

Style/TrailingCommaInHashLiteral:
EnforcedStyleForMultiline: comma

Style/TrailingCommaInArrayLiteral:
EnforcedStyleForMultiline: comma

Style/ExponentialNotation:
Enabled: true

Style/HashEachMethods:
Enabled: true

Style/HashTransformKeys:
Enabled: true

Style/HashTransformValues:
Enabled: true

Style/RedundantRegexpCharacterClass:
Enabled: true

Style/RedundantRegexpEscape:
Enabled: true

Style/SlicingWithRange:
Enabled: true
39 changes: 22 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,31 +1,36 @@
# README
# Angora

This README would normally document whatever steps are necessary to get the
application up and running.
## Dependencies

Things you may want to cover:
* Docker https://docs.docker.com
* Docker Compose https://docs.docker.com/compose/

* Ruby version
## Running

* System dependencies
Run the below code:

* Configuration

* Database creation
```
docker-compose run web
```

* Database initialization
## Setup

* How to run the test suite
You need execute migrations to neo4j and populate database with default test data to see the system works.

* Services (job queues, cache servers, search engines, etc.)
```
docker-compose run web rails neo4j:migrate RAILS_ENV=development
docker-compose run web rake db:seed
```

* Deployment instructions
## Testing

* ...
Run the below code:

## Debugging with `byebug`
```
docker-compose run web rails test
```

## Docker compos config
## Debugging with `byebug` inside the Docker

```yaml
app:
Expand All @@ -37,7 +42,7 @@ This also makes it possible to after the containers are up do `docker attach ang

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

## Neo4j
## Neo4j tips

### Removing data

Expand Down
6 changes: 5 additions & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ class ApplicationController < ActionController::Base

def set_session_user_id
if session[:session_user_id].present?
@current_user = User.find(session[:session_user_id]) rescue nil
@current_user = begin
User.find(session[:session_user_id])
rescue StandardError
nil
end
end

@current_user = User.first if @current_user.nil?
Expand Down
39 changes: 20 additions & 19 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class UsersController < ApplicationController
ACTIVE_MODEL_CLASS = User # UserNeo4j

before_action :set_user, only: [:show, :friends, :edit, :update, :destroy, :invite]
before_action :set_user, only: %i[show friends edit update destroy invite]

# GET /users
# GET /users.json
Expand All @@ -12,13 +12,22 @@ def index
# GET /users/1
# GET /users/1.json
def show
@user_neo4j = UserNeo4j.find(@user.neo4j_uuid)
@friends_path = {}
user_neo4j = UserNeo4j.find(@user.neo4j_uuid)

if params[:q].present?
@friends, @friends_path = @user_neo4j.friendsBySearch(params[:q])
elsif @user_neo4j.present?
@friends = @user_neo4j.friends
friends, friends_path = user_neo4j.friends_by_search(params[:q])
elsif user_neo4j.present?
friends = user_neo4j.friends
end

# FIXME: Change this When use ACTIVE_MODEL_CLASS as UserNeo4j
@friends = User.where(:neo4j_uuid.in => friends.pluck(:uuid))
@friends_path = friends_path&.transform_values do |item|
uuids = item.map { |node| node.props[:uuid] }

# it is necessary to order after query in database to preser correct path
User.where(:neo4j_uuid.in => uuids).
to_a.sort_by { |value| uuids.index(value.neo4j_uuid) }
end
end

Expand All @@ -39,8 +48,7 @@ def new
end

# GET /users/1/edit
def edit
end
def edit; end

# POST /users
# POST /users.json
Expand Down Expand Up @@ -76,19 +84,12 @@ def update
# GET /users/1/invite.json
def invite
notice = 'User was successfully invited.'
friend_a = UserNeo4j.find(@current_user.neo4j_uuid)
friend_b = UserNeo4j.find(@user.neo4j_uuid)

if friend_a.friends(rel_length: 1).where({ uuid: @user.neo4j_uuid }).present?
notice = 'User was successfully uninvited.'
friend_a.friends.delete(friend_b)
friend_b.friends.delete(friend_a)
else
friend_a.friends << friend_b
friend_b.friends << friend_a
end

neo4j = UserNeo4j.find(@current_user.neo4j_uuid)
neo4j.invite(@user.neo4j_uuid)

respond_to do |format|
format.js { render json: { status: :ok, message: notice } }
format.html { redirect_to user_path(@user), notice: notice }
format.json { render json: { status: :ok, message: notice } }
end
Expand Down
8 changes: 6 additions & 2 deletions app/helpers/users_helper.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
module UsersHelper
def invite_link_to(user)
neo4j = UserNeo4j.find(@current_user.neo4j_uuid)
invited = neo4j.friends.find(user.neo4j_uuid).present? rescue nil
invited = begin
neo4j.friends.find(user.neo4j_uuid).present?
rescue StandardError
nil
end

link_to invite_user_path(user), class: "card-footer-item btn-invite #{'is-active' if invited}" do
content_tag(:div, nil, class: 'heart') + ' Invite'
end
rescue
rescue StandardError
nil
end

Expand Down
26 changes: 15 additions & 11 deletions app/models/concerns/user_sync.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@ module UserSync
def save_neoj4
return unless (attributes.keys - ['updated_at']).any?

params = attributes.slice(*%w[
first_name
last_name
slug
website
titles
subtitles
introduction
]).merge({ 'skip_get_website_titles' => true })
params = attributes.slice(
'first_name',
'last_name',
'slug',
'website',
'titles',
'subtitles',
'introduction'
).merge({ 'skip_load_website_titles' => true })

if neo4j_uuid.present?
user = UserNeo4j.find(neo4j_uuid)
user.update(params)
elsif
else
user = UserNeo4j.create(params)

# update without callbacks
Expand All @@ -33,7 +33,11 @@ def save_neoj4
def destroy_neoj4
return unless neo4j_uuid.present?

UserNeo4j.find(neo4j_uuid).destroy rescue nil
begin
UserNeo4j.find(neo4j_uuid).destroy
rescue StandardError
nil
end

set({ neo4j_uuid: nil })
end
Expand Down
32 changes: 16 additions & 16 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ class User
field :introduction, type: String
field :neo4j_uuid, type: String

attr_accessor :skip_get_website_titles
attr_accessor :skip_load_website_titles

slug :first_name, reserve: ['admin', 'root'] + RouteRecognizer.initial_path_segments
slug :first_name, reserve: %w[admin root] + RouteRecognizer.initial_path_segments

validates :first_name, presence: true
validates :website, uniqueness: true, presence: true, website: true, unless: :skip_get_website_titles
validates :website, uniqueness: true, presence: true, website: true, unless: :skip_load_website_titles

after_save :get_website_titles, unless: :skip_get_website_titles
after_save :load_website_titles, unless: :skip_load_website_titles
after_create :clear_database

def full_name
Expand All @@ -42,7 +42,7 @@ def reload_titles!

protected

def get_website_titles
def load_website_titles
return unless changed_attributes.keys.include?('website')

reload_titles!
Expand All @@ -61,15 +61,15 @@ def clear_database

# Used as default user list on start project
DEFAULT_USERS = [
{ first_name: 'Vinnie', last_name: 'Lintott', website: 'http://indiatimes.com', titles: [''], skip_get_website_titles: true },
{ first_name: 'Otha', last_name: 'Kunes', website: 'https://edublogs.org', titles: ['.NET'], skip_get_website_titles: true },
{ first_name: 'Dayle', last_name: 'Caswill', website: 'http://sohu.com', titles: ['C', 'C++'], skip_get_website_titles: true },
{ first_name: 'Erastus', last_name: 'Quilligan', website: 'http://skyrock.com', titles: ['Javascript', 'ASP'], skip_get_website_titles: true },
{ first_name: 'Jamal', last_name: 'Cullington', website: 'https://webmd.com', titles: ['Pascal'], skip_get_website_titles: true },
{ first_name: 'Joice', last_name: 'Brooke', website: 'http://marketwatch.com', titles: ['Python'], skip_get_website_titles: true },
{ first_name: 'Gwenni', last_name: 'Dines', website: 'http://typepad.com', titles: ['Go'], skip_get_website_titles: true },
{ first_name: 'Glyn', last_name: 'Clouter', website: 'https://google.ru', titles: ['PHP', 'Ruby'], skip_get_website_titles: true },
{ first_name: 'Lucienne', last_name: 'Ready', website: 'http://myspace.com', titles: ['Ruby'], skip_get_website_titles: true },
{ first_name: 'Katuscha', last_name: 'Tinman', website: 'http://umn.edu', titles: ['Java'], skip_get_website_titles: true },
]
{ first_name: 'Vinnie', last_name: 'Lintott', website: 'http://indiatimes.com', titles: [''], skip_load_website_titles: true },
{ first_name: 'Otha', last_name: 'Kunes', website: 'https://edublogs.org', titles: ['.NET'], skip_load_website_titles: true },
{ first_name: 'Dayle', last_name: 'Caswill', website: 'http://sohu.com', titles: ['C', 'C++'], skip_load_website_titles: true },
{ first_name: 'Erastus', last_name: 'Quilligan', website: 'http://skyrock.com', titles: %w[Javascript ASP], skip_load_website_titles: true },
{ first_name: 'Jamal', last_name: 'Cullington', website: 'https://webmd.com', titles: ['Pascal'], skip_load_website_titles: true },
{ first_name: 'Joice', last_name: 'Brooke', website: 'http://marketwatch.com', titles: ['Python'], skip_load_website_titles: true },
{ first_name: 'Gwenni', last_name: 'Dines', website: 'http://typepad.com', titles: ['Go'], skip_load_website_titles: true },
{ first_name: 'Glyn', last_name: 'Clouter', website: 'https://google.ru', titles: %w[PHP Ruby], skip_load_website_titles: true },
{ first_name: 'Lucienne', last_name: 'Ready', website: 'http://myspace.com', titles: ['Ruby'], skip_load_website_titles: true },
{ first_name: 'Katuscha', last_name: 'Tinman', website: 'http://umn.edu', titles: ['Java'], skip_load_website_titles: true },
].freeze
end
Loading

0 comments on commit 16b3b9e

Please sign in to comment.