Rails 3.1 Asset Pipeline manifest file won't pick up javascripts - ruby-on-rails-3.1

I have a whole MESS of javascripts in vendor/assets/javascripts. In my app/assets/javascripts/application.js file, I have the directive:
//= require_tree .
Does that only reference the current app/assets/javascripts directory, and not lib/assets or vendor/assets?
If I explicitly include the javascripts, it works. I just don't really want to do that if I don't have to.
Is there something I am missing that will allow the assets pipeline to be able to serve up assets from outside the app directory (lib and vendor) automatically?

require_tree only pulls in assets that are under the application.js file.
lib/assets and vendor/assets are already included in the lookup paths for the pipeline (refer this code).
You can include these vendored files by using a second manifest.
Go to vendor/assets/javascripts and create a file called misc_vendor.js
Inside that add put a require_tree directive.
Then refer to that file from your application.js manifest:
require misc_vendor
If you have any issues because of loading order you can manually require the vendor files in the order you need instead of using require_tree.
As part of the conversion to the pipeline it may be a good chance to clean up things! :-)

Also, you can do it without a second manifest like this:
//= require_tree ../../../vendor/assets/javascripts/.
The path should be relative to the 'app/assets/javascripts/application.js' manifest file.

You need to extend path in application.rb file like this.
config.assets.paths << "#{Rails.root}/vendor/assets/some file name"
Refer this Guide for more details

Related

Image paths in SASS file which has been included?

Im my project I have a main SASS file in the root. I also have a folder with _other.sass and icon.png in it.
main.sass
folder/_other.sass
folder/icon.png
My main SASS file includes my other SASS file:
In main.sass:
#include 'folder/other'
In _other.sass:
div {
background: url(icon.png);
}
Should the path to the image resolve correctly in this case? From the point of view of _other.sass the path is correct, but its not correct relative to main.sass.
Since all the compiled code will be included in your final *.css file, all urls must be relative to that resulting *.css file. In the code you have posted, the url is assigned as regular css code, so it wont be transformed in any way.
If you would use compass, what is a great extension to sass, you can use the provided functions, f. i. those to generate the needed urls. With compass you can configure all the base paths and the stuff alike, sass itself wont do that for you.

Make folder/scripts autoloadable in rails 3

I have downloaded a gem and cuztomized few of its methods and placed it in scripts folder. How do I auto load those scripts instead of requiring them in every controller/other scripts that I need.
lib folder is the place to put those files. Then you should edit your config/application.rb file to configure auto load paths like this:
# Custom directories with classes and modules you want to be autoloadable.
config.autoload_paths += %W(#{config.root}/lib)

Circular dependency error in SCSS file in Rails 3.1 asset pipeline

I've got application.css.scss in my assets/stylesheets directory, along with a number of controller-specific scss files.
The application.css.scss file has
*= require_self
*= require_tree .
And I have some ordinary scss following these directives.
When both require statements are enabled, I get "/app/assets/stylesheets/application.css.scss has already been required." (There is no application.css, I've double-checked.)
Commenting out the *require_tree .* line eliminates the error, but obviously doesn't include the other scss files in the directory. The workaround is to require these files individually but that's not the long term solution. (Taking out the require_self line doesn't eliminate the error, either.)
I had thought Rails was smart enough to resolve this -- I have a feeling I've missed something obvious. Thoughts?
You say it doesn't include the other scss files. What are the other scss/css files and did you inserted the *= require_tree . in at least one of them? This would load the application.css file again. I had the same problem. I just removed these lines from all other css/scss files.

How to add JS/CSS files to Joomla modules?

I am starting out with Joomla and am writing a simple Joomla module. I am using some custom CSS and JS in my module. Now when I distribute this module I need my JS/CSS files to go with the ZIP. I have added my files in my module ZIP file.
This is what i need to know -
How do I refer to these CSS/JS files in my module so that even if I distribute the module as a zip i would not have to send the css/js files separately?
I tried looking at different solutions including
http://www.howtojoomla.net/how-tos/development/how-to-add-cssjavascript-to-your-joomla-extension But I was not able to figure out what the URL for the JS/CSS file should be?
I am using Joomla 1.7 hosted on a cloud hosting site.
Thanks
I'd say that the HowToJoomla Site's article pretty much sums up the process.
Here is the process with a few more steps added - hopefully this will help.
I am assuming you have got as far as packaging your extension and have been able to get your css and javascript files to install on the server. They may be in your module folder, or probably more correctly they should be within your modules sub-folder under the /media/ folder.
If after installing the module you can not locate your css and js files it is because you haven't referenced them correctly within your component's xml installation file. This page contains info about the xml installation / manifest file for 1.6/1.7 add-ons although it is for a component: http://docs.joomla.org/Developing_a_Model-View-Controller_%28MVC%29_Component_for_Joomla!1.6_-_Part_01 they are very similar.
Either way - find your files within Joomla's folder structure and make a note of their relative path from the root of the website - ie the folder containing configuration.php
Now somewhere within your module's php - add the line that gets a reference to the JDocument object.
$document = JFactory::getDocument();
Now add the line that adds your javascript to the html head area:
$document->addScript('url/to/my/script.js');
obviously replace 'url/to/my/script.js' with the actual relative path to your javascript.
Now add the line that adds your css to the html head:
$document->addStyleSheet('url/to/my/stylesheet.css');
again adjust the path - it may for example be media/mod_mymodule/mymodule.css (if your module were called 'mymodule').
Only things to be aware of are that you need to add these lineswithin executable php tags NOT within a html area after exiting php mode.
You could add your js/css files to /media folder.
http://docs.joomla.org/Manifest_files#Media_files
Just add to your manifest file:
<files>
...
</files>
<media folder="media" destination="mod_your_module">
<folder>css</folder>
<folder>js</folder>
</media>
Inside your installable package, you should now have the /media folder.
Then add to view file:
$doc =& JFactory::getDocument();
$doc->addScript("/media/mod_your_module/js/script.js");
This article explains the benefits of this approach:
http://blog.joomlatools.com/2008/09/hidden-feature-joomlas-media-folder.html
You could use:
JHTML::script('modules/mod_your_module/js/script.js');
JHTML::stylesheet('modules/mod_your_module/css/stylesheet.css');
This example does not require JFactory::getDocument()
$document = JFactory::getDocument();
$modulePath = JURI::base() . 'modules/mod_your_module_name/';
//Adding JS Files
$document->addScript($modulePath.'js/myscript.js');
//Adding CSS Files
$document->addStyleSheet($modulePath.'css/style.css');
<script type="text/javascript" src="<?php echo JURI::BASE()?>modules/your_module_name/js/your_js_file"></script>
for Css:
<link href="<?php echo JURI::BASE()?>modules/your_module_name/css/your_css _file" type="text/css" media="all" rel="stylesheet">

Directory layout for pure Ruby project

I'm starting to learn ruby. I'm also a day-to-day C++ dev.
For C++ projects I usually go with following dir structure
/
-/bin <- built binaries
-/build <- build time temporary object (eg. .obj, cmake intermediates)
-/doc <- manuals and/or Doxygen docs
-/src
--/module-1
--/module-2
-- non module specific sources, like main.cpp
- IDE project files (.sln), etc.
What dir layout for Ruby (non-Rails, non-Merb) would you suggest to keep it clean, simple and maintainable?
As of 2011, it is common to use jeweler instead of newgem as the latter is effectively abandoned.
Bundler includes the necessary infrastructure to generate a gem:
$ bundle gem --coc --mit --test=minitest --exe spider
Creating gem 'spider'...
MIT License enabled in config
Code of conduct enabled in config
create spider/Gemfile
create spider/lib/spider.rb
create spider/lib/spider/version.rb
create spider/spider.gemspec
create spider/Rakefile
create spider/README.md
create spider/bin/console
create spider/bin/setup
create spider/.gitignore
create spider/.travis.yml
create spider/test/test_helper.rb
create spider/test/spider_test.rb
create spider/LICENSE.txt
create spider/CODE_OF_CONDUCT.md
create spider/exe/spider
Initializing git repo in /Users/francois/Projects/spider
Gem 'spider' was successfully created. For more information on making a RubyGem visit https://bundler.io/guides/creating_gem.html
Then, in lib/, you create modules as needed:
lib/
spider/
base.rb
crawler/
base.rb
spider.rb
require "spider/base"
require "crawler/base"
Read the manual page for bundle gem for details on the --coc, --exe and --mit options.
The core structure of a standard Ruby project is basically:
lib/
foo.rb
foo/
share/
foo/
test/
helper.rb
test_foo.rb
HISTORY.md (or CHANGELOG.md)
LICENSE.txt
README.md
foo.gemspec
The share/ is rare and is sometimes called data/ instead. It is for general purpose non-ruby files. Most projects don't need it, but even when they do many times everything is just kept in lib/, though that is probably not best practice.
The test/ directory might be called spec/ if BDD is being used instead of TDD, though you might also see features/ if Cucumber is used, or demo/ if QED is used.
These days foo.gemspec can just be .gemspec --especially if it is not manually maintained.
If your project has command line executables, then add:
bin/
foo
man/
foo.1
foo.1.md or foo.1.ronn
In addition, most Ruby project's have:
Gemfile
Rakefile
The Gemfile is for using Bundler, and the Rakefile is for Rake build tool. But there are other options if you would like to use different tools.
A few other not-so-uncommon files:
VERSION
MANIFEST
The VERSION file just contains the current version number. And the MANIFEST (or Manifest.txt) contains a list of files to be included in the project's package file(s) (e.g. gem package).
What else you might see, but usage is sporadic:
config/
doc/ (or docs/)
script/
log/
pkg/
task/ (or tasks/)
vendor/
web/ (or site/)
Where config/ contains various configuration files; doc/ contains either generated documentation, e.g. RDoc, or sometimes manually maintained documentation; script/ contains shell scripts for use by the project; log/ contains generated project logs, e.g. test coverage reports; pkg/ holds generated package files, e.g. foo-1.0.0.gem; task/ could hold various task files such as foo.rake or foo.watchr; vendor/ contains copies of the other projects, e.g. git submodules; and finally web/ contains the project's website files.
Then some tool specific files that are also relatively common:
.document
.gitignore
.yardopts
.travis.yml
They are fairly self-explanatory.
Finally, I will add that I personally add a .index file and a var/ directory to build that file (search for "Rubyworks Indexer" for more about that) and often have a work directory, something like:
work/
NOTES.md
consider/
reference/
sandbox/
Just sort of a scrapyard for development purposes.
#Dentharg: your "include one to include all sub-parts" is a common pattern. Like anything, it has its advantages (easy to get the things you want) and its disadvantages (the many includes can pollute namespaces and you have no control over them). Your pattern looks like this:
- src/
some_ruby_file.rb:
require 'spider'
Spider.do_something
+ doc/
- lib/
- spider/
spider.rb:
$: << File.expand_path(File.dirname(__FILE__))
module Spider
# anything that needs to be done before including submodules
end
require 'spider/some_helper'
require 'spider/some/other_helper'
...
I might recommend this to allow a little more control:
- src/
some_ruby_file.rb:
require 'spider'
Spider.include_all
Spider.do_something
+ doc/
- lib
- spider/
spider.rb:
$: << File.expand_path(File.dirname(__FILE__))
module Spider
def self.include_all
require 'spider/some_helper'
require 'spider/some/other_helper'
...
end
end
Why not use just the same layout? Normally you won't need build because there's no compilation step, but the rest seems OK to me.
I'm not sure what you mean by a module but if it's just a single class a separate folder wouldn't be necessary and if there's more than one file you normally write a module-1.rb file (at the name level as the module-1 folder) that does nothing more than require everything in module-1/.
Oh, and I would suggest using Rake for the management tasks (instead of make).
I would stick to something similar to what you are familiar with: there's no point being a stranger in your own project directory. :-)
Typical things I always have are lib|src, bin, test.
(I dislike these monster generators: the first thing I want to do with a new project is get some code down, not write a README, docs, etc.!)
So I went with newgem.
I removed all unnecessary RubyForge/gem stuff (hoe, setup, etc.), created git repo, imported project into NetBeans. All took 20 minutes and everything's on green.
That even gave me a basic rake task for spec files.
Thank you all.

Resources