Module: CustomFields::Types::File::Target::ClassMethods
- Defined in:
- lib/custom_fields/types/file.rb
Instance Method Summary collapse
-
#apply_file_custom_field(klass, rule) ⇒ Object
Adds a file field (using carrierwave).
-
#file_attribute_get(instance, name) ⇒ Hash
Build a hash storing the url for a file custom field of an instance.
-
#file_attribute_set(instance, name, attributes) ⇒ Object
Set the value for the instance and the file field specified by the 2 params.
Instance Method Details
#apply_file_custom_field(klass, rule) ⇒ Object
Adds a file field (using carrierwave)
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/custom_fields/types/file.rb', line 17 def apply_file_custom_field(klass, rule) name = rule['name'] klass.mount_uploader name, FileUploader klass.field :"#{name}_size", type: ::Hash, default: {} klass.replace_field name, ::String, true if rule['localized'] == true return unless rule['required'] # FIXME: previously, we called "klass.validates_presence_of name" # but it didn't work well with localized fields. klass.validate do |object| UploaderPresenceValidator.new(object, name).validate end end |
#file_attribute_get(instance, name) ⇒ Hash
Build a hash storing the url for a file custom field of an instance.
41 42 43 44 45 46 47 48 |
# File 'lib/custom_fields/types/file.rb', line 41 def file_attribute_get(instance, name) if instance.send(:"#{name}?") # " value = instance.send(name.to_sym).url { name => value, "#{name}_url" => value } else {} end end |
#file_attribute_set(instance, name, attributes) ⇒ Object
Set the value for the instance and the file field specified by the 2 params.
57 58 59 60 61 |
# File 'lib/custom_fields/types/file.rb', line 57 def file_attribute_set(instance, name, attributes) [name, "remote_#{name}_url", "remove_#{name}"].each do |_name| default_attribute_set(instance, _name, attributes) end.compact end |