2020-04-28 11:27:59 -07:00
|
|
|
class Ajax::GroupController < AjaxController
|
2015-01-17 09:24:36 -08:00
|
|
|
def create
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:status] = :err
|
2015-01-17 09:24:36 -08:00
|
|
|
|
|
|
|
unless user_signed_in?
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:status] = :noauth
|
|
|
|
@response[:message] = I18n.t('messages.noauth')
|
2015-01-17 09:24:36 -08:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2015-01-17 10:53:34 -08:00
|
|
|
begin
|
|
|
|
params.require :name
|
2020-04-28 11:32:36 -07:00
|
|
|
rescue ActionController::ParameterMissing => e
|
|
|
|
NewRelic::Agent.notice_error(e)
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:status] = :toolong
|
|
|
|
@response[:message] = I18n.t('messages.group.create.noname')
|
2015-01-17 10:53:34 -08:00
|
|
|
return
|
|
|
|
end
|
2015-01-17 09:24:36 -08:00
|
|
|
params.require :user
|
|
|
|
|
|
|
|
begin
|
|
|
|
target_user = User.find_by_screen_name(params[:user])
|
|
|
|
group = Group.create! user: current_user, display_name: params[:name]
|
2020-04-28 11:32:36 -07:00
|
|
|
rescue ActiveRecord::RecordInvalid => e
|
|
|
|
NewRelic::Agent.notice_error(e)
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:status] = :toolong
|
|
|
|
@response[:message] = I18n.t('messages.group.create.toolong')
|
2015-01-17 09:24:36 -08:00
|
|
|
return
|
2020-04-28 11:32:36 -07:00
|
|
|
rescue ActiveRecord::RecordNotFound => e
|
|
|
|
NewRelic::Agent.notice_error(e)
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:status] = :notfound
|
|
|
|
@response[:message] = I18n.t('messages.group.create.notfound')
|
2015-01-17 09:24:36 -08:00
|
|
|
return
|
2020-04-28 11:32:36 -07:00
|
|
|
rescue ActiveRecord::RecordNotUnique => e
|
|
|
|
NewRelic::Agent.notice_error(e)
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:status] = :exists
|
|
|
|
@response[:message] = I18n.t('messages.group.create.exists')
|
2015-01-17 09:24:36 -08:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:status] = :okay
|
|
|
|
@response[:success] = true
|
|
|
|
@response[:message] = I18n.t('messages.group.create.okay')
|
2020-04-28 17:49:07 -07:00
|
|
|
@response[:render] = render_to_string(partial: 'modal/group/item', locals: { group: group, user: target_user })
|
2015-01-17 09:24:36 -08:00
|
|
|
end
|
|
|
|
|
2015-01-17 09:57:23 -08:00
|
|
|
def destroy
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:status] = :err
|
2015-01-17 09:57:23 -08:00
|
|
|
|
|
|
|
unless user_signed_in?
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:status] = :noauth
|
|
|
|
@response[:message] = I18n.t('messages.noauth')
|
2015-01-17 09:57:23 -08:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
params.require :group
|
|
|
|
|
|
|
|
begin
|
|
|
|
Group.where(user: current_user, name: params[:group]).first.destroy!
|
2020-04-28 11:32:36 -07:00
|
|
|
rescue ActiveRecord::RecordNotFound => e
|
|
|
|
NewRelic::Agent.notice_error(e)
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:status] = :notfound
|
|
|
|
@response[:message] = I18n.t('messages.group.destroy.notfound')
|
2015-01-17 09:57:23 -08:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:status] = :okay
|
|
|
|
@response[:success] = true
|
|
|
|
@response[:message] = I18n.t('messages.group.destroy.okay')
|
2015-01-17 09:57:23 -08:00
|
|
|
end
|
|
|
|
|
2015-01-17 08:50:25 -08:00
|
|
|
def membership
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:status] = :err
|
2015-01-17 08:50:25 -08:00
|
|
|
|
|
|
|
unless user_signed_in?
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:status] = :noauth
|
|
|
|
@response[:message] = I18n.t('messages.noauth')
|
2015-01-17 08:50:25 -08:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
params.require :user
|
|
|
|
params.require :group
|
|
|
|
params.require :add
|
|
|
|
|
|
|
|
add = params[:add] == 'true'
|
|
|
|
|
|
|
|
begin
|
|
|
|
group = current_user.groups.find_by_name(params[:group])
|
2020-04-28 11:32:36 -07:00
|
|
|
rescue ActiveRecord::RecordNotFound => e
|
|
|
|
NewRelic::Agent.notice_error(e)
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:status] = :notfound
|
|
|
|
@response[:message] = I18n.t('messages.group.membership.notfound')
|
2015-01-17 08:50:25 -08:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
target_user = User.find_by_screen_name(params[:user])
|
|
|
|
|
|
|
|
if add
|
|
|
|
group.add_member target_user if group.members.find_by_user_id(target_user.id).nil?
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:checked] = true
|
|
|
|
@response[:message] = I18n.t('messages.group.membership.add')
|
2015-01-17 08:50:25 -08:00
|
|
|
else
|
|
|
|
group.remove_member target_user unless group.members.find_by_user_id(target_user.id).nil?
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:checked] = false
|
|
|
|
@response[:message] = I18n.t('messages.group.membership.remove')
|
2015-01-17 08:50:25 -08:00
|
|
|
end
|
2015-05-08 16:31:31 -07:00
|
|
|
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:status] = :okay
|
|
|
|
@response[:success] = true
|
2015-01-17 08:50:25 -08:00
|
|
|
end
|
|
|
|
end
|