Rails Engines Extending Functionality

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'.

Extending controllers of a Rails 3 Engine in the main app

Why not just inherit from the Engine's controller class in your application (and point your routes at the new child controllers)? Sounds conceptually similar to the way that you extend built-in Devise controllers.

Rails engine extending views, not overriding

There is no way to extend views that way in Rails. You can however, accomplish this by using partials. In your engine, write a partial view file users/_show.html.erb and then render it in your app's view:

# app/views/users/show

# will look for a partial called "_show.html.erb" in the engine's and app's view paths.
render partial: 'show'

It's just as easy as your suggestion.

This gem tries to implement partial extension of views, but it works similarly to what I just described: https://github.com/amatsuda/motorhead#partially-extending-views-in-the-main-app

Ruby on Rails: Extend Engine's Functionality to Entire Application

To reference navigation in your engine from your main app

<%= link_to 'Link text', your_engine_name.path_name_from_the_engines_routes_path, title:"Rails engine" %>

In your example

<%= link_to thredded_current_user, thredded.user_path(thredded_current_user) %>

Use lowercase thredded in all references to the engine from the main app.

If your engine is mounted and configured properly this will work. I only mention this because I don't know what your config is, and rails engines are entirely convention dependent.



Related Topics



Leave a reply



Submit