app/controllers/contacts_controller.rb in social_stream-base-1.1.11 vs app/controllers/contacts_controller.rb in social_stream-base-2.0.0.beta1
- old
+ new
@@ -1,63 +1,91 @@
class ContactsController < ApplicationController
- before_filter :authenticate_user!
- before_filter :exclude_reflexive, :except => [ :index, :pending ]
+ before_filter :authenticate_user!, except: [ :index ]
+ before_filter :exclude_reflexive, :except => [ :index, :suggestion, :pending ]
def index
+ subject = profile_or_current_subject!
+
+ params[:d] ||= 'sent'
+ params[:type] ||= subject.class.contact_index_models.first.to_s
+
+ @contacts = Contact
+
@contacts =
- total_contacts.
- merge(Actor.letter(params[:letter])).
- merge(Actor.name_search(params[:search])).
- related_by_param(params[:relation])
+ if params[:d] == 'received'
+ @contacts.received_by(subject).joins(:sender)
+ else
+ @contacts.sent_by(subject).joins(:receiver)
+ end
+ @contacts =
+ @contacts.
+ positive.
+ merge(Actor.subject_type(params[:type])).
+ merge(Actor.name_search(params[:q])).
+ related_by_param(params[:relation]).
+ page(params[:page])
+
respond_to do |format|
- format.html { @contacts = @contacts.page(params[:page]).per(10) }
- format.js { @contacts = @contacts.page(params[:page]).per(10) }
- format.json { render :text => to_json(@contacts) }
+ format.html { render @contacts if request.xhr? }
+ format.json { render json: @contacts.map(&:receiver), helper: self }
end
end
- def edit
- end
-
def update
# Record who is manipulating the contact, mainly in groups
@contact.user_author = current_user
# FIXME: This should be in the model
params[:contact][:relation_ids].present? &&
params[:contact][:relation_ids].delete("0")
- if @contact.update_attributes(params[:contact])
- redirect_to @contact.receiver_subject
- else
- render :action => 'edit'
+ @contact.update_attributes(params[:contact])
+
+ respond_to do |format|
+ format.html {
+ if @contact.errors.blank?
+ redirect_to @contact.receiver_subject
+ else
+ render :action => 'edit'
+ end
+ }
+
+ format.js
end
end
def destroy
- @contact.relation_ids = [Relation::Reject.instance.id]
+ relation_ids = []
+ if params[:reject].present?
+ relation_ids << Relation::Reject.instance.id
+ end
+
+ @contact.relation_ids = []
+
respond_to do |format|
format.js
end
end
- def pending
- total_contacts
+ # Return a suggestion for this contact
+ def suggestion
+ @contact = current_subject.suggestions.first
- @contacts = current_subject.pending_contacts
+ respond_to do |format|
+ format.html { @contact.present? ? render(partial: @contact) : render(text: "") }
+ format.json { render json: @contact }
+ end
+ end
+ def pending
+ @contact = current_subject.pending_contacts.last
+
respond_to do |format|
- format.html {
- @contacts = Kaminari.paginate_array(@contacts).page(params[:page]).per(10)
- render :action => :index
- }
- format.js {
- @contacts = Kaminari.paginate_array(@contacts).page(params[:page]).per(10)
- render :action => :index
- }
+ format.html { @contact.present? ? render(partial: @contact) : render(text: "") }
+ format.json { render json: @contact }
end
end
private
@@ -65,28 +93,9 @@
@contact = current_subject.sent_contacts.find params[:id]
if @contact.reflexive?
redirect_to home_path
end
- end
-
- def to_json(contacts)
- contacts.map{ |c|
- if params[:form].present?
- {
- 'key' => c.receiver_id.to_s,
- 'value' => self.class.helpers.truncate_name(c.receiver.name)
- }
- else
- {
- 'name' => c.receiver.name,
- 'url' => polymorphic_url(c.receiver_subject),
- 'image' => {
- 'url' => root_url + c.receiver.logo.url
- }
- }
- end
- }.to_json
end
def total_contacts
@total_contacts ||=
Contact.sent_by(current_subject).