Created
August 20, 2009 17:40
-
-
Save lukemelia/171219 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Admin::SportsController < Admin::AdminController | |
before_filter :validate_admin_rights | |
admin_assistant_for Sport do |aa| | |
aa.actions :index, :create, :update | |
aa.index.columns :id, :name, :member_count, :groups_count, :enabled, :drills_enabled, :organizations, :player_term | |
aa.index.per_page 100 | |
aa.index.sort_by 'name ASC' | |
aa.index.include :organizations | |
aa[:enabled].label = "Hub Enabled?" | |
aa[:enabled].boolean_labels = %w(Yes No) | |
aa[:drills_enabled].boolean_labels = %w(Yes No) | |
aa[:member_count].label = "# Members" | |
aa[:groups_count].label = "# Groups" | |
aa.form.columns :name, :enabled, :drills_enabled, :player_term | |
aa.form[:enabled].input = :check_box | |
aa.form[:drills_enabled].input = :check_box | |
end | |
protected | |
def do_list | |
super | |
preload_member_and_group_counts | |
end | |
def preload_member_and_group_counts | |
member_counts = id_to_count_hash_from_sql('select sports.id, count(users.id) as count from sports left outer join users on users.sport_id = sports.id group by sports.id') | |
group_counts = id_to_count_hash_from_sql('select sports.id, count(groups.id) as count from sports left outer join groups on groups.sport_id = sports.id group by sports.id') | |
@records.each do |sport_record| | |
sport_record.member_count = member_counts[sport_record.id] | |
sport_record.groups_count = group_counts[sport_record.id] | |
end | |
end | |
def id_to_count_hash_from_sql(sql) | |
Hash[*Sport.connection.execute(sql).all_hashes.map{|h| [h['id'].to_i, h['count'].to_i]}.flatten] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment