We're building a Chat service, which people are able to use from within their code.
Amongst the tools we're building, we've made a Ruby gem to allow people to quickly add a Chat window to their Ruby web-application.
We'd like to create a Rails-specific wrapper, though, because currently the user has to manually call .html_safe.
How is it possible to use these Rails-specific features from within a Ruby gem? I've heard this might be called 'Railsties' but I have not been able to find any comprehensive documentation about these, and how to use them.
Specifically, we'd like to:
- Call
html_safeon some string output so the user does not have to do this manually. - Put some configuration settings in a file in
config/initializers/some_name.rbrather than having to specify these inline. - Potentially create a generator that the user can run to fill this initializer automatically.
How can we use these features? Is there some other gem-dependency we can include in our gem to access these features?
An engine can contain models, controllers, routes, generators, middleware and any arbitrary code that you can mount in the host application. Engines are usually packaged as gems.
Devise for example is a rails engine that provides authorization.
Rails has a generator command for creating engines:
For this example lets call it chatty.
Since an engine is mounted in a Rails application you have full access to the Rails stack (such as
.html_safe). This also means that you test engines by mounting them in a dummy application.If you have packaged the application as a gem than you simply mount it in the host application by adding it to the Gemfile.
To make your engine configurable you can follow the "MyGem.configure pattern":
To create a user configuration file you use a generator:
And a code template:
You can now run
rails g chatty:installand it will create the file in the host application.