Create Rails project structure without installing full Rails stack - ruby-on-rails

I use rvm for development. In order to create a new Rails project directory initially, I will install the full Rails stack of gems in my default gemset so I can run "rails new". I then will create the ".ruby-version" and ".ruby-gemset" files in the new project directory so rvm can automatically switch me the proper ruby version and gemset when navigating into the project. I then have to install the full Rails stack again in the project's gemset. So I never really end up using my Rails stack installed in my default gemset (except to run "rails new").
So is there a subset of the Rails gems which I can install just to run the equivalent of "rails new" to generate the new project? Is there something else I can use to do the same, but is technically outside of Rails?

I don't know how to specify when creating the rails project which portions of the Rails stack to include/exclude, but once it's installed you can choose which portions to not import into your application by modifying your application.rb file.
Your application.rb file contains the following code:
# Pick the frameworks you want:
require "active_model/railtie"
require "active_job/railtie"
require "active_record/railtie"
require "action_controller/railtie"
require "action_mailer/railtie"
require "action_view/railtie"
require "action_cable/engine"
require "sprockets/railtie"
require "dotenv-rails"
Simply comment out the frameworks you don't want to include in your running application and they won't be imported.
Not sure if this is what you're looking for or not, hope it helps though.

Related

Convert Rails 6 generator to make Minitest test files after being an RSpec project

Summary
I am converting a Rails project from RSPec to Minitest and want my Rails project generator to create minitest test files.
In detail
I have a Rails project which I generated with skipped tests:
rails new example --skip-test-unit
I then added RSpec (following the standard rails generate rspec:install). Then I thought I'd like to try minitest instead.
I made a project doppelganger and copied test directory into my rails project:
rails new example # so we have test unit now
While I can run minitest tests the generator does not make the files for me.
rails g model Contact --skip-migration
Running via Spring preloader in process 13814
invoke active_record
create app/models/contact.rb
# no test files!
I have tried:
Reading through stackoverflow - everyone is converting it into RSpec
Stop and started Spring
commenting out a line in application.rb config.generators.system_tests = nil
How can I fix this to get minitest files generated?
I diffed a new application with my current application and application.rb's requires were very different.
App not generating test files
# rails/config/application.rb
require "rails"
# Pick the frameworks you want:
require "active_model/railtie"
require "active_job/railtie"
require "active_record/railtie"
require "active_storage/engine"
require "action_controller/railtie"
require "action_mailer/railtie"
require "action_mailbox/engine"
require "action_text/engine"
require "action_view/railtie"
require "action_cable/engine"
require "sprockets/railtie"
# require "rails/test_unit/railtie" <= generator left test_unit commented out
App Generating test files
# rails/config/application.rb
require "rails/all"
I made the require statements in my non-generating app like the generating app. ran spring stop and ran rails generator which now worked.
rails g model Contact --skip-migration
Running via Spring preloader in process 16286
invoke active_record
create app/models/contact.rb
invoke test_unit
create test/models/contact_test.rb
invoke factory_bot
create test/factories/contacts.rb

Rails: Migrating database From Postgres to MongoDB

My project was created using PostgresSQL but we really intend to use MongoDB, what should I change in order for that to happen?
A lot of scaffolding already took place, so we are trying to savage any of the work already done...
Remove database adapter gems from your Gemfile (mysql2, sqlite3, etc.)
Change your config/application.rb
Remove require 'rails/all' line and require frameworks you want to use, for example:
require "action_controller/railtie"
require "action_view/railtie"
require "action_mailer/railtie"
require "active_job/railtie"
require "action_cable/engine"
require "sprockets/railtie"
require "rails/test_unit/railtie"
NOTE: You should be using the require snippet from the rails/all.rb file that is current with your version of Rails without the active_record railtie. Here is a link to rails/all.rb on the Rails master branch.
Delete your config/database.yml file, db/schema.rb and migrations (if any)
Delete migration check in test/test_helper.rb
Delete any ActiveRecord configuration from your config/environments files
Better late than never! This should come in handy to somebody, someday!!!!
The following python-based migration framework does the job.
https://github.com/datawrangl3r/pg2mongo
Coming to the performance, the migration of each JSON object is dynamic and there shouldn't be any memory lock issues when you use the above framework.
The same situation I also I have to face . I am adding some additional points to above answer .
1) Create a file in initializers and put this code
Mongoid.load!(Rails.root.join("config/mongoid.yml"))
2) You have to remove from every model which are inheriting from ApplicationRecord and delete the application_record file.
3) If you have installed devise so you have to change
**From **
require 'devise/orm/active_record'
to
require 'devise/orm/mongoid'
4) If you are using carrierwave so in gem file you have to replace
From
gem 'carrierwave', github: 'carrierwaveuploader/carrierwave'
To
gem 'carrierwave-mongoid', :require => 'carrierwave/mongoid'

Cannot create Rails3 inside Rails2 in order to upgrade

I'm having a problem here. I'm trying to update my application from rails v2.3.11 to v3.0.20 and then move up from there. I've followed Ryan Bate's and other tutorials to do this however, when trying to create the rails3 app on top of the rails2 one, I get the following:
Tomas-MacBook-Pro:yfl_curr tomas$ rails new . --force
Can't initialize a new Rails application within the directory of another, please change to a non-Rails directory first.
Type 'rails' for help.
Environment:
Tomas-MacBook-Pro:yfl_curr tomas$ ruby -v
ruby 1.9.2p320 (2012-04-20 revision 35421) [x86_64-darwin12.4.0]
Tomas-MacBook-Pro:yfl_curr tomas$ rails -v
Rails 3.0.20
I had to change the Rakefile and convert it to rails3 as it will not work if using v2:
# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
require File.expand_path('../config/application', __FILE__)
require 'rake'
SampleApp::Application.load_tasks
Also boot.rb was changed to reflect this:
require 'rubygems'
# Set up gems listed in the Gemfile.
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE'])
I'm using bundler by the way. If I run the "rake rails:upgrade:check" it works fine.
What am I doing wrong?
Thanks guys for the help!!
Regards,
Tom

Rails Engine with Mongoid

I'm creating standalone rails engine application which will be further added to main application, using mongoid. So, I started like this.
rails plugin new some_engine --mountable --skip-active-record
Then in the gem file:
gem 'mongoid', "~> 3.0.15"
Then I run this command for generating mongo config file:
some_engine\test\dummy>rails g mongoid:config
which generate mongoid.yml under test/dummy/config folder
Now if I generate some model it's still invoking active record
some_engine>rails generate scaffold post title:string
it gives output:
invoke active_record
create db/migrate/20121219170013_create_some_engine_posts.rb
...........
Then I change the rails file under script/rails folder as it's having:
require 'rails/all'
to
require "action_controller/railtie"
require "action_mailer/railtie"
require "active_resource/railtie"
require "sprockets/railtie"
require "rails/test_unit/railtie"
Then if I generate something it still not invokes mongoid
some_engine>rails generate model post
Nothing happens here.
Also when I run
some_engine>rails generate scaffold post title:string
invoke resource_route.....
Is that I'm missing something here to use mongoid with Rails Engine?
It sounds like mongoid still isn't loaded. I'm sorry, but I don't have time to reproduce. What happens if you explicitly require mongoid in the engine.rb file?
In Rails 4 if we do not need active_resource.The generated plugin app has rails modules loaded in bin/rails.
require "action_controller/railtie"
require "action_mailer/railtie"
require "sprockets/railtie"
require "rails/test_unit/railtie"
require "mongoid"
require 'rails/engine/commands'

Remove ActiveRecord in Rails 3

Now that Rails 3 beta is out, I thought I'd have a look at rewriting an app I have just started work on in Rails 3 beta, both to get a feel for it and get a bit of a head-start. The app uses MongoDB and MongoMapper for all of its models and therefore has no need for ActiveRecord. In the previous version, I am unloading activerecord in the following way:
config.frameworks -= [ :active_record ] # inside environment.rb
In the latest version this does not work - it just throws an error:
/Library/Ruby/Gems/1.8/gems/railties-3.0.0.beta/lib/rails/configuration.rb:126:in
`frameworks': config.frameworks in no longer supported. See the generated
config/boot.rb for steps on how to limit the frameworks that will be loaded
(RuntimeError)
from *snip*
Of course, I have looked at the boot.rb as it suggested, but as far as I can see, there is no clue here as to how I might go about unloading AR. The reason I need to do this is because not only is it silly to be loading something I don't want, but it is complaining about its inability to make a DB connection even when I try to run a generator for a controller. This is because I've wiped database.yml and replaced it with connection details for MongoDB in order to use this gist for using database.yml for MongoDB connection details. Not sure why it needs to be able to initiate a DB connection at all just to generate a controller anyway....
Is anyone aware of the correct Rails 3 way of doing this?
I'm going by this from reading the source, so let me know if it actually worked. :)
The rails command that generates the application template now has an option -O, which tells it to skip ActiveRecord.
If you don't feel like rerunning rails, you should check the following in your existing app:
Check that your config/application.rb doesn't have require 'rails/all' or require "active_record/railtie". Instead, for a standard Rails setup without ActiveRecord, it should have only the following requires:
require File.expand_path('../boot', __FILE__)
require "action_controller/railtie"
require "action_mailer/railtie"
require "active_resource/railtie"
require "rails/test_unit/railtie"
require "sprockets/railtie"
# Auto-require default libraries and those for the current Rails environment.
Bundler.require :default, Rails.env
If, in config/application.rb, you are using the config.generators section, make sure it doesn't have the line g.orm :active_record. You can set this explicitly to nil, if you want, but this should be the default when g.orm is completely omitted.
Optional, but in your Gemfile, remove the gem line that loads the module for your database. This could be the line gem "mysql" for example.
Rails 4
I was looking for how to disable it in rails 4 and only found this answer which no longer works in rails 4. So this is how you can do it in rails 4 (tested in RC1).
In a new project
rails new YourProject --skip-active-record
In an existing project
In your Gemfile, remove the database driver gem, e.g. gem 'sqlite3' or gem 'pg'.
In config/application.rb, replace require 'rails/all' with
require "action_controller/railtie"
require "action_mailer/railtie"
require "sprockets/railtie"
require "rails/test_unit/railtie"
In config/environments/development.rb, remove or comment out config.active_record.migration_error = :page_load
Potentially you have to remove active_record helpers from the spec_helper (via VenoM in the comments)
Potentially you have to remove the ConnectionManagement middleware (seems to be the case with unicorn): config.app_middleware.delete "ActiveRecord::ConnectionAdapters::ConnectionManagement" (via https://stackoverflow.com/a/18087332/764342)
I hope this helps others looking for how to disable ActiveRecord in Rails 4.
For a new rails app, you can have it exclude active record by specifying the --skip-active-record parameter. Eg:
rails new appname --skip-active-record
If you generated a new project using Rails 3.2, you will also need to comment out:
config.active_record.mass_assignment_sanitizer = :strict
and
config.active_record.auto_explain_threshold_in_seconds = 0.5
in your development.rb file.
All of the above are true. The one more thing which I had to do in rails 3.1 is to comment out
config.active_record.identity_map = true
in config/application.rb.
If you're running rspec, you also need to remove (in spec_helper):
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"
and remove
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = true
Also comment out
# config/application.rb
config.active_record.whitelist_attributes = true
(noted on rails 3.2.13)

Resources