komposable / komponent

An opinionated way of organizing front-end code in Ruby on Rails, based on components

Home Page:http://komponent.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Unable to use component helper

JoseMPena opened this issue · comments

As per the README, component helper module should(?) be wired up to the component 'view' right out of the box:

Helpers

If your partial becomes too complex and you want to extract logic from it, you may want to define custom helpers in the ButtonComponent module:

# frontend/components/button/button_component.rb

module ButtonComponent
  property :href, required: true
  property :text, default: "My button"

  def external_link?
    @href.starts_with? "http"
  end
end

However, none of the methods defined on my helpers are accessible from the component 'view':

#frontend/components/direct_attachment_field/direct_attachment_field_component.rb
module DirectAttachmentFieldComponent
  extend ComponentHelper

  def something
    raw('something')
  end
end
#frontend/components/direct_attachment_field/_direct_attachment_field-html.erb
<div data-controller="direct-attachment-field" data-target="report-form.fileFieldGroup" data-index="0" class="direct-attachment-field input-group">
  <%= @attacheable.file_field :document, direct_upload: true,
                              class: 'form-control-file',
                              data: { action: 'report-form#loadFile', target: 'report-form.fileField'} %>
  <label class="file-field-label">Seleccionar archivo...</label>
  <%= something %>
</div>

Always returns:
undefined local variable or method 'something' for #<#<Class:0x007fab224dbca8>:0x007fab21b09b58>

Is there any extra configuration I might be missing or it is not behaving as expected?

Thanks

Hello @JoseMPena thanks for the bug report.

It's weird, it should work out of the box. Did you try to restart the server after creating the component?

Which version of Komponent / Rails / Ruby do you use?

Hi @Spone, thanks for your answer.
Actually, just found out that (at least in my case) have to restart the Rails server (not even webpack-dev-server) to get the new helper methods working. Looks like an ActionView thing.
My versions:
Rails 5.2.0.rc1
Ruby 2.4.1

Ok, so it's an autoloading problem.

In the latest version of Komponent, when you run the install generator, it will take care of it automatically (you can update Komponent, run rails generate komponent:install again and choose to overwrite just the config/application.rb file).

If you want to do it manually, just add these two lines to config/application.rb:

config.autoload_paths << config.root.join('frontend/components')
config.i18n.load_path += Dir[config.root.join('frontend/components/**/*.yml')]

And that should fix it :)