Skip to content

Commit

Permalink
Added separation of recipient suggestions based on model. All specs p…
Browse files Browse the repository at this point in the history
…assing.
  • Loading branch information
nerakdon committed Aug 25, 2015
1 parent bf3675a commit ce51cbc
Show file tree
Hide file tree
Showing 16 changed files with 66 additions and 53 deletions.
11 changes: 6 additions & 5 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,20 @@ The `message_train` mixin takes the following options:

=== Smaller address book

By default, the address book will contain all objects of the current_user_method object's model type. To change this behavior, define an address book method on your user model, something like this:
By default, the address book will contain all objects of the current_user_method object's model type. To change this behavior, define an address book method on your recipient models, something like this:

def address_book
friends
def self.valid_recipients_for(user)
# Supposing you use rolify
with_role(:friend, user)
end

And in your model:

message_train address_book_method: :address_book
message_train address_book_method: :valid_recipients_for

Or in your initializer:

config.address_book_methods[:users] = :address_book
config.address_book_methods[:users] = :valid_recipients_for

== View Helpers

Expand Down
17 changes: 10 additions & 7 deletions app/assets/javascripts/message_train.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,17 @@ function process_results(data) {
}
}

function add_tag_magic(node) {
div = $(node);
$.getJSON('/box/in/participants.json', function(data){
input = div.children('input');
form = div.parents('form');
function add_tag_magic(selector) {
var div = $(selector);
var model = div.data('model');
var request_string = '/box/in/participants/' + model + '.json';
$.getJSON(request_string, function(data){
var form = div.parents('form');
var suggestions = [];
$.each(data.participants, function (i, participant) {
suggestions.push(participant.slug);
});
tags = div.tags({
var tags = div.tags({
suggestions: suggestions,
tagSize: 'lg',
promptText: 'Comma separated list'
Expand Down Expand Up @@ -134,5 +135,7 @@ $(document).ready(function(){
$(this).find('.collapse').collapse('hide');
});

add_tag_magic(".recipient-input");
$('.recipient-input').each(function() {
add_tag_magic('#' + $(this).attr('id'));
});
});
6 changes: 3 additions & 3 deletions app/controllers/message_train/boxes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ module MessageTrain
class BoxesController < MessageTrain::ApplicationController
before_filter :load_conversations

# GET /box/in
# GET /box/:division
def show
@conversations = @conversations.page(params[:page])
render :show
end

# PATCH/PUT /box/in
# PATCH/PUT /box/:division
def update
if params[:mark_to_set].present? && @objects.present?
@box.mark(params[:mark_to_set], @objects)
end
respond_to_marking
end

# DELETE /box/in
# DELETE /box/:division
def destroy
if ['ignore', 'unignore'].include? params[:mark_to_set]
@box.send(params[:mark_to_set], @objects)
Expand Down
6 changes: 3 additions & 3 deletions app/controllers/message_train/conversations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@ class ConversationsController < MessageTrain::ApplicationController
before_filter :load_conversation
after_filter :mark_as_read

# GET /box/in/conversations/1
# GET /box/:division/conversations/:id
def show
@messages = @conversation.messages.page(params[:page])
render :show
@box.mark :read, @messages
end

# PATCH/PUT /box/in/conversations/1
# PATCH/PUT /box/:division/conversations/:id
def update
if params[:mark_to_set].present? && @objects.present?
@box.mark params[:mark_to_set], @objects
end
respond_to_marking
end

# DELETE /box/in/conversations/1
# DELETE /box/:division/conversations/:id
def destroy
if ['ignore', 'unignore'].include? params[:mark_to_set]
@box.send(params[:mark_to_set], @conversation)
Expand Down
10 changes: 5 additions & 5 deletions app/controllers/message_train/messages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ def show
end
end

# GET /box/in/messages/new
# GET /box/:division/messages/new
def new
@message = @box.new_message(conversation_id: params[:conversation_id])
end

# GET /box/in/messages/1/edit
# GET /box/:division/messages/:id/edit
def edit
unless @message.draft
raise ActiveRecord::RecordNotFound
end
end

# POST /box/in/messages
# POST /box/:division/messages
def create
@message = @box.send_message(message_params)
if @box.errors.all.empty?
Expand All @@ -36,7 +36,7 @@ def create
end
end

# PATCH/PUT /box/in/messages/1
# PATCH/PUT /box/:division/messages/:id
def update
unless @message.draft
raise ActiveRecord::RecordNotFound
Expand Down Expand Up @@ -67,7 +67,7 @@ def message_params
:body,
:draft,
attachments: [:attachment],
recipients_to_save: MessageTrain.configuration.recipient_tables
recipients_to_save: MessageTrain.configuration.recipient_tables.keys
)
end
end
Expand Down
20 changes: 14 additions & 6 deletions app/controllers/message_train/participants_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ class ParticipantsController < MessageTrain::ApplicationController
before_filter :load_participants
before_filter :load_participant, only: :show

# GET /box/in/participants
# GET /box/:division/participants/:model
def index
respond_to do |format|
format.json { render :index }
end
end

# GET /box/in/participants/1
# GET /box/:division/participants/:model/:id
def show
respond_to do |format|
format.json { render :show }
Expand All @@ -20,12 +20,20 @@ def show
private

def load_participants
if params[:model].nil?
raise ActiveRecord::RecordNotFound
end
model_sym = params[:model].to_sym
model = MessageTrain.configuration.recipient_tables[model_sym].constantize
method = MessageTrain.configuration.address_book_methods[model_sym]
fallback_method = MessageTrain.configuration.address_book_method
current_participant = send(MessageTrain.configuration.current_user_method)
method = MessageTrain.configuration.address_book_method
if current_participant.respond_to? method
@participants = current_participant.send(method)
if !method.nil? && model.respond_to?(method)
@participants = model.send(method, current_participant)
elsif !fallback_method.nil? && model.respond_to?(fallback_method)
@participants = model.send(fallback_method, current_participant)
else
@participants = current_participant.class.all
@participants = model.all
end
end

Expand Down
2 changes: 1 addition & 1 deletion app/views/message_train/messages/_form.html.haml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
- resource = message.new_record? ? message_train.box_messages_path(@box) : message_train.box_message_path(@box, message)
= bootstrap_form_for message, url: resource do |f|
- MessageTrain.configuration.recipient_tables.each do |table_sym|
- MessageTrain.configuration.recipient_tables.each do |table_sym, class_name|
= render partial: 'message_train/participants/field', locals: { message: message, field_name: table_sym }
= f.text_field :subject
= f.text_area :body
Expand Down
2 changes: 1 addition & 1 deletion app/views/message_train/participants/_field.html.haml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.form-group
%label.control-label= "#{field_name.to_s.singularize.humanize.titleize} Recipients"
.recipient-input.tag-list{ data: { field_name: "message[recipients_to_save][#{field_name}]" }, id: "message_recipients_to_save_#{field_name}" }
.recipient-input.tag-list{ data: { field_name: "message[recipients_to_save][#{field_name}]", model: field_name }, id: "message_recipients_to_save_#{field_name}" }
.tag-data= message.recipients_to_save[field_name]
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ json.id participant.id
json.model_name participant.class.name
json.slug box_participant_slug(participant)
json.name box_participant_name(participant)
json.path message_train.box_participant_path(@box, participant.id)
json.path message_train.box_model_participant_path(@box, participant.class.table_name, participant.id)
3 changes: 2 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
resources :boxes, path: 'box', param: :division, only: [:show, :update, :destroy] do
resources :conversations, only: [:show, :update, :destroy]
resources :messages, except: [:index, :destroy]
resources :participants, only: [:index, :show]
get 'participants/:model', as: :model_participants, to: 'participants#index'
get 'participants/:model/:id', as: :model_participant, to: 'participants#show'
end
end

Expand Down
4 changes: 2 additions & 2 deletions lib/message_train/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ class Configuration
:recipient_tables

def initialize
self.recipient_tables = [ :users ]
self.recipient_tables = {}
self.slug_columns = { users: :slug }
self.name_columns = { users: :name }
self.current_user_method = :current_user
self.user_sign_in_path = '/users/sign_in'
self.user_route_authentication_method = :user
self.address_book_method = :address_book
self.address_book_method = :address_book # This is a fallback
self.address_book_methods = {}
end

Expand Down
2 changes: 1 addition & 1 deletion lib/message_train/mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def message_train(options = {})
end

if relationships.include? :recipient
config.recipient_tables |= [table_sym] # This adds the table to the array if not present
config.recipient_tables[table_sym] = name
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

describe "GET #index" do
before do
get :index, box_division: 'in', format: :json
get :index, box_division: 'in', model: 'users', format: :json
end
it_should_behave_like 'a successful page', which_renders: 'index'

Expand All @@ -23,7 +23,7 @@

describe "GET #show" do
before do
get :show, box_division: 'in', id: first_user.id, format: :json
get :show, box_division: 'in', model: 'users', id: first_user.id, format: :json
end
it_should_behave_like 'a successful page', which_renders: 'show'

Expand Down
8 changes: 4 additions & 4 deletions spec/dummy/app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require 'group' #TODO This is a hack to get the group model to load in production. Works, but for how long?
require 'group' #TODO This is a hack to get the group model to load in development. Works, but for how long?

class User < ActiveRecord::Base
# Rolify Gem
Expand All @@ -15,9 +15,9 @@ class User < ActiveRecord::Base
:recoverable, :rememberable, :trackable, :validatable

# MessageTrain Gem
message_train slug_column: :slug, name_column: :display_name
message_train name_column: :display_name, address_book_method: :valid_recipients_for

def contacts
User.all + Group.all
def self.valid_recipients_for(sender)
all
end
end
Binary file modified spec/dummy/db/test.sqlite3
Binary file not shown.
22 changes: 11 additions & 11 deletions spec/dummy/public/capybara.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,24 @@
<div id="navbar-to-collapse" class="collapse navbar-collapse">
<ul class="nav navbar-nav navbar-right">
<li class="dropdown ">
<a role="button" id="1440294236.5479236" href="#" data-toggle="dropdown" class="dropdown-toggle">
<a role="button" id="1440524439.1923783" href="#" data-toggle="dropdown" class="dropdown-toggle">
Inbox <span class="badge ">
15
14
</span>

<b class="caret"></b>
</a>
<ul role="menu" class="dropdown-menu" aria-labelledby="1440294236.5479236">
<ul role="menu" class="dropdown-menu" aria-labelledby="1440524439.1923783">
<li class="">
<a href="/box/in">Inbox <span class="badge badge-info pull-right">
15
14
</span>
</a>
</li>

<li class="">
<a href="/box/sent">Sent Messages <span class="badge badge-info pull-right">
12
13
</span>
</a>
</li>
Expand Down Expand Up @@ -107,15 +107,15 @@ <h3>Messages</h3>
<li class="list-group-item">
<a href="/box/in">Inbox</a>
<span class="badge ">
15
14
</span>

</li>

<li class="list-group-item">
<a href="/box/sent">Sent Messages</a>
<span class="badge ">
12
13
</span>

</li>
Expand Down Expand Up @@ -164,15 +164,15 @@ <h1>New Message</h1>

</div>
<form method="post" accept-charset="UTF-8" action="/box/in/messages" id="new_message" class="new_message" role="form"><input type="hidden" value="" name="utf8" /><div class="form-group">
<label class="control-label">User Recipients</label>
<div id="message_recipients_to_save_users" data-field-name="message[recipients_to_save][users]" class="recipient-input tag-list bootstrap-tags bootstrap-3" style="padding-bottom: 2.6px;">
<label class="control-label">Group Recipients</label>
<div id="message_recipients_to_save_groups" data-model="groups" data-field-name="message[recipients_to_save][groups]" class="recipient-input tag-list bootstrap-tags bootstrap-3" style="padding-bottom: 2.6px;">
<div class="tag-data"></div>
<div class="tags"></div><input type="text" class="form-control tags-input input-lg" placeholder="Comma separated list" style="padding-left: 12px; padding-top: 0px; width: 663px;" /><ul class="tags-suggestion-list dropdown-menu"></ul></div>
</div>

<div class="form-group">
<label class="control-label">Group Recipients</label>
<div id="message_recipients_to_save_groups" data-field-name="message[recipients_to_save][groups]" class="recipient-input tag-list bootstrap-tags bootstrap-3" style="padding-bottom: 2.6px;">
<label class="control-label">User Recipients</label>
<div id="message_recipients_to_save_users" data-model="users" data-field-name="message[recipients_to_save][users]" class="recipient-input tag-list bootstrap-tags bootstrap-3" style="padding-bottom: 2.6px;">
<div class="tag-data"></div>
<div class="tags"></div><input type="text" class="form-control tags-input input-lg" placeholder="Comma separated list" style="padding-left: 12px; padding-top: 0px; width: 663px;" /><ul class="tags-suggestion-list dropdown-menu"></ul></div>
</div>
Expand Down

0 comments on commit ce51cbc

Please sign in to comment.