我创建了一个名为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的预编译中,该配置被忽略。任何人都知道为什么会这样吗?

最佳答案

我知道这可能没有帮助,但我无法重现您的错误。当我RAILS_ENV=development bundle exec rake assets:precompile然后rails s时,您的宝石表现出预期的效果

09-25 21:56