Experimental gem to encapsulate existing partials via sidecar Ruby files.
This gem doesn't replace partials, but instead builds on top of them:
- Logic is encapsulated in a co-located Ruby file, not spread across helpers and external view model objects.
- Partial arguments can be required.
- Partial arguments can have default values, so no more
<%= my_value = "default" if !local_assigns.has_key?(:my_value) %>
- Add
auto_view_modelto your Rails application'sGemfile. - Run
bundle install. - In
config/application.rb, addrequire "auto_view_model/engine". - Start adding Ruby files to your
viewsdirectory.
Using AutoViewModel is simple, create a Ruby file in the same directory as any
partial with a matching name (sans _ prefix).
e.g.
Given the template app/views/posts/_post.html.erb:
<h1><%= title.upcase %></h1>
<p>
<%= markdown(body) %>
</p>You would create a sidecar Ruby file: app/views/posts/post.rb
class Posts::Post < AutoViewModel::Base
requires :title # Raises if partial is not passed a title
accepts :body, default: "" # Allows default parameters for non-required attributes
def title
@title.upcase
end
def rendered_body
# Application and Rails provided helpers are usable in the view models
markdown(body)
end
endAnd update the template to use the view object:
<h1><%= view.title %></h1>
<p>
<%= view.rendered_body %>
</p>To render the partial, use render like you would any other partial.
<%= render "posts/post", title: "Hello world", body: "Welcome!" %>After checking out the repo, run bin/setup to install dependencies. Then, run rake test to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and the created tag, and push the .gem file to rubygems.org.
Bug reports and pull requests are welcome on GitHub at https://github.com/BlakeWilliams/auto_view_model.
The gem is available as open source under the terms of the MIT License.