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

[Jupiter] Refactor thumbnail generation code #983

Closed
murny opened this issue Jan 15, 2019 · 1 comment
Closed

[Jupiter] Refactor thumbnail generation code #983

murny opened this issue Jan 15, 2019 · 1 comment

Comments

@murny
Copy link
Contributor

murny commented Jan 15, 2019

Noticed this when working on this issue: #982.

Some refactors could be had here. We basically got 5 places where thumbnail generation happens (communities, items, theses, draft items and draft theses). Some are done in the model, others done in the view. Lots of repeated code across the application.

These two views for example are exactly the same:
https://github.com/ualbertalib/jupiter/blob/c88f89cabb034deac19f0872c1d41e4d2ea4103b/app/views/admin/theses/draft/_thumbnail.html.erb
https://github.com/ualbertalib/jupiter/blob/c88f89cabb034deac19f0872c1d41e4d2ea4103b/app/views/items/draft/_thumbnail.html.erb

These code blocks are pretty similar:

# compatibility with item thumbnail API
def thumbnail_url(args = { resize: '100x100', auto_orient: true })
return nil if logo_attachment.blank?
Rails.application.routes.url_helpers.rails_representation_path(logo_attachment.variant(args).processed)
end

# compatibility with the thumbnail API used in Items/Theses and Communities
def thumbnail_url(args = { resize: '100x100', auto_orient: true })
return nil unless thumbnail.present? && thumbnail.blob.present?
Rails.application.routes.url_helpers.rails_representation_path(thumbnail.variant(args).processed)
rescue ActiveStorage::InvariableError
begin
Rails.application.routes.url_helpers.rails_representation_path(thumbnail.preview(args).processed)
rescue ActiveStorage::UnpreviewableError
return nil
end
end

def thumbnail_url(args = { resize: '100x100', auto_orient: true })
logo = files_attachment_shim.logo_file
return nil if logo.blank?
Rails.application.routes.url_helpers.rails_representation_path(logo.variant(args).processed)
rescue ActiveStorage::InvariableError
begin
Rails.application.routes.url_helpers.rails_representation_path(logo.preview(args).processed)
rescue ActiveStorage::UnpreviewableError
return nil
end
end

Be nice if we could DRY up a lot of this duplicated code (maybe move it into a concern/helper etc?). Or just reevaluating what this code does and why we need it in the model etc? Maybe we can all share the same thumbnail partial everywhere?

@weiweishi weiweishi changed the title Refactor thumbnail generation code [Jupiter] Refactor thumbnail generation code Feb 8, 2019
@mbarnett
Copy link
Contributor

Think this is now being tracked on #1343

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

No branches or pull requests

2 participants