Avo - Add helpers to Resource files

Public
Use your own helpers in Resource files
Icons/chart bar
Used 28 times
Created by
A Adrian Marin

Usage
Docs: https://docs.avohq.io/2.0/recipes/use-own-helpers-in-resource-configuration.html

Run this command in your Rails app directory in the terminal:

rails app:template LOCATION="https://railsbytes.com/script/V2Gsb9"
Template Source

Review the code before running this template on your machine.

TEMPLATE = <<~CONTENT

  module FieldExtensions
    # Include all helpers
    helper_names = ActionController::Base.all_helpers_from_path Rails.root.join("app", "helpers")
    helpers = ActionController::Base.modules_for_helpers helper_names
    helpers.each do |helper|
      send(:include, helper)
    end
  end

  Rails.configuration.to_prepare do
    Avo::Fields::BaseField.include FieldExtensions
  end
CONTENT

f = File.open(Rails.root.join("config", "initializers", "avo.rb").to_s, "a")
f.write TEMPLATE
f.close
Comments

Sign up or Login to leave a comment.