Skip to content

Homework #6 #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,5 @@

# Ignore master key for decrypting credentials and more.
/config/master.key

/config/application.yml
6 changes: 5 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '2.5.1'

gem 'will_paginate'
gem 'jquery-rails'
gem 'bootstrap', '~> 4.1.3'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
Expand Down Expand Up @@ -63,3 +62,8 @@ end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
gem 'wice_grid', git: 'https://github.com/patricklindsay/wice_grid.git'
gem "figaro"
gem 'bootstrap-datepicker-rails'
gem 'jquery-ui-rails'

24 changes: 22 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
GIT
remote: https://github.com/patricklindsay/wice_grid.git
revision: fbce87432eec24c9e9bec05d6698f150cfb17329
specs:
wice_grid (4.0.1)
coffee-rails (> 3.2)
kaminari (~> 0.16)
rails (~> 5.0, < 5.3)

GEM
remote: https://rubygems.org/
specs:
Expand Down Expand Up @@ -57,6 +66,8 @@ GEM
autoprefixer-rails (>= 6.0.3)
popper_js (>= 1.12.9, < 2)
sass (>= 3.5.2)
bootstrap-datepicker-rails (1.8.0.1)
railties (>= 3.0)
builder (3.2.3)
byebug (10.0.2)
capybara (3.10.1)
Expand Down Expand Up @@ -84,6 +95,8 @@ GEM
erubi (1.7.1)
execjs (2.7.0)
ffi (1.9.25)
figaro (1.1.1)
thor (~> 0.14)
globalid (0.4.1)
activesupport (>= 4.2.0)
i18n (1.1.1)
Expand All @@ -96,6 +109,11 @@ GEM
rails-dom-testing (>= 1, < 3)
railties (>= 4.2.0)
thor (>= 0.14, < 2.0)
jquery-ui-rails (6.0.1)
railties (>= 3.2.16)
kaminari (0.17.0)
actionpack (>= 3.0.0)
activesupport (>= 3.0.0)
listen (3.1.5)
rb-fsevent (~> 0.9, >= 0.9.4)
rb-inotify (~> 0.9, >= 0.9.7)
Expand Down Expand Up @@ -199,7 +217,6 @@ GEM
websocket-driver (0.7.0)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.3)
will_paginate (3.1.6)
xpath (3.2.0)
nokogiri (~> 1.8)

Expand All @@ -210,12 +227,15 @@ DEPENDENCIES
bcrypt (~> 3.1.7)
bootsnap (>= 1.1.0)
bootstrap (~> 4.1.3)
bootstrap-datepicker-rails
byebug
capybara (>= 2.15)
chromedriver-helper
coffee-rails (~> 4.2)
figaro
jbuilder (~> 2.5)
jquery-rails
jquery-ui-rails
listen (>= 3.0.5, < 3.2)
puma (~> 3.11)
rails (~> 5.2.1)
Expand All @@ -228,7 +248,7 @@ DEPENDENCIES
tzinfo-data
uglifier (>= 1.3.0)
web-console (>= 3.3.0)
will_paginate
wice_grid!

RUBY VERSION
ruby 2.5.1p57
Expand Down
18 changes: 6 additions & 12 deletions app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,16 @@
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require jquery
//= require rails-ujs
//= require activestorage
//= require turbolinks
//= require_tree .

function openPopup () {
$('.modal').show(500, function () {
$(this).addClass('active')
})
}

function closePopup () {
$('.modal').removeClass('active')
$('.modal').hide(500)
}
//= require jquery3
//= require bootstrap
//= require jquery_ujs
//= require jquery-ui
//= require wice_grid
//= require bootstrap-datepicker

function handleCheckbox (event) {
if (event.target.id === 'task_status') {
Expand Down
68 changes: 2 additions & 66 deletions app/assets/stylesheets/application.scss
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,10 @@ body {
min-width: 100vw;
padding: 100px 0;
width: 100%;
z-index: -10;
overflow-x: hidden;
color: white;
font-size: 1.3rem;
}
.hero {
//background-color: rgba(31, 34, 118, 0.5);
color: white;
.container {
text-align: center;
button.open {
Expand Down Expand Up @@ -74,6 +71,7 @@ body {
display: flex;
flex-direction: column;
align-items: center;
padding-top: 20px;
}
h1 {
margin: 0;
Expand Down Expand Up @@ -147,65 +145,3 @@ body {
background-color: #343a4069;
}
}

.navbar {
.container {
display: flex;
justify-content: space-between;
}
.user-data-container {
display: flex;
align-items: center;
p {
margin: 0 10px 0 0;
font-size: 14px;
}
}
}

.modal {
justify-content: center;
align-items: center;
transition: background 0.1s ease;
&.active {
display: flex;
background: rgba(0, 0, 0, 0.6);
}
.modal-dialog {
height: 100%;
display: flex;
align-items: center;
}
.modal-content {
position: relative;
display: flex;
flex-direction: column;
align-items: center;
padding: 50px 50px 25px;
.close {
position: absolute;
top: 5px;
right: 5px;
width: 25px;
outline: none;
}
button.save {
margin-top: 20px;
}
}
form.new_task {
display: flex;
width: 100%;
flex-direction: column;
align-items: center;
.input-group, .form-group, .field_with_errors, input {
width: 100%;
}
button {
width: 100px;
}
}
.error {
color: red;
}
}
38 changes: 35 additions & 3 deletions app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,57 @@
class SessionsController < ApplicationController
before_action :require_guest, only: [:new, :create]

def new
@user = User.new
end

def create
@user = User.find_by(name: user_params[:name])
@user ||= User.create(user_params)
@existed_user = User.find_by(name: user_params[:name])
@user = @existed_user ? @existed_user : User.create(user_params)

if (user_params[:email] && !@existed_user)
UserMailer.welcome_email(@user).deliver_later
end

if @user.valid? && @user.authenticate(user_params[:password])
session[:user_id] = @user.id
redirect_to tasks_path
end
end

def find
@user = User.find_by(name: params[:user][:name])

return share_error('No such user') unless @user

if @user.id != current_user.id
current_user.users.push(@user)
flash[:success] = 'Successful shared'
redirect_to root_path
else
share_error('U cant do it with yourself ;)')
end
end

def share
@user = User.new
end

def destroy
session[:user_id] = nil
redirect_to new_session_path
end

private

def share_error(error)
flash[:error] = error

@user = User.new
render 'share'
end

def user_params
params.require(:user).permit(:name, :password)
params.require(:user).permit(:name, :password, :email)
end
end
7 changes: 6 additions & 1 deletion app/controllers/tasks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ class TasksController < ApplicationController

def index
@task = Task.new
@tasks = Task.for_dashboard(params).where(user_id: params[:user_id] || current_user)
@tasks = Task.for_dashboard(params, current_user)
@tasks_grid = initialize_grid(@tasks)
end

def new
@task = Task.new
end

def create
Expand Down
18 changes: 18 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,20 @@
module ApplicationHelper

def bootstrap_class_for flash_type
{
success: "alert-success",
error: "alert-danger",
alert: "alert-warning",
notice: "alert-info"}.stringify_keys[flash_type.to_s] || flash_type.to_s
end

def flash_messages(opts = {})
flash.each do |msg_type, message|
concat(content_tag(:div, message, class: "alert #{bootstrap_class_for(msg_type)}", role: "alert") do
concat content_tag(:button, 'x', class: "close", data: {dismiss: 'alert'})
concat message
end)
end
nil
end
end
9 changes: 9 additions & 0 deletions app/mailers/user_mailer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class UserMailer < ApplicationMailer
default from: '[email protected]'

def welcome_email(user)
@user = user

mail(to: @user.email, subject: "Wellcome, #{@user.name}#")
end
end
14 changes: 11 additions & 3 deletions app/models/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,20 @@ class Task < ApplicationRecord
validates :title, presence: true
validates :description, presence: true
validates_length_of :description, minimum: 5
validates :expire_at, inclusion: { in: (Date.today..Date.today+5.years) }
validates :expire_at, inclusion: {in: (Date.today..Date.today + 5.years)}
enum status: %i[todo done]

scope :q, ->(q) { where("title LIKE '%#{q}%'") unless q.blank? }
scope :status, ->(status) { where(status: status) unless status.blank? }
scope :expired, ->(val) { where("expire_at #{val == 'Expired' ? '<' : '>'} ?", Time.now) unless val.blank?}
scope :for_dashboard, ->(params) { q(params[:q]).status(params[:status]).expired(params[:expired]).paginate(page: params[:page], per_page: 5) }
scope :expired, ->(val) { where("expire_at #{val == 'Expired' ? '<' : '>'} ?", Time.now) unless val.blank? }
scope :for_dashboard, ->(params, current_user) do
users_tasks(current_user).q(params[:q]).status(params[:status]).expired(params[:expired])
end

scope :users_tasks, ->(current_user) {
id = current_user.id
friends = UserConnection.find_by_user_b_id(id)
where(user_id: friends ? [id, friends.user_a_id] : id)
}

end
5 changes: 4 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@ class User < ApplicationRecord

has_secure_password validations: false

has_many :tasks
has_many :user_connections, :foreign_key => :user_a_id, :dependent => :destroy
has_many :users, :through => :user_connections, :source => :user_b
has_many(:reverse_user_connections, :class_name => :UserConnection,
:foreign_key => :user_b_id, :dependent => :destroy)
end
4 changes: 4 additions & 0 deletions app/models/user_connection.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class UserConnection < ApplicationRecord
belongs_to :user_a, :class_name => :User
belongs_to :user_b, :class_name => :User
end
13 changes: 8 additions & 5 deletions app/views/layouts/_header.html.erb
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
<nav class="navbar navbar-expand-lg navbar-dark bg-dark fixed-top">
<nav class="navbar navbar-expand-sm navbar-dark bg-dark fixed-top">
<div class="container">
<a class="navbar-brand" href="#">
<img src="https://s.dou.ua/img/announces/RC-logo.png" width="150" height="30" alt="">
</a>
<div class="user-data-container">
<p class="name"><%= @current_user.name if @current_user %></p>
<%= button_to('Logout', 'session', method: "delete", remote: true, class: 'btn btn-outline-light',
data: {confirm: 'Are you sure?', disable_with: 'loading...'}) %>
<div class="navbar-nav mr-auto">
<%= link_to_if(@current_user, 'Add task', new_task_path, {:remote => true, 'data-toggle' => "modal", class: 'nav-link', 'data-target' => '#add-task-modal'}){} %>
<%= link_to_if(@current_user, 'Share', sessions_share_path, {:remote => true, 'data-toggle' => "modal", class: 'nav-link', 'data-target' => '#window-modal'}){} %>
</div>
<div class="navbar-nav ">
<span><%= "#{@current_user.name}," if @current_user %></span>
<%= link_to_if(@current_user, 'Logout', 'session', method: "delete", remote: true, class: 'btn btn-outline-light'){} %>
</div>
</div>
</nav>
Loading