Skip to content

Commit

Permalink
Category model associations, category controller and views, show cate…
Browse files Browse the repository at this point in the history
…gories on root
  • Loading branch information
jrdnbwmn committed Apr 27, 2018
1 parent d494549 commit 849595f
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 54 deletions.
3 changes: 0 additions & 3 deletions app/assets/javascripts/welcome.coffee

This file was deleted.

24 changes: 24 additions & 0 deletions app/controllers/categories_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class CategoriesController < ApplicationController

def index
end

def show
end

def new
end

def edit
end

def create
end

def update
end

def destroy
end

end
8 changes: 7 additions & 1 deletion app/controllers/documents_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ class DocumentsController < ApplicationController
# GET /documents.json
def index
@documents = Document.where(user_id: current_user)
@categories = Category.where(user_id: current_user)
@user = current_user
@categories = @user.categories.all
end

# GET /documents/1
Expand All @@ -26,6 +27,8 @@ def edit
# POST /documents.json
def create
@document = current_user.documents.build(document_params)
@user = current_user
@categories = @user.categories.all

respond_to do |format|
if @document.save
Expand All @@ -41,6 +44,9 @@ def create
# PATCH/PUT /documents/1
# PATCH/PUT /documents/1.json
def update
@user = current_user
@categories = @user.categories.all

respond_to do |format|
if @document.update(document_params)
format.html { render :edit }
Expand Down
2 changes: 2 additions & 0 deletions app/helpers/categories_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module CategoriesHelper
end
3 changes: 3 additions & 0 deletions app/models/category.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
class Category < ApplicationRecord

has_many :documents

end
2 changes: 2 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ class User < ApplicationRecord
:recoverable, :rememberable, :trackable, :validatable

has_many :documents

has_many :categories, through: :documents

end
5 changes: 5 additions & 0 deletions app/views/categories/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<p>
<strong>Category:</strong>
</p>

<%= link_to 'Back', root_path %>
75 changes: 27 additions & 48 deletions app/views/documents/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,55 +12,34 @@

<!-- Tags -->
<ul class="no-bullets">
<!-- Todo -->
<li class="">
<a href="" class="link link--highlight pl2 pr2 pts pbs block">
<div class="grid-row">
<span class="grid-column ellipsis pr0">All</span>
<span class="grid-column span-3 align--right pl0">22</span>
</div>
</a>
</li>
<li class="">
<a href="" class="link link--highlight pl2 pr2 pts pbs block">
<div class="grid-row">
<span class="grid-column ellipsis pr0">blog</span>
<span class="grid-column span-3 align--right pl0">7</span>
</div>
</a>
</li>
<li class="">
<a href="" class="link link--highlight pl2 pr2 pts pbs block">
<div class="grid-row">
<span class="grid-column ellipsis pr0">business</span>
<span class="grid-column span-3 align--right pl0">3</span>
</div>
</a>
</li>
<li class="">
<a href="" class="link link--highlight pl2 pr2 pts pbs block">
<div class="grid-row">
<span class="grid-column ellipsis pr0">essays</span>
<span class="grid-column span-3 align--right pl0">2</span>
</div>
</a>
</li>
<li class="">
<a href="" class="link link--highlight pl2 pr2 pts pbs block">
<div class="grid-row">
<span class="grid-column ellipsis pr0">journal</span>
<span class="grid-column span-3 align--right pl0">10</span>
</div>
</a>
</li>
<li class="">
<a href="" class="link link--highlight pl2 pr2 pts pbs block">
<div class="grid-row">
<span class="grid-column">Archived</span>
<span class="grid-column span-3 align--right"></span>
</div>
</a>
<li>
<%= link_to root_path, class: 'link link--highlight pl2 pr2 pts pbs block' do %>
<div class="grid-row">
<span class="grid-column ellipsis pr0">All</span>
<span class="grid-column span-3 align--right pl0">22</span>
</div>
<% end %>
</li>
<% @categories.each do |category| %>
<li>
<%= link_to category_path(category), class: 'link link--highlight pl2 pr2 pts pbs block' do %>
<div class="grid-row">
<span class="grid-column ellipsis pr0"><%= category.name %></span>
<span class="grid-column span-3 align--right pl0">#</span>
</div>
<% end %>
</li>
<% end %>

<!-- Todo -->
<li class="">
<a href="" class="link link--highlight pl2 pr2 pts pbs block">
<div class="grid-row">
<span class="grid-column">Archived</span>
<span class="grid-column span-3 align--right"></span>
</div>
</a>
</li>

</ul>

Expand Down
2 changes: 0 additions & 2 deletions app/views/documents/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
<p id="notice"><%= notice %></p>

<p>
<strong>Title:</strong>
<%= @document.title %>
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
get 'welcome/index'

resources :documents
resources :categories

authenticated :user do
root to: "documents#index", as: "authenticated_root"
Expand Down
7 changes: 7 additions & 0 deletions test/controllers/categories_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class CategoriesControllerTest < ActionDispatch::IntegrationTest
# test "the truth" do
# assert true
# end
end

0 comments on commit 849595f

Please sign in to comment.