How to Call a Parent App's Helper Method from a Rails 3.1 Engine

How to call a parent app's helper method from a rails 3.1 engine

Something like this should work, as long as you have only one cornerstone user defined in your application :

module Cornerstone
module ActsAsCornerstoneUser
extend ActiveSupport::Concern

module ClassMethods
def acts_as_cornerstone_user(options = {})

#= Associations
has_many :cornerstone_discussions

#= Options
Cornerstone::Config.auth_with = options[:auth_with] if options[:auth_with]
end
end

module InstanceMethods

end

def self.included(base)
base.extend(ClassMethods)
base.include(InstanceMethods)
end
end

ActiveRecord::Base.send :include, ActsAsCornerstoneUser
end

Then define a helper in your gem (ie. in app/helpers/cornerstone_helper.rb) :

module Cornerstone
module CornerStoneHelper
def current_cornerstone_user
Config.auth_with.call(controller)
end
end
end

The acts_as_cornerstone method is the used like this :

class MyUser < ActiveRecord::Base
acts_as_cornerstone_user :auth_with => Proc.new { |controller| controller.current_user }
end

You can then use the current_cornerstone_user helper to get the current authenticated user.

This method breaks when acts_as_cornerstone_user is used on multiple classes. But you then have the problem of having multiple cornerstone users without knowing anything about the application models (you're supposed to be in your gem).

Update

If you'd like to have a syntax like :auth_with => :warden, you could replace the helper with the following :

module Cornerstone
module CornerStoneHelper
def current_cornerstone_user
if Config.auth_with.respond_to?(:call)
Config.auth_with.call(controller)
elsif Config::AUTH_MODES.keys.include?(Config.auth_with)
Config::AUTH_MODES[Config.auth_with].call(controller)
end
end
end
end

with Cornerstone::Config::AUTH_MODES set up like this :

module Cornerstone
class Config
AUTH_MODES = {
:warden => Proc.new { |controller| controller.env['warden'].user },
:devise => Proc.new { |controller| controller.current_user }
}
end
end

Rails 3.1: Better way to expose an engine's helper within the client app

You can create an initializer to accomplish this like so:

module MyEngine
class Engine < Rails::Engine
initializer 'my_engine.action_controller' do |app|
ActiveSupport.on_load :action_controller do
helper MyEngine::ImportantHelper
end
end
end
end

How to access Rails Engines methods from main application?

This question is a bit old, but here is a possible answer, for the record.

Maybe you can work around by using a piece of the spree code in your controller. It's generally a better idea to have the code in the controller (or, in the case of Spree, in a controller_decorator), than in the view.

For example if you need this in your products view as @current_order:

Spree::ProductsController.class_eval do
@current_order = Spree::Order.find(session[:order_id])
end

But I don't know why it's working in development and not in production.

Rails Parent app route to engine route and children

To override the default CKEditor routes create a config.js file within the host application.

# in app/assets/javascripts/ckeditor/config.js

CKEDITOR.editorConfig = function( config )
{
/* Filebrowser routes */
// The location of an external file browser, that should be launched when "Browse Server" button is pressed.
config.filebrowserBrowseUrl = "/some_path/ckeditor/attachment_files";

// The location of an external file browser, that should be launched when "Browse Server" button is pressed in the Flash dialog.
config.filebrowserFlashBrowseUrl = "/some_path/ckeditor/attachment_files";

// The location of a script that handles file uploads in the Flash dialog.
config.filebrowserFlashUploadUrl = "/some_path/ckeditor/attachment_files";

// The location of an external file browser, that should be launched when "Browse Server" button is pressed in the Link tab of Image dialog.
config.filebrowserImageBrowseLinkUrl = "/some_path/ckeditor/pictures";

// The location of an external file browser, that should be launched when "Browse Server" button is pressed in the Image dialog.
config.filebrowserImageBrowseUrl = "/some_path/ckeditor/pictures";

// The location of a script that handles file uploads in the Image dialog.
config.filebrowserImageUploadUrl = "/some_path/ckeditor/pictures";

// The location of a script that handles file uploads.
config.filebrowserUploadUrl = "/some_path/ckeditor/attachment_files";
};


Related Topics



Leave a reply



Submit