Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Tuxedo Decorate provides an easy to use Presenter
layer on top of Rails views.
It does not make any assumptions about your underline object or uses black
magic voodoo code to determine your ActiveRecord Relations.
Currently rails 4.0 or higher and ruby version of 2.0.0 or higher are supported. Add this line to your application's Gemfile:
gem 'tuxedo_decorate', require: 'tuxedo'
Notice the require statement!
And then execute:
$ bundle
Or install it yourself as:
$ gem install tuxedo_decorate
To use Tuxedo simply include it in your presenter class. For example
class BananaPresenter
include Tuxedo
def name
'hello'
end
def name_with_args(name, surname: '')
"hello #{name}, #{surname}"
end
end
In your views you can use the presenter_for
helper to use the presenter.
By default Tuxedo looks for a class with the same name but ending in Presenter
.
For example, we now use BananaPresenter
.
<% presenter_for(Banana.new) do |present| %>
<span><%= present.name %></span>
<div><%= present.name_with_args('Jan', surname: 'Stevens') %></div>
end
A shorthand is also included that allows to easily call a presenter method for to present object. For example
<%= prac(Banana.new, :name) %>
This basically translates to the following:
<%= presenter_for(Banana.new).name %>
Extra arguments are passed along to the presenter method, so the following also workes
<%= prac(Banana.new, :name_with_args, 'Jan', surname: 'Stevens') %>
We have a presenters
folder in our app folder. In their we have an ApplicationPresenter
that defines reusable methods. Every presenter inherits from ApplicationPresenter
.
For example
class ApplicationPresenter
include Tuxedo
# Here we setup our typical delegation methods
delegate(:current_user,
:link_to,
:content_tag,
to: :_h)
# Bunch of shared methods
def html_id
# ...
end
end
class BananaPresenter < ApplicationPresenter
def name
'hello'
end
def name_with_args(name, surname: '')
"hello #{name}, #{surname}"
end
end
Sure got you covered
<% present = presenter_for(Banana.new) %>
<span><%= present.name %></span>
<div><%= present.name_with_args('Jan', surname: 'Stevens') %></div>
Allright you can configure the suffix to be used by using a config block and placing it somewhere in the Rails initialization chain
Tuxedo.configure do |config|
config.suffix = 'ObscureName'
end
Easily, you can use object
but thats quite obscure so by default Tuxedo "guesses" (demodulize
+ gsub
+ underscore
)
the name of your object. For example if my Presenter class is name BananaPresenter
then
I can access the original object by using banana
class BananaPresenter
include Tuxedo
def greetings
"hello #{banana.name}"
end
end
If thats not good enough you can overwrite the object_alias
used. This allows for
using monkey
instead of banana
.
class BananaPresenter
include Tuxedo
object_alias :monkey
def greetings
"hello #{monkey.name}"
end
end
prac
inside my Presenters?Sure you can, for your convenience `prac`` is made available. This allows you to reuse presenter methods.
class BananaPresenter
def name
"BANANA BANANA BANANA"
end
end
class MonkeyPresenter
def eat
prac monkey.banana, :name
end
end
We expose a method _h
that delegates to the view context. So you have to prefix
all your url helpers with _h
, for example:
class BananaPresenter
include Tuxedo
def edit_link
_h.edit_banana_path(banana)
end
end
I'ts ruby just use delegate
, for example in our application we have to following setup
class BananaPresenter
include Tuxedo
delegate(:current_user,
:link_to,
:auth_link_to,
:auth_service,
to: :_h)
def edit_link
_h.edit_banana_path(banana)
end
end
After checking out the repo, run bin/setup
to install dependencies. 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 tags, and push the .gem
file to rubygems.org.
git checkout -b my-new-feature
)git commit -am 'Add some feature'
)git push origin my-new-feature
)The gem is available as open source under the terms of the MIT License.
FAQs
Unknown package
We found that tuxedo_decorate demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
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.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.