help_center 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. checksums.yaml +7 -0
  2. data/.github/FUNDING.yml +12 -0
  3. data/.gitignore +10 -0
  4. data/.travis.yml +5 -0
  5. data/CHANGELOG.md +1 -0
  6. data/CODE_OF_CONDUCT.md +74 -0
  7. data/Gemfile +6 -0
  8. data/LICENSE.txt +21 -0
  9. data/README.md +153 -0
  10. data/Rakefile +10 -0
  11. data/app/assets/stylesheets/help_center.scss +119 -0
  12. data/app/controllers/help_center/application_controller.rb +37 -0
  13. data/app/controllers/help_center/notifications_controller.rb +19 -0
  14. data/app/controllers/help_center/support_categories_controller.rb +17 -0
  15. data/app/controllers/help_center/support_posts_controller.rb +72 -0
  16. data/app/controllers/help_center/support_threads_controller.rb +72 -0
  17. data/app/helpers/help_center/support_posts_helper.rb +29 -0
  18. data/app/helpers/help_center/support_threads_helper.rb +28 -0
  19. data/app/jobs/help_center/support_post_notification_job.rb +42 -0
  20. data/app/jobs/help_center/support_thread_notification_job.rb +40 -0
  21. data/app/mailers/help_center/user_mailer.rb +28 -0
  22. data/app/models/support_category.rb +13 -0
  23. data/app/models/support_post.rb +15 -0
  24. data/app/models/support_subscription.rb +19 -0
  25. data/app/models/support_thread.rb +85 -0
  26. data/app/views/help_center/support_posts/_form.html.erb +32 -0
  27. data/app/views/help_center/support_posts/_support_post.html.erb +65 -0
  28. data/app/views/help_center/support_posts/edit.html.erb +28 -0
  29. data/app/views/help_center/support_threads/_form.html.erb +56 -0
  30. data/app/views/help_center/support_threads/_support_thread.html.erb +30 -0
  31. data/app/views/help_center/support_threads/edit.html.erb +7 -0
  32. data/app/views/help_center/support_threads/index.html.erb +23 -0
  33. data/app/views/help_center/support_threads/new.html.erb +5 -0
  34. data/app/views/help_center/support_threads/show.html.erb +39 -0
  35. data/app/views/help_center/user_mailer/new_post.html.erb +12 -0
  36. data/app/views/help_center/user_mailer/new_thread.html.erb +12 -0
  37. data/app/views/layouts/help_center.html.erb +120 -0
  38. data/app/views/shared/_spacer.html.erb +1 -0
  39. data/bin/console +14 -0
  40. data/bin/setup +8 -0
  41. data/config/locales/en.yml +52 -0
  42. data/config/routes.rb +24 -0
  43. data/db/migrate/20170417012930_create_support_categories.rb +19 -0
  44. data/db/migrate/20170417012931_create_support_threads.rb +18 -0
  45. data/db/migrate/20170417012932_create_support_posts.rb +12 -0
  46. data/db/migrate/20170417012933_create_support_subscriptions.rb +11 -0
  47. data/help_center.gemspec +29 -0
  48. data/lib/generators/help_center/controllers_generator.rb +13 -0
  49. data/lib/generators/help_center/helpers_generator.rb +13 -0
  50. data/lib/generators/help_center/views_generator.rb +13 -0
  51. data/lib/help_center.rb +24 -0
  52. data/lib/help_center/engine.rb +10 -0
  53. data/lib/help_center/slack.rb +22 -0
  54. data/lib/help_center/support_user.rb +10 -0
  55. data/lib/help_center/version.rb +3 -0
  56. data/lib/help_center/will_paginate.rb +53 -0
  57. metadata +168 -0
@@ -0,0 +1,19 @@
1
+ class HelpCenter::NotificationsController < HelpCenter::ApplicationController
2
+ before_action :authenticate_user!
3
+ before_action :set_support_thread
4
+
5
+ def create
6
+ @support_thread.toggle_subscription(current_user)
7
+ redirect_to help_center.support_thread_path(@support_thread)
8
+ end
9
+
10
+ def show
11
+ redirect_to help_center.support_thread_path(@support_thread)
12
+ end
13
+
14
+ private
15
+
16
+ def set_support_thread
17
+ @support_thread = SupportThread.friendly.find(params[:support_thread_id])
18
+ end
19
+ end
@@ -0,0 +1,17 @@
1
+ class HelpCenter::SupportCategoriesController < HelpCenter::ApplicationController
2
+ before_action :set_category
3
+
4
+ def index
5
+ @support_threads = SupportThread.where(support_category: @category) if @category.present?
6
+ @support_threads = @support_threads.pinned_first.sorted.includes(:user, :support_category).paginate(per_page: 10, page: page_number)
7
+ render "help_center/support_threads/index"
8
+ end
9
+
10
+ private
11
+
12
+ def set_category
13
+ @category = SupportCategory.friendly.find(params[:id])
14
+ rescue ActiveRecord::RecordNotFound
15
+ redirect_to help_center.support_threads_path
16
+ end
17
+ end
@@ -0,0 +1,72 @@
1
+ class HelpCenter::SupportPostsController < HelpCenter::ApplicationController
2
+ before_action :authenticate_user!
3
+ before_action :set_support_thread
4
+ before_action :set_support_post, only: [:edit, :update, :destroy]
5
+ before_action :require_mod_or_author_for_post!, only: [:edit, :update, :destroy]
6
+ before_action :require_mod_or_author_for_thread!, only: [:solved, :unsolved]
7
+
8
+ def create
9
+ @support_post = @support_thread.support_posts.new(support_post_params)
10
+ @support_post.user_id = current_user.id
11
+
12
+ if @support_post.save
13
+ HelpCenter::SupportPostNotificationJob.perform_later(@support_post)
14
+ redirect_to help_center.support_thread_path(@support_thread, anchor: "support_post_#{@support_post.id}")
15
+ else
16
+ render template: "help_center/support_threads/show"
17
+ end
18
+ end
19
+
20
+ def edit
21
+ end
22
+
23
+ def update
24
+ if @support_post.update(support_post_params)
25
+ redirect_to help_center.support_thread_path(@support_thread)
26
+ else
27
+ render action: :edit
28
+ end
29
+ end
30
+
31
+ def destroy
32
+ @support_post.destroy!
33
+ redirect_to help_center.support_thread_path(@support_thread)
34
+ end
35
+
36
+ def solved
37
+ @support_post = @support_thread.support_posts.find(params[:id])
38
+
39
+ @support_thread.support_posts.update_all(solved: false)
40
+ @support_post.update_column(:solved, true)
41
+ @support_thread.update_column(:solved, true)
42
+
43
+ redirect_to help_center.support_thread_path(@support_thread, anchor: ActionView::RecordIdentifier.dom_id(@support_post))
44
+ end
45
+
46
+ def unsolved
47
+ @support_post = @support_thread.support_posts.find(params[:id])
48
+
49
+ @support_thread.support_posts.update_all(solved: false)
50
+ @support_thread.update_column(:solved, false)
51
+
52
+ redirect_to help_center.support_thread_path(@support_thread, anchor: ActionView::RecordIdentifier.dom_id(@support_post))
53
+ end
54
+
55
+ private
56
+
57
+ def set_support_thread
58
+ @support_thread = SupportThread.friendly.find(params[:support_thread_id])
59
+ end
60
+
61
+ def set_support_post
62
+ if is_moderator?
63
+ @support_post = @support_thread.support_posts.find(params[:id])
64
+ else
65
+ @support_post = current_user.support_posts.find(params[:id])
66
+ end
67
+ end
68
+
69
+ def support_post_params
70
+ params.require(:support_post).permit(:body)
71
+ end
72
+ end
@@ -0,0 +1,72 @@
1
+ class HelpCenter::SupportThreadsController < HelpCenter::ApplicationController
2
+ before_action :authenticate_user!, only: [:mine, :participating, :new, :create]
3
+ before_action :set_support_thread, only: [:show, :edit, :update]
4
+ before_action :require_mod_or_author_for_thread!, only: [:edit, :update]
5
+
6
+ def index
7
+ @support_threads = SupportThread.pinned_first.sorted.includes(:user, :support_category).paginate(page: page_number)
8
+ end
9
+
10
+ def answered
11
+ @support_threads = SupportThread.solved.sorted.includes(:user, :support_category).paginate(page: page_number)
12
+ render action: :index
13
+ end
14
+
15
+ def unanswered
16
+ @support_threads = SupportThread.unsolved.sorted.includes(:user, :support_category).paginate(page: page_number)
17
+ render action: :index
18
+ end
19
+
20
+ def mine
21
+ @support_threads = SupportThread.where(user: current_user).sorted.includes(:user, :support_category).paginate(page: page_number)
22
+ render action: :index
23
+ end
24
+
25
+ def participating
26
+ @support_threads = SupportThread.includes(:user, :support_category).joins(:support_posts).where(support_posts: { user_id: current_user.id }).distinct(support_posts: :id).sorted.paginate(page: page_number)
27
+ render action: :index
28
+ end
29
+
30
+ def show
31
+ @support_post = SupportPost.new
32
+ @support_post.user = current_user
33
+ end
34
+
35
+ def new
36
+ @support_thread = SupportThread.new
37
+ @support_thread.support_posts.new
38
+ end
39
+
40
+ def create
41
+ @support_thread = current_user.support_threads.new(support_thread_params)
42
+ @support_thread.support_posts.each{ |post| post.user_id = current_user.id }
43
+
44
+ if @support_thread.save
45
+ HelpCenter::SupportThreadNotificationJob.perform_later(@support_thread)
46
+ redirect_to help_center.support_thread_path(@support_thread)
47
+ else
48
+ render action: :new
49
+ end
50
+ end
51
+
52
+ def edit
53
+ end
54
+
55
+ def update
56
+ if @support_thread.update(support_thread_params)
57
+ redirect_to help_center.support_thread_path(@support_thread), notice: I18n.t('your_changes_were_saved')
58
+ else
59
+ render action: :edit
60
+ end
61
+ end
62
+
63
+ private
64
+
65
+ def set_support_thread
66
+ @support_thread = SupportThread.friendly.find(params[:id])
67
+ end
68
+
69
+ def support_thread_params
70
+ params.require(:support_thread).permit(:title, :content, :support_category_id, support_posts_attributes: [:body])
71
+ end
72
+ end
@@ -0,0 +1,29 @@
1
+ module HelpCenter::SupportPostsHelper
2
+ # Override this to use avatars from other places than Gravatar
3
+ def avatar_tag(email)
4
+ gravatar_image_tag(email, gravatar: { size: 40 }, class: "rounded avatar")
5
+ end
6
+
7
+ def support_category_link(category)
8
+ link_to category.name, help_center.support_category_support_threads_path(category),
9
+ style: "color: #{category.color}"
10
+ end
11
+
12
+ # Override this method to provide your own content formatting like Markdown
13
+ def formatted_content(text)
14
+ simple_format(text)
15
+ end
16
+
17
+ def support_post_classes(support_post)
18
+ klasses = ["support-post", "card", "mb-3"]
19
+ klasses << "solved" if support_post.solved?
20
+ klasses << "original-poster" if support_post.user == @support_thread.user
21
+ klasses
22
+ end
23
+
24
+ def support_user_badge(user)
25
+ if user.respond_to?(:moderator) && user.moderator?
26
+ content_tag :span, "Mod", class: "badge badge-default"
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,28 @@
1
+ module HelpCenter::SupportThreadsHelper
2
+ # Used for flagging links in the navbar as active
3
+ def support_link_to(path, opts={}, &block)
4
+ link_to path, class: support_link_class(path, opts), &block
5
+ end
6
+
7
+ def support_link_class(matches, opts={})
8
+ case matches
9
+ when Array
10
+ "active" if matches.any?{ |m| request.path.starts_with?(m) }
11
+ when String
12
+ "active" if opts.fetch(:exact, false) ? request.path == matches : request.path.starts_with?(matches)
13
+ end
14
+ end
15
+
16
+ # A nice hack to manipulate the layout so we can have sub-layouts
17
+ # without any changes in the user's application.
18
+ #
19
+ # We use this for rendering the sidebar layout for all the support pages
20
+ #
21
+ # https://mattbrictson.com/easier-nested-layouts-in-rails
22
+ #
23
+ def parent_layout(layout)
24
+ @view_flow.set(:layout, output_buffer)
25
+ output = render(file: "layouts/#{layout}")
26
+ self.output_buffer = ActionView::OutputBuffer.new(output)
27
+ end
28
+ end
@@ -0,0 +1,42 @@
1
+ class HelpCenter::SupportPostNotificationJob < ApplicationJob
2
+ include HelpCenter::Engine.routes.url_helpers
3
+
4
+ def perform(support_post)
5
+ send_emails(support_post) if HelpCenter.send_email_notifications
6
+ send_webhook(support_post) if HelpCenter.send_slack_notifications
7
+ end
8
+
9
+ def send_emails(support_post)
10
+ support_thread = support_post.support_thread
11
+ users = support_thread.subscribed_users - [support_post.user]
12
+ users.each do |user|
13
+ HelpCenter::UserMailer.new_post(support_post, user).deliver_later
14
+ end
15
+ end
16
+
17
+ def send_webhook(support_post)
18
+ slack_webhook_url = Rails.application.secrets.help_center_slack_url
19
+ return if slack_webhook_url.blank?
20
+
21
+ support_thread = support_post.support_thread
22
+ payload = {
23
+ fallback: "#{support_post.user.name} replied to <#{support_thread_url(support_thread, anchor: ActionView::RecordIdentifier.dom_id(support_post))}|#{support_thread.title}>",
24
+ pretext: "#{support_post.user.name} replied to <#{support_thread_url(support_thread, anchor: ActionView::RecordIdentifier.dom_id(support_post))}|#{support_thread.title}>",
25
+ fields: [
26
+ {
27
+ title: "Thread",
28
+ value: support_thread.title,
29
+ short: true
30
+ },
31
+ {
32
+ title: "Posted By",
33
+ value: support_post.user.name,
34
+ short: true,
35
+ },
36
+ ],
37
+ ts: support_post.created_at.to_i
38
+ }
39
+
40
+ HelpCenter::Slack.new(slack_webhook_url).post(payload)
41
+ end
42
+ end
@@ -0,0 +1,40 @@
1
+ class HelpCenter::SupportThreadNotificationJob < ApplicationJob
2
+ include HelpCenter::Engine.routes.url_helpers
3
+
4
+ def perform(support_thread)
5
+ send_emails(support_thread) if HelpCenter.send_email_notifications
6
+ send_webhook(support_thread) if HelpCenter.send_slack_notifications
7
+ end
8
+
9
+ def send_emails(support_thread)
10
+ support_thread.notify_users.each do |user|
11
+ HelpCenter::UserMailer.new_thread(support_thread, user).deliver_later
12
+ end
13
+ end
14
+
15
+ def send_webhook(support_thread)
16
+ slack_webhook_url = Rails.application.secrets.simple_discussion_slack_url
17
+ return if slack_webhook_url.blank?
18
+
19
+ support_post = support_thread.support_posts.first
20
+ payload = {
21
+ fallback: "A new discussion was started: <#{support_thread_url(support_thread, anchor: ActionView::RecordIdentifier.dom_id(support_posts))}|#{support_thread.title}>",
22
+ pretext: "A new discussion was started: <#{support_thread_url(support_thread, anchor: ActionView::RecordIdentifier.dom_id(support_posts))}|#{support_thread.title}>",
23
+ fields: [
24
+ {
25
+ title: "Thread",
26
+ value: support_thread.title,
27
+ short: true
28
+ },
29
+ {
30
+ title: "Posted By",
31
+ value: support_post.user.name,
32
+ short: true,
33
+ },
34
+ ],
35
+ ts: support_post.created_at.to_i
36
+ }
37
+
38
+ HelpCenter::Slack.new(slack_webhook_url).post(payload)
39
+ end
40
+ end
@@ -0,0 +1,28 @@
1
+ class HelpCenter::UserMailer < ApplicationMailer
2
+ # You can set the default `from` address in ApplicationMailer
3
+
4
+ helper HelpCenter::ForumPostsHelper
5
+ helper HelpCenter::Engine.routes.url_helpers
6
+
7
+ def new_thread(support_thread, recipient)
8
+ @support_thread = support_thread
9
+ @support_post = support_thread.support_posts.first
10
+ @recipient = recipient
11
+
12
+ mail(
13
+ to: "#{@recipient.name} <#{@recipient.email}>",
14
+ subject: @support_thread.title
15
+ )
16
+ end
17
+
18
+ def new_post(support_post, recipient)
19
+ @support_post = support_post
20
+ @support_thread = support_post.support_thread
21
+ @recipient = recipient
22
+
23
+ mail(
24
+ to: "#{@recipient.name} <#{@recipient.email}>",
25
+ subject: "New post in #{@support_thread.title}"
26
+ )
27
+ end
28
+ end
@@ -0,0 +1,13 @@
1
+ class SupportCategory < ApplicationRecord
2
+ extend FriendlyId
3
+ friendly_id :name, use: :slugged
4
+
5
+ scope :sorted, ->{ order(position: :asc) }
6
+
7
+ validates :name, :slug, :color, presence: true
8
+
9
+ def color
10
+ colour = super
11
+ colour.start_with?("#") ? colour : "##{colour}"
12
+ end
13
+ end
@@ -0,0 +1,15 @@
1
+ class SupportPost < ApplicationRecord
2
+ belongs_to :support_thread, counter_cache: true, touch: true
3
+ belongs_to :user
4
+ has_many :reactions, as: :reactable
5
+
6
+ validates :user_id, :body, presence: true
7
+
8
+ scope :sorted, ->{ order(:created_at) }
9
+
10
+ after_update :solve_support_thread, if: :solved?
11
+
12
+ def solve_support_thread
13
+ support_thread.update(solved: true)
14
+ end
15
+ end
@@ -0,0 +1,19 @@
1
+ class SupportSubscription < ApplicationRecord
2
+ belongs_to :support_thread
3
+ belongs_to :user
4
+
5
+ scope :optin, ->{ where(subscription_type: :optin) }
6
+ scope :optout, ->{ where(subscription_type: :optout) }
7
+
8
+ validates :subscription_type, presence: true, inclusion: { in: %w{ optin optout } }
9
+ validates :user_id, uniqueness: { scope: :support_thread_id }
10
+
11
+ def toggle!
12
+ case subscription_type
13
+ when "optin"
14
+ update(subscription_type: "optout")
15
+ when "optout"
16
+ update(subscription_type: "optin")
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,85 @@
1
+ class SupportThread < ApplicationRecord
2
+ extend FriendlyId
3
+ friendly_id :title, use: :slugged
4
+
5
+ belongs_to :support_category
6
+ belongs_to :user
7
+ has_many :support_posts
8
+ has_many :support_subscriptions
9
+ has_many :optin_subscribers, ->{ where(support_subscriptions: { subscription_type: :optin }) }, through: :support_subscriptions, source: :user
10
+ has_many :optout_subscribers, ->{ where(support_subscriptions: { subscription_type: :optout }) }, through: :support_subscriptions, source: :user
11
+ has_many :users, through: :support_posts
12
+
13
+ has_rich_text :content
14
+
15
+ accepts_nested_attributes_for :support_posts
16
+
17
+ validates :support_category, presence: true
18
+ validates :user_id, :title, presence: true
19
+ validates_associated :support_posts
20
+
21
+ scope :pinned_first, ->{ order(pinned: :desc) }
22
+ scope :solved, ->{ where(solved: true) }
23
+ scope :sorted, ->{ order(position: :asc) }
24
+ scope :unpinned, ->{ where.not(pinned: true) }
25
+ scope :unsolved, ->{ where.not(solved: true) }
26
+
27
+ def subscribed_users
28
+ (users + optin_subscribers).uniq - optout_subscribers
29
+ end
30
+
31
+ def subscription_for(user)
32
+ return nil if user.nil?
33
+ support_subscriptions.find_by(user_id: user.id)
34
+ end
35
+
36
+ def subscribed?(user)
37
+ return false if user.nil?
38
+
39
+ subscription = subscription_for(user)
40
+
41
+ if subscription.present?
42
+ subscription.subscription_type == "optin"
43
+ else
44
+ support_posts.where(user_id: user.id).any?
45
+ end
46
+ end
47
+
48
+ def toggle_subscription(user)
49
+ subscription = subscription_for(user)
50
+
51
+ if subscription.present?
52
+ subscription.toggle!
53
+ elsif support_posts.where(user_id: user.id).any?
54
+ support_subscriptions.create(user: user, subscription_type: "optout")
55
+ else
56
+ support_subscriptions.create(user: user, subscription_type: "optin")
57
+ end
58
+ end
59
+
60
+ def subscribed_reason(user)
61
+ return I18n.t('.not_receiving_notifications') if user.nil?
62
+
63
+ subscription = subscription_for(user)
64
+
65
+ if subscription.present?
66
+ if subscription.subscription_type == "optout"
67
+ I18n.t('.ignoring_thread')
68
+ elsif subscription.subscription_type == "optin"
69
+ I18n.t('.receiving_notifications_because_subscribed')
70
+ end
71
+ elsif support_posts.where(user_id: user.id).any?
72
+ I18n.t('.receiving_notifications_because_posted')
73
+ else
74
+ I18n.t('.not_receiving_notifications')
75
+ end
76
+ end
77
+
78
+ # These are the users to notify on a new thread. Currently this does nothing,
79
+ # but you can override this to provide whatever functionality you like here.
80
+ #
81
+ # For example: You might use this to send all moderators an email of new threads.
82
+ def notify_users
83
+ []
84
+ end
85
+ end