35
loading...
This website collects cookies to deliver better user experience
<%= form_for @project do |f|
<%= abymize(:tasks, f) %>
<% end %>
<%= form_for @project do |f|
<%= f.abyme_for :tasks %>
<% end %>
f
actually stands for, find where it was originally defined, and how to add behaviour to it. The only way out was to - you probably guessed it - dig into some source code. simple_form
gem source code, since we knew that it was fiddling with the same mysterious f
object - which turned out to be an instance of ActionView::Helpers::FormBuilder
class. It gave us a few hints (most notably in the way we should register our method) but it wasn't enough to make it work ; it was only after reading through Rails class definition (particularly line 2241 and line 2629) that we found the solution to our problem : we just had to call our existing helper method on the instance variable called @template
. Happy devs we were.test
directory for - take a guess - test duties). Then loading the different pieces was as easy as :module Abyme
class Engine < ::Rails::Engine
isolate_namespace Abyme
config.after_initialize do
ActiveSupport.on_load :action_view do
include Abyme::ViewHelpers
end
ActiveSupport.on_load :action_controller do
include Abyme::Controller
end
end
end
end
Abyme::Model
part ; this may very well change in the future but we decided not to load it by default, so that users include it manually in their model (unless using our generators) like so :class Task < ApplicationRecord
include Abyme::Model
has_many :comments, inverse_of: :task, dependent: :destroy
abymize :comments
end
SimpleForm
's way of registering their simple_fields_for
method on Rails's original FormBuilder
class :module Abyme
module ActionViewExtensions
module Builder
def abyme_for(association, options = {}, &block)
# Call to our #abyme_for view helper, defined in Abyme::ViewHelpers
@template.abyme_for(association, self, options, &block)
end
end
end
end
# The registration takes place below
module ActionView::Helpers
class FormBuilder
include Abyme::ActionViewExtensions::Builder
end
end
rake release
. You'll just have to update your gem version appropriately before running it, and it will take care of releasing the gem for you (you'll need to register an account on rubygems though). In a few seconds, you'll be ready to just bundle add
your gem !npm
or yarn
package of its own. However, nothing holds you from using your gem repository as your yarn package repository as well, which is the approach we took. Naming the package like the gem, it allows installation with bundle add abyme
and yarn add abyme
which makes for a nice symmetry 👩🏾🤝👩🏼CHANGELOG
to keep track of all changes and code additionsREADME
sucks, chances are no one will bother to even try using it. Which kind of defeats the purpose of a community-driven endeavour.