Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

exposant

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

exposant

  • 0.2.0
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

Exposant

In a Rails application, it is often required to fill a gap between Models and Views. There are of course, many differents ways to fill this gap, one of these is decorators and its variants exhibits or presenters.

The main difference between decorators, exhibits and presenters are their proximity with the rendering layer. Typically a decorator is not meant to be contextualized, whereas an exhibit is intended to have access to rendering context.

This gem provide an easy way to create theese concepts in Ruby.

Installation

Add this line to your application's Gemfile:

gem 'exposant'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install exposant

Usage

Exposant objects are intended to overload class (scopes) and instance methods of any other object. The default type is exposant, choosing between decorator, exhibit or any other type name is up to you. There is no magic involved for the context, you just have to call contextualize and provide the required context.

Basic example

Consider having a User model with first_name and last_name

# app/models/user.rb
class User < ActiveRecord::Base
  include Exposant::Model
  has_exposant type: :decorator
end

# app/decorators/user_decorator.rb
class UserDecorator < Exposant::Base
  exposant_type :decorator

  def full_name
    "#{first_name} #{last_name}"
  end
end

Then you may want to use your brand new decorator in your controller

# app/controllers/users_controller.rb
class UsersController < DefaultController
  def index
    @users = User.decorator(User.all)
  end

  def show
    @user = User.find(...).decorator
  end
end

Contextualization example

If you want to contextualize a presenter, for example in a Rails application.

# app/controllers/users_controller.rb
class UsersController < DefaultController
  def index
    @users = User.presenter(User.all)
    @users.contextualize(self)
  end

  def show
    @user = User.find(...).presenter
    @user.contextualize(self)
  end
end

Development

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.

Contributing

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

License

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

FAQs

Package last updated on 06 Nov 2023

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