Skip to content

Commit

Permalink
Improve user interface for creating new messages in messages plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
paroga committed Sep 30, 2017
1 parent 2264351 commit 445883d
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 15 deletions.
32 changes: 27 additions & 5 deletions plugins/messages/app/models/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Message < ActiveRecord::Base
belongs_to :reply_to_message, :class_name => "Message", :foreign_key => "reply_to"

serialize :recipients_ids, Array
attr_accessor :send_to_all, :recipient_tokens
attr_accessor :send_method, :recipient_tokens, :order_id

scope :pending, -> { where(:email_state => 0) }
scope :sent, -> { where(:email_state => 1) }
Expand All @@ -25,13 +25,18 @@ class Message < ActiveRecord::Base
validates_length_of :subject, :in => 1..255
validates_inclusion_of :email_state, :in => EMAIL_STATE.values

after_initialize do
@send_method = 'recipients' unless @send_method
end

before_create :create_salt
before_validation :clean_up_recipient_ids, :on => :create

def clean_up_recipient_ids
add_recipients Group.find(group_id).users unless group_id.blank?
add_recipients Order.find(order_id).users_ordered unless order_id.blank?
self.recipients_ids = recipients_ids.uniq.reject { |id| id.blank? } unless recipients_ids.nil?
self.recipients_ids = User.undeleted.collect(&:id) if send_to_all == "1"
self.recipients_ids = User.undeleted.collect(&:id) if send_method == :all
end

def add_recipients(users)
Expand All @@ -40,14 +45,31 @@ def add_recipients(users)
end

def group_id=(group_id)
@group_id = group_id
add_recipients Group.find(group_id).users unless group_id.blank?
group = Group.find(group_id)
@send_method = 'workgroup' if group.type == 'Workgroup'
@send_method = 'messagegroup' if group.type == 'Messagegroup'
super
end

def workgroup_id
@group_id if @send_method == 'workgroup'
end

def messagegroup_id
@group_id if @send_method == 'messagegroup'
end

def workgroup_id=(workgroup_id)
@group_id = workgroup_id if @send_method == 'workgroup'
end

def messagegroup_id=(messagegroup_id)
@group_id = messagegroup_id if @send_method == 'messagegroup'
end

def order_id=(order_id)
@order_id = order_id
add_recipients Order.find(order_id).users_ordered unless order_id.blank?
@send_method = 'order' unless @send_method
end

def recipient_tokens=(ids)
Expand Down
67 changes: 57 additions & 10 deletions plugins/messages/app/views/messages/new.haml
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,55 @@
theme: 'facebook'
});

$('#message_send_to_all').on('change', function() {
if ($(this).is(':checked')) {
$('#recipients').slideUp();
function slideRecipients() {
if ($('#message_send_to_all').is(':checked')) {
$('#recipients').slideDown();
} else {
$('#recipients').slideUp();
}
}

function updateSlideState() {
switch($('input[name="message[send_method]"]:checked').val()) {
case 'all':
$('#workgroup').slideUp();
$('#messagegroup').slideUp();
$('#order').slideUp();
$('#recipients').slideUp();
break;

case 'workgroup':
$('#workgroup').slideDown();
$('#messagegroup').slideUp();
$('#order').slideUp();
$('#recipients').slideDown();
break;

case 'messagegroup':
$('#workgroup').slideUp();
$('#messagegroup').slideDown();
$('#order').slideUp();
$('#recipients').slideDown();
break;

case 'order':
$('#workgroup').slideUp();
$('#messagegroup').slideUp();
$('#order').slideDown();
$('#recipients').slideDown();
break;

case 'recipients':
$('#workgroup').slideUp();
$('#messagegroup').slideUp();
$('#order').slideUp();
$('#recipients').slideDown();
break;
}
});
// make sure state is correct when loading
$('#recipients').toggle(!$('#message_send_to_all').is(':checked'));
}

$('input[name="message[send_method]"]').on('click', updateSlideState);
updateSlideState();
});

- title t('.title')
Expand All @@ -29,9 +69,7 @@
- if @message.reply_to
%p= t('.reply_to', link: link_to(t('.message'), message_path(@message.reply_to))).html_safe

- if FoodsoftConfig[:mailing_list].blank?
= f.input :send_to_all, :as => :boolean
- else
- unless FoodsoftConfig[:mailing_list].blank?
%b= t('.list.desc', list: mail_to(FoodsoftConfig[:mailing_list])).html_safe
%br/
%small{:style => "color:grey"}
Expand All @@ -42,9 +80,18 @@
- else
= t('.list.mail', email: mail_to(FoodsoftConfig[:mailing_list_subscribe])).html_safe

- send_method_collection = [:workgroup, :messagegroup, :order, :recipients]
- send_method_collection.unshift :all if FoodsoftConfig[:mailing_list].blank?
= f.input :send_method, :as => :radio_buttons, :collection => send_method_collection, :label => false, :label_method => ->(obj){ t("activerecord.attributes.message.send_method.#{obj}") }

#workgroup
= f.input :workgroup_id, :as => :select, include_blank: false, :collection => Workgroup.undeleted.order(:name).includes(:memberships).reject { |g| g.memberships.empty? }
#messagegroup
= f.input :messagegroup_id, :as => :select, include_blank: false, :collection => Messagegroup.undeleted.order(:name)
#order
= f.input :order_id, :as => :select, include_blank: false, :collection => Order.finished_not_closed.order('pickup DESC').includes(:supplier).limit(25)
#recipients
= f.input :recipient_tokens, :input_html => { 'data-pre' => User.where(id: @message.recipients_ids).map(&:token_attributes).to_json }
= f.input :group_id, :as => :select, :collection => Group.undeleted.order('type DESC', 'name ASC').reject { |g| g.memberships.empty? }
= f.input :private, inline_label: t('.hint_private')
= f.input :subject, input_html: {class: 'input-xxlarge'}
= f.input :body, input_html: {class: 'input-xxlarge', rows: 13}
Expand Down

0 comments on commit 445883d

Please sign in to comment.