Class: Releaf::ResourceUtilities

Inherits:
Object
  • Object
show all
Defined in:
app/lib/releaf/resource_utilities.rb

Class Method Summary collapse

Class Method Details

.association_controller(association) ⇒ Object

Attempts to guess associated controllers name

Returns:

  • controller name


28
29
30
31
# File 'app/lib/releaf/resource_utilities.rb', line 28

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

.destroyable?(resource) ⇒ Boolean

Returns:

  • (Boolean)

33
34
35
# File 'app/lib/releaf/resource_utilities.rb', line 33

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

.restricted_association?(resource, association) ⇒ Boolean

Returns:

  • (Boolean)

21
22
23
# File 'app/lib/releaf/resource_utilities.rb', line 21

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

.restricted_associations(resource) ⇒ Object


15
16
17
18
19
# File 'app/lib/releaf/resource_utilities.rb', line 15

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

.restricted_relations(resource) ⇒ Object

Lists relations for @resource with dependent: :restrict_with_exception

Returns:

  • hash of all related objects, who have dependancy :restrict_with_exception


5
6
7
8
9
10
11
12
13
# File 'app/lib/releaf/resource_utilities.rb', line 5

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