ruby on rails - Using Apotomo gem to build a user-driven widget page -
i trying use apotomo
setting widgets in app , have been running few issues. i've tried researching, haven't found useful yet. hopefully, might have experience here.
here setup:
- i have users model (set via devise), can each create custom pages (pages model).
- each page can have number of widgets.
pagescontroller#show
view used display published version of page or preview page user.pagescontroller#edit
view used display page in "edit" mode w/ forms each widget, allowing user update them needed. - i have widgets model serialized "data" field holds values each widget. (things such
title
,url
, etc).
the issue running displaying each user's customized widget.
for sake of question, let's assume have single widget "pictureswidget". i've added widget pagescontroller
so:
class pagescontroller < applicationcontroller has_widgets |root| root << widget(:pictures) end ... end
i have 2 views in pages controller: "pagescontroller#edit" , "pagescontroller#show". defined 2 views in widget controller, 1 "pictureswidget#edit" , default "pictureswidget#display". edit , show views, use load widgets:
# in views/pages/show.html.erb: <%= render_widget :pictures, :display, ... %> # in views/pages/edit.html.erb: <%= render_widget :pictures, :edit, ... %>
however, main issue running fact user can have multiple widgets per page. in other words, have pass in serialized data
field each widget, can display content in pictureswidget#display
or pictureswidget#edit
views. in pagescontroller
do:
def show @user = ... @page = @user.pages.find(params[:page_id]) @widgets = @page.widgets end
and in views/pages/show.html.erb
use following loop:
<% @widgets.each |widget| %> <%= render_widget :pictures, :display, :widget => widget %> <% end %>
this ends passing-in each individual widget activerecord argument pictureswidget
controller. in widget controller, added "args" both display
, edit
methods:
def display(args) @widget = args[:widget] @title = @widget.data[:title] @url = @widget.data[:url] render end
this causing me issues on place because when have re-render view or replace another, complaints "args" not present, , have no way pass in.
in apotomo documentation found supposed use options instead of args in newer version of gem. options pulled "has_widget" method in original pagescontroller
:
has_widgets |root| root << widget(:pictures, :data => @data) end
in case options[:data]
return @data
. however, need provide data per each widget, not global @data
var. wasn't able find examples of how others doing this. suggestions? or should scrap whole thing , write own widget controller? or downgrade cells , write own ajax handlers?
sorry long write-up. reckon else might run issue , helps them out.
thanks, nick
[edit] suggested, ended adding unique widgets so:
@page.widgets.each |indiv_wid| root << widget(indiv_wid.kind.to_sym, indiv_wid.id, data: indiv_wid.data) end
had override 1 of modules allow use of apotomo_root in controller/views:
module apotomo module rails module actionviewmethods delegate :render_widget, :apotomo_root, :url_for_event, :to => :controller end end end
(added :apotomo_root
above)
used in controller list of loaded widgets:
apotomo_root.each |x| @widget_ids << x.widget_id unless x.widget_id == :root end
and iterated on in view:
<% @widget_ids.each |id| %> <%= render_widget id %> <% end %>
you can have separate options per widgets. should instantiate different widgets each configuration, anyway. passing id each widget: http://rdoc.info/github/apotonick/apotomo/apotomo/widgetshortcuts:widget
class pagescontroller < applicationcontroller has_widgets |root| root << widget(:pictures, 'one', :data => ...) root << widget(:pictures, 'two', :data => ...) end ... end
you can use widget id in #render_widget
, then.
render_widget 'one'
Comments
Post a Comment