Rake vs. Warbler in Ruby? - ruby-on-rails

I have used Warble to make .war files. That worked pretty well.
Some of the tutorials online suggest using the "rake" command at various times. If rake is for compilation, I thought Ruby didn't need compilation. Is it a substitute for Warble? Or do these two play different functions?
When is rake meant to be used?

Rake is a tool written in Ruby for automating tasks, which you also write using a Ruby syntax. Ruby program's don't have to be built, but there are still plenty of other tasks involved in development that you can automate instead of doing yourself each time.
Some examples from Rails include migrating your database to a new schema or creating a new database.
Rake lets you write tasks with a Ruby syntax, and you can also specify dependencies between tasks, so that running one task will cause all of its dependencies to be ran first.

Think of rake as a make for Ruby. For example for one of the gems I develop, the Rakefile includes several tasks, like running all the tests (rake test) or building the gem (rake gem:build). More info on the web site:
http://rake.rubyforge.org/

Related

Accessing two different Rails environments from a single Ruby script

I'm trying to write a Rake task that will retrieve data stored in the databases of two different Rails apps, with different Ruby and gem versions. What I have so far is
task :get_data do
puts `/<path>/<to>/<first>/<app>/bin/rails runner 'FirstDataRetriever.new.as_set'`
puts `/<path>/<to>/<second>/<app>/bin/rails runner 'SecondDataRetriever.new.as_set'`
end
The problem is that Rails runner is trying to execute the DataRetriever classes using the Ruby version of the Rakefile, which is in a separate repo using different infrastructure.
Both the DataRetriever classes make use of ActiveRecord models in each app, so they must be run in the Rails environment of each app, but I'm not sure how to go about this.
You can specify Ruby version in .ruby-version file inside of root directory of your projects if you are using RVM or rbenv.
You can specify Rails environment use RAILS_ENV environment variable i.e. RAILS_ENV=production bin/rails runner ...
I hope it can help to solve your problems.

What is the scope of rake in Rails

I am very new to ruby on rails. What is the function of rake. For example here one command like
rake db:create
Here this command which is used to create the new database. But before we use rake. Why we use rake word here, what it is function.
And what is the different between rake rake-task & rakefile.
Thanks for your advice.
Rake is a Ruby build command. When you invoke rake, you are actually using the Rake tool and you ask Rake to read and use the Rakefile available in your Rails application folder.
Rake is used to automate several tasks, from the creation of a database, to a database migration, to the generation of new models or controllers.
I encourage you to read this article about Rake to learn more about this tool. It will be one of the fundamental tools you'll use working with Rails, so you really need to understand what it is before moving forward.
Rake is a gem that acts like make in the *nix world. You use it to run scripts, usually the Rakefile. You use it to run tests or set up databases. Rails uses it extensively because there are many steps that need to be run between deployments.
Rake can do pretty much anything you need to automate.
There is a Wikipedia article on it that is a good starting point: http://en.wikipedia.org/wiki/Rake_(software)
The main thing it does, really, is to be a central point for command-line tasks for the application. You don't really need it for half the things it's used for (and quite frequently isn't the best IMO), but by running rake -D you'll get a list of all the commands available, which is helpful.

What does the term "vendoring" or "to vendor" mean for Ruby on Rails?

Examples:
"Vendoring Rails application at test/dummy"
"What is the recommended way of vendoring Rails for production?"
Vendoring is the moving of all 3rd party items such as plugins, gems and even rails into the /vendor directory.
This is one method for ensuring
that all files are deployed to the production server the same as the dev environment.
Best way to do this is either:
rake gems:unpack
Or
rake rails:freeze:gems
Use rake -T to see a full list of rake tasks.

What is the logic behind tasks in Rake versus under `rails`

I try to grasp the logic behind some tasks being "rails" whereas others, the majority, is found as rake task. Why rails server and not rake server for example?
I can understand that the bootstrapping cannot be done in rake: after all, you first need a rakefile and other requirements before you can start using rake. So creating the project with a rails binary seems only practical.
But why generate, server, console, yet not migrate or assets? I don't see the logic. Is there any?
IMO the rails scripts are for "live" console usage, like during development.
The rake tasks are more "automated" tasks, for example, that might be run as part of a build or deploy cycle, like by a CI server. Some rake tasks might group rails/etc. commands together (like tests).
A rake script is a utility/build tool for some common tasks when developing. For example, you need to do deployment, run test, database stuffs, truncate log files, compile assets .... You can create your own custom rake scripts.
A rails script is ruby file located under script directory for the purpose of the gem rails. This is what the gem does. Rails is a ruby web framework, so the command rails is for starting the rails apps, go to rails console, generate files. It's bundled when you install the gem.
You can think of rails command like bundle command for bundler. bundle install, bundle update ... all are related to resolving gem dependencies. rspec command for running tests...
Some gems has an executable script such as rails, bundler, capistrano, whenever, rspec. Some other gems doesn't have such as builder, will_paginate....
You can check this out for how to add executable to a gem, http://guides.rubygems.org/make-your-own-gem/#adding-an-executable

how to re-initialize a ruby on rails project

I've just been given a folder containing everything in a ruby on rails project that was developed 2 years ago. The folder doesn't contain a sqlite3 file however, so I am unable to view the database and I currently don't know how to run the application on my local computer.
What are the steps to re-starting a previously developed project so that further development can be done to it?
Thanks!
Owwwwww that is hard.
I see two main problems: gem dependencies and the database.
Gem dependencies are now cleanly managed with bundler, but two years ago that was not the case.
Getting all the gems you need for your project together will be the hardest part (depending on the size of the project).
Gem dependencies
Previously gems had to be defined in config/environment.rb, but actually require could be scattered all over the app, and depending on the discipline of the original developers, I guess no versions were specified. So it will be hard to compose a set of working gems, since a lot of gems change between versions.
In config/environment.rb you will also find the rails version.
Use rvm and a clean gemset. If the rails version is something 2.3.x, you could start using bundler.
Database
If you find a config/database.yml: then you know which type of database was expected. Adapt it to connect to your database (of the same type). If there is no such thing, start off with sqlite3, as it will cope with most simple cases (it will get you started).
The simplest way to get the database up and running is
rake db:create
rake db:setup
This will create an empty database (from your configuration), and then load the schema and the seeds.
Next steps
get the tests running (hopefully there are any)
consider upgrading to rails 3
Hope this helps.
First you'll need a config/database.yml file that defines how Rails connects to your database. If that's already in there, hopefully it's not in your versioning system :)
Second, you'll need to create the databases. You can do that with rake db:create.
Third, you will need to load the database schema. DO NOT do rake db:migrate as Dave mentioned in the comment above. This is not the recommend way to load the schema since it's slower and may not accurately represent the schema. Instead, use rake db:schema:load.

Resources