I am just trying to figure out what's the best way to manage Plugin's Asset in Rails.
For Eg. Let's consider Bootstrap.
I understand there is "vendor" directory for that but even there
I like to keep below directory structure:
App
|-- assets
| |-- plugins
| | |-- bootstrap
| | | |-- fonts
| | | |-- css
| | | | |-- bootstrap.css
| | | | |-- bootstrap.min.css
| | | |-- js
| | | | |-- bootstrap.js
| | | | |-- bootstrap.min.js
I DON'T LIE to follow below directory structure:
App
|-- assets
| |-- fonts
| |-- css
| | |-- application.css
| | |-- bootstrap.css
| | |-- bootstrap.min.css
| | |-- common-style.css
| |-- js
| | |-- application.js
| | |-- bootstrap.js
| | |-- bootstrap.min.js
| | |-- common-script.css
As you can notice above, now if you start adding plugins, files goes into general folders rather grouping all assets with respect to a plugin stay together. If not grouped it will look messy, isn't it?
I understand there is "vendor" directory even there I LIKE to keep below directory structure:
|-- App
|-- vendor
| |-- assets
| | |-- javascripts
| | |-- stylesheets
| |-- bootstrap
| | |-- fonts
| | |-- css
| | | |-- bootstrap.css
| | | |-- bootstrap.min.css
| | |-- js
| | | |-- bootstrap.js
| | | |-- bootstrap.min.js
I DON'T LIKE to follow below directory structure:
|-- App
|-- vendor
| |-- fonts
| |-- assets
| |-- stylesheets
| | |-- bootstrap.css
| | |-- bootstrap.min.css
| | |-- iCheck.css
| |-- javascripts
| | |-- bootstrap.js
| | |-- bootstrap.min.js
| | |-- iCheck.js
Rails has a special directory for adding third party assets. It's called vendor. Underneath it you will find vendor/assets, which contains javascripts and stylesheets subdirectories. So use that instead of app/assets. The latter is where you are supposed to save your own files.
If you are using something like Bootstrap, and this stands true for most plugins, well, it's even easier. Most frameworks are packaged into gems. This means you don't have to worry about manually adding the asset files as the Asset Pipeline handles all of that for you. At most you just have to include the files in your manifest.
A JavaScript file in application.js, for example:
//= require jquery
If you want to go a step further and enforce your own directory structure, you will have to change Rails' config:
config.assets.precompile += %w(vendor/custom_dir/*.jpg)
config.assets.precompile += %w(vendor/custom_dir/*.js)
You can read more about this on Rails guides
You might also find this issue helpful.
Related
I want to backup all .log* files under $JENKINS_HOME/logs via thinBackups Backup additional files Feature. Folder structure is as follows:
/var/lib/jenkins/logs/
|-- health-checker.log
|-- slaves
| |-- Slave\ 1
| | `-- slave.log
| |-- Slave\ 2
| | `-- slave.log
| `-- Slave\ 3
| `-- slave.log
`-- tasks
|-- Connection\ Activity\ monitoring\ to\ agents.log
|-- Connection\ Activity\ monitoring\ to\ agents.log.1
|-- Download\ metadata.log
|-- Download\ metadata.log.1
|-- Fingerprint\ cleanup.log
|-- Fingerprint\ cleanup.log.1
|-- Periodic\ background\ build\ discarder.log
|-- Periodic\ background\ build\ discarder.log.1
|-- Workspace\ clean-up.log
|-- Workspace\ clean-up.log.1
|-- telemetry\ collection.log
|-- telemetry\ collection.log.1
What I have now is the following regex:
^(logs|tasks|.*\.log.*)
which catches the top level health-checker.log and also all logs below tasks. But how do I extend this regex to also include all logs from all slaves (Slave 1, Slave 2 and Slave 3)?
I tried the following regex
^(logs|tasks|.*\.log.*)^(logs|slaves|Slave\ 1|.*\.log.*)
which does not work. I also cannot find any further explanation for the regex formatting thinBackup is using.
P.S. Our security departement requires backup of all logs for the last 90 days.
I think this plugin is buggy when it comes to include and exclude regex. If you exclude .*xml it shouldn't backup any files ending in xml but some files (not all) are still backed up.
.
|-- app
| |-- assets
| | |-- images
| | ........
| | ........
| |-- controllers
| | ........
| | ........
| |-- helpers
| | ........
| | ........
| |-- mailers
| | ........
| | ........
| |-- models
| | |--comments
| | |--new_events # Inherites `events`
| | |--old_events # Taking name to follow the sequence by name
| | |--posts
| | *-- comment.rb
| | *-- new_event.rb
| | *-- old_event.rb
| | *-- post.rb
| | ........
| | ........
Represents:
`|--` : Folder
`*--` : File
Note:
`new_events` -> Folder which also contains other folders and files
`old_events` -> Folder which also contains other folders and files
Both folders are somehow identical, new_folder's files inherites same name file from old_events to get the properties.
new_events inherites old_events. But Rails auto-loading, loads new_events first and it inherites old_events so that cause issue because it is not yet loaded.
Tried:
I have tried to load old_events before new_evensts but old_events also inherites some other classes like old_event.rb etc.
In application.rb file, I have added below:
config.autoload_paths += %W(#{config.root}/app/models/old_events/)
but that even causes issue that, Rails tried load it first but it also has dependenices. It seems blocker to me.
Expected:
I want the loading process should everything as ususal but it should load old_events folder before new_folder. I can't change the name so it would appear before.
Or a way by which I load new_events in the last so that events would load before and rest of the models as well?
Not sure, autoload_paths works here You can do like this:
in config/application.rb
config.autoload_paths << Rails.root.join('lib')
and keep the right naming convention in lib.
in lib/test.rb
class Test
end
in lib/test/subtest.rb
class Test::SubTest
end
For more understand go through this links blog or articles
I can't seem to get this to work in my rails app. At a high level, I wan't to use LESS in my app and have it go through the asset pipeline.
This is what my assets look like:
assets
|
|--- javascript
|
|--- stylesheets
|
|-- reset.less
|-- my-mixins.less
|-- my-variables.less
|-- base.less
|
|--- images
|
|--- libs
|
|---foo
|
|--css
|
|--- foo-mixins.less
|
|--- bar.less
I would like to import "my-mixins.less", "my-variables.less" and "foo-mixins.less" into "base.less", as well as other files. "bar.less" should also be added to specific pages if needed.
How would I go about doing this?
This should work, assuming you're using the less-rails gem.
First, add this to somewhere in your configuration (I have mine in my environment.rb):
YourApp::Application.configure do
config.less.paths << File.join(Rails.root, 'app', 'assets', 'less')
end
Now, if you have a file setup like this:
assets
|
|--- javascript
|
|--- css
| |--- site-stylesheet.css.less
|
|--- less
|--- my-mixins.less
You can import my-mixins in site-stylesheet by using:
#import "my-mixins"
And import the stylesheet into your app with:
<%= stylesheet_link_tag "site-stylesheet" %>
You should be able to extrapolate from there for your specific directory structure.
In base.css.less you can write #import "my-variables.less"; and it should work.
The files have to be in the "vendor/assets" or "vendor/plugins" (both at the root level) folder in order to reference them with relative paths.
I have these files in my lib/assets folder (sketchyPad is a jQuery plugin for drawing on html5 canvas and farbastic is a color picker used by sketchyPad):
lib/
|-- assets
| |-- javascripts
| | `-- lib.js
| `-- sketchyPad
| |-- README.md
| |-- brushes
| | |-- simple.js
| | `-- smooth.js
| |-- example
| | |-- index.html
| | |-- jquery-ui-1.8.16.custom
| | | |-- css
| | | | `-- ui-lightness
| | | | |-- images
| | | | | |-- ui-bg_diagonals-thick_18_b81900_40x40.png
| | | | | |-- ui-bg_diagonals-thick_20_666666_40x40.png
| | | | | |-- ui-bg_flat_10_000000_40x100.png
| | | | | |-- ui-bg_glass_100_f6f6f6_1x400.png
| | | | | |-- ui-bg_glass_100_fdf5ce_1x400.png
| | | | | |-- ui-bg_glass_65_ffffff_1x400.png
| | | | | |-- ui-bg_gloss-wave_35_f6a828_500x100.png
| | | | | |-- ui-bg_highlight-soft_100_eeeeee_1x100.png
| | | | | |-- ui-bg_highlight-soft_75_ffe45c_1x100.png
| | | | | |-- ui-icons_222222_256x240.png
| | | | | |-- ui-icons_228ef1_256x240.png
| | | | | |-- ui-icons_ef8c08_256x240.png
| | | | | |-- ui-icons_ffd27a_256x240.png
| | | | | `-- ui-icons_ffffff_256x240.png
| | | | `-- jquery-ui-1.8.16.custom.css
| | | `-- js
| | | `-- jquery-ui-1.8.16.custom.min.js
| | |-- jquery.min.js
| | `-- mattfarina-farbtastic-4bb6bbf
| | |-- CHANGELOG.html
| | |-- LICENSE.txt
| | |-- README.html
| | |-- README.md
| | |-- demo1.html
| | |-- demo2.html
| | |-- farbtastic.css
| | |-- farbtastic.js
| | |-- farbtastic.min.js
| | |-- marker.png
| | |-- mask.png
| | `-- wheel.png
| |-- sketchyPad.css
| `-- sketchyPad.js
So I want to include sketchyPad.js, sketchyPad.css and also all the js in the brushes folder and also the farbtastic.min js file.
In app/assets/javascripts/application.js I put:
//= require sketchyPad (this part works fine and correctly includes sketchyPad.js)
but I also want to include
lib/assets/sketchyPad/brushes/simple.js
lib/assets/sketchyPad/brushes/smooth.js
and any other js files in the brushes folder, how do I use require_directory when it is not in the app/assets but in the lib/assets or vendor/assets?
Update:
I have tried
require_tree sketchyPad
but errors with:
require_tree argument must be a relative path
I have tried
require_tree ./sketchyPad
but errors with:
require_tree argument must be a directory
I think require_tree must be relative to the app/assets folder, but putting something like
require_tree ./../lib/assets/sketchyPad/brushes
also gives error:
require_tree argument must be a directory
Eitherway I don't want to include the entire sketchyPad tree, because it does contain some js files that I don't want to include... like examples and it's own version of older jQuery.
UPDATE:
What I ended up doing was creating a lib.js in the lib/assets/javascripts folder with a manifest to include the sketchyPad js files as relative to the lib/assets folder rather than the app/assets folder.
in app/assets/javascripts/application.js I put:
//= require jquery
//= require jquery_ujs
//= require jquery-ui
//= require lib
and in lib/assets/javascripts/lib.js I put:
//= require ./../sketchyPad/sketchyPad
//= require_tree ./../sketchyPad/brushes
//= require ./../sketchyPad/example/mattfarina-farbtastic-4bb6bbf/farbtastic.min
That appears to include the correct files. Was there an easier way to do this?
Perhaps:
//= require_tree ./sketchyPad
No need to create/change manifest, just add the following line to application.rb
config.autoload_paths += %W(#{config.root}/lib)
config.autoload_paths += Dir["#{config.root}/lib/**/"]
The Rails way (according to this answer which worked for me) would be to create an index.js file in lib/assets/sketchyPad with just
//= require_tree .
Although it looks like the plug-in is quite extensive, so I'd imagine you have to split it further into subdirectories.
I have some submodules organized like this:
|-- app
| |-- models
| | |-- foo
| | | |-- foo-1.rb
| | | |-- foo-2.rb
| | | |-- foo-3.rb
| | |-- foo.rb
How can I get autotest to notice changes made to foo-*.rb, and then run the appropriate specs?
You can populate your autotest/discover.rb file with mappings:
Autotest.add_hook :initialize do |at|
# match the model name (the whole Regex object is in _)
at.add_mapping(%r%^app/models/(foo)/\w+\.rb$%, true) do |filename, _|
"spec/models/#{_[1]}_spec.rb"
end
end
You can find more how to use the mappings and hooks in the API docs.
Perhaps you should investigate watchr
https://github.com/mynyml/watchr
It's similar to autotest, while being quite a bit more configurable and more easily setup.
Another interesting alternative is guard.