Skip to content

Commit

Permalink
Drugi komit tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
msypniewski511 committed Feb 25, 2022
1 parent 0602c8f commit 89d2d9b
Show file tree
Hide file tree
Showing 57 changed files with 778 additions and 55 deletions.
1 change: 1 addition & 0 deletions app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require jquery
//= require rails-ujs
//= require activestorage
//= require turbolinks
Expand Down
3 changes: 3 additions & 0 deletions app/assets/javascripts/channels.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
3 changes: 3 additions & 0 deletions app/assets/javascripts/replies.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
10 changes: 8 additions & 2 deletions app/assets/stylesheets/application.scss
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,13 @@
.discussion-title {
margin-bottom: 0;
}


.pt{
&-1 {
margin-top: 1rem;
}
}
.pt-5 {
margin-top: 5rem;
}


3 changes: 3 additions & 0 deletions app/assets/stylesheets/channels.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the Channels controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/replies.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the Replies controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
73 changes: 73 additions & 0 deletions app/controllers/channels_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
class ChannelsController < ApplicationController
before_action :set_channel, only: %i[ show edit update destroy ]

# GET /channels or /channels.json
def index
@channels = Channel.all
@discussions = Discussion.all
end

# GET /channels/1 or /channels/1.json
def show
@discussions = @channel.discussions
@channels = Channel.all
end

# GET /channels/new
def new
@channel = Channel.new
end

# GET /channels/1/edit
def edit
end

# POST /channels or /channels.json
def create
@channel = Channel.new(channel_params)

respond_to do |format|
if @channel.save
format.html { redirect_to channels_path, notice: "Channel was successfully created." }
format.json { render :show, status: :created, location: @channel }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @channel.errors, status: :unprocessable_entity }
end
end
end

# PATCH/PUT /channels/1 or /channels/1.json
def update
respond_to do |format|
if @channel.update(channel_params)
format.html { redirect_to channels_path, notice: "Channel was successfully updated." }
format.json { render :show, status: :ok, location: @channel }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @channel.errors, status: :unprocessable_entity }
end
end
end

# DELETE /channels/1 or /channels/1.json
def destroy
@channel.destroy

respond_to do |format|
format.html { redirect_to channels_url, notice: "Channel was successfully destroyed." }
format.json { head :no_content }
end
end

private
# Use callbacks to share common setup or constraints between actions.
def set_channel
@channel = Channel.find(params[:id])
end

# Only allow a list of trusted parameters through.
def channel_params
params.require(:channel).permit(:channel)
end
end
17 changes: 12 additions & 5 deletions app/controllers/discussions_controller.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
class DiscussionsController < ApplicationController
before_action :set_discussion, only: %i[ show edit update destroy ]
before_action :set_channels, only: %i[index show new edit create]
before_action :authenticate_user!, except: [:index, :show]

# GET /discussions or /discussions.json
def index
@discussions = Discussion.all
@discussions = Discussion.order('created_at desc')
end

# GET /discussions/1 or /discussions/1.json
def show
@discussions = Discussion.all.order('created_at desc')
end

# GET /discussions/new
def new
@discussion = Discussion.new
@discussion = current_user.discussions.build
end

# GET /discussions/1/edit
Expand All @@ -21,8 +24,8 @@ def edit

# POST /discussions or /discussions.json
def create
@discussion = Discussion.new(discussion_params)

@discussion = current_user.discussions.build(discussion_params)
@discussion.errors.each{|error| puts '-----------------------------'}
respond_to do |format|
if @discussion.save
format.html { redirect_to discussion_url(@discussion), notice: "Discussion was successfully created." }
Expand Down Expand Up @@ -63,8 +66,12 @@ def set_discussion
@discussion = Discussion.find(params[:id])
end

def set_channels
@channels = Channel.all.order('created_at desc')
end

# Only allow a list of trusted parameters through.
def discussion_params
params.require(:discussion).permit(:title, :content)
params.require(:discussion).permit(:title, :content, :channel_id)
end
end
78 changes: 78 additions & 0 deletions app/controllers/replies_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
class RepliesController < ApplicationController
before_action :authenticate_user!
before_action :set_reply, only: %i[ show edit update destroy ]
before_action :set_discussion

# GET /replies or /replies.json
def index
@replies = Reply.all
end

# GET /replies/1 or /replies/1.json
def show
end

# GET /replies/new
def new
@reply = @discussion.replies.new
end

# GET /replies/1/edit
def edit
end

# POST /replies or /replies.json
def create
@reply = @discussion.replies.create(reply_params)
@reply.user_id = current_user.id
respond_to do |format|
if @reply.save
format.html { redirect_to discussion_path(@discussion), notice: "Reply was successfully created." }
format.js
format.json { render :show, status: :created, location: @reply }
else
format.html { redirect_to discussion_path(@discussion), notice: "Reply wasn't successfully created." }
format.js
format.json { render json: @reply.errors, status: :unprocessable_entity }
end
end
end

# PATCH/PUT /replies/1 or /replies/1.json
def update
respond_to do |format|
if @reply.update(reply_params)
format.html { redirect_to discussion_path(@discussion), notice: "Reply was successfully updated." }
format.json { render :show, status: :ok, location: @reply }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @reply.errors, status: :unprocessable_entity }
end
end
end

# DELETE /replies/1 or /replies/1.json
def destroy
@reply.destroy

respond_to do |format|
format.html { redirect_to discussion_path(@discussion), notice: "Reply was successfully destroyed." }
format.json { head :no_content }
end
end

private
# Use callbacks to share common setup or constraints between actions.
def set_discussion
@discussion = Discussion.find(params[:discussion_id])
end

def set_reply
@reply = Reply.find(params[:id])
end

# Only allow a list of trusted parameters through.
def reply_params
params.require(:reply).permit(:reply)
end
end
2 changes: 2 additions & 0 deletions app/helpers/channels_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module ChannelsHelper
end
2 changes: 2 additions & 0 deletions app/helpers/replies_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module RepliesHelper
end
4 changes: 4 additions & 0 deletions app/models/channel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Channel < ApplicationRecord
has_many :discussions
has_many :users, through: :discussions
end
5 changes: 5 additions & 0 deletions app/models/discussion.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
class Discussion < ApplicationRecord
belongs_to :channel
belongs_to :user
has_many :replies, dependent: :destroy

validates :title, :content, presence: true
end
5 changes: 5 additions & 0 deletions app/models/reply.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Reply < ApplicationRecord
belongs_to :discussion
belongs_to :user
validates :reply, presence: true
end
2 changes: 2 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ class User < ApplicationRecord
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
has_many :discussions
has_many :channels, through: :discussions
end
2 changes: 2 additions & 0 deletions app/views/channels/_channel.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
json.extract! channel, :id, :channel, :created_at, :updated_at
json.url channel_url(channel, format: :json)
11 changes: 11 additions & 0 deletions app/views/channels/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<%= simple_form_for(@channel) do |f| %>
<%= f.error_notification %>

<div class="field">
<div class="control">
<%= f.input :channel, input_html: { class: 'input' }, wrapper: false, label_html: { class: 'label' } %>
</div>
</div>

<%= f.button :submit, class: 'button is-info' %>
<% end %>
3 changes: 3 additions & 0 deletions app/views/channels/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<h1 class="title is-3">Editing Channel</h1>

<%= render 'form', channel: @channel %>
37 changes: 37 additions & 0 deletions app/views/channels/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<h1 class="title is-2">Topics/Channels</h1>

<% if true #has_role?(:admin) %>
<div class="level">
<div class="level-left"></div>
<div class="level-right">
<%= link_to 'New Channel', new_channel_path, class:'button is-dark' %>
</div>
</div>
<table class="table is-fullwidth">
<thead>
<tr>
<th>Channel</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @channels.each do |channel| %>
<tr>
<td><%= link_to channel.channel, channel %></td>
<td>
<div class="buttons is-right">
<%= link_to 'View Channel', channel, class:'button' %>
<%= link_to 'Edit Channel', edit_channel_path(channel), class:'button' %>
<%= link_to 'Delete', channel, class:'button is-danger', method: :delete, data: { confirm: "Are you sure you want to delete this channel? " } %>
</div>
</td>
</tr>
<% end %>
</tbody>
</table>
<% else %>
<p class="has-text-center">Sorry! You don't have access this page.</p>
<% end %>
1 change: 1 addition & 0 deletions app/views/channels/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.array! @channels, partial: "channels/channel", as: :channel
6 changes: 6 additions & 0 deletions app/views/channels/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<div class="columns is-centered">
<div class="column is-4">
<h1 class="title is-3">Create a new topic/channel</h1>
<%= render 'form', channel: @channel %>
</div>
</div>
16 changes: 16 additions & 0 deletions app/views/channels/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<div class="level">
<div class="level-left">
<h1 class="title is-2"><%= @channel.channel %></h1>
</div>
<div class="level-right">
<% if true #has_role?(:admin) %>
<%= link_to 'Edit Channel', edit_channel_path(@channel), class:"button" %>
<% end %>
</div>
</div>
<hr />
<div class="columns">
<%= render 'shared/discussions' %>
<%= render 'discussions/sidebar' %>
</div>
1 change: 1 addition & 0 deletions app/views/channels/show.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.partial! "channels/channel", channel: @channel
Loading

0 comments on commit 89d2d9b

Please sign in to comment.