New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

auto_view_model

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

auto_view_model

  • 0.1.0
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

AutoViewModel

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) %>

Installation

  1. Add auto_view_model to your Rails application's Gemfile.
  2. Run bundle install.
  3. In config/application.rb, add require "auto_view_model/engine".
  4. Start adding Ruby files to your views directory.

Usage

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
end

And 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!" %>

Development

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.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/BlakeWilliams/auto_view_model.

License

The gem is available as open source under the terms of the MIT License.

FAQs

Package last updated on 01 May 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc