Rya*_*ith 8 ruby ruby-on-rails less asset-pipeline ruby-on-rails-3.2
我正在Rails 3.2中构建一个站点.自从我触及Rails或Ruby以来已经有3年了,所以我两个都生锈了,再加上我最后一次使用rails就是Rails 2.3.不用说,请原谅下面的任何"简单"问题.
@textColor
,@bodyBackground
等)less-rails-bootstrap
宝石用于Twitter Bootstrap的外观/感觉等.less
)...鉴于我基本上必须找到一些方法来动态编译CSS,这意味着我必须包含我通常不会在生产环境中使用的GEMS.表现非常重要.有没有办法隔离这个?一旦CSS被无效并重新生成,我就可以获取内容并将其写入磁盘或存储在某些memcached/redis/etc中.表现的实例.
任何评论,即使只是指向一般方向,我们将不胜感激.
谢谢!
这是我最终找到的解决方案:
bootstrap-sass
我最终切换到https://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
Run Code Online (Sandbox Code Playgroud)使用了 KrautComputing 的 Manuel Meure(谢谢 Manuel!)提供的概念,网址为http://www.krautcomputing.com/blog/2012/03/27/how-to-compile-custom-sass-stylesheets-dynamically-运行时/ .
在我的模型(我们称之为“ Site ”)中,我有一段代码,如下所示:
# .../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
Run Code Online (Sandbox Code Playgroud)我希望这有帮助。我知道它与原始帖子有偏差,但它有所偏差,因为这似乎是问题最容易实现的解决方案。
如果我没有回答您的具体问题,请随时发表评论,以便我可以尽可能地扩展。
谢谢!