Module: Tolaria

Defined in:
lib/tolaria/config.rb,
lib/tolaria/engine.rb,
lib/tolaria/manage.rb,
lib/tolaria/reload.rb,
lib/tolaria/routes.rb,
lib/tolaria/version.rb,
lib/tolaria/markdown.rb,
lib/tolaria/categories.rb,
lib/tolaria/help_links.rb,
lib/tolaria/display_name.rb,
lib/tolaria/managed_class.rb,
lib/tolaria/random_tokens.rb,
lib/tolaria/form_buildable.rb,
lib/generators/tolaria/install/install_generator.rb

Defined Under Namespace

Modules: FormBuildable, RandomTokens, VERSION Classes: Configuration, Engine, HelpLink, InstallGenerator, ManagedClass, MarkdownRendererProxy, ResourceController, TolariaController

Class Method Summary collapse

Class Method Details

.categoriesObject

Returns the developer-configured categories for constructing the nav menu, in addition to any unexpected categories from configured managed classes.


6
7
8
# 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) ⇒ Object

Returns all of the managed classes for the given category, sorted by their priority.


12
13
14
15
16
17
18
19
# 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

.configObject

Returns Tolaria’s configuration as an object


37
38
39
40
41
42
# 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 ||= Tolaria::Configuration.new| ... } ⇒ Object

Configure Tolaria, block-style. Use something similar to:

Tolaria.configure do |config|
  # Assign to config properties here
end

Yields:


49
50
51
# File 'lib/tolaria/config.rb', line 49

def self.configure(&block)
  yield @configuration ||= Tolaria::Configuration.new
end

.discard_managed_class(klass) ⇒ Object

Discard a managed class instance for the given ActiveRecord::Base class


51
52
53
54
55
# 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) ⇒ Object

Using this method, you can attempt to get a pretty “display” string for presenting the passed resource as a label.


5
6
7
8
9
10
11
# 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) ⇒ Object

The developer calls Tolaria.draw_routes(self) inside the router’s scope. Tolaria automatically adds routes for managed classes.


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 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.


16
17
18
# File 'lib/tolaria/help_links.rb', line 16

def self.help_links
  @help_links ||= []
end

.initialize_help_links!Object

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.


8
9
10
11
12
13
# 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

.manage(klass, options = {}) ⇒ Object

Internal factory for adding managed classes. Developers should use ActiveRecord::Base#manage_with_tolaria.


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 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_classesObject

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.


21
22
23
# File 'lib/tolaria/manage.rb', line 21

def self.managed_classes
  @managed_classes
end

.reload!Object

To provide admin pane development magic, Tolaria has this method to force Rails to autoload/reload all model files.


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 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) ⇒ Object

Calls the configured Markdown renderer


23
24
25
26
# File 'lib/tolaria/markdown.rb', line 23

def self.render_markdown(document)
  @markdown_renderer ||= Tolaria::MarkdownRendererProxy.new
  return @markdown_renderer.render(document)
end

.safe_managementObject

True if the application has actually booted and Tolaria is safe to start referencing models


8
9
10
# File 'lib/tolaria/manage.rb', line 8

def self.safe_management
  @safe_mangagment
end

.safe_management=(bool) ⇒ Object

Set the value of Tolaria.safe_management. bool should be truthy. Don't call this method directly.


14
15
16
# File 'lib/tolaria/manage.rb', line 14

def self.safe_management=(bool)
  @safe_mangagment = !!bool
end

.versionObject

Returns Tolaria’s version number


4
5
6
# File 'lib/tolaria/version.rb', line 4

def self.version
  Gem::Version.new("3.0.0")
end