Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
A simple and Rubyish view helper for Rails 4, Rails 5, Rails 6, and Rails 7. Keep your helpers and views Object-Oriented!
:collection
or :object
or :locals
explicitly or implicitly)content_tag
or link_to
Ruby 2.1.x, 2.2.x, 2.3.x, 2.4.x, 2.5.x, 2.6.x, 2.7.x, 3.0.x, 3.1.x, 3.2.x, and 3.3 (trunk)
Rails 4.2.x, 5.0, 5.1, 5.2, 6.0, 6.1, 7.0, and 7.1 (edge)
ActiveRecord, ActiveResource, and any kind of ORMs that uses Ruby Objects as model objects
User
should be named UserDecorator
.
You can use the generator for doing this ( % rails g decorator user
)render
class Author < ActiveRecord::Base
# first_name:string last_name:string
end
class AuthorsController < ApplicationController
def show(id) # powered by action_args
@author = Author.find id
end
end
module AuthorDecorator
def full_name
"#{first_name} #{last_name}"
end
end
<%# @author here is auto-decorated in between the controller and the view %>
<p><%= @author.full_name %></p>
class Author < ActiveRecord::Base
# name:string
has_many :books
end
class Book < ActiveRecord::Base
# title:string url:string
belongs_to :author
end
class AuthorsController < ApplicationController
def show(id)
@author = Author.find id
end
end
module BookDecorator
def link
link_to title, url
end
end
<p><%= @author.name %></p>
<ul>
<% @author.books.order(:id).each do |book| %>
<%# `book` here is auto-decorated because @author is a decorated instance %>
<li><%= book.link %></li>
<% end %>
</ul>
Sometimes you may want to use decorators outside of Action View, for example,
for background tasks for ActiveJob.
For such use case, ActiveDecorator module provides run_with
method
that takes some kind of Action View and a block.
ActiveDecorator::ViewContext.run_with ApplicationController.new.view_context do
## perform some heavy jobs here
end
You can test a decorator using your favorite test framework by decorating the model instance with
ActiveDecorator::Decorator.instance.decorate(model_instance)
Considering an Organization
model and its simple decorator:
module OrganizationDecorator
def full_name
"#{first_name} #{last_name}"
end
end
An RSpec test would look like:
describe '#full_name' do
it 'returns the full organization name' do
organization = Organization.new(first_name: 'John', last_name: 'Doe')
decorated_organization = ActiveDecorator::Decorator.instance.decorate(organization)
expect(decorated_organization.full_name).to eq('John Doe')
end
end
By default, ActiveDecorator searches a decorator module named target_class.name + "Decorator"
If you would like a different rule, you can configure in your initializer.
ActiveDecorator.configure do |config|
config.decorator_suffix = 'Presenter'
end
Copyright (c) 2011 Akira Matsuda. See MIT-LICENSE for further details.
FAQs
Unknown package
We found that active_decorator demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.