Sha256: d72b26c38012b4b6c918e776d9b9849d3946e879ef307c99d802315131811f48
Contents?: true
Size: 1.75 KB
Versions: 2
Compression:
Stored size: 1.75 KB
Contents
module RailsAdminImageManager module HasManagedFile def has_managed_file(attribute, id, options={}) var_options_name = "@has_managed_file_#{attribute}_options" var_options_value = instance_variable_get(var_options_name) # If the same attribute is passed twice, we'll skip it if var_options_value.nil? instance_variable_set(var_options_name, options) add_managed_file_belongs_to(attribute, managed_file_options_mandatory(options)) add_managed_file_validates_presence_of(id) if managed_file_options_mandatory(options) add_managed_file_before_validation(attribute, id) unless managed_file_options_mandatory(options) end end def managed_file_is_mandatory?(attribute) validators_on(attribute).any?{|validator| validator.kind_of?(ActiveModel::Validations::PresenceValidator)} end private def managed_file_options_mandatory(options) options.key?(:mandatory) && options[:mandatory] == true ? true : false end def add_managed_file_belongs_to(attribute, mandatory) if Rails.version.to_i >= 5 belongs_to attribute, class_name: "RailsAdminImageManager::File", optional: !mandatory else belongs_to attribute, class_name: "RailsAdminImageManager::File", required: mandatory end end def add_managed_file_validates_presence_of(id) validates_presence_of id end def add_managed_file_before_validation(attribute, id) attr_accessor "#{id}_deselect" define_method "#{id}_deselect=" do |val| attribute_will_change!("#{id}_deselect") if val == '1' instance_variable_set("@#{id}_deselect", val) end before_validation { send("#{attribute}=", nil) if send("#{id}_deselect") == '1' } end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
rails_admin_image_manager-0.1.34 | lib/rails_admin_image_manager/has_managed_file.rb |
rails_admin_image_manager-0.1.33 | lib/rails_admin_image_manager/has_managed_file.rb |