宝石资产内的配置不起作用

时间:2014-06-27 19:22:05

标签: javascript ruby-on-rails ruby gem erb

我创建了一个名为Supportator的gem,当浏览器版本不受支持时会放置一个警告模式。目前它接受配置哈希,您可以传递每个浏览器不支持的最新版本。

守则

这是我的应用程序config/initializers/supportator.rb

中的初始化程序
   Supportator.configure({:chrome => 30, :ie => 11})

这是gem lib/supportator.rb的配置文件:

   require "supportator/version"
   require "engine"

   module Supportator

     @config = {
                 :chrome => 14,
                 :safari => 5,
                 :firefox => 3,
                 :ie => 9,
                 :opera => 12
     }

     @valid_config_keys = @config.keys

     def self.configure(opts = {})
       opts.each {|k,v| @config[k.to_sym] = v if @valid_config_keys.include? k.to_sym}
     end

     def self.config
       @config
     end

   end

这是触发gem lib/assets/javascripts/supportator.js.erb模态的js.erb文件的一部分:

    ...

    if (browserName == "Chrome" && majorVersion >= <%= Supportator.config[:chrome] %>)
        supported = true;
    else if (browserName == "Safari" && majorVersion >= <%= Supportator.config[:safari] %>)
        supported = true;
    else if (browserName == "Firefox" && majorVersion >= <%= Supportator.config[:firefox] %>)
        supported = true;
    else if (browserName == "Microsoft Internet Explorer" && majorVersion >= <%= Supportator.config[:ie] %>)
        supported = true;
    else if (browserName == "Opera" && majorVersion >= <%= Supportator.config[:opera] %>)
        supported = true;
    else
        supported = false;

    ....

问题

当我在预编译后看到代码时,结果是:

    ...

    if (browserName == "Chrome" && majorVersion >= 14)
        supported = true;
    else if (browserName == "Safari" && majorVersion >= 5)
        supported = true;
    else if (browserName == "Firefox" && majorVersion >= 3)
        supported = true;
    else if (browserName == "Microsoft Internet Explorer" && majorVersion >= 9)
        supported = true;
    else if (browserName == "Opera" && majorVersion >= 12)
        supported = true;
    else
        supported = false;

    ....

但是当我放入控制台Supportator.config[:ie]时,结果是12。

显然,在js.erb的预编译中,配置被忽略。任何人都知道为什么会发生这种情况?

1 个答案:

答案 0 :(得分:1)

我知道这可能没有用,但我无法重现你的错误。当我RAILS_ENV=development bundle exec rake assets:precompile然后rails s

时,您的宝石按预期行事

enter image description here