module Tolaria
Public Class Methods
Returns the developer-configured categories for constructing the nav menu, in addition to any unexpected categories from configured managed classes.
# File lib/tolaria/categories.rb, line 6 def self.categories (self.config.menu_categories + self.managed_classes.collect(&:category)).uniq end
Returns all of the managed classes for the given `category`, sorted by their priority.
# File lib/tolaria/categories.rb, line 12 def self.classes_for_category(category) classes = Tolaria.managed_classes.select do |managed_class| managed_class.category == category end classes.sort_by do |klass| [klass.priority, klass.model_name.human] end end
Returns Tolaria’s configuration as an object
# File lib/tolaria/config.rb, line 37 def self.config if block_given? raise ArgumentError, "You passed a block to Tolaria.config but such a block will be ignored. Did you mean to call Tolaria.configure instead?" end @configuration ||= Tolaria::Configuration.new end
Configure Tolaria
, block-style. Use something similar to:
Tolaria.configure do |config| # Assign to config properties here end
# File lib/tolaria/config.rb, line 49 def self.configure(&block) yield @configuration ||= Tolaria::Configuration.new end
Discard a managed class instance for the given ActiveRecord::Base
class
# File lib/tolaria/manage.rb, line 51 def self.discard_managed_class(klass) @managed_classes.delete_if do |managed_class| klass.to_s == managed_class.klass.to_s end end
Using this method, you can attempt to get a pretty “display” string for presenting the passed `resource` as a label.
# File lib/tolaria/display_name.rb, line 5 def self.display_name(resource) Tolaria.config.display_name_methods.each do |method_name| if resource.respond_to?(method_name) return resource.send(method_name).to_s.truncate(40, omission:"…") end end end
The developer calls `Tolaria.draw_routes(self)` inside the router’s scope. Tolaria
automatically adds routes for managed classes.
# File lib/tolaria/routes.rb, line 5 def self.draw_routes(router) self.reload! router.instance_exec(managed_classes) do |managed_classes| namespace :admin do # Create routes for AdminController root to:"admin#root", as:"root" get "help/:slug", to:"admin#help_link", as:"help_link" post "api/markdown", to:"admin#markdown" # Create routes for the authentication/passcode flow get "signin", to:"sessions#new", as:"new_session" post "signin/code", to:"sessions#request_code" post "signin", to:"sessions#create" delete "signout", to:"sessions#destroy", as:"destroy_session" # Create routes for every managed class managed_classes.each do |managed_class| resources managed_class.plural, only:managed_class.allowed_actions end end end end
Returns all of Tolaria's configured Help Links.
# File lib/tolaria/help_links.rb, line 16 def self.help_links @help_links ||= [] end
Reads `Tolaria.config.help_links` and constructs an internal array of HelpLink
objects with the given options. Call `Tolaria.help_links` to receive the results.
# File lib/tolaria/help_links.rb, line 8 def self.initialize_help_links! @help_links = [] self.config.help_links.each do |hashy| @help_links << Tolaria::HelpLink.new(**hashy) end end
Internal factory for adding managed classes. Developers should use ActiveRecord::Base#manage_with_tolaria.
# File lib/tolaria/manage.rb, line 27 def self.manage(klass, options = {}) # If we already have a class of this name, discard it discard_managed_class(klass) # Wrap the Rails model inside a Tolaria::ManagedClass managed_klass = Tolaria::ManagedClass.create(klass, options) # Add class to the internal tracker @managed_classes.push(managed_klass) # Check if there is already a correctly named controller because # this means the end-developer made one and we don't want to unseat it. # Otherwise create a controller for the model to use in the admin namespace. unless "Admin::#{managed_klass.controller_name}".safe_constantize managed_controller = Class.new(Tolaria::ResourceController) ::Admin.const_set(managed_klass.controller_name, managed_controller) end return managed_klass end
Tolaria
keeps a list of all managed classes and the controllers for those classes internally so that other parts of the system can iterate over them. Return the list.
# File lib/tolaria/manage.rb, line 21 def self.managed_classes @managed_classes end
To provide admin pane development magic, Tolaria
has this method to force Rails to autoload/reload all model files.
# File lib/tolaria/reload.rb, line 5 def self.reload! # Re/Initialize HelpLinks self.initialize_help_links! # Manage the models included with the engine Administrator.generate_tolaria_bindings! # Reference each ActiveRecord::Base model so Rails autoloads it or # reloads changed files. Call generate_tolaria_bindings! on them. Dir["#{Rails.root}/app/models/*.rb"].each do |file| File.basename(file, ".rb").camelize.safe_constantize.try(:generate_tolaria_bindings!) end end
Calls the configured Markdown renderer
# File lib/tolaria/markdown.rb, line 23 def self.render_markdown(document) @markdown_renderer ||= Tolaria::MarkdownRendererProxy.new return @markdown_renderer.render(document) end
True if the application has actually booted and Tolaria
is safe to start referencing models
# File lib/tolaria/manage.rb, line 8 def self.safe_management @safe_mangagment end
Set the value of Tolaria.safe_management
. `bool` should be truthy. Don't call this method directly.
# File lib/tolaria/manage.rb, line 14 def self.safe_management=(bool) @safe_mangagment = !!bool end
Returns Tolaria’s version number
# File lib/tolaria/version.rb, line 4 def self.version Gem::Version.new("3.0.0") end