Class: Voltron::Upload::Field::UploadField
- Inherits:
-
Object
- Object
- Voltron::Upload::Field::UploadField
- Includes:
- ActionDispatch::Routing::PolymorphicRoutes, ActionView::Helpers::TextHelper
- Defined in:
- lib/voltron/upload/action_view/field.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#template ⇒ Object
readonly
Returns the value of attribute template.
Instance Method Summary collapse
- #accept ⇒ Object
- #caches ⇒ Object
- #files ⇒ Object
-
#has_preview_markup? ⇒ Boolean
Eventually, consider utilizing Nokogiri to detect whether content also is actually HTML markup Right now the overhead and frustration of that gem is not worth it.
- #has_preview_template? ⇒ Boolean
-
#initialize(model, method, template, options) ⇒ UploadField
constructor
A new instance of UploadField.
-
#input_name ⇒ Object
def add_preview_class options ||= ” classes = options.split(/s+/) classes << “dz-layout-##preview_name” options = classes.join(‘ ’) end.
- #multiple? ⇒ Boolean
- #prepare ⇒ Object
- #preview ⇒ Object
- #preview_markup ⇒ Object
-
#preview_name ⇒ Object
Strip tags, they cause problems in the lookup_context
exists?
andrender_to_string
. - #preview_options ⇒ Object
- #removals ⇒ Object
Constructor Details
#initialize(model, method, template, options) ⇒ UploadField
Returns a new instance of UploadField.
31 32 33 34 35 36 37 |
# File 'lib/voltron/upload/action_view/field.rb', line 31 def initialize(model, method, template, ) @model = model @method = method.to_sym @template = template @options = .with_indifferent_access prepare end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
29 30 31 |
# File 'lib/voltron/upload/action_view/field.rb', line 29 def @options end |
#template ⇒ Object (readonly)
Returns the value of attribute template.
29 30 31 |
# File 'lib/voltron/upload/action_view/field.rb', line 29 def template @template end |
Instance Method Details
#accept ⇒ Object
92 93 94 |
# File 'lib/voltron/upload/action_view/field.rb', line 92 def accept @accept ||= .delete(:accept).to_s end |
#caches ⇒ Object
136 137 138 139 140 141 142 143 |
# File 'lib/voltron/upload/action_view/field.rb', line 136 def caches if multiple? cache = @model.send("cache_#{@method}") rescue [] Array.wrap(cache) else Array.wrap(@model.send("cache_#{@method}")) end end |
#files ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/voltron/upload/action_view/field.rb', line 121 def files # If set to not preserve files, return an empty array so nothing is shown return [] if [:preserve] === false # Return an array of files' json data uploads = Array.wrap(@model.send(@method)).map(&:to_upload_json).reject(&:blank?) # Remove all uploads from the array that have been flagged for removal removals.each do |removal| uploads.reject! { |upload| upload[:id] == removal } end uploads end |
#has_preview_markup? ⇒ Boolean
Eventually, consider utilizing Nokogiri to detect whether content also is actually HTML markup Right now the overhead and frustration of that gem is not worth it
102 103 104 |
# File 'lib/voltron/upload/action_view/field.rb', line 102 def has_preview_markup? preview.present? end |
#has_preview_template? ⇒ Boolean
96 97 98 |
# File 'lib/voltron/upload/action_view/field.rb', line 96 def has_preview_template? preview_name.present? && template.lookup_context.exists?(preview_name, 'voltron/upload/preview', true) end |
#input_name ⇒ Object
def add_preview_class
[:class] ||= ''
classes = [:class].split(/\s+/)
classes << "dz-layout-#{preview_name}"
[:class] = classes.join(' ')
end
113 114 115 |
# File 'lib/voltron/upload/action_view/field.rb', line 113 def input_name ActionView::Helpers::Tags::Base.new(ActiveModel::Naming.param_key(@model), @method, nil).send(:tag_name) + (multiple? ? '[]' : '') end |
#multiple? ⇒ Boolean
117 118 119 |
# File 'lib/voltron/upload/action_view/field.rb', line 117 def multiple? @model.respond_to?("#{@method}_urls") end |
#prepare ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/voltron/upload/action_view/field.rb', line 39 def prepare #add_preview_class if has_preview_template? .merge!({ ':multiple' => multiple?, ':files' => files.to_json, ':cached' => caches.to_json, ':removed' => removals.to_json, ':upload_options' => .to_json, 'accept' => accept, 'preview' => preview_name, 'param' => input_name, 'url' => polymorphic_path(@model.class, action: :upload) }) #options[:data] ||= {} #options[:data].merge!({ # upload_files: files, # upload_cache: caches, # upload_remove: removals, # upload_options: preview_options #}) end |
#preview ⇒ Object
88 89 90 |
# File 'lib/voltron/upload/action_view/field.rb', line 88 def preview @preview ||= .delete(:preview).to_s end |
#preview_markup ⇒ Object
73 74 75 76 77 78 79 80 81 |
# File 'lib/voltron/upload/action_view/field.rb', line 73 def preview_markup if has_preview_template? # Fetch the html found in the partial provided ActionController::Base.new.render_to_string(partial: "voltron/upload/preview/#{preview_name}").squish elsif has_preview_markup? # If not blank, value of +preview+ is likely (should be) raw html, in which case, just return that markup preview.squish end end |
#preview_name ⇒ Object
Strip tags, they cause problems in the lookup_context exists?
and render_to_string
84 85 86 |
# File 'lib/voltron/upload/action_view/field.rb', line 84 def preview_name (preview) end |
#preview_options ⇒ Object
63 64 65 66 67 68 69 70 71 |
# File 'lib/voltron/upload/action_view/field.rb', line 63 def previews = Voltron.config.upload.previews || {} opts = previews.with_indifferent_access.try(:[], preview_name) || {} opts.merge!({ preview_template: preview_markup, }) opts.merge!(.delete(:options) || {}) opts.map { |k,v| { k.to_s.camelize(:lower) => v } }.reduce(Hash.new, :merge).compact end |
#removals ⇒ Object
145 146 147 |
# File 'lib/voltron/upload/action_view/field.rb', line 145 def removals Array.wrap(@model.send("remove_#{@method}")).compact.reject { |i| !i } end |