将常量文件夹文件从Rails引擎中的constantizer gem复制到主应用程序

时间:2019-05-30 05:02:44

标签: ruby-on-rails rubygems constants rails-engines rails-generators

我目前正在构建Rails引擎,并且添加了constantizer宝石并在constants文件夹中添加了一些文件

 config
   constants
     state.yml
     postcode.yml

state.yml

state:
 en:
  - name: "PERLIS INDERA KAYANGAN"
    code: 01
  - name: "KEDAH DARUL AMAN"
    code: 02
  - name: "PULAU PINANG"
    code: 03
  - name: "PERAK DARUL RIDZUAN"
    code: 04
  - name: "SELANGOR DARUL EHSAN"
    code: 05
  - name: "WILAYAH PERSEKUTUAN"
    code: 06
  - name: "JOHOR DARUL TAKZIM"

insurance.gemspec

$:.push File.expand_path("../lib", __FILE__)

# Maintain your gem's version:
require "insurance/version"

# Describe your gem and declare its dependencies:
Gem::Specification.new do |spec|
  spec.name        = "insurance"
  spec.version     = Insurance::VERSION
  spec.authors     = ["Loanstreet Tech"]
  spec.email       = ["techgroup@finology.com.my"]
  spec.homepage    = "https://github.com/loanstreet/insurance_loanstreet_plugin"
  spec.summary     = "Insurance Loanstreet."
  spec.description = "Loanstreet Insurance."
  spec.license     = "MIT"

  spec.files = Dir["{app,config,db,lib}/**/*", "MIT-LICENSE", "Rakefile", "README.md"]

  spec.add_dependency "rails", "~> 5.0.1"

  spec.add_dependency "pg", "0.18"
  spec.add_dependency "constantizer"

end

但是当我运行我的代码并尝试调用STATE或Insurance :: STATE时,我得到一个错误,提示uninitialized constant STATE or uninitialized Insurance::STATE

我试图自动加载整个文件夹,但这似乎也不起作用

config.autoload_paths += Dir["#{Insurance::Engine.root}/config/constants/"]

是否可以将所有文件复制到主应用程序中的constants文件夹中,或者可以在Rails引擎中调用或使用这些常量?

谢谢

0 个答案:

没有答案