Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mikker/render_with_view
☝️ Be explicit about the things you send from your Rails controller to the view.
https://github.com/mikker/render_with_view
rails rails-controller ruby
Last synced: 3 months ago
JSON representation
☝️ Be explicit about the things you send from your Rails controller to the view.
- Host: GitHub
- URL: https://github.com/mikker/render_with_view
- Owner: mikker
- License: mit
- Created: 2016-06-07T08:34:28.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2023-08-29T11:41:47.000Z (over 1 year ago)
- Last Synced: 2024-09-22T21:19:33.535Z (4 months ago)
- Topics: rails, rails-controller, ruby
- Language: Ruby
- Homepage:
- Size: 19.5 KB
- Stars: 7
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# render_with_view
Be explicit about the things you send from your Rails controller to the view.
### Example
`app/controllers/application_controller.rb`:
```ruby
class ApplicationController
include RenderWithView
end
````app/controllers/home_controller.rb`:
```ruby
class HomeController < ApplicationController
def index
render_with_view posts: Post.all
end
end
````app/views/home/index.html.erb`:
```erb
- <%= link_to post.title, post %>
<% view.posts.each do |post| %>
<% end %>
```
### Why not just use instance variables?
They feel like _magic_. Instead I like how this forces me to be explicit in what I send along to my templates. It's like a small step towards having a presenter/view layer (or whatever) but not going further than just adding the convention of using a single variable.
### What's `view`?
An object with reader methods for every key in the hash you gave it. A [HalfOpenStruct](https://gist.github.com/henrik/19c68b2a41ab4d098ce8) in a way.
## Installation
Add `render_with_view` to your Gemfile:
```ruby
gem 'render_with_view'
```
Include it in your `ApplicationController`:
```ruby
class ApplicationController
include RenderWithView
end
```
## Bonus RSpec matcher
```ruby
require 'render_with_view/rspec_matcher'
describe ThingController do
subject { get :index }
it { should set_view_local :key, optional_value }
end
```
# License
MIT