module Tolaria

Public Class Methods

categories() click to toggle source

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
classes_for_category(category) click to toggle source

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
config() click to toggle source

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() { |configuration ||= configuration| ... } click to toggle source

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_managed_class(klass) click to toggle source

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
display_name(resource) click to toggle source

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
draw_routes(router) click to toggle source

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
manage(klass, options = {}) click to toggle source

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
managed_classes() click to toggle source

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
reload!() click to toggle source

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
render_markdown(document) click to toggle source

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
safe_management() click to toggle source

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
safe_management=(bool) click to toggle source

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
version() click to toggle source

Returns Tolaria’s version number

# File lib/tolaria/version.rb, line 4
def self.version
  Gem::Version.new("3.0.0")
end