Rails Engines rendered View -
i have rails engine i'm developing. first one, besides example in rails documentation, don't have ref on works. has partial i'm requiring application render.
<%=render partial: 'my_engine/foo/bar'%>
that works fine. , understand why need reference partial through engine name space.
but within partial, apparently have use name space well. when render photo
<%= image_tag('my_engine/addphoto.jpg') %>
that counter intuitive based on documentation. doing wrong, or way is?
okay rich peck think understand better. let me enumerate can correct me if i'm still confused.
i confused how namespace generated engine. assuming structure generated name space, , when inside engine, operate if in normal app. , namespace automagic:).
but if understand correctly name space rails component generated encapsulating them in :::: module enginename taken care of generators, if you're creating file manually, need add encapsulation in files. since first engine didn't know missing.
for asset pipeline namespace generated structure image/enginename/....
so within engine unless inside module, have honor namespace enginename::modelname::method.
the namespace isn't applied task file, doesn't seem being incapsulated in module. redundant anyway since rake task provide namespace method.
ala namespace :db
i surprised find need manually include namespaced application controller in namespaced controllers ala
require_dependency "enginename/application_controller"
but rich says, engine glorified module, guess needs little , then.
and of course javascript files isolated because they're loaded when 1 of pages engine loaded, , not when partial engine rendered. can have user of engine include javascript file in application.js, need namespaced manually avoid confusing them similar js other sources. there's blog on doing kenneth truyers i'd bookmarked future exercise, maybe i'll need now.
lso locales not name spaced, need yourself. simple add :engine_name locale file hierarchy ala de: :engine_name :variables recommend adding prefix engine name x avoid getting engine locales clobbered user using same name variable. xadmin: better admin: since it's me use admin: variable admin function:) , discover localization in engines disappeared.
rich, closer
now extend question,
i'm not sure initialization files namespaced. enginename.rb file in apps initializers directory overwrite 1 same name in engine's initializer directory? assume if it's true can use different name in engine initializer since think there no real associations file name, bookkeeping.
now how extend model in app.
so add columns apps model okay in engine.rb (located in lib)
mattr_accessor :user_class mattr_accessor :user_table
in engine_initializer.rb
engine.user_class= 'user' engine.user_table='users'
then in migration
add_column engine.user_table.to_sym, :attribute add_index engine.user_table.to_sym, :attribute, name: "index_#{engine.user_table}_on_engine_attribute".to_sym
which seems work okay
but how add methods model. i'm having user add require 'engine/user_include.rb'
is there nice way that
modules & classes
am doing wrong, or way is?
you're not doing wrong, it's not "the way is"... let me explain how works
rails engines nothing more glorified modules
& classes
. when create engine (gem), it's creating namespaced controller -'
#app/controllers/namespace/controller.rb class namespace::controller < applicationcontroller ... end
you have remember ruby on rails
bunch of modules
& classes
. it's been created very - have realize in rails involved classes or modules in way
and since engines
way manage these modules
& classes
, you'll appreciate engine
way add extensibility more of them.
module blorgh class engine < rails::engine ... end end
engines designed give ability add standardized functionality system without intruding on codebase (basically how gems
work). question whether need call engine's name in paths etc:
<%= image_tag('my_engine/addphoto.jpg') %>
the bottom line if bundling assets engine, how else rails going access files? keeping engine-based files separate other application means able bundle files engine (they won't conflict).
this reason why engines
exist - allow provide functionality without intruding on other parts of application
gems
having worked on gem, can attest way of engines
a rails gem engine other files attached. if you're including various controllers
, models
or assets
gem - doesn't make sense make them unique engine
(as not conflict other parts of app)?
Comments
Post a Comment