Sha256: 87b6cd1663f0c21347176ca8baccb3d3b436dc696fd5139f5f0d7752065aaa5e

Contents?: true

Size: 1.26 KB

Versions: 43

Compression:

Stored size: 1.26 KB

Contents

class Releaf::ResourceUtilities
    # Lists relations for @resource with dependent: :restrict_with_exception
    #
    # @return hash of all related objects, who have dependancy :restrict_with_exception
  def self.restricted_relations(resource)
    restricted_associations(resource).inject({}) do|relations, association|
      relations[association.name.to_sym] = {
        objects: resource.send(association.name),
        controller: association_controller(association)
      }
      relations
    end
  end

  def self.restricted_associations(resource)
    resource.class.reflect_on_all_associations.select do |association|
      restricted_association?(resource, association)
    end
  end

  def self.restricted_association?(resource, association)
    association.options[:dependent] == :restrict_with_exception && resource.send(association.name).exists?
  end

  # Attempts to guess associated controllers name
  #
  # @return controller name
  def self.association_controller(association)
    guessed_name = association.name.to_s.pluralize
    guessed_name if Releaf.application.config.controllers.values.map { |v| v.controller_name }.grep(/(\/#{guessed_name}$|^#{guessed_name}$)/).present?
  end

  def self.destroyable?(resource)
    restricted_associations(resource).empty?
  end
end

Version data entries

43 entries across 43 versions & 1 rubygems

Version Path
releaf-core-3.1.0 app/lib/releaf/resource_utilities.rb
releaf-core-3.0.3 app/lib/releaf/resource_utilities.rb
releaf-core-3.0.2 app/lib/releaf/resource_utilities.rb
releaf-core-3.0.1 app/lib/releaf/resource_utilities.rb
releaf-core-3.0.0 app/lib/releaf/resource_utilities.rb
releaf-core-2.2.1 app/lib/releaf/resource_utilities.rb
releaf-core-2.2.0 app/lib/releaf/resource_utilities.rb
releaf-core-2.1.2 app/lib/releaf/resource_utilities.rb
releaf-core-2.1.1 app/lib/releaf/resource_utilities.rb
releaf-core-2.1.0 app/lib/releaf/resource_utilities.rb
releaf-core-2.0.1 app/lib/releaf/resource_utilities.rb
releaf-core-2.0.0 app/lib/releaf/resource_utilities.rb
releaf-core-1.1.22 app/lib/releaf/resource_utilities.rb
releaf-core-1.1.21 app/lib/releaf/resource_utilities.rb
releaf-core-1.1.20 app/lib/releaf/resource_utilities.rb
releaf-core-1.1.19 app/lib/releaf/resource_utilities.rb
releaf-core-1.1.18 app/lib/releaf/resource_utilities.rb
releaf-core-1.1.17 app/lib/releaf/resource_utilities.rb
releaf-core-1.1.16 app/lib/releaf/resource_utilities.rb
releaf-core-1.1.15 app/lib/releaf/resource_utilities.rb