codenoble / cache-crispies

Speedy Rails JSON serialization with built-in caching
MIT License
156 stars 14 forks source link

Conditionally render attributes DSL #31

Closed uxxman closed 3 years ago

uxxman commented 3 years ago

First of all great work. Great performance gains with clear and concise DSL. 👍

Currently the function for conditionally rendering attributes can easily get out of hand if you have a few checks. They don't quite feel right inside a proc. example

show_if -> (model, options) { model.published? && options[:current_user].admin? && options[:current_user].active? && options[:current_user].id == model.user_id } do
  serialize :published_at
end

Can we have something like rails callback filters, the above example would become

show if: :include_published_at? do
  serialize :published_at
end

def include_published_at?
  model.published? &&
  options[:current_user].admin?  &&
  options[:current_user].active? &&
  options[:current_user].id == model.user_id
end
adamcrown commented 3 years ago

Thanks for the PR. It's merged so I'm closing this.