Make AR concerns work
@sfcgeorge wrote: Concerns using ActiveSupport::Concern don't work because that module doesn't exist in Opal/ActiveSupport. I tried copy pasting the file but a bug in Opal meant that the way ActiveSupport::Concern is implemented doesn't work. That was with Opal 0.10.5 though, Opal 0.11 may have fixed the issue. I did manage to tweak it to work on the client though, but then I had the constant issue and mistakenly thought that was an issue with concerns so I switched to the non magic way of doing concerns as I showed above. Adding app/hyperloop/models/concerns to eager_load_paths and autoload_paths makes PORO concerns work today. ActiveSupport::Concern can be tweaked to work with Opal 0.10.5 ActiveSupport::Concern may work verbatim with Opal 0.11 but I haven't tried yet
There is still a bug in Opal 0.11 and prior, I think something wrong with append_features. https://github.com/opal/opal/issues/1761
I also opened an issue with opal/active-support to see if they want to add concerns there.
If Opal fixes the language bug but doesn't want to add the concerns file it would be easy enough to copy paste into Hyperloop.
My patched version works right now in Opal 0.10.5 and 0.11. So YES you can get concerns to work in Hyperloop on the frontend 🎉
First you have to add these load paths otherwise you get weird constant lookup errors:
# config/application.rb
config.autoload_paths += %W[#{config.root}/app/hyperloop/models/concerns]
config.eager_load_paths += %W[#{config.root}/app/hyperloop/models/concerns]
Then copy paste my tweaked version of concern.rb, put it into app/hyperloop/components/concern.rb that way it will only be loaded on the frontend and won't clobber the real ActiveSupport concern on the backend.
https://gist.github.com/sfcgeorge/bdf4ab6a95f74f75a502bb7d8d3155df#file-concern-rb-L125
I made a pull request to opal/active-support which has been merged https://github.com/opal/opal-activesupport/pull/19
I'm a little confused - is the gist all that is needed RIGHT NOW on current opal to get everything to work?
Yes :D or even use opal/active-support master.
Plus you have to add the hyperloop/models/concerns directory to load paths as above, which should eventually be in hyper-config.
hyperloop/models/concerns are already configured in hyperloop-config if you use ruby-hyperloop/hyperloop-config branch 1_0_0 from github, will be in lap21
Great, thanks! In other words, after the next release of both concerns will just work.
@janbiedermann so what we should do is put the gist into Hyperloop now wrapped with
if defined?(ActiveSupport::Concerns)
the gist
end
that way it will work out of the box, and then once the opal fix is released (or if somebody is using opal master) we won't redefine it unnecessarily.