How to run "rails new.." from inside an engine - ruby-on-rails

I have a rails engine that exposes a command called myengine create. That command generates a full Rails application with myengine already integrated into it. The command calls rails new ... to generate the app. Everything works fine as long as I execute this command outside of the engine folder. However, if I execute it inside the engine folder, it breaks because it runs the bin/rails command inside the engine, instead of the regular rails command that is installed on the system. The bin/rails inside the engine folder is configured to work with engines so it doesn't have a rails new command.
I'm trying to write automated tests that run the myengine create command from within the engine to validate the output. If I'm anywhere inside the engine directory though, calling rails new fails.
I've tried specifying the exact rails command like "/home/me/.rvm/gems/ruby-2.3.1/bin/rails new ...", but this doesn't work.
The only workaround I've been able to come up with is to build the application outside of the engine folder by using Dir.chdir or Thor's inside method. I can test against it there, or even move the newly generated app folder back into my spec/ folder. It works, but it feels sloppy. There must be some way to generate a new from inside an engine.

If anyone else runs into this, I ended up enhancing my command to rename bin/rails to a temporary new name before it tried to run rails new. Then the command renamed it back once rails new completed.

Related

Directory for custom shell scripts within rails application

In my Rails (Release 6) web application I'll use a bash script to do some action. The app will call the script in a controller with a fragment inside backticks.
def action
...
`#{custom_script_dir}/my_script.sh`
...
render ...
end
Which directory I should place my script into? Is there a standard place for such custom extra shell scripts?
There isn't a strong convention for this.
Maybe you can use the bin folder. Look at this answer as an example:
What's inside my rails ./bin directory?
...
I personally do put some stuffs[sic] in the bin folder. Scripts to connect
to remote servers, or ruby scripts I need but that I don't want to run
as rake tasks since they're more general than my application.
...
However, it is up to you to decide which folder is the best for you. Also, feel free to create as many folders as you want in your Rails application if it makes sense for your project.

Can I use gitlab-rails to create a new rails app?

I have installed gitlab with the omnibus package on a Ubuntu-based machine, on which I'm trying to develop a rails app as well. Since gitlab installs ruby and the rails framework for itself, could I be able to use it to create new rails applications? When I run gitlab-rails new app_name under my home directory I get an error message that reads:
/usr/bin/gitlab-rails error: could not load /opt/gitlab/etc/gitlab-rails/gitlab-rails-rc
Either you are not allowed to read the file, or it does not exist yet.
You can generate it with: sudo gitlab-ctl reconfigure
And when I run sudo gitlab-rails new app_name I get:
Can't initialize a new Rails application within the director of another, please change to a non-Rails directory first.
Type 'rails' for help
I've tried to run this under newly created, empty directories, but keep getting the same message. I assume gitlab-rails tries to create the new app under the gitlab project directory for some reason?
As far as I know, this isn't the purpose of the gitlab-rails command and isn't a supported functionality. It'd be a much better idea to create a new Rails app by just using the Rails gem itself.

cannot run rails server command for already created project

I have got an application which was zipped and I unzipped the files and it gave me a folder with all the required structure of a rails application. But when I am going inside the directory and running the rails server command, its not doing anything, but showing me the list of options rails command can do. What would have been the problem?
thanks
Maybe this will help from app folder try:
ruby script/rails server
As explained by Dave Isaacs, Showing the list of options is what the rails command does when it is executed outside the context of an application (i.e., not in the application directories).
Type the full command, including the /script path to the CLI.
script/rails server
instead of
rails server
where script is the path to the rails command that, in a Rails 3 application, lives in the script folder.
I would assume that the application you got was written with rails 2.x. In that case, you have to start it with
script/server

Rails 3 Create Script to Manipulate My DB

I'm trying to create a script to mess around with the db entries in my rails application, but I don't know how to properly set it up to gain access to all my models etc.
I can do this easily with scripts like seeds.rb using 'rake db:seed' to execute or in my application controllers, but I want to create scripts outside of these that I can run in the background or just once.
Do I need to include something, or call the script with a certain rails command? And as a second related question, is there any way for me to execute rails commands like 'rake db:seed' from within a ruby script? The only method I know of right now that works is running 'rails console' and executing commands there.
require 'config/environment.rb'
When the script is in your rails root...

Daemon Start at the application bootup

I have a daemon that should run behind my rails app doing db modifications.I implemented that daemon using ruby daemons gem. I want to start that daemon at the start of my app. Whenever my app starts, I need to start that daemon.
How can I do this..?
If you must start it during Rails initialization:
Create a ruby file that will start the daemon. Say invoke_daemon.rb
Put this file in config/initializers/invoke_daemon.rb
However if it isn't mandatory, I would suggest creating a binary executable or a rake task and manually starting it through command line. This way it runs as a separate process. You can simply add it to your deployment scripts for production boxes and on development box run it manually. A few examples would be searchd, the search daemon for sphinx and thinking_sphinx:delayed_delta rake task from thinking_sphinx.
For your knowledge you have to take look of
Rails Life cycle
I have just implemented this thing. I have implemented on Windows7.
I have created one batch file let's say my_batch.bat, which contains ruby command i.e. ruby my_daemon.rb file.
In addition, to execute this file when my app starts , I have just added one statement in environment.rb file which executes that batch file. i.e. system ("my_batch.bat").
But I am not sure that this is a good way to implement this task.

Resources