Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing Apotomo gem to build a user-driven widget page
    primarykey
    data
    text
    <p>I am trying to use <code>apotomo</code> for setting up widgets in my app and have been running into a few issues. I've tried researching, but haven't found anything useful yet. Hopefully, someone might have some experience here.</p> <p>Here is my setup:</p> <ul> <li>I have Users model (set up via Devise), who can each create custom pages (Pages model).</li> <li>Each Page can have a number of widgets. The <code>PagesController#show</code> view is used to display a published version of the page or preview the page for the user. The <code>PagesController#edit</code> view is used to display the page in "edit" mode w/ forms for each widget, allowing the user to update them as needed.</li> <li>I have a Widgets model with a serialized "data" field that holds the values for each widget. (Things such as <code>title</code>, <code>url</code>, etc).</li> </ul> <p>The issue I am running into is displaying each user's customized widget.</p> <p>For the sake of this question, let's assume I have a single widget "PicturesWidget". I've added the widget to the <code>PagesController</code> like so:</p> <pre><code>class PagesController &lt; ApplicationController has_widgets do |root| root &lt;&lt; widget(:pictures) end ... end </code></pre> <p>I have two views in the Pages controller: "PagesController#Edit" and "PagesController#Show". I also defined two views in the widget controller, one is "PicturesWidget#edit" and the default is "PicturesWidget#display". From the edit and show views, I use something like this to load the widgets:</p> <pre><code># In views/pages/show.html.erb: &lt;%= render_widget :pictures, :display, ... %&gt; # In views/pages/edit.html.erb: &lt;%= render_widget :pictures, :edit, ... %&gt; </code></pre> <p>However, the main issue I am running into is the fact that the user can have multiple widgets per page. In other words, I have to pass in the serialized <code>data</code> field into each widget, so it can display the content in <code>PicturesWidget#display</code> or <code>PicturesWidget#edit</code> views. In the <code>PagesController</code> I do:</p> <pre><code>def show @user = ... @page = @user.pages.find(params[:page_id]) @widgets = @page.widgets end </code></pre> <p>And in <code>views/pages/show.html.erb</code> I use the following loop:</p> <pre><code>&lt;% @widgets.each do |widget| %&gt; &lt;%= render_widget :pictures, :display, :widget =&gt; widget %&gt; &lt;% end %&gt; </code></pre> <p>This ends up passing-in each individual widget ActiveRecord as an argument to the <code>PicturesWidget</code> controller. In the widget controller, I added "args" to both <code>display</code> and <code>edit</code> methods:</p> <pre><code>def display(args) @widget = args[:widget] @title = @widget.data[:title] @url = @widget.data[:url] render end </code></pre> <p>This is causing me issues all over the place because when I have to re-render the view or replace it with another, I usually get complaints that "args" is not present, and I have no way to pass it in.</p> <p>In Apotomo documentation I found that we are supposed to use options instead of args in the newer version of the gem. Options are pulled from the "has_widget" method in the original <code>PagesController</code>:</p> <pre><code>has_widgets do |root| root &lt;&lt; widget(:pictures, :data =&gt; @data) end </code></pre> <p>In this case <code>options[:data]</code> would return <code>@data</code>. However, I need to provide data per each widget, not a global <code>@data</code> var. I wasn't able to find any examples of how others are doing this. Any suggestions? Or should I just scrap the whole thing and write my own Widget Controller? Or downgrade to cells and write my own AJAX handlers?</p> <p>Sorry for the long write-up. I reckon someone else might run into this issue and hopefully this helps them out.</p> <p>Thanks, Nick</p> <hr> <p>[Edit] As suggested, I ended up adding unique widgets as so:</p> <pre><code>@page.widgets.each do |indiv_wid| root &lt;&lt; widget(indiv_wid.kind.to_sym, indiv_wid.id, data: indiv_wid.data) end </code></pre> <p>Had to override one of the modules to allow use of apotomo_root in the controller/views:</p> <pre><code>module Apotomo module Rails module ActionViewMethods delegate :render_widget, :apotomo_root, :url_for_event, :to =&gt; :controller end end end </code></pre> <p>(Added <code>:apotomo_root</code> above)</p> <p>Used this in the controller to get a list of all the loaded widgets:</p> <pre><code>apotomo_root.each do |x| @widget_ids &lt;&lt; x.widget_id unless x.widget_id == :root end </code></pre> <p>And then I simply iterated over it in the view:</p> <pre><code>&lt;% @widget_ids.each do |id| %&gt; &lt;%= render_widget id %&gt; &lt;% end %&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload