Last active
December 14, 2015 08:39
-
-
Save lucasrenan/5059556 to your computer and use it in GitHub Desktop.
m magick app_helper_old
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module MediaMagick | |
| module ApplicationHelper | |
| def attachment_container(model, relation, options = {}) | |
| data = data_attributes(model, relation, options) | |
| id = "#{model.class.to_s.downcase}-#{relation.to_s}" | |
| classes = "attachmentUploader #{relation.to_s}" | |
| content_tag :div, id: id, class: classes, data: data do | |
| if block_given? | |
| yield | |
| else | |
| options[:load_attachments] = true unless options[:load_attachments] == false | |
| render '/upload', partial_attributes(model, relation, options) | |
| end | |
| end | |
| end | |
| def attachment_container_for_video(model, relation, options = {}) | |
| data = data_attributes(model, relation, options) | |
| id = "#{model.class.to_s.downcase}-#{relation.to_s}" | |
| classes = "attachmentUploader attachmentVideoUploader #{relation.to_s}" | |
| content_tag :div, id: id, class: classes, data: data do | |
| if block_given? | |
| yield | |
| else | |
| render '/video', partial_attributes(model, relation, options) | |
| end | |
| end | |
| end | |
| private | |
| def get_partial_name(options) | |
| if options[:partial] | |
| options[:partial] | |
| else | |
| if options[:as] | |
| "/#{options[:as]}" | |
| else | |
| '/image' | |
| end | |
| end | |
| end | |
| def data_attributes(model, relation, options) | |
| data_attributes = { | |
| model: model.class.to_s, | |
| id: model.id.to_s, | |
| relation: relation.to_s | |
| } | |
| data_attributes.merge!(:partial => get_partial_name(options)) | |
| data_attributes.merge!(:embedded_in_id => options[:embedded_in].id.to_s, :embedded_in_model => options[:embedded_in].class.to_s) if options[:embedded_in] | |
| data_attributes | |
| end | |
| def partial_attributes(model, relation, options) | |
| partial_attributes = { | |
| model: model, | |
| relations: relation, | |
| newAttachments: options[:newAttachments] || {}, | |
| loadedAttachments: options[:loadedAttachments] || {}, | |
| load_attachments: options[:load_attachments] || false, | |
| partial: get_partial_name(options) | |
| } | |
| partial_attributes | |
| end | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ### Form View | |
| ``` erb | |
| <%= attachment_container @album, :files %> | |
| <%= attachment_container @album, :files, newAttachments: { class: 'thumbnails' }, loadedAttachments: { class: 'span3' } %> | |
| ``` | |
| or use a custom layout: | |
| ``` html | |
| <%= attachment_container @album, :photos do %> | |
| <a class="pickAttachments btn" href="javascript://">select files</a> | |
| <a class="uploadAttachments btn" href="javascript://">upload files</a> | |
| <ul class="loadedAttachments"> | |
| <%= render :partial => 'photos', :collection => @album.photos, :as => 'photo' %> | |
| </ul> | |
| <% end %> | |
| ``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment