https://github.com/mrkamel/view_utils
Rails helpers are shitty. ViewUtils is an alternative which is as easy, but without the downsides.
https://github.com/mrkamel/view_utils
Last synced: over 1 year ago
JSON representation
Rails helpers are shitty. ViewUtils is an alternative which is as easy, but without the downsides.
- Host: GitHub
- URL: https://github.com/mrkamel/view_utils
- Owner: mrkamel
- License: mit
- Created: 2019-02-03T20:27:30.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2019-02-03T20:58:27.000Z (over 7 years ago)
- Last Synced: 2025-02-03T14:35:46.702Z (over 1 year ago)
- Language: Ruby
- Size: 7.81 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# ViewUtils
ViewUtils is an alternative to rails helpers.
Everyone knows rails helpers are shitty, mainly because
* they pollute the global view namespace
* once added, you have a hard time removing them
* it's often not clear in which file they are living
* naming them is hard
ViewUtils are as easy as helpers, without the downsides:
```
<%= util(:user).avatar_image(user) %>
```
## Installation
Add this line to your application's Gemfile:
```ruby
gem 'view_utils'
```
And then execute:
$ bundle
Or install it yourself as:
$ gem install view_utils
## Usage
To create your own ViewUtil:
```ruby
class UserUtil < ViewUtils::Base
include ActionView::Helpers::AssetTagHelper
def avatar_image(user, size: :small)
image_tag user.avatar_image(size), class: "avatar-image"
end
end
```
In your view:
```
<%= util(:user).avatar_image(user) %>
```
To use another util from within your util, you can use the
util helper within your utils as well:
```ruby
class UserUtil < ViewUtils::Base
# ...
def some_util
util(:login).other_util
end
end
```
For the rare cases, where you need access to the view object,
you can use the view directly:
```ruby
class UserUtil < ViewUtils::Base
# ...
def some_util
view.root_url
end
end
```
## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/mrkamel/view_utils.
## License
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).