Listing 'rake routes' for a mountable Rails 3.1 engine - ruby-on-rails

I'm working on a mountable engine for use with Rails 3.1, and I want to list the engine's routes.
I created the engine using:
$ rails plugin new rails_blog_engine --mountable
And edited the 'test/dummy/config/routes' file to read:
Rails.application.routes.draw do
mount RailsBlogEngine::Engine => "/blog"
end
...and 'config/routes' to read:
RailsBlogEngine::Engine.routes.draw do
resources :posts
end
I want to list the routes generated for ':posts', but it's not clear how I can do this. When I run 'rake app:routes', I get only the "/blog" route:
$ rake app:routes
rails_blog_engine /blog {:to=>RailsBlogEngine::Engine}
When I run 'rake routes', I get an error:
$ rake routes
rake aborted!
Don't know how to build task 'routes'
How can I see the routes for ':posts'? Can I do this without rewriting the relevant rake tasks?

In case people are missing it in the comments, as of Rails 3.2.2, you can now use
$ rake app:routes

If you copy code from the standard Rails 3.1.0 rake routes task into your Rakefile, and change the top part to read:
task :routes => 'app:environment' do
Rails.application.reload_routes!
all_routes = RailsBlogEngine::Engine.routes.routes
...replacing RailsBlogEngine with the name of your engine, then you can get a rudimentary list of routes by running:
rake routes
Note that in Rails 3.1.1 and later, you'll need a newer version of the rake routes task.

For rails 3.x engine, rake routes does not work under engine's root (that's why it needs some hack by copying rake file). However rake routes works under test/dummy (or spec/dummy if using rspec). It will list all the pathes which belong to the engine in development and other engines mounted.

For rails 3
desc 'Print out all defined routes inside engine match order, with names. Target specific controller with CONTROLLER=x.'
task engine_routes: :environment do
Rails.application.reload_routes!
app = ENV['ENGINE'] || "Rails.application"
all_routes = app.constantize.routes.routes
require 'rails/application/route_inspector'
inspector = Rails::Application::RouteInspector.new
puts inspector.format(all_routes, ENV['CONTROLLER']).join "\n"
end
Rails 4
desc 'Print out all defined routes inside engine match order, with names. Target specific controller with CONTROLLER=x.'
task engine_routes: :environment do
app = ENV['ENGINE'] || "Rails.application"
all_routes = app.constantize.routes.routes
require 'action_dispatch/routing/inspector'
inspector = ActionDispatch::Routing::RoutesInspector.new(all_routes)
puts inspector.format(ActionDispatch::Routing::ConsoleFormatter.new, ENV['CONTROLLER'])
end
Then you can call like
$rake engine_routes ENGINE="IssueTracker::Engine"

in Rails 5, I could get the routes of the engine using the following command:
bundle exec rake app:routes

In rails 3.2X
If you are in you "host_app" and have mounted a engine you could probably list the routes by executing (should work out of the box):
bundle exec rake engine_name:routes

Related

How to fix "uninitialized constant" in a Rake task

I have a problem when I do:
namespace :xaaron do
task :get_roles do
roles = Xaaron::Role.all
puts roles
end
task :get_role, [:name] do |t, args|
role = Xaaron::Role.find(args[:name].parameterize)
puts role
end
end
The first task will work fine. I can even add binding.pry and run Xaaron::Role and get information about Roles back. But the second task fails with:
NameError: uninitialized constant Xaaron::Role
I run each task in my main app because these tasks are inside an engine, using:
bin/rake xaaron:get_roles` and `bin/rake xaaron:get_role
I can run bin/rails c in the main application that uses the engine and run Xaaron::Role and get information about Roles table.
Why is the second one failing but the first one is not? Is there scoping with arguments?
I'm not sure why either works, but if this is Rails and those are Rails models, your tasks should depend on the environment:
task :get_roles => [ :environment ] do
By depending on the :environment task, it first loads Rails.
Also see: What's the 'environment' task in Rake?.
You can also run a Rake task as
bundle exec rake environment xaaron:get_role
This will load the Rails environment first.
I kept getting uninitialized constant errors for a Rake task, even after depending on :environment and running with bundle exec.
The issue was that I was making a Rake::TestTask and, even though the Rake task had access to all constants, the test files themselves did not have access to constants.
The solution was to add this line to the top of my test file:
require_relative '../config/environment'
This is the Rake task:
require "rake/testtask"
Rake::TestTask.new(:test) do |t|
t.libs << "test"
t.libs << "lib"
t.test_files = FileList["test/**/test_*.rb"]
end
To add, as of Ruby 1.9 and above, you can use this hash syntax:
namespace :xaaron do
desc "Rake task to get roles"
task get_roles: :environment do
roles = Xaaron::Role.all
puts roles
end
#####
end
And then you can run the command below to run the Rake task:
rake xaaron:get_roles
or
bundle exec rake xaaron:get_roles

Rails 4 not showing routes in 'rake routes' but views are working

Pretty much as the title states, I have the following in my routes file:
root to: 'assets#index'
resources :assets do
member do
get :download
end
end
Yet my output for rake routes and visiting rails/info/routes are both simply:
Prefix Verb URI Pattern Controller#Action
root GET / assets#index
However the routes work fine in my views.
I also tried with bundle exec and I've updated to the latest version of bundle as some other posts suggested. It still works for my Rails 3 apps.
:assets is a reserved path in Rails. So you cannot really use it.

Ruby Script to create new model value

Project is the model name and I want to do something like:
Project.create(:name => 'projectname', :identifier => 'projectidentifier')
This should be done in the terminal through a ruby script. I am not going to use rails console to create it nor use seeds.rb in a db file to migrate this as rake db:seed.
Can someone help. Thanks
The easiest way would be using rails runner (which essentially loads rails):
rails runner your_script.rb
The line of code would be a content of that script.
What about a rake task?
on lib/tasks, create a file named data.rake, and the content:
namespace :data
desc "Create project data"
task create_project_data: :environment do
Project.create(name: 'projectname', identifier: 'projectidentifier')
end
end
And you can run it as any rake task
rake data:create_project_data
And it will also appear when you list your rake tasks
rake -T

HttpHelpers routing methods in mounted rails engine results in uninitialized constant "controller name"

My environment:
Rails 3.2.8
Ruby 1.9.3p194
Fedora 16 x86_64
This problem seems specific to Rails Engines.
It seems that when using the HttpHelpers in a Rails Engine's routes file, I get "uninitialized constant Controller" when accessing a route via a browser. But, if I use a URL matcher in the Engine's routes file, it routes correctly.
Here's how I created a failing example:
$ rails plugin new my_engine --mountable
$ cd my_engine
$ rails g controller things index
$ rails s -p 3005
The controller generator uses the HttpHelpers#get method by default, so at this point the Rails Engine's config/routes.rb file looks like:
MyEngine::Engine.routes.draw do
get "things/index"
end
And, the test/dummy application's config/routes.rb file looks like:
Rails.application.routes.draw do
mount MyEngine::Engine => "/my_engine"
end
So, I should be able to hit http://locahost:3005/my_engine/things/index and see the Things#index view from the Engine. But, instead in the browser I see:
Routing Error
uninitialized constant ThingsController
If I manually change the Engine's config/routes.rb file to:
MyEngine::Engine.routes.draw do
#get "things/index"
match "things/index" => "things#index"
end
... and hit http://locahost:3005/my_engine/things/index, I see the correct Things#index view.
I noticed that when I use the HttpHelpers#get method in the Engine's config/routes.rb file, and run rake routes from the test/dummy directory, I see:
$ rake routes
my_engine /my_engine MyEngine::Engine
Routes for MyEngine::Engine:
things_index GET /things/index(.:format) things#index
But, if I change the Engine's config/routes.rb file to use the URL matcher method, I see:
$ rake routes
my_engine /my_engine MyEngine::Engine
Routes for MyEngine::Engine:
things_index /things/index(.:format) my_engine/things#index
Notice that when using the URL matcher, the controller and action are correctly namespaced under the engine. While, when using the HttpHelpers#get, the controller and action seem to be non-namespaced.
So, my question: Am I doing something wrong here? Or, is this a bug?
Note: I searched the rails issues and didn't see anything directly related to this. Though I did see several other engine and routing issues.

How do I pass parameter to a rake task that is invoked using Rake::Task

Here is my rake task
task :lab => :enviroment do
Rake::Task["db:rollback"].invoke('STEP=5')
end
It is not doing what I want. What I want is
rake db:rollback STEP=5
I am using Rails 3.2.1 on ruby 1.9.2.
On the command line I want to execute
rake lab
The real case is much more complicated but this is the jist.
task :lab => :enviroment do
ENV['STEP'] ||= 5
Rake::Task["db:rollback"].invoke
end
Options can be passed into rake by specifying key/value pairs on the rake command:
rake options:show opt1=value1
These command line options are then automatically set as environment variables which can be accessed within your rake task:
namespace :options do
desc "Show how to read in command line options"
task :show do
p "option1 is #{ENV['opt1']}"
end
end
Passing this as an environment variable might be your best bet. Try:
task :lab => :enviroment do
Rake::Task["db:rollback"].invoke(ENV['STEP'])
end
rake db:rollback STEP=5

Resources