Use compass in gem for rails application - ruby-on-rails

I'm trying to create a gem which represents a JS and CSS library and can be included in rails projects (currently using 3.2). All the style sheets are written in SASS and depend on the compass library.
I tried adding compass-rails to the Gemfile of the "external" gem and including it in the gems SASS files using
#import "compass"
However, back in the rails application (which has a dependency to this gem), this results in an error message:
File to import not found or unreadable: compass.
Am I doing something wrong?
Update: It seems to work if I add gem compass-rails to the Gemfile of the rails application. Any change to work around that?
Thanks a lot for your help!

Besides adding compass-rails to the Gemfile of the gem, you also need to require 'compass-rails' somewhere, adding it to lib/your_gem_name.rb inside the gem worked for me.

Related

How do I import ruby gems assets to project? [duplicate]

I'm trying to wrap the bootstrap-sass gem inside another gem (let's call it my-engine). Along the way, I'm building a small Rails application to test things out. As a first step, I wanted to make sure I could get bootstrap-sass working directly in my Rails application. The Gemfile for the Rails app looks like this:
gem 'bootstrap-sass', '3.3.1.0'
gem 'my-engine, path: "~/dev/my-engine"
This works fine. The bootstrap assets are loaded into my Rails application and everything looks good. Now, I want to take bootstrap-sass out of my Rails app and let it load through my-engine. So, my Rails application Gemfile now looks like:
gem 'my-engine, path: "~/dev/my-engine"
The .gemspec for my-engine has:
spec.add_runtime_dependency 'bootstrap-sass', '3.3.1.0'
I can re-bundle the my-engine gem with no problems. I can re-bundle the Rails application with no problems. However, when I refresh the page of the Rails app, I get the following error:
File to import not found or unreadable: bootstrap-sprockets.
That break occurs when sprockets is trying to build the application.css file. Sometimes this will pass and I'll get a different error about missing the bootstrap.js javascript file when the application.js is being built.
Why is this happening? I'm wondering if it has something to with the fact that I'm developing the gems locally and haven't published them, although I'm not sure why that would affect bootstrap-sass which is published. I'm using bundler 1.5.3.
Make sure 'bootstrap-sass' is required in your engine. One sensible place to do this is in your lib/my-engine.rb file:
require 'bootstrap-sass'
Adding the bootstrap-sass gem as a runtime dependency in the .gemspec isn't enough when you're trying to wrap gems.
As you want to use more and more scss/js/coffeescript libraries, you may want to consider moving to bower vs gemfiles as the source for bootstrap-sass-official. We use bower-rails for rake tasks and auto-configuration. It's a really lite config/rake task layer over standard bower.
Addressing your answer, bootstrap problems via the gem was one of the reasons I switched our engine over to just bower assets. We now import bootstrap-sass-official and have full control, note however that for sass files you will need to import the longer path to the source file, i.e. in our engine _application.scss:
# our custom variable overrides
#import 'overrides/variables';
#import 'bootstrap-sass-official/assets/stylesheets/bootstrap-sprockets';
#import 'bootstrap-sass-official/assets/stylesheets/bootstrap';
NOTE: if you want your app sass variables to override engine and sass variables, make sure your engine has _application.scss not application.scss, the leading underscore is critical for variable context/scope.
Thinking ahead, you may need to ignore bower transitive dependencies as we did.
(i.e. some dependencies may use 'bootstrap' while others use 'bootstrap-sass-official' etc)
We use it like this in our .bowerrc such as the following:
{
"ignoredDependencies": [
"bootstrap",
"bootstrap-sass",
"bootstrap-sass-official"
]
}
In conclusion
We have been using this for several months with success. bower-rails will install the dependencies in /vendor/assets and if referenced in your engine, you won't need to reference them at all in your application project. It has been fast and easy to maintain/add/update libraries and know exactly how files are included.

Using Sass in rails 3.0.1

i am trying to use sass in a rails 3.0.1 project that i am working on. I install the gem but when i create a scss file and add some style nothing happens. I've been searching for an answer and it seems others have haml installed as well, do I need this?
I think you should add the assets pipeline (Sprockets) in addition to SASS.
Here is a gist that explains the setup for Rails 3.0.x.
https://gist.github.com/911003

Rails 3.1 Compass and Sprockets. Confused

I am generally confused about Rails 3.2, Sprockets, and Compass.
At the top of this article, it says *= require directives, are no longer needed using Sass-Rails, just use #import instead. The Sass-Rails Github talks about a little about glob #import feature, but doesn't talk at all about #import being the new *= require. Which one or combo :(
do I use?
Having coderay.css.sass in vendor/assets/stylesheets and using #import in application.css.sass will import my_coderay.css.sass?
I don't know because I'm using Compass Mixins in my_coderay.css.sass (thus putting a #import "compass/css3" at the top of that file) and I'm getting an error:
"File to import not found or unreadable: compass"
In this article it talks about configuring Rail's Sass engine to know about the assets path. But I thought Sass-Rails already teaches #import where asset path is?
Also when I try to include the 31 branch of compass...
gem 'compass', :git => 'git://github.com/chriseppstein/compass.git', :branch => 'rails31'
I get errors...
Using compass (0.12.0.alpha.0.22e2458) from git://github.com/chriseppstein/compass.git (at rails31) /Users/george.norris/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/site_ruby/1.9.1/rubygems/installer.rb:365:in `initialize': No such file or directory - /Users/george.norris/.rvm/gems/ruby-1.9.2-p180/bundler/gems/gems/compass-0.12.0.alpha.0.22e2458/bin/compass (Errno::ENOENT)
And finally, if I take coderay.css.sass out of vendor/assets and put it in app/assets I don't get the above error. But I getting it's spitting out errors in public/application.css.css (.css.css ???)
Syntax error: File to import not found or unreadable: base.
Load paths:
Compass::SpriteImporter
/Users/geonorr/Sites/quasar/public/stylesheets/sass
/Users/geonorr/Sites/quasar/app/assets/stylesheets
/Users/geonorr/.rvm/gems/ruby-1.9.2-p180/gems/compass-0.11.5/frameworks/blueprint/stylesheets
/Users/geonorr/.rvm/gems/ruby-1.9.2-p180/gems/compass-0.11.5/frameworks/compass/stylesheets
on line 3 of /Users/geonorr/Sites/quasar/app/
I am using Rails (3.1.0), Sass (3.1.7), Sass-Rails (3.1.0), Compass (0.11.5)
Thanks for the response. Feel like I am getting closer...
Including Compass Master Branch and getting this error:
gem 'compass', :git => 'git://github.com/chriseppstein/compass', :branch => 'master'
Using compass (0.12.alpha.0.d03d529) from git://github.com/chriseppstein/compass (at master) /Users/gnee/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/site_ruby/1.9.1/rubygems/installer.rb:365:in initialize': No such file or directory - /Users/gnee/.rvm/gems/ruby-1.9.2-p180/bundler/gems/gems/compass-0.12.alpha.0.d03d529/bin/compass (Errno::ENOENT)
from /Users/gnee/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/site_ruby/1.9.1/rubygems/installer.rb:365:inopen'
from /Users/gnee/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/site_ruby/1.9.1/rubygems/installer.rb:365:in shebang'
from /Users/gnee/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/site_ruby/1.9.1/rubygems/installer.rb:456:inapp_script_text'
*= require comments can be seen as a hacky workaround from the perspective of Sass, because Sass has a dedicated language feature for that: the #import directive. #import tells Sprockets what to do. When you are migrating from a Rails 3.0 project, leave all #import statements as they are. At the moment, the only gotcha is "globbing": How to tell Rails/Sprockets to regard also other Sass files than application.css.s(ac)ss.
Here is the latest Compass installation guide for Rails 3.1. It also tackles the globbing problem.
As it is said in the Rails Guide, app/assets, lib/assets or vendor/assets directories in your app as well as in your gems/engines are included in Rails.application.config.assets.paths. I verified that +opacity(.5) from the compass/css3 library works as expected if you install compass properly. Which leads us to the next point:
The rails31 branch is merged into master now and version 0.12.alpha has been released that should support Rails 3.1. However, this is version is not yet without bugs. So I recommend to rely on the newest master revision:
gem 'compass', git: 'git://github.com/chriseppstein/compass', branch: 'master'
The only bug I am encountering at the moment is a "undefined method nil.[]" Exception when trying to compile statements that create sprites. See this issue. A workaround for that is to set sass_options = {quite: true} in your config/compass.rb
Further note: I do not have Sass in my Gemfile any more. I think you can remove that dependency when relying on sass-rails.

Updating HAML & SASS to 3.1

I noticed today that haml & sass have split in their upgrade to 3.1.
I used to get them both in my Rails project with gem 'haml-rails' (though, perhaps I manually added SASS to my gem directory?!? Anyway...)
I'm trying to understand dependencies & whatnot and wondering what I need to do now to get both haml & sass updated to 3.1 in my project(s)...
I see haml docs now say to use gem 'haml' to get haml...does this mean haml-rails is unnecessary/redundant now?
Based on my test, you still need haml-rails if you want .haml views automatically generated when you run rails g controller or rails g scaffold
I think it is, yes. I've just started using SASS in my Rails 3 project, and my Gemfile just contains:
gem 'haml'
... and everything appears to be working fine, as far as I can see.
UPDATE: Just realised I was actually running 3.0.25 when I wrote this post, but gem 'haml' already worked at that stage. I've just upgraded to 3.1.1, and it's still working fine :)

Best way to require Haml on Rails3 engines

I'm developing a Rails3 engine application, and I want to use Haml for the views.
First, what I have done was to add this to the engine Gemfile:
gem "haml"
While I was testing my engine, it was working OK (I have used https://github.com/josevalim/enginex to generate the gem and test it with the dummy application).
My problems started when I tried to use the engine on a real Rails application. The application does not have gem "haml" on it's own Gemfile, and so it was not initializing Haml, so I was receiving template not found errors as it was not looking for the .haml views. I was thinking that by requiring Haml on the Engine it would be enought for it to be also required by the Rails application.
What I have done for now was to add a config/initializers/haml.rb on the engine with this code:
require 'haml'
Haml.init_rails(binding)
It's working now, but I'm wondering if this is really a good way to do it.
Why Rails is not calling Haml "init.rb" file and so initializing Haml correctly by just adding gem "haml" to the engine Gemfile?
Two things are necessary. First, in the .gemspec:
s.add_dependency 'haml', ['>= 3.0.0']
And in your lib/gem_name.rb:
require 'haml'
And then run bundle both inside the gem and app directories.
I think you will have to put haml in the engine gemspec as a dependency in order for bundler to install haml in the target application (and show up in its Gemfile.lock). Something like this:
Gem::Specification.new do |s|
s.add_dependency(%q<haml>, [">= 0"])
end
I just tested this out on one of my engines. Without the dependency in the .gemspec it did not install haml in the target app (did not appear in Gemfile.lock). After I added haml to the gemspec as a dependency, it does show up:
PATH
remote: /rails_plugins/mine/my_engine
specs:
my_engine (0.0.0)
formtastic
haml
inherited_resources
settingslogic
sqlite3-ruby
GEM
remote: http://rubygems.org/
specs:
#................
haml (3.0.25)
#................
If you are using jeweler, it will add the dependencies to the gemspec automatically based on what is in your Gemfile.. it even adds a developement_dependency if you have the group defined in your Gemfile. I have only looked at enginex briefly, so I don't know if it has a similar rake task to build the gemspec.
This might help clarify some things:
http://yehudakatz.com/2010/12/16/clarifying-the-roles-of-the-gemspec-and-gemfile/

Resources