is ruby on rails supporting folder hiearachy? - ruby-on-rails

i using ruby on rails programing language for the making a very simple project which is need to create folder hierarchy just like yahoomail folder. but i need to also subfolder create in this directory so is support ruby on rails?

Yes, it does.
You can create recursive directories using
require 'fileutils'
FileUtils.mkdir_p 'my/path/to/something'
Following links can be useful for you
http://rosettacode.org/wiki/Walk_a_directory/Recursively#Ruby,
Ruby: how do I recursively find and remove empty directories?

Related

how to overwrite rails engine files that are in the lib directory?

I want to overwrite a file of a rails engine, but that file is in that engine's lib/rails directory. When I take the same file and drop it in my lib/rails directory it doesn't overwrite the file. Seems like rails handles the lib directory differently than say files that are in the app directory.
Also I can't place this file in the initializer folder (per this solution) as the filename is the same as an exisiting file that is needed. What is the proper way of doing this?
The best way I have found to solve this problem is by adding
require "#{Rails.root}/lib/path/to/file"
at the end of config/application.rb. The pollution is minimal, and achieves the same effect as the override would.
Currently using it in a Spree project to override lib/spree_samples.rb.
Edit
Found a better way in this answer:
Overloading lib files from gem mounted as engine
Essentially, put the file you want to override in your config/initializers directory.

How can I use separate class to be used in rails app?

If I have a ruby class which I want to use it as a library in a rails app, how (what folder) should I include it in the application?
I want to use the class in my controller. Do I just simply use 'require' to include the class?
You can just do a require in the controller assuming the file is somewhere in your load path. I follow a few simple strategies:
If its a class that adds plugin like behavior but isnt quite large enough to write a plugin, I just put it in config/initializers since they the lib is in the load path and all the files are required for you on startup. No additional requires needed.
The other option is to put them in the lib directory and just require them when you need them. If you are using rails 3, the lib directory is not a part of the load path and you will need to add it.
I'd recommend throwing it in the lib folder - you then can require it from your config/environment.rb file and if I recall correctly, that should be all you need to do.

How to test lib files in Rails?

I understand the benefit of putting classes, modules, etc. in the lib folder in Rails, but I haven't been able to find a clean way of testing these files. For the most part, it seems like unit tests would be the logical approach.
I guess my question is: What is the "rails way" for testing lib files?
Your lib directory is not automatically loaded by rails.
You can use ActiveSupport::Dependencies to override const_missing. Basically rails will try to load your constants when it boots, if they are undefined or not in memory it will look at your load paths.
If you have a file like my_class.rb, rails expects it to be MyClass.
The beauty of this is if you have some stuff in your lib directory, you don't have to require it with a relative path you can just say require 'something', instead of require 'lib/something'.

Does it make sense to create a Ruby gem that consists of only Rails template partials?

I'm trying to understand what does and doesn't work in Ruby gems (primarily from the perspective of creating one for myself to reuse functionality from one project to the next).
When you create a Ruby gem, are you limited to including only Ruby functionality or could you create a gem that consisted of Rails templates, CSS files and JavaScripts?
If so, how would the user of the gem access those resources in a Rails project (besides adding a 'config gem' statement to the environment.rb file?
No it doesn't. Gems are code libraries, not static files.
When you load a gem, you don't move the files to your public directory. You load a ruby file and get access to a module. So, you can only execute ruby code within it.
But, your ruby code can give you some helpers allowing you to easily build your design for example.
If you want to include some static file resources in each of your ruby projects, I'd see two solutions:
Create a template project, including all the css, javascript etc. that you use every time. You can take suspenders as an example of this.
Create a new repository with all these static files and add it as a submodule (with git or svn) and include it in each of your projects.
The first solution would be my favorite, but you might want to take the other one.
As dmathieu notes, gems are for ruby libraries that are installed in the system load path, not for things that go into your project directory. If you do want to package your rails project skeleton as a gem, though, it would be simple enough to have a small ruby executable that served as a generator to copy the skeleton project tree into a target directory. You could then package the whole thing up into a gem, your skeleton files could sit in the gem directory, and you could say generate_new_project someproject to copy them into your new project directory.
You can easily add partials and other HAML, Builder, or ERB views. The following structure should work:
# in my_gem/rails/init.rb
ActionController::Base.append_view_path(File.expand_path(File.join(File.dirname(__FILE__), '..', 'views')))
# in my_gem/views/my_gem/_some_partial.html.erb
This is a partial from MyGem!
# in your_rails_app/views/some_controller/some_view.html.erb:
<%= render :partial => '/my_gem/some_partial' -%>
That doesn't directly answer your question about static files, though. Often the best bet is a generator that copies the CSS, JS, and other files to your public directory. Alternatively, you could use a StaticFilesController and put the static files in that views directory.

Adding standalone Ruby Files to a Ruby on Rails project

I have a ruby on rails project with scaffolding , views, layouts, models and such. I also have some standalone ruby (.rb) files which i would like to include in my afforementioned project. Is it possible to simply call these methods defined in these rb files by placing them somewhere and calling them from the controller or so?
If not, how can i go about and add them?
Any assistance would be highly appreciated thanks
Place your code in the lib folder, then create a new initializer file in config/initializers and require the files u want.

Resources