I'm building a site in Rails 3.2. Its been 3 years since I've touched Rails or Ruby, so I'm rusty on both, plus the last time I used rails is was Rails 2.3. Needless to say, please excuse any "simple" questions below.
@textColor
, @bodyBackground
, etc.)less-rails-bootstrap
gem to the Twitter Bootstrap look/feel, etc.less
)...given that I essentially will have to find some way to compile the CSS on the fly, that means I have to include GEMS I typically would not in a production environment. Performance will be very important. Is there a way to isolate this? Once the CSS has been invalidated and regenerated, I could take the content and either write it out to disk or store is in some memcached/redis/etc. instance for performance.
Any comments, even if just to point me in a general direction would be appreciated.
Thanks!
Here is the solution I finally landed on:
bootstrap-sass
instead https://github.com/thomas-mcdonald/bootstrap-sass
Made the following changes to my application.rb
file to ensure that the :asset
group is always included despite the environment:
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
Used the concepts provided by Manuel Meure (Thank you Manuel!) of Kraut Computing found at http://www.krautcomputing.com/blog/2012/03/27/how-to-compile-custom-sass-stylesheets-dynamically-during-runtime/ .
In my model (lets call it "Site"), I have a snippet of code that looks like this:
# .../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
I hope this helps. I know its a deviation from the original post, but its deviated because this seemed to be the most attainable solution to the problem.
If I haven't answered a specific question you have, feel free to comment so I can expand where possible.
Thanks!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With