Created
July 11, 2012 20:31
-
-
Save jtsagata/3093129 to your computer and use it in GitHub Desktop.
ActiveAdmin template
This file contains 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
# lib/templates/active_admin/resource/admin.rb | |
ActiveAdmin.register <%= class_name %> do | |
<% attributes = Kernel.const_get(class_name).attribute_names %> | |
<% columns = Kernel.const_get(class_name).columns %> | |
<% content_columns = Kernel.const_get(class_name).content_columns %> | |
# <%= class_name %> attributes are: | |
# <%= attributes.join(" ") %> | |
# The Index Page | |
index do |i| | |
selectable_column | |
<% if attributes.include?("id") -%> | |
id_column | |
<% end -%> | |
<% content_columns.each do |attr| -%> | |
<% if attr.type == :datetime -%> | |
column "<%= attr.name.humanize.sub(" "," ") -%>".html_safe, :sortable => :<%= attr.name -%> do |r| | |
I18n.l r.<%= attr.name -%>, :format => :long | |
end | |
<% elsif attr.type == :boolean -%> | |
column "<%= attr.name.humanize -%>", :sortable => :<%= attr.name -%> do |<%= class_name.downcase -%>| | |
status_tag <%= class_name.downcase -%>.<%= attr.name -%> ? 'On' : 'Off' | |
end | |
<% elsif attr.name == "id" -%> | |
<% else -%> | |
column :<%= attr.name -%> # <%= attr.type %> | |
<% end -%> | |
<% end -%> | |
default_actions | |
end | |
# Filters on index Page | |
<% | |
resource_class = Kernel.const_get(class_name) | |
reflections = if resource_class.respond_to?(:reflections) | |
resource_class.reflections.collect{|name, r| name } | |
else | |
[] | |
end | |
contentcols = if resource_class.respond_to?(:content_columns) | |
resource_class.content_columns.collect{|c| c.name } | |
else | |
[] | |
end | |
default_filters = reflections + contentcols | |
-%> | |
<% default_filters.each do |attr| -%> | |
<%= "filter :#{attr}" %> | |
<% end -%> | |
# The Show Page | |
show do |<%= class_name.downcase %>| | |
attributes_table do | |
<% attributes.each do |attr| %><%= " row :#{attr}\n" %><% end %> | |
end | |
active_admin_comments | |
end | |
# The form | |
form do |f| | |
f.inputs do | |
<% attributes.each do |attribute| %> f.input :<%= attribute %>, :label => <%= "'#{attribute.humanize}'\n" %><% end %> | |
end | |
f.buttons | |
end | |
# Extra controller methods and customizations | |
controller do | |
defaults :finder => :find_by_url | |
def role_given? | |
true | |
end | |
def as_role | |
user.is_admin? ? {:as => :admin} : {} | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment