Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Best practice for safe dynamic drops? #763

Open
kieraneglin opened this issue Jun 10, 2016 · 1 comment
Open

Best practice for safe dynamic drops? #763

kieraneglin opened this issue Jun 10, 2016 · 1 comment

Comments

@kieraneglin
Copy link

Given models User and Publications where User has_many: publications, I need to iterate over a user's publications by calling {% for publication in user.publications %} and have access to methods defined in PublicationDrop.

user.publications is defined in the UserDrop class as just @user.publications.to_a.

For clarity, here is pseudocode of what I want to do:

Publications Authored by {{ user.full_name}}:

{% for publication in user.publications %}

  # These methods should come from PublicationDrop
  {{ publication.title }}

  # Displays 'No Comments' if 0, otherwise displays number of comments
  {{ publication.comment_count }}
{% endfor %}

I saw #64, and I'm sure I could adapt that to my needs. However, it is said in that thread that this method 'can have a huge security and performance impact'.

What I'm wondering is if the method described in #64 is still the best method, and if not, what is?

@pushrax
Copy link
Contributor

pushrax commented Jun 11, 2016

If you define Publication#to_liquid to return a PublicationDrop then the iteration should work automatically. When you access {{ publication.title }}, you are effectively accessing publication.to_liquid.title.to_liquid.

I'm not sure if I missed something unique about this question, feel free to follow up.

Performance note: as hinted above, to_liquid is called every time a variable is accessed, so if your to_liquid is expensive to compute then it's a good idea to memoize it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants