8

我正在 Rails 3.2 中建立一个站点。我接触 Rails 或 Ruby 已经 3 年了,所以我对两者都生疏了,而且我最后一次使用 Rails 是 Rails 2.3。不用说,请原谅下面的任何“简单”问题。

这是规格

  • 多租户 CMS/商店站点
  • 每个“商店”(又名子域)都可以通过 CSS 自定义拥有自己的外观、感觉等
    • 自定义可以在应用程序内的 UI 中执行,允许用户更改 Bootstrap 的基本变量(即@textColor@bodyBackground等)
  • 我正在将less-rails-bootstrapgem 用于 Twitter Bootstrap 外观/感觉等。

以下是挑战

  1. 我需要能够将 CSS 的变量动态输出到混入 Bootstrap 的文件中,以便提取变量以创建最终的 CSS
  2. 当用户更改 CSS 的变量时,现有样式基本上无效。我需要重新编译完整的 CSS 并将其写回磁盘、内存流或其他我可以得到它的位置(记住这是使用less
  3. 我需要不同的 CSS 来为每个子域吐出。关于如何解决这个问题的任何建议?

让事情变得更加复杂……

...鉴于我基本上必须找到某种方法来即时编译 CSS,这意味着我必须包含 GEMS,而我通常不会在生产环境中。性能将非常重要。有没有办法隔离这个?一旦 CSS 失效并重新生成,我可以获取内容并将其写入磁盘或存储在某些 memcached/redis/etc 中。例如性能。

任何评论,即使只是为了给我指出一个大致的方向,也将不胜感激。

谢谢!

4

1 回答 1

3

这是我最终找到的解决方案:

  • 我最终切换bootstrap-sasshttps://github.com/thomas-mcdonald/bootstrap-sass
  • 对我的application.rb文件进行了以下更改,以确保无论:asset环境如何都始终包含该组:

    if defined?(Bundler)
        # If you precompile assets before deploying to production, use this line
        # Bundler.require(*Rails.groups(:assets => %w(development test)))
        # If you want your assets lazily compiled in production, use this line
        Bundler.require(:default, :assets, Rails.env)
    end
    
  • 使用了在http://www.krautcomputing.com/blog/2012/03/27/how-to-compile-custom-sass-stylesheets-dynamically-找到的 Kraut Computing 的 Manuel Meure(谢谢 Manuel!)提供的概念运行时/ .

    • 我根据自己的需要做了一些调整,但 Manuel 阐述的核心概念是我编译过程的基础。
  • 在我的模型中(我们称之为“站点”),我有一段代码如下所示:

    # .../app/models/site.rb
    ...
    
    BASE_STYLE = "
      @import \"compass/css3\";
    
      <ADDITIONAL_STYLES>
    
      @import \"bootstrap\";
      @import \"bootstrap-responsive\";
    ".freeze
    
    # Provides the SASS/CSS content that would 
    # be included into your base SASS content before compilation
    def sass_content
      "
      $bodyBackground: #{self.body_background};
      $textColor: #{self.text_color};
      " + self.css # Any additional CSS/SASS you would want to add
    end
    
    def compile_css(test_only = false, force_recompile = false)
    
      # SassCompiler is a modification of the information made available at the Kraut Computing link
      compiler = SassCompiler.new("#{self.id}/site.css", {:syntax => :scss, :output_dir => Rails.root.join('app', 'assets', 'sites')})
    
      # Bail if we're already compiled and we're not forcing recompile
      return if compiler.compiled? && !force_recompile && !test_only
    
      # The block here yields the content that will be rendered
      compiler.compile(test_only) {
        # take our base styles, slap in there some vars that we make available to be customized by the user
        # and then finally add in our css/scss that the user updated... concat those and use it as
        # our raw sass to compile
        BASE_STYLE.gsub(/<ADDITIONAL_STYLES>/, self.sass_content)
      }
    end
    

我希望这有帮助。我知道它与原始帖子有偏差,但它有所偏差,因为这似乎是解决该问题的最可行的解决方案。

如果我没有回答您的具体问题,请随时发表评论,以便我尽可能扩展。

谢谢!

于 2013-02-26T15:03:05.543 回答