Retrospring/app/controllers/ajax/group_controller.rb

116 lines
2.9 KiB
Ruby
Raw Normal View History

class Ajax::GroupController < ApplicationController
2015-05-08 16:31:31 -07:00
rescue_from(ActionController::ParameterMissing) do |param_miss_ex|
@status = :parameter_error
2015-06-07 09:24:01 -07:00
@message = I18n.t('messages.parameter_error', parameter: param_miss_ex.param.capitalize)
2015-05-08 16:31:31 -07:00
@success = false
render partial: "ajax/shared/status"
end
2015-01-17 09:24:36 -08:00
def create
@status = :err
@success = false
unless user_signed_in?
@status = :noauth
2015-06-07 09:24:01 -07:00
@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
rescue ActionController::ParameterMissing
@status = :toolong
2015-06-07 09:24:01 -07:00
@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]
rescue ActiveRecord::RecordInvalid
2015-01-17 10:53:34 -08:00
@status = :toolong
2015-06-07 09:24:01 -07:00
@message = I18n.t('messages.group.create.toolong')
2015-01-17 09:24:36 -08:00
return
rescue ActiveRecord::RecordNotFound
@status = :notfound
2015-06-07 09:24:01 -07:00
@message = I18n.t('messages.group.create.notfound')
2015-01-17 09:24:36 -08:00
return
rescue ActiveRecord::RecordNotUnique
@status = :exists
2015-06-07 09:24:01 -07:00
@message = I18n.t('messages.group.create.exists')
2015-01-17 09:24:36 -08:00
return
end
@status = :okay
@success = true
2015-06-07 09:24:01 -07:00
@message = I18n.t('messages.group.create.okay')
2015-01-17 09:24:36 -08:00
@render = render_to_string(partial: 'user/modal_group_item', locals: { group: group, user: target_user })
end
2015-01-17 09:57:23 -08:00
def destroy
@status = :err
@success = false
unless user_signed_in?
@status = :noauth
2015-06-07 09:24:01 -07:00
@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!
rescue ActiveRecord::RecordNotFound
@status = :notfound
2015-06-07 09:24:01 -07:00
@message = I18n.t('messages.group.destroy.notfound')
2015-01-17 09:57:23 -08:00
return
end
@status = :okay
@success = true
2015-06-07 09:24:01 -07:00
@message = I18n.t('messages.group.destroy.okay')
2015-01-17 09:57:23 -08:00
end
def membership
@status = :err
@success = false
unless user_signed_in?
@status = :noauth
2015-06-07 09:24:01 -07:00
@message = I18n.t('messages.noauth')
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])
rescue ActiveRecord::RecordNotFound
@status = :notfound
2015-06-07 09:24:01 -07:00
@message = I18n.t('messages.group.membership.notfound')
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?
@checked = true
2015-06-07 09:24:01 -07:00
@message = I18n.t('messages.group.membership.add')
else
group.remove_member target_user unless group.members.find_by_user_id(target_user.id).nil?
@checked = false
2015-06-07 09:24:01 -07:00
@message = I18n.t('messages.group.membership.remove')
end
2015-05-08 16:31:31 -07:00
@status = :okay
@success = true
end
end