Skip to content

Instantly share code, notes, and snippets.

@naquad
Created June 7, 2014 18:06
Show Gist options
  • Save naquad/9d98054fa09d2e95b7fb to your computer and use it in GitHub Desktop.
Save naquad/9d98054fa09d2e95b7fb to your computer and use it in GitHub Desktop.
ActiveAdmin.register User do
controller do
def update_resource(object, attributes)
update_method = attributes.first[:password].present? ? :update_attributes : :update_without_password
object.send(update_method, *attributes)
end
def scoped_collection
User.
joins('LEFT OUTER JOIN states ON states.user_id = users.id').
select(%{
users.*,
count(distinct states.channel_id) as channels,
count(distinct states.streamer_id) as streamers
}).
group('users.id')
end
end
permit_params :email, :password, :password_confirmation, :confirm, role_ids: []
index do
selectable_column
id_column
column :email
column :current_sign_in_at
column :sign_in_count
column :created_at
column 'Roles' do |u|
u.roles.map(&:name).join(', ')
end
column :channels
column :streamers
actions
end
filter :email
filter :current_sign_in_at
filter :sign_in_count
filter :created_at
show do
attributes_table do
row :id
row :email
row 'Roles' do |u|
u.roles.map(&:name).join(', ')
end
row 'Watching' do |u|
u.watching.presence && u.watching.map {|c| link_to c.name, admin_channel_path(c.id)}.join(', ').html_safe
end
row 'Streamers' do |u|
u.streamers.presence && u.streamers.map {|s| link_to s.name, admin_streamer_path(s.id)}.join(', ').html_safe
end
row :sign_in_count
row :current_sign_in_at
row :last_sign_in_at
row :confirmed_at
row :confirmation_sent_at
row :failed_attempts
row :locked_at
row :created_at
row :updated_at
end
active_admin_comments
end
form do |f|
f.inputs "Admin Details" do
f.input :email
f.input :password
f.input :password_confirmation
f.input :roles, as: :check_boxes, collection: Role.all
unless f.object.confirmed?
f.input :confirm, as: :boolean
end
end
f.actions
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment