Skip to content

Instantly share code, notes, and snippets.

@5minpause
Created August 21, 2015 12:48
Show Gist options
  • Save 5minpause/c899b2feb5796af39960 to your computer and use it in GitHub Desktop.
Save 5minpause/c899b2feb5796af39960 to your computer and use it in GitHub Desktop.
After optimizations
collection_action :load_overviewtree_as_json do
if params[:root_id].present?
objects = Goldencobra::Article.where(id: params[:root_id]).first.children.reorder(:url_name)
cache_key ||= ["articles", params[:root_id], objects.map(&:id), objects.maximum(:updated_at)]
articles = Rails.cache.fetch(cache_key) do
Goldencobra::Article.find(params[:root_id])
.children.reorder(:url_name).as_json(only: [:id, :url_path, :title, :url_name],
methods: [:has_children, :restricted])
end
else
objects = Goldencobra::Article.reorder(:url_name).roots
cache_key ||= ["articles", objects.map(&:id), objects.maximum(:updated_at)]
articles = Rails.cache.fetch(cache_key) do
Goldencobra::Article.order(:url_name).roots.as_json(only: [:id, :url_path, :title, :url_name],
methods: [:has_children, :restricted])
end
end
render json: Oj.dump({"articles" => articles})
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment