Integrates Liquid Templating Language Drops to Active Record
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
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']
.
We've included two helpful helpers:
-
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']
-
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.
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
The gem is available as open source under the terms of the MIT License.