-
Notifications
You must be signed in to change notification settings - Fork 0
Chat feature #8
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
IgorChalenko
wants to merge
15
commits into
master
Choose a base branch
from
chat_feature
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Chat feature #8
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
b6b660a
Add poll model, make some tests, create view dor poll, also make poll…
IgorChalenko 0c6d9de
Makes some changes
IgorChalenko b5a1dbe
Create some view update models
IgorChalenko 096e9ce
Create edit and destroy action, update some view
IgorChalenko 2429ec9
Add PollOption model
IgorChalenko b865207
Add polyci to controller, test option model, scopes, add options to view
IgorChalenko 7cf537e
Make user invite functional, poll options with poll, chat view
IgorChalenko 0a54e80
Make some changes
IgorChalenko 1f4f250
Add js to add button, change vote controller, add vote policy
IgorChalenko d9b3261
Try Active Cable
IgorChalenko 1149111
some change
IgorChalenko 94efdad
Create simple chat functional
IgorChalenko 95f8492
delete coverage
IgorChalenko a58089d
add coverage to ignore
IgorChalenko df99b02
Some changes but not work
IgorChalenko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,3 +39,4 @@ | |
/yarn-error.log | ||
yarn-debug.log* | ||
.yarn-integrity | ||
dump.rdb |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class PollChannel < ApplicationCable::Channel | ||
|
||
def subscribed | ||
stream_from "poll_#{params[:room]}" | ||
end | ||
|
||
def speak(data) | ||
ActionCable.server.broadcast("poll_#{params[:room]}", { message: data["message"] }) | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class ChatsController < ApplicationController | ||
|
||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
class InvitesController < ApplicationController | ||
rescue_from ActiveRecord::RecordNotFound do | ||
render :new | ||
flash[:error] = 'User not found' | ||
end | ||
|
||
def new | ||
@poll = Poll.find(params[:poll_id]) | ||
authorize! @poll, to: :invite? | ||
end | ||
|
||
def create | ||
@poll = Poll.find(params[:poll_id]) | ||
authorize! @poll, to: :invite? | ||
@invited_user = User.where(username: params[:query]).or(User.where(email: params[:query])).take! | ||
IgorChalenko marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if !invited? | ||
@poll.members << @invited_user | ||
redirect_to polls_path, success: "You invite #{@invited_user.username} to your poll" | ||
else | ||
render :new | ||
flash[:error] = 'User alredy invited' | ||
end | ||
end | ||
|
||
private | ||
def invited? | ||
@poll.memberships.where(user_id: @invited_user.id).exists? | ||
end | ||
|
||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
class PollsController < ApplicationController | ||
|
||
rescue_from ActiveRecord::RecordNotFound do | ||
redirect_to polls_path, error: I18n.t('flash.poll.not_found') | ||
end | ||
|
||
def index | ||
authorize! Poll, to: :index? | ||
@active = current_user.polls.active | ||
@upcoming = current_user.polls.upcoming | ||
@ended = current_user.polls.ended | ||
end | ||
|
||
def show | ||
@poll = Poll.find(params[:id]) | ||
authorize! @poll, to: :show? | ||
@options = @poll.options | ||
@members = @poll.members.count | ||
end | ||
|
||
def new | ||
authorize! Poll, to: :create? | ||
@poll = Poll.new | ||
2.times { @poll.options.build } | ||
end | ||
|
||
def create | ||
authorize! Poll, to: :create? | ||
@poll = current_user.own_polls.new(poll_params) | ||
if @poll.save | ||
@poll.members << current_user | ||
redirect_to polls_path, success: I18n.t('flash.poll.create') | ||
else | ||
render :new | ||
end | ||
end | ||
|
||
def edit | ||
@poll = Poll.find(params[:id]) | ||
@options = @poll.options | ||
authorize! @poll, to: :update? | ||
end | ||
|
||
def update | ||
@poll = Poll.find(params[:id]) | ||
authorize! @poll, to: :update? | ||
|
||
if @poll.update(poll_params) | ||
redirect_to poll_path(@poll.id), success: I18n.t('flash.poll.update') | ||
else | ||
render :new | ||
end | ||
end | ||
|
||
def destroy | ||
@poll = Poll.find(params[:id]) | ||
authorize! @poll, to: :destroy? | ||
@poll.destroy | ||
redirect_to polls_path, success: I18n.t('flash.poll.deleted') | ||
end | ||
|
||
|
||
private | ||
|
||
def poll_params | ||
params.require(:poll).permit(:title, :description, :start_date, :end_date, options_attributes: [:id, :vote_option, :_destroy]) | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
class VotesController < ApplicationController | ||
|
||
def update | ||
@poll = current_user.polls.find(params[:poll_id]) | ||
@option = @poll.options.find(params[:id]) | ||
authorize! @poll, to: :vote? | ||
|
||
if @poll.memberships.update(poll_option_id: @option.id) | ||
redirect_to poll_path(@poll.id), success: 'Successfully voted' | ||
else | ||
render :show | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import consumer from "./consumer" | ||
|
||
|
||
|
||
const poll = document.querySelector('#poll'); | ||
const poll_id = poll.dataset.pollId; | ||
|
||
const pollChannel = consumer.subscriptions.create({channel: "PollChannel", room: `poll_${poll_id}`}, { | ||
connected() { | ||
// Called when the subscription is ready for use on the server | ||
|
||
console.log("Connected to the poll room!"); | ||
}, | ||
received(data) { | ||
let mess = document.createElement('span'), | ||
col = document.createElement('div'); | ||
col.classList.add('col', 'mb-4') | ||
mess.classList.add('bg-info', 'rounded-pill', 'text-white', 'p-2'); | ||
mess.innerHTML = data.message; | ||
col.insertAdjacentElement('afterbegin', mess); | ||
document.querySelector('#chat_holder').insertAdjacentElement('beforeend', col) | ||
|
||
console.log(data); | ||
}, | ||
|
||
disconnected() { | ||
// Called when the subscription has been terminated by the server | ||
}, | ||
speak(message) { | ||
this.perform('speak', { message: message }) | ||
} | ||
}); | ||
|
||
|
||
export default pollChannel; | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
document.addEventListener('DOMContentLoaded', () => { | ||
|
||
const addButton = document.querySelector('#btn'), | ||
options = document.querySelector('#options'); | ||
|
||
|
||
let counter = 2; | ||
|
||
addButton.addEventListener('click', (e) => { | ||
const optionArray = document.querySelectorAll('[name*="[vote_option]"]'); | ||
e.preventDefault(); | ||
if (optionArray.length < 5) { | ||
const col = document.createElement('div'); | ||
const input = `<input id="poll_options_attributes_${counter}_vote_option" class="form-control" placeholder="Option" | ||
type="text" name="poll[options_attributes][${counter}][vote_option]"></input> | ||
<input id="poll_options_attributes_${counter}__destroy" type="checkbox" value="1" | ||
name="poll[options_attributes][${counter}][_destroy]"></input>`; | ||
|
||
col.classList.add('col-4'); | ||
col.innerHTML = input; | ||
options.insertAdjacentElement('beforeend', col); | ||
|
||
counter++; | ||
} else { | ||
addButton.style.display = 'none'; | ||
|
||
} | ||
}); | ||
|
||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
class Poll < ApplicationRecord | ||
belongs_to :user | ||
has_many :memberships, class_name: 'PollMembership', foreign_key: "poll_id" | ||
has_many :members, -> { distinct }, through: :memberships, source: :user, dependent: :destroy | ||
has_many :options, class_name: "PollOption", dependent: :destroy | ||
|
||
accepts_nested_attributes_for :options, allow_destroy: true, reject_if: lambda {|attributes| attributes['vote_option'].blank?} | ||
|
||
|
||
validates :title, presence: true | ||
validates :start_date, presence: true | ||
validates :end_date, presence: true | ||
validate :end_date_before_start_date | ||
|
||
scope :upcoming, -> { where('start_date > ?', Date.today) } | ||
scope :active, -> { where('start_date <= ? and end_date >= ?', Date.today, Date.today) } | ||
scope :ended, -> { where('end_date < ?', Date.today) } | ||
|
||
private | ||
def end_date_before_start_date | ||
return if start_date.blank? || end_date.blank? | ||
|
||
errors.add(:end_date, "can't be earlier than start date ") if end_date <= start_date | ||
end | ||
|
||
|
||
|
||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class PollMembership < ApplicationRecord | ||
belongs_to :user | ||
belongs_to :poll | ||
belongs_to :poll_option, dependent: :destroy, optional: true | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class PollOption < ApplicationRecord | ||
has_many :memberships, dependent: :nullify, class_name: 'PollMembership' | ||
|
||
belongs_to :poll, counter_cache: :vote_count | ||
|
||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,4 +7,5 @@ class ApplicationPolicy < ActionPolicy::Base | |
def logged_in? | ||
user.present? | ||
end | ||
|
||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# frozen_string_literal: true | ||
|
||
class PollPolicy < ApplicationPolicy | ||
def index? | ||
logged_in? | ||
end | ||
|
||
def create? | ||
logged_in? | ||
end | ||
|
||
def show? | ||
invited? | ||
end | ||
|
||
def update? | ||
owner? | ||
end | ||
|
||
def destroy? | ||
owner? | ||
end | ||
|
||
def invite? | ||
owner? | ||
end | ||
def vote? | ||
invited? && !voted? | ||
end | ||
|
||
def owner? | ||
user.id == record.user_id | ||
end | ||
|
||
def invited? | ||
record.memberships.where(user_id: user.id).exists? | ||
end | ||
|
||
def voted? | ||
record.memberships.where(user_id: user.id).and(record.memberships.where.not(poll_option_id: nil)).exists? | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
# frozen_string_literal: true | ||
|
||
class UserPolicy < ApplicationPolicy | ||
|
||
def create? | ||
!logged_in? | ||
end | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<%= form_tag poll_invites_path, method: :post, class: "border rounded p-4 mt-5" do %> | ||
<div class="mb-3"> | ||
<%= text_field_tag :query, params[:query], class: "form-control" %> | ||
</div> | ||
<div class="col-3"> | ||
<%= submit_tag "Send invite", class: "btn btn-primary" %> | ||
</div> | ||
<% end %> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.