-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
169 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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,10 +1,36 @@ | ||
class CategoriesController < ApplicationController | ||
before_action :authenticate_user!, only: [:subscribe, :unsubscribe] | ||
before_action :find_category, only: [:show, :subscribe, :unsubscribe] | ||
|
||
def index | ||
@categories = Category.order(:name) | ||
end | ||
|
||
def show | ||
@category = Category.find_by_permalink! params[:permalink] | ||
@questions = @category.questions.includes(:answers, :author).recent.page(params[:page]).per(20) | ||
end | ||
|
||
def subscribe | ||
current_user.subscribe_to @category | ||
|
||
respond_to do |format| | ||
format.html { redirect_to @category } | ||
format.js | ||
end | ||
end | ||
|
||
def unsubscribe | ||
current_user.unsubscribe_from @category | ||
|
||
respond_to do |format| | ||
format.html { redirect_to @category } | ||
format.js | ||
end | ||
end | ||
|
||
private | ||
|
||
def find_category | ||
@category = Category.find_by_permalink! params[:permalink] | ||
end | ||
end |
This file contains 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,15 @@ | ||
module CategoriesHelper | ||
def link_to_category_subscription(category) | ||
return link_to('subscribe', new_user_session_path) unless user_signed_in? | ||
|
||
if current_user.subscribed_to? category | ||
title = 'unsubscribe' | ||
path = unsubscribe_category_path(category) | ||
else | ||
title = 'subscribe' | ||
path = subscribe_category_path(category) | ||
end | ||
|
||
link_to title, path, method: :post, remote: true, class: 'btn btn-primary', data: { category_id: category.id } | ||
end | ||
end |
This file contains 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 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 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 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 @@ | ||
$('a[data-category-id=<%= @category.id %>]').replaceWith('<%= escape_javascript(link_to_category_subscription @category) %>'); |
This file contains 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 @@ | ||
$('a[data-category-id=<%= @category.id %>]').replaceWith('<%= escape_javascript(link_to_category_subscription @category) %>'); |
This file contains 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 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 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,6 +1,9 @@ | ||
FactoryGirl.define do | ||
factory :subscription do | ||
subscriber | ||
# association :subscribable, factory: :category | ||
end | ||
|
||
factory :category_subscription, parent: :subscription do | ||
association :subscribable, factory: :category | ||
end | ||
end |
This file contains 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