Created
March 26, 2013 09:47
-
-
Save amolpujari/5244214 to your computer and use it in GitHub Desktop.
activeadmin timestamps columns, and other customizations
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
module ActiveAdmin | |
module Views | |
class IndexAsTable < ActiveAdmin::Component | |
class IndexTableFor < ::ActiveAdmin::Views::TableFor | |
def timestamps_columns | |
column :created_at, :sortable => :created_at do |resource| | |
resource.created_at and resource.created_at.strftime('%b %-d %Y, %l %P') | |
end | |
column :updated_at, :sortable => :updated_at do |resource| | |
resource.updated_at and resource.updated_at.strftime('%b %-d %Y, %l %P') | |
end | |
end | |
end | |
end | |
end | |
end | |
# shows active gateway counts in scopes | |
module ActiveAdmin | |
module Views | |
class Scopes < ActiveAdmin::Component | |
def get_scope_count(scope) | |
if [:egauge, :blueline31100, :ted5000, :current_cost_pachube, :api].include? scope.scope_method | |
return "#{collection_size(scope_chain(scope, collection_before_scope).where('last_reported_time != NULL '))}/#{collection_size(scope_chain(scope, collection_before_scope))}" | |
else | |
return collection_size(scope_chain(scope, collection_before_scope)) | |
end | |
end | |
end | |
end | |
end | |
#### HABTM filter fix | |
# https://github.com/Daxter/active_admin/commit/268f15af98e8ac12f35e64c20d21f4db38f68eee | |
# | |
module ActiveAdmin | |
module Filters | |
# This form builder defines methods to build filter forms such | |
# as the one found in the sidebar of the index page of a standard resource. | |
class FormBuilder < ::ActiveAdmin::FormBuilder | |
def filter(method, options = {}) | |
return "" if method.blank? || | |
(options[:as] ||= default_input_type(method)).nil? | |
content = input(method, options) | |
form_buffers.last << content.html_safe if content | |
end | |
protected | |
# Returns the default filter type for a given attribute | |
def default_input_type(method, options = {}) | |
if reflection_for method.to_s.sub(/_id$/,'').to_sym | |
:select | |
elsif column = column_for(method) | |
case column.type | |
when :date, :datetime | |
:date_range | |
when :string, :text | |
:string | |
when :integer | |
:numeric | |
when :float, :decimal | |
:numeric | |
when :boolean | |
:boolean | |
end | |
end | |
end | |
def custom_input_class_name(as) | |
"Filter#{as.to_s.camelize}Input" | |
end | |
def active_admin_input_class_name(as) | |
"ActiveAdmin::Inputs::Filter#{as.to_s.camelize}Input" | |
end | |
# Returns the column for an attribute on the object being searched if it exists. | |
def column_for(method) | |
@object.base.columns_hash[method.to_s] if @object.base.respond_to?(:columns_hash) | |
end | |
# Returns the association reflection if it exists. | |
# Formtastic has this function, but we override it because .class doesn't represent the resource | |
def reflection_for(method) | |
@object.base.reflect_on_association(method) if @object.base.respond_to?(:reflect_on_association) | |
end | |
end | |
# This module is included into the view | |
module ViewHelper | |
# Helper method to render a filter form | |
def active_admin_filters_form_for(search, filters, options = {}) | |
options[:builder] ||= ActiveAdmin::Filters::FormBuilder | |
options[:url] ||= collection_path | |
options[:html] ||= {} | |
options[:html][:method] = :get | |
options[:html][:class] ||= "filter_form" | |
options[:as] = :q | |
clear_link = link_to(I18n.t('active_admin.clear_filters'), "#", :class => "clear_filters_btn") | |
form_for search, options do |f| | |
filters.group_by{ |o| o[:attribute] }.each do |attribute, array| | |
options = array.last # grab last-defined `filter` call from DSL | |
if_block = options[:if] || proc{ true } | |
unless_block = options[:unless] || proc{ false } | |
if call_method_or_proc_on(self, if_block) && !call_method_or_proc_on(self, unless_block) | |
f.filter options[:attribute], options.except(:attribute, :if, :unless) | |
end | |
end | |
buttons = content_tag :div, :class => "buttons" do | |
f.submit(I18n.t('active_admin.filter')) + | |
clear_link + | |
hidden_field_tags_for(params, :except => [:q, :page]) | |
end | |
f.form_buffers.last + buttons | |
end | |
end | |
end | |
end | |
end | |
module ActiveAdmin | |
module Inputs | |
class FilterSelectInput < ::Formtastic::Inputs::SelectInput | |
include FilterBase | |
def input_name | |
reflection_for(method) ? "#{method}_id_eq" : "#{super}_eq" | |
end | |
def input_options | |
super.merge(:include_blank => I18n.t('active_admin.any')) | |
#super.merge(:include_blank => multiple? ? false : I18n.t('active_admin.any')) | |
end | |
# NOTE: I don't know why, but Formtastic is throwing in a hidden text input along with the dropdown. | |
# This line renames the params name it has, so Metasearch doesn't grab it and throw and error. | |
def input_html_options_name | |
"this_really_shouldnt_be_here" | |
end | |
# TODO: get multiselect working! | |
# def input_html_options_name_multiple | |
# "#{object_name}[#{input_name}][]" | |
# end | |
def extra_input_html_options | |
{}#multiple? ? {:multiple => true, :size => options[:size]} : {} | |
end | |
end | |
end | |
end | |
#### HABTM filter fix ends | |
module ActiveAdmin | |
module Views | |
class IndexAsTable < ActiveAdmin::Component | |
def time_status_tag time, hrs_range=[2,24], format='%F-%H:%M(%Z)' | |
ts = time and time.strftime(format) | |
ts = ts.to_s | |
if time | |
hrs_diff = ((Time.now - time)/1.hour) | |
if hrs_diff > hrs_range[1] | |
output = status_tag(ts, :red) | |
elsif hrs_diff > hrs_range[0] | |
output = status_tag(ts, :orange) | |
else | |
output = status_tag(ts, :green) | |
end | |
else | |
output = status_tag('Never', :red) | |
end | |
output | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment