Browse files

Documented my own changes (the configurable

authentication system and the exmport mechanism).
  • Loading branch information...
1 parent a47a294 commit fd61ad05982c7b9143ad56a90c55ad0d13c2d5c7 @baldowl committed Jul 20, 2008
Showing with 86 additions and 3 deletions.
  1. +3 −3 README
  2. +61 −0 lib/auto_admin_configuration.rb
  3. +3 −0 lib/auto_admin_helper.rb
  4. +7 −0 lib/auto_admin_save_as.rb
  5. +7 −0 lib/save_as/csv.rb
  6. +5 −0 lib/save_as/pdf.rb
View
6 README
@@ -134,9 +134,9 @@ few lines to the bottom of your environment.rb or in an initializer file:
end
Having done that, you can now (re-)start <tt>script/server</tt>, and navigate
-to http://localhost:3000/admin/. Yes, it installs its own routes. Yes, they're
-hard-coded. Yes, that needs to change... for now, just don't try to use
-<tt>/admin/</tt> for anything else. :)
+to http://localhost:3000/admin/. Yes, it installs its own routes, but they are
+partially configurable; for now, just don't try to use <tt>/admin/</tt> for
+anything else.
To customise which fields appear in the edit and list screens, you go on
to...
View
61 lib/auto_admin_configuration.rb
@@ -6,11 +6,18 @@ def self.config
end
module AutoAdminConfiguration
DefaultTheme = :django
+
+ # Returns the name of the active theme module.
def self.theme; Object.const_get("AutoAdmin#{theme_name.to_s.camelize}Theme"); end
+
+ # Returns the name of the active theme.
def self.theme_name; @@theme ||= DefaultTheme; end
+
+ # Change the name of the active theme.
def self.theme=(theme_name)
@@theme = theme_name.to_sym
end
+
def self.form_processor; theme::FormProcessor; end
def self.form_builder; theme::FormBuilder; end
def self.table_builder; theme::TableBuilder; end
@@ -20,26 +27,80 @@ def self.helpers; theme.respond_to?( :helpers ) ? [theme.helpers].flatten : [];
def self.controller_includes; theme.respond_to?( :controller_includes ) ? [theme.controller_includes].flatten : []; end
Site = Struct.new(:url, :short_url, :name)
+
+ # Set the basic informations about this site. Use it as:
+ #
+ # AutoAdmin.config do |admin|
+ # admin.set_site_info 'http://www.example.com/', 'example.com',
+ # 'Administration area for example.com'
+ # end
def self.set_site_info full_url, site_name, admin_site_title='Site Administration'
self.site = Site.new(full_url, site_name, admin_site_title)
end
+
+ # Returns informations set by #set_site_info
def self.site; @@site ||= raise("AutoAdmin not configured: site info not set"); end
+
+ # Lowlevel way to set informations about this site. Use #set_site_info.
def self.site= new_value; @@site = new_value; end
+
+ # Returns the list of models managed through the auto_admin interface.
def self.primary_objects; @@primary_objects ||= []; end
+
+ # Set the list of models available through the auto_admin interface. Use it
+ # as:
+ #
+ # AutoAdmin.config do |admin|
+ # admin.primary_objects = %w(actor film user)
+ # end
def self.primary_objects= new_value; @@primary_objects = new_value; end
+
def self.admin_model; @@admin_model ||= nil; end
+
+ # Set the application model used to authenticate the users. Use it as:
+ #
+ # AutoAdmin.config do |admin|
+ # admin.admin_model = account
+ # end
+ #
+ # See also #admin_model_id=.
def self.admin_model=(new_value)
@@admin_model = new_value.to_s.camelize.constantize
end
+
def self.admin_model_id; @@admin_model_id ||= nil; end
+
+ # Set the string/symbol used to store the admin object's id into the
+ # session. Use it as:
+ #
+ # AutoAdmin.config do |admin|
+ # admin.admin_model_id = :account_id
+ # end
+ #
+ # See also #admin_model=.
def self.admin_model_id=(new_value)
@@admin_model_id = new_value.to_sym
end
+
def self.controller_super_class; @@controller_super_class ||= ActionController::Base; end
def self.controller_super_class=(klass); @@controller_super_class = klass; end
+
+ # The string used as prefix in the custom routes; defaults to +admin+.
def self.url_prefix; @@url_prefix ||= 'admin'; end
+
+ # Set the string used as prefix in the custom routes.
def self.url_prefix= new_value; @@url_prefix = new_value; end
+
+ # Set the list of formats used by the optional export mechanism. Use it as:
+ #
+ # AutoAdmin.config do |admin|
+ # admin.save_as = %w(pdf csv)
+ # end
+ #
+ # For the availabe formats see the content of save_as directory in the
+ # plugin home.
def self.save_as=(formats); @@save_as_formats = formats; end
+
def self.save_as; @@save_as_formats ||= []; end
def self.model name
Object.const_get( name.to_s.camelcase )
View
3 lib/auto_admin_helper.rb
@@ -116,12 +116,15 @@ def admin_table(options={}, &proc)
fields_for(nil, nil, opts, &proc)
end
+ # Returns a link to the specific export action for the +format+ format. The
+ # link itself is not customizable.
def save_as_link_to format
link_to "Save as #{format.to_s.capitalize}", {:model => params[:model],
:format => format, :filter => params[:filter], :sort => params[:sort],
:sort_reverse => params[:sort_reverse], :search => params[:search]}
end
+ # Returns an array of links to the export actions; see #save_as_link_to.
def save_as_links
AutoAdmin::AutoAdminConfiguration.save_as.map {|format| save_as_link_to(format)}
end
View
7 lib/auto_admin_save_as.rb
@@ -1,10 +1,17 @@
+# Quick hack to inject at runtime the export mechanism into the plugin's
+# controller.
module AutoAdminSaveAs
+ # We use this hook to +require+ the format modules requested by the user via
+ # #save_as=.
def self.included receiver
AutoAdmin::AutoAdminConfiguration.save_as.each do |format|
require "save_as/#{format}"
end
end
+ # Inject into the plugin's controller a number of Proc objects to handle the
+ # formats requested by the user. See one of the format module's
+ # #save_as_proc method for details.
def save_as_blocks controller, block_handler, options
AutoAdmin::AutoAdminConfiguration.save_as.each do |format|
module_name = "AutoAdmin#{format.to_s.capitalize}".constantize
View
7 lib/save_as/csv.rb
@@ -1,6 +1,9 @@
require 'faster_csv'
+# Simple module to save data in CSV format.
module AutoAdminCsv
+ # Add a block into the plugin's controller's +respond_to+ construct, passing
+ # down the stream the +options+ set by users via the web interface.
def self.save_as_proc controller, block_handler, options
model = controller.model
block_handler.csv do
@@ -9,6 +12,8 @@ def self.save_as_proc controller, block_handler, options
end
end
+ # Using FasterCSV in a rather inflexbile way we dump +collection+ into a
+ # single CSV stream.
def self.export_into_csv_excel(controller, model, collection)
content_type = tweak_csv_excel_content_type controller.request
csv_content = FasterCSV.generate do |csv|
@@ -21,6 +26,8 @@ def self.export_into_csv_excel(controller, model, collection)
:filename => "#{model.human_name.downcase}.csv")
end
+ # Microsoft Windows users will appreciate the fact that Excel can
+ # automatically handle the CSV file if we change the reply's content type.
def self.tweak_csv_excel_content_type request
if request.respond_to?(:user_agent)
request.user_agent =~ /windows/i ? 'application/vnd.ms-excel' : 'text/csv'
View
5 lib/save_as/pdf.rb
@@ -1,7 +1,10 @@
Mime::Type.register 'application/pdf', :pdf
require 'pdf/simpletable'
+# Simple module to save data in PDF format.
module AutoAdminPdf
+ # Add a block into the plugin's controller's +respond_to+ construct, passing
+ # down the stream the +options+ set by users via the web interface.
def self.save_as_proc controller, block_handler, options
model = controller.model
block_handler.pdf do
@@ -10,6 +13,8 @@ def self.save_as_proc controller, block_handler, options
end
end
+ # Using PDF::Writer in a rather inflexbile way we dump +collection+ into a
+ # single PDF stream.
def self.export_into_pdf(controller, model, collection)
pdf = PDF::Writer.new :paper => 'A4', :orientation => :landscape
pdf.select_font "Times-Roman"

0 comments on commit fd61ad0

Please sign in to comment.