Skip to content

Commit

Permalink
Merge pull request #60 from RailsGEnthusiasts/guides
Browse files Browse the repository at this point in the history
Guides (issue #18)
  • Loading branch information
andrewhavens committed Sep 3, 2015
2 parents e279283 + 5dfee9c commit 44f96b9
Show file tree
Hide file tree
Showing 33 changed files with 559 additions and 10 deletions.
Binary file added app/assets/images/Guide.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions app/assets/stylesheets/application.css.scss
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@
@import "search";
@import "tags";
@import "vote";
@import "guide_categories";
@import "guides";
13 changes: 13 additions & 0 deletions app/assets/stylesheets/guide_categories.css.sass
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.category-btn
margin-right: 5px

.dropdown
display: inline-block

.create-new-field
padding: 0
padding-right: 5px
display: inline-block

.btn-new-category
margin-top: 5px
25 changes: 25 additions & 0 deletions app/assets/stylesheets/guides.css.sass
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
.guide-nav
padding-top: 10px

.guide-xs
display: inline-block

.guide-nav a:hover
background-color: #FF704D

.guide-nav a:link
font-size: 25px
font-weight: bold
color: #FFFFFF
text-decoration: none
text-shadow: 2px 2px #943327

.guide-nav a:visited
color: #FFFFFF

.guide-nav .text
vertical-align: middle

.show
margin-top: 10px
margin-right: 5px
2 changes: 0 additions & 2 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ class ApplicationController < ActionController::Base
protect_from_forgery with: :exception

include Clearance::Controller
alias_method :require_authentication, :authorize # to avoid conflict with Pundit's authorize method

include Pundit # for authorization

# TODO: enable this once most areas have started using pundit for authorization
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class CommentsController < ApplicationController
before_action :set_comment, only: [:show, :edit, :update, :destroy]
before_action :require_authentication, except: [:index, :show]
before_action :require_login, except: [:index, :show]

# GET /comments
# GET /comments.json
Expand Down
74 changes: 74 additions & 0 deletions app/controllers/guide_categories_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
class GuideCategoriesController < ApplicationController
before_action :set_guide_category, only: [:show, :edit, :update, :destroy]

# GET /guide_categories
# GET /guide_categories.json
def index
@guide_categories = GuideCategory.all
end

# GET /guide_categories/1
# GET /guide_categories/1.json
def show
end

# GET /guide_categories/new
def new
@guide_category = GuideCategory.new
end

# GET /guide_categories/1/edit
def edit
end

# POST /guide_categories
# POST /guide_categories.json
def create
@guide_category = GuideCategory.new(guide_category_params)

respond_to do |format|
if @guide_category.save
format.html { redirect_to @guide_category, notice: 'Guide Category was successfully created.' }
format.json { render action: 'show', status: :created, location: @guide_category }
else
format.html { render action: 'new' }
format.json { render json: @guide_category.errors, status: :unprocessable_entity }
end
end
end

# PATCH/PUT /guide_categories/1
# PATCH/PUT /guide_categories/1.json
def update
respond_to do |format|
if @guide_category.update(guide_category_params)
format.html { redirect_to @guide_category, notice: 'guide category was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @guide_category.errors, status: :unprocessable_entity }
end
end
end

# DELETE /guide_categories/1
# DELETE /guide_categories/1.json
def destroy
@guide_category.destroy
respond_to do |format|
format.html { redirect_to guide_categories_url }
format.json { head :no_content }
end
end

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

# Never trust parameters from the scary internet, only allow the white list through.
def guide_category_params
params.require(:guide_category).permit(:name)
end
end
97 changes: 97 additions & 0 deletions app/controllers/guides_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
class GuidesController < ApplicationController
before_action :set_guide, only: [:show, :edit, :update, :destroy]
before_action :require_login, except: [:index, :show]

# GET /guide
# GET /guide.json
def index
@guide_categories = GuideCategory.all
@guides = Guide.all
end

# GET /guide/1
# GET /guide/1.json
def show
@comment = Comment.new
@comment.parent = @guide
end

# GET /guide/new
def new
@guide_categories = GuideCategory.all
@guide = Guide.new(new_guide_params)
end

# GET /guide/1/edit
def edit
@guide_categories = GuideCategory.all
end

# POST /guide
# POST /guide.json
def create
@guide_categories = GuideCategory.all
@guide = Guide.new(guide_params)
@guide.user = current_user

respond_to do |format|
if @guide.save
format.html { redirect_to post_path(@guide), notice: 'Guide was successfully created.' }
format.json { render action: 'show', status: :created, location: @guide }
else
format.html { render action: 'new' }
format.json { render json: @guide.errors, status: :unprocessable_entity }
end
end
end

# PATCH/PUT /guide/1
# PATCH/PUT /guide/1.json
def update
authorize @guide
respond_to do |format|
if @guide.update(guide_params)
format.html { redirect_to post_path(@guide), notice: 'Guide was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @guide.errors, status: :unprocessable_entity }
end
end
end

# DELETE /guide/1
# DELETE /guide/1.json
def destroy
@guide.destroy
respond_to do |format|
format.html { redirect_to guide
_url }
format.json { head :no_content }
end
end

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

def new_guide_params
if params[:guide]
{guide_category_id: params[:guide][:guide_category_id]}
else
{}
end
end

# Never trust parameters from the scary internet, only allow the white list through.
def guide_params
params.require(:guide).permit(
:guide_category_id,
:body_markdown,
:user_id,
:title,
:tags_string)
end
end
2 changes: 1 addition & 1 deletion app/controllers/posts_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy]
before_action :require_authentication, except: [:index, :show]
before_action :require_login, except: [:index, :show]

# GET /posts
# GET /posts.json
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class UsersController < Clearance::UsersController

# all actions in this controller are scoped to the current_user
before_action :validate_editor, only: [:edit, :update]
before_action :require_authentication, only: [:edit_current_user]
before_action :require_login, only: [:edit_current_user]

def create
@user = User.new(user_params)
Expand Down
4 changes: 4 additions & 0 deletions app/models/guide.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Guide < InternalPost
belongs_to :category, class_name: 'GuideCategory', foreign_key: "guide_category_id"
validates_presence_of :category
end
8 changes: 8 additions & 0 deletions app/models/guide_category.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class GuideCategory < ActiveRecord::Base
has_many :guides
validates :name, presence: true

def add_category_index
@guide = Guide.new
end
end
11 changes: 11 additions & 0 deletions app/policies/guide_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
GuidePolicy = Struct.new(:user, :post) do
def update?
return false unless user
user.admin? || post.user == user
end

def destroy?
return false unless user
user.admin?
end
end
16 changes: 16 additions & 0 deletions app/views/guide_categories/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<h1>
Guide Categories
<%= link_to 'New Guide', new_guide_path, class: 'btn btn-primary pull-right' %>
</h1>

<table class="guide-categories table">
<tbody>
<% @guide_categories.each do |category| %>
<tr>
<td>
<%= link_to category.name, category %>
</td>
</tr>
<% end %>
</tbody>
</table>
20 changes: 20 additions & 0 deletions app/views/guide_categories/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<%= form_for(@guide_category) do |f| %>
<% if @guide_category.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@guide_category.errors.count, "error") %> prohibited this guide_category from being saved:</h2>

<ul>
<% @guide_category.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>

<div class="form-group">
<%= f.label :name, "Category Name" %>
<%= f.text_field :name, class: 'form-control input-lg' %>
</div>

<%= f.submit nil, class: 'btn btn-primary', value: 'Save Category' %>
<% end %>
17 changes: 17 additions & 0 deletions app/views/guide_categories/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<h1>
<%= @guide_category.name %>
<%= link_to 'New Guide', new_guide_path('guide[guide_category_id]' => @guide_category.id), class: 'btn btn-primary pull-right' %>
<%= link_to 'New Category', new_guide_category_path, class: 'btn btn-primary category-btn pull-right' %>
</h1>

<table class="guide-categories table">
<tbody>
<% @guide_category.guides.each do |guide| %>
<tr>
<td>
<%= link_to guide.title, guide %>
</td>
</tr>
<% end %>
</tbody>
</table>
39 changes: 39 additions & 0 deletions app/views/guides/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<h1>Editing Guide</h1>

<%= form_for(@guide, url: (@guide.new_record? ? guides_path : guide_path(@guide))) do |f| %>
<% if @guide.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@guide.errors.count, "error") %> prohibited this guide from being saved:</h2>

<ul>
<% @guide.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>

<div class="form-group">
<%= f.label :guide_category_id, 'Choose Category' %>
<%= f.select :guide_category_id, @guide_categories.map { |gc| [gc.name, gc.id] }, {}, {class: 'form-control'} %>
<%= link_to 'New Category', new_guide_category_path, class: 'btn btn-primary btn-xs btn-new-category' %>
</div>

<div class="form-group">
<%= f.label :title %>
<%= f.text_field :title, class: 'form-control input-lg' %>
</div>

<div class="form-group">
<%= f.label :body_markdown %>
<%= f.text_area :body_markdown, class: 'form-control', rows: 5 %>
</div>

<div class="form-group">
<%= f.label :tags_string, "Tags" %>
<%= f.text_field :tags_string, class: 'form-control' %>
</div>

<%= f.submit nil, class: 'btn btn-default' %>
<% end %>
Loading

0 comments on commit 44f96b9

Please sign in to comment.