<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Sergio Sebastián Arriagada on Medium]]></title>
        <description><![CDATA[Stories by Sergio Sebastián Arriagada on Medium]]></description>
        <link>https://medium.com/@sarriagada87?source=rss-59d07f57d6e3------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*O7whmK-9uKVFPNAw.</url>
            <title>Stories by Sergio Sebastián Arriagada on Medium</title>
            <link>https://medium.com/@sarriagada87?source=rss-59d07f57d6e3------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 12:18:28 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sarriagada87/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Be reactive on Rails with 100% ruby]]></title>
            <link>https://medium.com/@sarriagada87/be-reactive-on-rails-with-100-ruby-d369d9ce482a?source=rss-59d07f57d6e3------2</link>
            <guid isPermaLink="false">https://medium.com/p/d369d9ce482a</guid>
            <category><![CDATA[ruby]]></category>
            <category><![CDATA[ruby-on-rails]]></category>
            <category><![CDATA[components]]></category>
            <category><![CDATA[reactive]]></category>
            <dc:creator><![CDATA[Sergio Sebastián Arriagada]]></dc:creator>
            <pubDate>Mon, 07 Sep 2020 19:14:46 GMT</pubDate>
            <atom:updated>2020-09-07T21:48:42.142Z</atom:updated>
            <content:encoded><![CDATA[<h3>Be reactive on Rails with 100% ruby code</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/667/1*KT8I4tF2Yo7Oyi6SY3_AMw.png" /></figure><p>I’m not a senior developer or anything like that. I only have some web frameworks experience, especially with Ruby on Rails and when the client requested it, AngularJS and React. Where I feel like a “happy developer” is in Rails, somewhat because of the benefits of ruby, and the same because of the way Rails works. But I must admit that reactivity was something cumbersome to achieve without falling into mixtures. Lately, there have been significant community attempts, such as Stimulus Reflex (Thanks a lot for their contributions and efforts to make the Ruby on Rails Universe a better place) I always had the feeling that although the way to work the reactivity was improving, there was still a screw adjustment. <br>That is why I was thrilled to find this gem. That is an excellent job from the Motion team. Thanks to them and the entire community. Please visit the repo <a href="https://github.com/unabridged/motion">https://github.com/unabridged/motion</a></p><p>Motion’s promise is attractive: “Motion allows you to build reactive, real-time frontend UI components in your Rails application using pure Ruby.”<br>Being reactive in Rails writing 100% ruby code, and not stopping being Rails in the attempt, it is priceless. I wanted to clarify that I have nothing against JS and its entire ecosystem. It is just a joy to work with the language that makes me feel more comfortable.</p><p>Installing it is very simple and you just have to follow the steps that they detail in their <a href="https://github.com/unabridged/motion#installation">repository</a> so I’m not going to stop at that, except to comment that they rely on another great development such as <a href="https://github.com/github/view_component">ViewComponent,</a> a gem of the Github people that will be there in the Rails core since version 6.1.</p><p>This combination is a winner, reactive components!</p><p>Let’s see an example taken from your documentation:<br>The “Hello world” of reactivity, a reactive counter button!</p><p>When installing Motion we have a generator available to</p><pre>rails g motion:component &lt;component-name&gt;</pre><p>We are going to run this command that our component will generate. We will see how we create a folder “components” and within it our files “button_component.rb” where our component logic will be and “button_component.html.erb” where our HTML will be</p><pre>&gt; rails g motion:component button</pre><pre>Running via Spring preloader in process 21091<br>    generate  component<br>       rails  generate component Button<br>Running via Spring preloader in process 21096<br>      create  app/components/button_component.rb<br>      invoke  test_unit<br>   identical    test/components/button_component_test.rb<br>      invoke  erb<br>      create    app/components/button_component.html.erb<br>      insert  app/components/button_component.rb</pre><p>we can render our component in any view we have using the following line</p><pre>&lt;%= render ButtonComponent.new %&gt;</pre><p>Let’s put some reactive logic to our component</p><pre>class ButtonComponent &lt; ViewComponent::Base<br>  include Motion::Component</pre><pre>  attr_reader :total</pre><pre>  def initialize(total: 0)<br>    @total = total<br>  end</pre><pre>  map_motion :add<br>  def add<br>    @total += 1<br>  end<br>end</pre><p>The important thing here is map_motion :add<br>what will be the union between our HTML component and the method in our ruby ​​component</p><p>I am using <a href="https://bulma.io/">Bulma</a> for this test but you can use any CSS framework or even none.</p><p>Let’s add our HTML to the component</p><pre>&lt;div&gt;<br>  &lt;span&gt;&lt;%= total %&gt;&lt;/span&gt;<br>  &lt;%= button_tag &quot;Increment&quot;, data: { motion: &quot;add&quot; }, class: &quot;button is-small is-info&quot; %&gt;<br>&lt;/div&gt;</pre><p>Two things to highlight here:<br>One limitation is that everything in our component template must have a main node, in our case we wrap everything with a simple div, this is because of how Motion works from behind, very similar to how React works.<br>The other thing to highlight is ´data: {motion: “add”} ´ is the connection with our component in ruby, it is what will trigger the method with the same name.</p><p>And that’s all we need, with that we should have a fully functional reactive button and without writing a line of javascript.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/320/0*2WjmwvV-n7_svugl.gif" /></figure><p>Another small example is the classic I write and react, our code could be something like this:</p><pre>&lt;div&gt;<br>  &lt;p&gt;My name is &lt;%= name %&gt;  &lt;%= &#39;ok!&#39; if @is_ok %&gt;&lt;/p&gt;<br>  &lt;input type=&quot;text&quot; data-motion=&quot;keyup-&gt;update&quot; class=&quot;input&quot;&gt;<br>&lt;/div&gt;</pre><p>And the component on ruby:</p><pre>class InputComponent &lt; ViewComponent::Base<br>  include Motion::Component</pre><pre>  attr_reader :name</pre><pre>  def initialize(name: &quot;&quot;)<br>    @name = name<br>  end</pre><pre>  map_motion :update<br>  def update(event)<br>    new_value = event.current_target.value<br>    @name = new_value<br>    @is_ok = new_value == &quot;sebastián&quot;<br>  end<br>end</pre><p>Two other things to highlight here.<br>By default motion will take the <em>change</em> event as the trigger in <em>input</em> but we can define which event to bind, as seen in the example ´data-motion = “keyup-&gt; update” ´ we are binding <em>keyup</em> Another interesting aspect is that our ruby ​​method receives an ´event´ as a parameter, where we have available data such as the current_target to obtain its value (in this example) or to be able to access its attributes, etc.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/320/0*XsyQlDi3yHQSG4-i.gif" /></figure><p>Imagine the potential of this using ActiveRecord, CableReady, chartkick, etc …</p><p>Thanks for getting here. <br>This is not intended to be a tutorial or an introduction to Motion. It was to share with you this tool that is undoubtedly going to become an everyday part of my work.<br>God bless you!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d369d9ce482a" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>