Rails 3 - how do I avoid database altogether? - ruby-on-rails-3

I'm trying to use rails 3 without any db backend, but it still insists on requiring 'sqlite3' gem when I try to access a page, and throws an error no such file to load -- sqlite3, even though no code in the application requires sqlite, except I left database.yml with its default setting for sqlite3, since removing the content raised other errors. Any idea how I could use rails without any database and avoid said errors? thanks.
(also, I'm familiar with Sinatra - just prefer rails for this project).

Rails 3:
In application.rb, remove the require 'rails/all' line and instead add these lines:
require "action_controller/railtie"
require "action_mailer/railtie"
require "active_resource/railtie"
require "rails/test_unit/railtie"
require "sprockets/railtie"
Also see Remove ActiveRecord in Rails 3
and look into the Active Model railscast
Rails 3.2.x:
You'll also need to remove/comment out this line in application.rb
config.active_record.whitelist_attributes = true
And remove/comment these two lines from development.rb
config.active_record.mass_assignment_sanitizer = :strict
config.active_record.auto_explain_threshold_in_seconds = 0.5
Rails 2.x:
In config/environment.rb add (or uncomment) the line
config.frameworks -= [ :active_record, :active_resource, :action_mailer ]
This will cause Rails not to use those frameworks. (Note the nearly-invisible -= !)

Also, in Rails 3, remove any references to active_record in
config/environments/development.rb
config/environments/test.rb and
config/environments/production.rb such as
config.active_record.mass_assignment_sanitizer = :strict
config.active_record.auto_explain_threshold_in_seconds = 0.5
as well as removing require "rails/all" and adding the require lines in comment 21 (above).
if you are not using a database (this works with Rails 3.1.1)

Related

In Rails 5, setting config.active_record.schema_format = :sql but still getting schema.rb created on db:migrate

Working on a Rails 5 app, I want to use structure.sql instead of schema.rb (we're using PostGIS with lots of custom SQL calls...). In config/initializers/database_options.rb I have the following:
# use structure.sql, not schema.rb
Rails.application.config.active_record.schema_format = :sql
If I run the following:
$ rake db:migrate
it generates db/schema.rb, not db/structure.sql.
The rails guides say:
There are two ways to dump the schema. This is set in
config/application.rb by the config.active_record.schema_format
setting, which may be either :sql or :ruby.
What magic am I missing here?
I think you should put your rails component config before Initializers.
The rails application initialize by the following order.
config/application.rb
Environment-specific configuration files
Initializers
After-initializers
You could put your config config.active_record.schema_format = :sql either in config/application.rb or config/environments/development.rb depends on environment you used.
That should work.
In your initializer do:
Rails.application.configure do
config.active_record.schema_format = :sql
end

config.active_record.whitelist_attributes = false in Rails 4.2 App

Our rails 4.2 app consists of several rails engines with or without gem 'protected_attributes'. What we find out is that in app's application.rb, it has to be:
config.active_record.whitelist_attributes = false
Otherwise, any create/update can not be carried out because the params can not be assigned to instance variable. Our question is that, if there is no gem protected_attributes in rails app, do we still need config.active_record.whitelist_attributes = false in application.rb? Is this for Rails 3.x or app with gem protected_attributes?
I've looked through several rails 4 apps I have, none of them contained that config, and when I googled the config name, the protected_attributes gem came in the results, so I think you could assume that it's only related to the protected_attributes gem and that you don't need it
This comes from the protected_attributes gem.
When whitelist_attributes = true it causes attr_accessible(nil) to be added by default for all models meaning you ca not mass assign any attributes for any models unless it has its own attr_accessible or attr_protected
This can be seen in an older version of the README.md
As of version 1.1.2 this is now depreciated and does nothing other than log a depreciation warning
So if you are on a Rails 4+ application and do not have the protected_attributes gem then you can safely remove it

Gem with models

I created a Gem with models (actually, extracted it from the main project) to share amongst the projects we have in our platform.
We have dozens of models, so instead of requiring them one by one, I wrote the following code:
Gem.find_files("my_gem/models/*.rb").each { |path| require path }
I access one of the projects that has my_gem in the gem file and running rails c I get the following output:
/Users/myuser/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/dynamic_matchers.rb:50:in `method_missing': undefined method `has_attached_file' for #<Class:0x007fad4b93ccb8> (NoMethodError)
One of my models is using the gem paperclip, what is weird is:
If I remove the line declared above to load all the models
automatically, rails c runs fine
If I try to include onlye the model that uses paperclip require
"my_gem/models/paperclip_model" I receive the same error
So then I change my gem to not load any model, and when I try to reference any model from rails console, it says the class is not loaded, but then I run Gem.find_files("my_gem/models/*.rb").each { |path| require path } or require "my_gem/models/paperclip_model" 'they work perfectly and I am able to work with the model.
Has any of you seen the same issue?
Seems that changing require for autoload solved the problem
I changed Gem.find_files("my_gem/models/*.rb").each { |path| require path }
for
Gem.find_files("my_gem/models/*.rb").each do |f|
filename = File.basename(f, '.*')
class_name_symbol = filename.classify.to_sym
autoload class_name_symbol, "my_gem/models/#{filename}"
end
and now it is working.
It sounds like one of the models in your gem depends on Paperclip, but you don't explicitly set it as a dependency. So what's happening is that if your models get loaded before paperclip gets loaded, you'll see the UndefinedMethod error for has_attached_file.
If you use your models in a Rails application which has paperclip as a dependency, and you load those models after the console (or server) has spun up, Paperclip will be present, so you won't see this error.
The solution is to explicitly add paperclip as a dependency in your gemspec, something like:
s.add_dependency('paperclip')
Assuming that this gem will always be used in the context of a Rails application, this should work. If not, you might also need to add the following line to the top of your models that use paperclip:
require "paperclip"

Changing the en.yaml for another one in rails

I managed to get a fr.yaml. I want this to be the default, an old Rails 2.2 tutorial says to add:
config.i18n.default_locale = :fr
in environment.rb
It does not work, my rails server crashes on this config line.
What is the syntax for this setting in Rails 3? I do not want a multi-language app, I want to use only the french settings.
In Rails3, you add this line to application.rb
config.i18n.default_locale = :fr
This goes in config/application.rb in rails 3.

How do I make gemspec dependencies autoload in a Rails 3 app, using a Gemfile

I have a Rails 3 app that I am turning into a Rails engine / gem.
This engine has some gem dependencies that I have put inside it's .gemspec file.
I have created a new 'parent' Rails 3 app, and I would like to add my engine gem to the Gemfile and have the gem's dependencies automatically 'loaded', but this does not work for me! bundle install installs the gem dependencies fine, but when I start the server, the app crashes because they are not loaded.
For example, my engine's gemspec contains these lines:
s.add_runtime_dependency(%q<rails>, ["= 3.0.7"])
s.add_runtime_dependency(%q<acts_as_commentable>, [">= 3.0.1"])
s.add_runtime_dependency(%q<haml>, [">= 3.1.1"])
.. and the parent Rails 3 application has these lines in its Gemfile:
source 'http://rubygems.org'
gem 'my_engine', :path => "~/src/gems/my_engine"
But I get the following error:
undefined local variable or method `acts_as_commentable'
from /home/user/src/gems/my_engine/app/models/account.rb:66:in `<class:Account>'
But if I add gem 'acts_as_commentable', '>= 3.0.1' to the Gemfile of the parent Rails 3 app, then the gem is loaded and the error disappears.
I am using Rails 3.0.8.
Does anyone have any suggestions? Do I need to change something about the way my engine is loading?
During main Rails app boot, Bundler will only require dependencies directly listed in the Gemfile but not any sub-dependencies. It's your library's/Engine's responsibility to require its dependencies when it itself gets required. You can do so using initializers in your Railtie.
class MyRailtie < Rails::Railtie
initializer "require stuff" do
require "stuff"
end
end
In our Rails Engine we used a small trick to require dependencies automatically. Unfortunately you can't specify whether or not they should load in the .gemspec, which would allow for greater control.
Gem.loaded_specs["our_rails_engine"].dependencies.each do |d|
begin
require d.name
rescue LoadError => le
# Put exceptions here.
raise le if d.name !~ /factory_girl_rails/
end
end
I'm looking at Spree (the superhero of Rails Engines!), and they do this in spree_core-0.60.1/lib/spree_core.rb:
require "rails/all"
require 'state_machine'
require 'paperclip'
require 'stringex'
require 'will_paginate'
require 'nested_set'
require 'acts_as_list'
require 'resource_controller'
require 'active_merchant'
require "meta_search"
require "find_by_param"
So the answer is that within your gem, you have to require all of it's gem dependencies one by one. Well, that's how I will do it for now. But please comment if this ever changes in the future.
Seems it don't work, i create a host project and a sub-project with rails 3 engine.
Added the gem to engine's gemspec
s.add_dependency 'simple_form'
then added the require to engine_name.rb like below
require 'simple_form'
But if delete the line [gem 'simple_form'] in host project's Gemfile, it will show undefined immediatly