engine knows the path where it is mounted #5008

Closed
wants to merge 1 commit into
from

Conversation

Projects
None yet
3 participants
@cpetschnig

In some cases, an engine might want to know the path where it is mounted by the main app. Here is an example when using the paperclip gem inside an engine:

module MyEngine
  class MyAttachment < ActiveRecord::Base
    has_attached_file :document, :styles => { :thumb => "100x100>" },
        :url => "/document/:id/:style.:extension"   # where did the main_app mount me?
  end
end

See also #4940

+ route.app == self
+ end
+ raise "#{self} is not yet mounted." unless my_route
+ my_route.path.spec.to_s

This comment has been minimized.

Show comment Hide comment
@josevalim

josevalim Feb 12, 2012

Contributor

I don't think this is a public API from the router. Also, an engine can be mounted inside another engine, so it is unlikely that this will give you the proper result. The best option is probably to have a configuration option where the developer will set the path.

@josevalim

josevalim Feb 12, 2012

Contributor

I don't think this is a public API from the router. Also, an engine can be mounted inside another engine, so it is unlikely that this will give you the proper result. The best option is probably to have a configuration option where the developer will set the path.

This comment has been minimized.

Show comment Hide comment
@cpetschnig

cpetschnig Feb 13, 2012

Ok, I didn't think about that. But wouldn't it be in general nice for an engine to know its mount point?

@cpetschnig

cpetschnig Feb 13, 2012

Ok, I didn't think about that. But wouldn't it be in general nice for an engine to know its mount point?

This comment has been minimized.

Show comment Hide comment
@josevalim

josevalim Feb 13, 2012

Contributor

There are many factors that can make this simple approach fail or become overly complex. In the paperclip example you showed, why do you need to know the mounted path? Does it actually matter since the files will likely be stored in the main app anyway?

@josevalim

josevalim Feb 13, 2012

Contributor

There are many factors that can make this simple approach fail or become overly complex. In the paperclip example you showed, why do you need to know the mounted path? Does it actually matter since the files will likely be stored in the main app anyway?

This comment has been minimized.

Show comment Hide comment
@cpetschnig

cpetschnig Feb 13, 2012

It is easy to solve my problem, maybe best with the config option you suggested: mount MyEngine::Engine => "/my_engine" and MyEngine.config.mount_point = "/my_engine" isn't very DRY, though. In my app, the uploaded files will be available only to the authenticated user, therefore I need my controller action respond to that URL.

@cpetschnig

cpetschnig Feb 13, 2012

It is easy to solve my problem, maybe best with the config option you suggested: mount MyEngine::Engine => "/my_engine" and MyEngine.config.mount_point = "/my_engine" isn't very DRY, though. In my app, the uploaded files will be available only to the authenticated user, therefore I need my controller action respond to that URL.

This comment has been minimized.

Show comment Hide comment
@josevalim

josevalim Feb 13, 2012

Contributor

Yeah, a config option is better. For example, an engine can be mounted at (/:locale)/my_engine and then there is no way you can simply point at that path. You need to have separate config which then the user can set and it will work regardless of the specific routing syntax.

@josevalim

josevalim Feb 13, 2012

Contributor

Yeah, a config option is better. For example, an engine can be mounted at (/:locale)/my_engine and then there is no way you can simply point at that path. You need to have separate config which then the user can set and it will work regardless of the specific routing syntax.

This comment has been minimized.

Show comment Hide comment
@drogus

drogus May 3, 2012

Member

@josevalim engine already knows where it's mounted and it includes this information: #4940 (comment), it's just not a public API. If the implementation just uses _generate_prefix instead of trying to guess which route is correct do you think it's useful to make it public API.

Regarding (/:locale)/my_engine, it would require passing :locale as an argument, but I think that's ok as it behaves as any other path helper

@drogus

drogus May 3, 2012

Member

@josevalim engine already knows where it's mounted and it includes this information: #4940 (comment), it's just not a public API. If the implementation just uses _generate_prefix instead of trying to guess which route is correct do you think it's useful to make it public API.

Regarding (/:locale)/my_engine, it would require passing :locale as an argument, but I think that's ok as it behaves as any other path helper

@josevalim josevalim closed this Feb 12, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment