How can you load the Rails environment from CloudCrowd actions? - ruby-on-rails

I'm writing an "action" for CloudCrowd which needs access to the Rails environment (for some ActiveRecord stuff) but the standard means of loading the environment is resulting in fishy errors.
I tried each of the following at the top of my action .rb file:
require(File.join(File.dirname(__FILE__), '../..', 'boot'))
and
require File.expand_path(File.dirname(__FILE__) + "/../../environment")
When I try to start the node I get this error:
»crowd node
Starting CloudCrowd Node on port 9063...
Missing the Rails 2.3.2 gem. Please `gem install -v=2.3.2 rails`, update your RAILS_GEM_VERSION setting in config/environment.rb for the Rails version you do have installed, or comment out RAILS_GEM_VERSION to use the latest version installed.
And I of course do have the gem installed:
»gem list | grep -i rails
rails (2.3.4, 2.3.2, 2.2.2, 1.2.6)

Nice!
I've actually had some trouble with your RAILS_ROOT path and replaced '../../..' with '../..'. Also, since you've already declared the RAILS_ROOT constant, you could chop off a few things in the environment requirement. Here's my version:
RAILS_GEM_VERSION = nil
RAILS_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '../..'))
RAILS_ENV = ENV['RAILS_ENV'] = ENV['RACK_ENV']
if CloudCrowd.node?
require 'rubygems'
require 'activerecord'
ActiveRecord::Base.logger = Logger.new(STDOUT)
require "#{RAILS_ROOT}/config/environment"
# and if you need to import
# anything from lib just go ahead and
require 'my_custom_lib/name_of_file'
end

Somebody from #documentcloud saw my plea and helped me work through it. Had to prefix the action script with this:
RAILS_GEM_VERSION = nil
RAILS_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '../../..'))
RAILS_ENV = ENV['RAILS_ENV'] || 'development'
if CloudCrowd.node?
require 'rubygems'
require 'activerecord'
ActiveRecord::Base.logger = Logger.new(STDOUT)
require File.expand_path(File.join(File.dirname(__FILE__), '../..', 'environment'))
end

Related

Getting started with Nokogiri in Rails? No need for gem 'nokogiri'?

I'm just looking into Nokogiri and was thinking about using it in my app, but apparently when I do bundle install (without gem 'nokogiri') it's already "Using nokogiri 1.6.7.1".
When I add gem 'nokogiri' in my Gemfile, there's no "installing..." So, is nokogiri already pre installed in Rails? If so, do I still have to require these:
require 'nokogiri'
require 'open-uri'
Where do I put this? Within my controller? or application.rb?
This is my application.rb looks like
require File.expand_path('../boot', __FILE__)
require 'rails/all'
Bundler.require(*Rails.groups)
if Rails.env.test? || Rails.env.development?
CONFIG = YAML.load(File.read(File.expand_path('../aws.yml', __FILE__)))
CONFIG.merge! CONFIG.fetch(Rails.env, {})
CONFIG.symbolize_keys!
end
module App
class Application < Rails::Application
config.middleware.use Rack::Pjax
config.active_record.raise_in_transactional_callbacks = true
config.active_job.queue_adapter = :sidekiq
end
end
Nokogiri is required by another gem (rails-dom-testing). So it's already installed.
And you don't need to write require 'nokogiri' statement. Because Rails uses Bundler to manage dependencies and load gems. Nokogiri will be already loaded.

Minitest console output does not show colors

I installed ansicon to make ansi colorized console output possible for minitest test feedback on windows.
I am running minitest with the minitest-reporters gem to format the output, yet whatever I do I can't get the output to show colors (all text is black).
Here's my test_helper.rb file:
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require "minitest/reporters"
Minitest::Reporters.use! Minitest::Reporters::ProgressReporter.new( { :color => true } )
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all
# Add more helper methods to be used by all tests here...
end
Does anyone know this problem?
Reference: https://github.com/kern/minitest-reporters
I see the post is from 2015. 4 years passed since then.
Anyway, let me share how it worked for me yesterday.
My environment:
Ubuntu 18.04
rails 6.0.1
ruby 2.6.5
1) Gemfile:
gem 'minitest-reporters'
2) bundle install
3) test_helper.rb:
require "minitest/reporters"
Minitest::Reporters.use!
4) rake

NameError: uninitialized constant Rails with Rails.root in rails 3.2

I am getting following error while executing environment.rb
C:\RailsProject>jruby script/delayed_job start
NameError: uninitialized constant RAILS_ROOT
const_missing at org/jruby/RubyModule.java:2647
(root) at C:/RailsProject/config/environment.rb:20
require at org/jruby/RubyKernel.java:1062
(root) at script/delayed_job:3
environment.rb contains following code
# Be sure to restart your server when you modify this file
require "rubygems"
require 'memcache'
if ENV['LOCAL'] == 'Y'
require "bundler/setup"
end
# Uncomment below to force Rails into production mode when
# you don't control web/app server and can't set it the proper way
# ENV['Rails.env'] = 'production'
# Specifies gem version of Rails to use when vendor/rails is not present
RAILS_GEM_VERSION = '3.2.13' unless defined? RAILS_GEM_VERSION
ENABLE_FACEBOOK = false
# Bootstrap the Rails environment, frameworks, and default configuration
Dir["#{Rails.root}/lib/integration/*.rb"].each do | file|
require file
end
# Initialize the rails application
RailsProject::Application.initialize!
I am using rails 3.2.13 version.
Could you please help me on this?

Including gems in a rake file

I'm trying to write a rakefile outside of a rails project.
I've created a new directory, added a rakefile and set a basic default task. It works.
I want to use the premailer gem so in my default task I've added this -
premailer = Premailer.new('http://localhost/email.html', :warn_level => Premailer::Warnings::SAFE)
This doesn't work, I get the following error.
uninitialized constant Object::Premailer
How do I refer to the premailer gem in my task? Should I be including it in a gemfile of sorts?
You can work without a Gemfile like this:
require 'rubygems' # only needed for ruby 1.8.7
require 'premailer'
desc "My Task"
task :my_task do
..
premailer = Premailer.new(...)
...
end
or with a Gemfile:
require 'rubygems' # only needed for ruby 1.8.7
require 'bundler'
Bundler.setup
Bundler.require
desc "My Task"
task :my_task do
..
premailer = Premailer.new(...)
...
end
I hope this helps.

How to configure Ruby on Rails with no database?

It would be convenient to use Ruby on Rails for a small website project that has no current need for a database. I know I could create an empty database in MySQL and go from there, but does anyone know a better way to run Rails without a database?
Thanks
For Rails 3 and Rails 4:
Use -O(Capital 'O') or --skip-activerecord option to generate an application without a database.
rails new myApp -O
or
rails new myApp --skip-activerecord
This Answer is reshared from here
For Rails 5:
Use --skip-active-record option to generate an application without a database
Notice the extra hyphen '-' as opposed to previous Rails versions.
rails new myApp --skip-active-record
For an existing Rails 4-7 project, in your config/application.rb file you have the following line:
require 'rails/all' # or `require "rails"' in newer versions
(As reference that line is loading this file)
So instead of load ALL, you must to load each library separately as follows:
# active_record is what we're not going to use it, so comment it "just in case"
# require "active_record/railtie"
# This is not loaded in rails/all but inside active_record so add it if
# you want your models work as expected
require "active_model/railtie"
# And now the rest
require "action_controller/railtie"
require "action_mailer/railtie"
require "action_view/railtie"
require "active_job/railtie" # Only for Rails >= 4.2
require "action_cable/engine" # Only for Rails >= 5.0
require "sprockets/railtie" # Deprecated for Rails >= 7, so add it only if you're using it
require "rails/test_unit/railtie"
# All these depend on active_record, so they should be excluded also
# require "action_text/engine" # Only for Rails >= 6.0
# require "action_mailbox/engine" # Only for Rails >= 6.0
# require "active_storage/engine" # Only for Rails >= 5.2
Keep an eye to the comments to know what to load regarding your Rails version.
Also check the following files (in case you have them) and comment the following lines:
# package.json
"#rails/activestorage": "^6.0.0",
# app/javascript/packs/application.js
require("#rails/activestorage").start()
# bin/setup
system! 'bin/rails db:prepare'
# config/environments/development.rb
config.active_storage.service = :local # For Rails >= 5.2
config.active_record.migration_error = :page_load
config.active_record.verbose_query_logs = true
# config/environments/test.rb
config.active_storage.service = :test # For Rails >= 5.2
# config/environments/production.rb
config.active_storage.service = :local # For Rails >= 5.2
config.active_record.dump_schema_after_migration = false
# spec/rails_helper.rb
ActiveRecord::Migration.maintain_test_schema!
# test/test_helper.rb
fixtures :all # In case you're using fixtures
# Only for Rails >= 5.0
#config/initializers/new_framework_defaults.rb
Rails.application.config.active_record.belongs_to_required_by_default = true
Also remove any reference to ActiveRecord::Base in your model files (or simply delete the files if apply). For example, the autogenerated app/models/application_record.rb file.
Uncomment this line in the environment.rb file:
config.frameworks -= [ :active_record, :active_resource, :action_mailer]
In Rails 4 when starting a new project you can use -O or --skip-active-record
rails new my_project -O
rails new my_project --skip-active-record
If you've already created a project you will need to comment
require "active_record/railtie"
from config/application.rb and
config.active_record.migration_error = :page_load
from config/environments/development.rb
If you don't need a database then you probably don't need to have the bulk of Rails. You may want a smaller more customizable framework to work with.
Sinatra is a tiny framework that is great for serving up basic static pages.
But if you insist on using Rails here is an article that will show you how to do just that or here.
For support Rails 6 rc1 and activerecord-nulldb-adaptergem we need a monkey patching
In config/initializers/null_db_adapter_monkey_patches.rb
module ActiveRecord
module ConnectionAdapters
class NullDBAdapter < ActiveRecord::ConnectionAdapters::AbstractAdapter
def new_table_definition(table_name = nil, is_temporary = nil)
TableDefinition.new(table_name, is_temporary)
end
end
end
end

Resources