Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save pietia/2780425 to your computer and use it in GitHub Desktop.
Save pietia/2780425 to your computer and use it in GitHub Desktop.
Rails Admin config and runtime management
# add this to your ../app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
before_filter { |c| _admin_prolog(c) } # should be the first one!
after_filter { |c| _admin_epilog(c) } # should be the last one!
def _admin_prolog(c)
@is_admin = c.kind_of?(RailsAdmin::ApplicationController)
RA.prolog(@is_admin)
true
end
def _admin_epilog(c)
RA.epilog
true
end
end
These monkey patches and hacks address several issues:
To get around these:
https://github.com/sferik/rails_admin/issues/635
https://github.com/sferik/rails_admin/issues/620
To prevent RA from configuring everything when the access it not within its namespace (takes time)
To make it config easier (things like nav order, visibility, labels)
class Business < ActiveRecord::Base
ra_config(:plural => false) do
list do
field :active do
show do
User.current.super?
end
end
field :name
end
edit do
field :active do
show do
User.current.super? && !bindings[:object].super?
end
end
field :name
end
end
end
# .../config/initializers/z_rails_admin.rb
module RA
module_function
def models
%w(
Business
Contact
Site
Denomination
Table
Job
Screen
ScreenTab
User
OrderType
Menu
MenuCategory
MenuItem
ChoiceSet
Choice
ChoiceAction
OrderWheel
DisplayPipe
PrinterType
Printer
CashDrawerReason
CashDrawerRule
RevenueCenter
TaxType
VoidReason
PriceAdjuster
TenderType
PaymentProcessor
CheckFilter
GiftCardActivityFilter
)
end
def prolog(v=false)
Thread.current[:_ra_in_admin] = v
end
def epilog
Thread.current[:_ra_in_admin] = false
end
end
module RAConfig
def ra_config(options={},&block)
options = {
:plural => true,
:model => self
}.merge(options || {})
plural = options[:plural]
model = options[:model] || self
model_name = model.name
proceed = Thread.current[:_ra_in_admin] || Rails.env.production?
Rails.logger.info
Rails.logger.info "ra_config #{model_name} #{proceed ? '' : 'ignore'}"
Rails.logger.info
return unless proceed
RailsAdmin::Config.included_models ||= []
idx = (RA.models || []).index(model_name) || -1
if (idx == -1)
Rails.logger.warn "rails_admin: #{model_name} config'd but not in the list"
unless RailsAdmin::Config.included_models.include?(model_name)
RailsAdmin::Config.included_models << model_name
end
end
name = model_name.underscore.split('_').collect { |w| w.capitalize }.join(' ')
model.class_eval %Q{
include Admin::AdminHelper
}
m = RailsAdmin::Config.model(model,&block)
Scopify.ignore do
if (idx == -1)
m.instance_variable_set("@visible_registered",false)
end
if (m.instance_variable_get("@label_registered").nil?)
m.instance_variable_set("@label_registered", plural ? name.pluralize : name)
end
m.instance_variable_set("@weight_registered", idx)
end
end
end
ActiveRecord::Base.extend(RAConfig)
module ActiveRecord
Base.class_eval do
def ra_label(l=nil)
active = self.respond_to?(:active) ? self.active : true
label = l || (self.respond_to?(:to_label) && self.to_label) || "#{self.class.name}.#{self.id.to_s}"
return label if (active)
s = '<span class="inactive">' + label + '<span>'
s.html_safe
end
end
end
RailsAdmin.config do |config|
# in non-production the config gets evaluated before the models are lazy loaded by rails
# so need to set up the included models
models = RA.models.select { |m| (m !~ /^#/) }
models = models - config.included_models
config.included_models += models
if (Rails.env.production?)
RailsAdmin::AbstractModel.all_models = nil
RailsAdmin::AbstractModel.all_abstract_models = nil
RailsAdmin::AbstractModel.all # rebuild model arrays
end
config.label_methods = [:ra_label]
config.authenticate_with do
true
end
config.authorize_with :simple_authorization
end
module RailsAdmin
module Config
Model.class_eval do
# monkey patch to get around stale @excluded instance variable
def excluded?
#!RailsAdmin::AbstractModel.all.map(&:model).include?(abstract_model.model)
!RA.models.include?(abstract_model.instance_variable_get("@model_name"))
end
end
end
end
module RailsAdmin
module Config
module Fields
module Types
# monkey patch because RA doesn't handle a binary field. even though I
# don't configure one to be listed or edited it still wants to look at it
# during config so fake it out...
def self.load(type)
return @@registry[:text] if (type.to_sym == :binary)
@@registry[type.to_sym] or raise "Unsupported field datatype: #{type}"
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment