rails 3 sass compiling - ruby-on-rails-3

Hello I have one question I have my file main.scss which is in public/stylesheets/scss. In documentation is written:
By default, .sass and .scss files are
placed in public/stylesheets/sass
(this can be customized with the
:template_location option). Then,
whenever necessary, they’re compiled
into corresponding CSS files in
public/stylesheets. For instance,
public/stylesheets/sass/main.scss
would be compiled to
public/stylesheets/main.css.
I have in my gemfile gem 'haml'
And from my view I do sth like this
= stylesheet_link_tag 'main'
And the file is not found when I check the source(there is a file with with information about routing error). I guess that compiling it by hand it is not way to go so how I can make compile scss file to public/stylesheets automatic? What mean in documentation that they are compiled when necessary?
Thanks in advance

Put your .sass or .scss files in public/stylesheets/sass, not public/stylesheets/scss. Then the stylesheets should automatically generate whenever you change the corresponding sass/scss file. The generated stylesheets end up in public/stylesheets/.
Renaming the folder should make it all work.
EDIT: it looks like Rails 3.1 is going to be not only including SASS by default, but it will also be moving most of the stuff found in the public folder to the app folder... so this answer will only apply to versions of rails before 3.1.

Related

rails g creates haml file, need erb files to be generated

I have comfortable-mexican-sofa gem installed for my app. It has haml-rails as its dependency. So when I run rails g scaffold scaffoldname I get files with .haml extension in the view. But I don't want .haml I need .erb files to be generated. How do I do that?
You can set your default template engine as erb.
See the documentation here for changes you can make in your config/application.rb.

Is there a "Rails Way" include a jQuery plugin in the Asset Pipeline?

Many jQuery plugins have the following directory structures:
/<plugin name>
../css
../images
../js
The CSS files usually have relative links to the images in them. What I want to do is include these plugins in the Rails Way under the Asset Pipeline, and hopefully that doesn't involve having to renamed the file references to remove the relative links. Is there such a Rails Way?
Could it also be that it's overkill to include an already-minified jQuery plugin in the Asset Pipeline?
You should try to add your assets to the load path which is the recommended way, as far as I know. If the application you're running has the assets-pipeline activated, it should find your assets after expanding the path in your application.rb
config.assets.paths << Rails.root.join("plugins/plugin_name/assets/")
Not shure, if this is what you asked for but if not, you should check: http://guides.rubyonrails.org/asset_pipeline.html#asset-organization
Remeber to restart your server
I had the same issue and also tried to find "the Rails way" to do this. And this is what I ended up with at the end of the day:
As Rob already mentioned:
vendor/assets is for assets that are owned by outside entities, such as code for JavaScript plugins and CSS frameworks.
Source: 2.1 Asset Organization
Lets take a practical example: using the jquery_datepicker gem (Note: we had to use a workaround because of this issue: bundle pack does not work with git sources).
1) Installing the gem (pretty straighforward):
cd vendor/gems
git clone https://github.com/albertopq/jquery_datepicker.git
2) Add this to your Gemfile
gem 'jquery_datepicker', :path => 'vendor/gems/jquery_datepicker'
3) Install a jquery-ui theme
From ThemeRoller select a theme, check Datepicker and Slider
and the jQUery version
Download and extract the content of the package
CSS/images from the css/theme-name folder move them:
jquery-ui-1.8.xx.custom.css to app/vendor/stylesheets/
the images folder to app/vendor/images/ (yes, move the entire folder images so you end up with something like this app/vendor/images/images/ui-icons_256x240.png
i18n from the development-bundle/ui/i18n folder (optional) move them to:
Create a folder i18n under app/vendor/javascripts/
move jquery.ui.datepicker-xx.js to this folder app/vendor/javascripts/i18n/
make sure the i18n folder is loaded so include in application.js
//= require_directory ./i18n
vendor/assets is loaded automatically AFAIK so you don't have to include the path in the asset pipeline.
I'd like to see how others are approaching this, it's a very good question.
I think the reason you haven't received an answer is because it's kind of unclear what you're asking. Are you asking if it's overkill to put your plugins in the asset pipeline? Are you asking if you have to rename file references?
I always put all my jquery plugins in my asset pipeline. Overkill or not, there all in one place and they only get compiled once so even if compiling them takes longer, it doesn't affect my app.

How can I make my plugins work in a location other than "vendor" in Rails 3.2.8?

I followed some instructions, which I'm unable to locate again, to move my plugins to the "lib" directory.
So I have this structure now:
/lib
/lib/plugins
/lib/plugins/plugin1
/lib/plugins/plugin1/lib
/lib/plugins/plugin1/lib/plugin1.rb
I had tried this in my application.rb:
config.autoload_paths += %W(#{config.root}/lib #{config.root}/lib/plugins)
But that isn't working. What am I doing wrong?
UPDATE:
Trashing the plugins part of the path and putting the plugins directory directly in doesn't work either. Each plugin has an init.rb file that basically requires the main library, also.
I'm not sure if these are the instructions you were following, but you probably want the plugin folder itself at the root of lib:
/lib
/lib/plugin1
/lib/plugin1/plugin1.rb
You also may need an initializer: config/initializers/plugin1.rb.
If that doesn't help, please post more information about what you're expecting to see and why it's not working.

writing tests for modules in lib folder

I want to write unit tests for a module file I created and put it in lib directory. Under test/unit directory, I have created a mylib_test.rb file. In the file I have required mylib. When I run rake test:units it gives a const_missing: uninitialized constant mylib::constantname error. I'm thinking that this is because it is not loading the rails environment since the constant is defined in one of the initializers file. I'm I correct? How do I get it to work? What is the best way to write unit tests for modules?
I'm using rails 3.1.3 and the model works perfectly when I run the application both from terminal and from a browser.
I just ran into this as well. There are (at least?) 2 possible problems:
Your module is not in the autoload path
Look in config/application.rb for this line:
config.autoload_paths += %W(#{config.root}/extras)
If it's commented, uncomment it. This line will turn on autoloading for all files inside extras, and all files in subdirectories of extras, too. It's probably safest to move your modules into extras, but if you really want to leave them in lib, change the line to be:
config.autoload_paths += %W(#{config.root}/extras #{config.root}/lib)
Your module is in the autoload path, but not named the way Rails expects
(see this: Rails 2.3.5: How does one access code inside of lib/directory/file.rb?)
By convention, Rails wants the name of your module to match the directory hierarchy and the filename. So the file extras/mylib.rb would be expected to contain
module Mylib # not MyLib or My_lib
...
end
This works for subdirectories as well, so a file extras/mydir/mylib.rb should contain:
module Mydir
module Mylib # or class Mylib
...
end
end
This naming convention is the same as what Rails expects for controllers and models. Underscores in the filename turn into a camelcase class/module name. A file called my_lib.rb would be expected to have a module MyLib in it (but not Mylib).
NOTE that autoload does not mean that the module is automatically loaded at startup; rather, it's automatically loaded when it's first used. So even if you have some code like puts "hi from mylib" at the top of your mylib.rb file, you won't see that print until your code uses Mylib somewhere.
Finally, if you really want your modules to load at startup, go create a file called config/initializers/force_load_libraries.rb and put this in there:
Dir.glob("#{Rails.root}/extras/force_load/*.rb").each { |f| require f }
Now go put your libs in extras/force_load and they should load when Rails starts up.
I finally realized what was wrong. In my test files, I was including my modules from the lib directory, Instead of reopening the module and put the test files in the the module. After doing that rake test:units works perfectly. Test files should remain in test/unit directory

Getting SASS to work with Rails 3

I am trying to get SASS to work with Rails 3.
I have added gem 'haml' to my Gemfile (and ran bundle install) and I have added a styles.scss to my public/ directory, but it does not seem to be working. Can someone help me out? Thanks!
By default, Sass expects sass/scss files to go in public/stylesheets/sass, which are then compiled into css files in public. You can change this path by setting Sass::Plugin.options[:template_location] in your config, though.