Code Monkey home page Code Monkey logo

activerecord_liquid_drops's Introduction

Gem Version License: MIT Ruby Style Guide Conventional Commits unstable

Activerecord_liquid_drops

Integrates Liquid Templating Language Drops to Active Record

Installation

Add this line to your application's Gemfile:

gem "activerecord_liquid_drops"

And then execute:

$ bundle

Or install it yourself as:

$ gem install activerecord_liquid_drops

Usage

Streamlined Drops Definition

Leveraging the Liquid language and its Drops functionality offers significant advantages in terms of templating engine flexibility. However, the management of numerous Drops classes, often containing boilerplate code, can become cumbersome and unwieldy. activerecord_liquid_drops addresses this by automating the creation of Drops classes for each Active Record model seamlessly.

Defining safe attributes for exposure becomes straightforward. Take our User model with three database columns: first_name, last_name, and dob. To expose these as safe attributes, we simply add a Drops block and pass it symbols representing the columns.

class User < ActiveRecord::Base
  drops :first_name, :last_name, :dob
end

Additionally, the Drops block can accept a method name defined on the model, providing even greater customization and control over your data presentation.

class User < ActiveRecord::Base
  drops :name, :dob

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

Associations are supported as well. If we add a posts table to our example with title and body columns, we can include them in the Drops.

class User < ActiveRecord::Base
  has_many :posts
  drops :name, :dob

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

class Post < ActiveRecord::Base
  belongs_to :user
  drops :user, :title, :body
end

It's important to note that by introducing the user association as a drop on the Post class, the Post drops will inherit those of the user, resulting in ['title', 'body', 'user.name', 'user.age'].

Useful Helpers

We've included two helpful helpers:

  1. all_drops: This class method is added to your Active Record models, making it easy to retrieve an array containing all available Drops for the model.

    => Post.all_drops
    => ['title', 'body', 'user.name', 'user.age']
  2. drops: This instance method is available in your Active Record models, allowing you to instantiate a Drops class instance for your current model instance.

    => post = Post.create!(title: 'New Post', body: 'I hold an affinity for Ruby!!!')
    => post.drops
    => PostDrops

These features significantly simplify the management of Drops within your application, streamlining the process and enhancing flexibility.

Contributing

Fork it ( https://github.com/omarluq/activerecord_liquid_drops/fork )

Create your feature branch (git checkout -b my-new-feature)

Commit your changes (git commit -am 'feat: add some feature')

Push to the branch (git push origin my-new-feature)

Create a new Pull Request

License

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

activerecord_liquid_drops's People

Contributors

omarluq avatar

Watchers

 avatar

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.