class AdminAssistantsController
# 50/50 as to whether this will work the way I think it will.
# I think it should result in a route like this: /:scope/admin/:id/edit/ (etc.)
SuggestedRoute = %q{map.resources :admin, :path_prefix => "/:scope"}.freeze
layout 'admin'
respond_to :html
before_filter :discover_admin
def discover_admin scope
@admin_assistant = AdminAssistant.get_or_initialize(scope)
end
delegate :scope, :admin_assistant
index, show, new/create, edit/update, and destroy
def index
respond_with(@records = scope.all)
end
def show id
respond_with(@record = scope.get(id))
end
def new id
respond_with(@record = scope.new)
end
def create values
respond_with(@record = scope.create(values))
end
def edit id
respond_with(@record = scope.get(id))
end
def update id, values
@record = scope.get(id)
@record.update_attributes(values)
respond_with(@record)
end
def destroy id
@record = scope.get(id)
@record.destroy
respond_with(@record)
end
end
class ActiveModel
# TODO find a better way to reflect on all models
def self.all
Pathname.glob(Rails.root+'app/models/**/*.rb').map do |path|
path.basename.gsub(".rb").constantize
end
end
end
class Model
include DataMapper::Resource
property :scope, String, :length => 255, :null => false, :key => true
def self.get_or_create scope
get(scope) or create(scope)
end
def self.create scope
new.instance_eval { self.scope = scope; save }
end
def self.get scope
super scope.to_s.underscore
end
def scope= value
super value.to_s.underscore
end
def scope
super.camelize.constantize
end
# ensure a record exists for each model at startup
def self.generate_all_models
ActiveModel.all.map {|klass| get_or_create klass.to_s.underscore }
end
generate_all_models
end
class AdminAssistant
include DataMapper::Resource
property :id, Serial
belongs_to :model
has n, :admin_assistant_actions
has n, :admin_assistant_columns
validates_is_unique :model
def self.generate_all_admin_assistants
Model.all.each do |model|
assistant = find(:model => model).any? or create(:model => model)
model.properties.each do |property|
assistant.admin_assistant_columns.create(
:label = > name.humanize
)
end
end
end
generate_all_admin_assistants
end
class AdminAssistantColumn
include DataMapper::Resource
property :id, Serial
property :true_label, String, :default => 'true', :null => false
property :false_label, String, :default => 'false', :null => false
property :label, String, :length => 255, :null => false
property :include_blank, Boolean, :default => true, :null => false
property :read_only, Boolean, :default => false, :null => false
property :write_once, Boolean, :default => false, :null => false
property :default, Object
property :description, Text
property :nilify_message, Text, :default => "set the value to nil", :null => false
belongs_to :admin_assistant
has n, :admin_assistant_column_polymorphic_types
has n, :polymorpic_types, :class_name => 'Model', :through => :admin_assistant_column_polymorphic_types
end
class AdminAssistantColumnPolymorphicType
include DataMapper::Resource
property :id, Serial
belongs_to :model
belongs_to :admin_assistant_column
validates_is_unique :model, :scope => [:admin_assistant_column]
end
class AdminAssistantAction
include DataMapper::Resource
property :id, Serial
property :action, Discriminator
belongs_to :admin_assistant
end
class AdminAssistantStateTransformingAction < AdminAssistantAction
end
class AdminAssistantDisplayAction < AdminAssistantAction
class AdminAssistantDisplayColumnNotFromAssociatiedAdminAssistant < StandardError; end
has n, :admin_assistant_columns, :through => Resource
validates :admin_assistant_columns do
admin_assistant_columns.each do |column|
next if admin_assistant.admin_assistant_columns.include? column
raise AdminAssistantDisplayColumnNotFromAssociatiedAdminAssistant
end
end
end
class AdminAssistantFormAction < AdminAssistantDisplayAction
end
class AdminAssistantDisplayColumn
include DataMapper::Resource
end
class Index < AdminAssistantDisplayAction
end
class Search < AdminAssistantFormAction
end
class Show < AdminAssistantDisplayAction
end
class New < AdminAssistantFormAction
end
class Create < AdminAssistantStateTransformingAction
end
class Edit < AdminAssistantFormAction
end
class Update < AdminAssistantStateTransformingAction
end
class Destroy < AdminAssistantStateTransformingAction
end