Sha256: 80dc3be2c6726fb8ac8fd3f4a92939a99e829752d5253f29d1f91236b15ebcdc

Contents?: true

Size: 1.36 KB

Versions: 9

Compression:

Stored size: 1.36 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)
        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)
      belongs_to attribute, class_name: "RailsAdminImageManager::File"
    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"

      before_validation { send("#{attribute}=", nil) if send("#{id}_deselect") == '1' }
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
rails_admin_image_manager-0.1.27 lib/rails_admin_image_manager/has_managed_file.rb
rails_admin_image_manager-0.1.26 lib/rails_admin_image_manager/has_managed_file.rb
rails_admin_image_manager-0.1.25 lib/rails_admin_image_manager/has_managed_file.rb
rails_admin_image_manager-0.1.24 lib/rails_admin_image_manager/has_managed_file.rb
rails_admin_image_manager-0.1.23 lib/rails_admin_image_manager/has_managed_file.rb
rails_admin_image_manager-0.1.22 lib/rails_admin_image_manager/has_managed_file.rb
rails_admin_image_manager-0.1.21 lib/rails_admin_image_manager/has_managed_file.rb
rails_admin_image_manager-0.1.20 lib/rails_admin_image_manager/has_managed_file.rb
rails_admin_image_manager-0.1.15 lib/rails_admin_image_manager/has_managed_file.rb