Git Product home page Git Product logo

rails_settlement's Introduction

Rails Settlement

A simple gem that helps you set objects before any action.

Installation

Add to your Gemfile:

gem 'rails_settlement'

How to Use

Suppose you need to fetch a User before executing a controller action:

class UsersController < ApplicationController
  set_user only: %i[show] # It's essentially a before_action callback, allowing you to pass options directly.

  # Using the bang (!) with this method triggers an ActiveRecord::RecordNotFound Exception.
  set_user! only: %i[edit]

  # You can also define a scope with the `scope_to` option. It can be either a string/symbol or an Array of string/symbols.
  # This fetches the user after chaining all the scopes together.
  # set_user scope_to: :is_active, only: %i[show] #=> User.is_active.find_by
  # set_user! scope_to: %i[is_active is_admin] #=> User.is_active.is_admin.find_by

  # You can also set a namespace for the model with the `namespace` option. It can be a string or a symbol.
  # Suppose you have a model 'Admin::User', you can define:
  # set_user namespace: :admin, ...
  # If you have a model that's nested more than once, like 'Admin::Account::User', you can define as:
  # set_user namespace: 'admin/account', ...

  # If you want to search an object's relationship, you can use the `associated_to` option.
  # set_articles! associated_to: :user, ... # => Article.where(user => user)
  # set_articles! associated_to: :user, scope_to: :published # => Article.published.where(user => user).find_by

  def show
    do_something with: user # You also have an attribute reader with the same name available if the object is found. If not, it defaults to nil (unless a bang method is used, in which case an error is raised!)
  end
end
class User < ApplicationRecord
  scope :is_active, -> { where(is_active: true) }

  # This method is used internally by `set_user` to fetch the correct param (using `params_key`) and to send a key to `find_by` (using `model_key`).
  # You can override this on the fly by passing any of these two keys (or both!) in the controller directly.
  # Ex: set_user params_key: :user_username, only: %i[index], ...
  # You can also pass a `scope_to` option in the hash below.
  def self.settable_params
    { params_key: :username, model_key: :username }
  end
end

This approach allows you to use any model instead of a User. Just follow the naming convention set_[model_name_in_snake_case]!.

That's about it!

rails_settlement's People

Contributors

shettytejas avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

rails_settlement's Issues

Enhancement to add object search in associations.

There should be an easy way to find an object from relation, than always relying on a scope.

  User.is_active.find_by(...) # This is currently possible
  user.snippets.find_by(...) # This is not.

Proposal: A new settable option named associated_to makes sense for this context imo. It can be used something like this:

  set_user! ... # Will work as User.find_by ...
  set_snippet! associated_to: :user # Should work as user.snippets.find_by ...

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.