Rails Gem with model-relationships for Rails Engines
Ok,
so I've found a solution for that.
You can just monkey-patch your Engine with Decorators (you need to put it into config/initializers/initializer_name.rb)
see: Extending a ruby gem in Rails
Don't know if this is a good thing, but it works like a charm...
The downside is that you have to restart the server every single time you make a change to the monkey-patching...
EDIT: It seems that this monkey-patch will be garbage collected after a few requests.
EDIT 2:
This post helped me out
How to monkey-patch code that gets auto-loaded in Rails?
you need to add thin sin your Intializer to force rails to reload your patch for every request
Rails.configuration.to_prepare do
Rails' with Activesupport 3.2.0
Looks like you are running on Ruby 1.8.7, but the code you have written is only valid in Ruby 1.9.
If possible, I would recommend upgrading up to 1.9.3; that should fix the issue.
If you cannot use Ruby 1.9.3, just change the line from
key: '_blog_session'
to
:key=>'_blog_session'
How to mount Rails Engines automatically/dynamically?
module MyEngine
class Engine < ::Rails::Engine
isolate_namespace MyEngine
config.my_engine = ActiveSupport::OrderedOptions.new
initializer 'my_engine.configuration' do |app|
if app.config.my_engine[:mounted_path]
app.routes.append do
mount MyEngine::Engine => app.config.my_engine[:mounted_path]
end
end
end
end
end
And then in the main application in config/application.rb
, you can set
config.my_engine.mounted_path = "/some_path_here"
Decorate a Rails engine's helper to include a concern from the main app
So I finally found a way to do this in a "clean" way, as mentioned in the following comment : https://groups.google.com/g/rubyonrails-core/c/PaABJDXnxyo/m/k-QUJEi9a9wJ
The idea is to add an empty placeholder helper in the engine, for example :
module MyEngine
module ExtendableHelper
end
end
And then override it in the main app, by adding methods or including concerns, other gems' helpers, etc...
module MyEngine
module ExtendableHelper
extend OtherGemFromMainApp::UsefulHelper
include MainAppConcern
def other_useful_method
...
end
end
end
Thus the helper used by the engine is the one provided by the app, and the UsefulHelper methods can be called in the engine views.
Rails engines extending functionality
Just if anyone else runs into same issue some time in the future, this is the code I wrote that fixed my problem:
module ActiveSupport::Dependencies
alias_method :require_or_load_without_multiple, :require_or_load
def require_or_load(file_name, const_path = nil)
if file_name.starts_with?(RAILS_ROOT + '/app')
relative_name = file_name.gsub(RAILS_ROOT, '')
@engine_paths ||= Rails::Initializer.new(Rails.configuration).plugin_loader.engines.collect {|plugin| plugin.directory }
@engine_paths.each do |path|
engine_file = File.join(path, relative_name)
require_or_load_without_multiple(engine_file, const_path) if File.file?(engine_file)
end
end
require_or_load_without_multiple(file_name, const_path)
end
end
This will automatically require files from engine before requiring from application if file path starts with 'app'.
Related Topics
Setting Mime Type for .Ogv Files in Rails Development Environment
Suppresing Output to Console with Ruby
How to Loop Through Months in Ruby on Rails
What Are Symbols and How Do We Use Them
Keyword for Exclusive or in Ruby
How to Quickly Reorder a Ruby Array Given an Order
Heroku: How to Push to Specific App If You Have Multiple Apps in Heroku
How to Tell Which Openssl Lib Is Actually Being Used by an Rvm-Installed Ruby
Ruby Daemons and Jruby - Alternative Options
Case Expression Different in Ruby 1.9
In Ruby or Rails, Why Is "Include" Sometimes Inside the Class and Sometimes Outside the Class
Spacing Around Parentheses in Ruby
Get, or Calculate the Entropy of an Image with Ruby and Imagemagick
Getting Current Line of Code in Ruby
Using S3 Presigned-Url for Upload a File That Will Then Have Public-Read Access