module RailsAdminImageManager::HasManagedFile
Public Instance Methods
has_managed_file(attribute, id, options={})
click to toggle source
# File lib/rails_admin_image_manager/has_managed_file.rb, line 4 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
managed_file_is_mandatory?(attribute)
click to toggle source
# File lib/rails_admin_image_manager/has_managed_file.rb, line 18 def managed_file_is_mandatory?(attribute) validators_on(attribute).any?{|validator| validator.kind_of?(ActiveModel::Validations::PresenceValidator)} end
Private Instance Methods
add_managed_file_before_validation(attribute, id)
click to toggle source
# File lib/rails_admin_image_manager/has_managed_file.rb, line 40 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
add_managed_file_belongs_to(attribute, mandatory)
click to toggle source
# File lib/rails_admin_image_manager/has_managed_file.rb, line 28 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
add_managed_file_validates_presence_of(id)
click to toggle source
# File lib/rails_admin_image_manager/has_managed_file.rb, line 36 def add_managed_file_validates_presence_of(id) validates_presence_of id end
managed_file_options_mandatory(options)
click to toggle source
# File lib/rails_admin_image_manager/has_managed_file.rb, line 24 def managed_file_options_mandatory(options) options.key?(:mandatory) && options[:mandatory] == true ? true : false end