diff --git a/app/assets/javascripts/questionbox/all.coffee b/app/assets/javascripts/questionbox/all.coffee index ab3a8e70..f458b601 100644 --- a/app/assets/javascripts/questionbox/all.coffee +++ b/app/assets/javascripts/questionbox/all.coffee @@ -3,11 +3,18 @@ $(document).on "click", "button[name=qb-all-ask]", -> btn.button "loading" $("textarea[name=qb-all-question]").attr "readonly", "readonly" + rcptSelect = ($ 'select[name=qb-all-rcpt]') + + rcpt = if rcptSelect.length > 0 + rcptSelect.first().val() + else + 'followers' + $.ajax url: '/ajax/ask' type: 'POST' data: - rcpt: "followers" + rcpt: rcpt question: $("textarea[name=qb-all-question]").val() anonymousQuestion: false success: (data, status, jqxhr) -> diff --git a/app/controllers/ajax/question_controller.rb b/app/controllers/ajax/question_controller.rb index e7387107..68af9982 100644 --- a/app/controllers/ajax/question_controller.rb +++ b/app/controllers/ajax/question_controller.rb @@ -25,6 +25,19 @@ class Ajax::QuestionController < ApplicationController Inbox.create!(user_id: f.id, question_id: question.id, new: true) end end + elsif params[:rcpt].start_with? 'grp:' + unless current_user.nil? + begin + current_user.groups.find_by_name!(params[:rcpt].sub 'grp:', '').members.each do |m| + Inbox.create!(user_id: m.user.id, question_id: question.id, new: true) + end + rescue ActiveRecord::RecordNotFound + @status = :not_found + @message = "Group not found" + @success = false + return + end + end else Inbox.create!(user_id: params[:rcpt], question_id: question.id, new: true) end diff --git a/app/models/group.rb b/app/models/group.rb index 367a2821..6f80a27d 100644 --- a/app/models/group.rb +++ b/app/models/group.rb @@ -4,6 +4,7 @@ class Group < ActiveRecord::Base before_validation do self.name = self.display_name.downcase.sub(/\s+/, '-') + self.name = 'group-followers' if self.name == 'followers' end alias members group_members diff --git a/app/views/shared/_modal_ask_followers.html.haml b/app/views/shared/_modal_ask_followers.html.haml index e9b41fd1..1df6e78d 100644 --- a/app/views/shared/_modal_ask_followers.html.haml +++ b/app/views/shared/_modal_ask_followers.html.haml @@ -12,10 +12,10 @@ - if current_user.groups.count > 0 %label Choose group: - %select{name: 'qb-all-target', class: 'form-control', autocomplete: 'off'} - %option{value: '_followers', selected: true} Followers + %select{name: 'qb-all-rcpt', class: 'form-control', autocomplete: 'off'} + %option{value: 'followers', selected: true} Followers %optgroup{label: 'Groups'} - current_user.groups.each do |group| - %option{value: group.name}= group.display_name + %option{value: "grp:#{group.name}"}= group.display_name %button.btn.btn-default{"data-dismiss" => "modal", :type => "button"} Cancel %button.btn.btn-primary{name: 'qb-all-ask', :type => "button", data: {loading_text: 'Asking...' }} Ask \ No newline at end of file