Class: Section

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
AuthoredModel, SearchableModel, SluggedModel
Defined in:
app/models/section.rb

Overview

A control objective (inverse of risk)

The slug of a Section has to have the slug of the parent as prefix.

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Object) create_in_tree(params)

Create the section and assign the correct parent_id



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/models/section.rb', line 39

def self.create_in_tree(params)
  section = self.new(params)
  if section.parent_id.nil?
    section.parent = self.find_parent_by_slug(section.slug)
  end

  ActiveRecord::Base.transaction do
    if section.save
      self.find_descendents_by_slug(section.slug).each do |child|
        child.parent_id = section.id if child.parent_id == section.parent_id
      end
      section
    end
  end
end

+ (Object) find_ancestors_by_slug(slug)



55
56
57
58
59
60
61
62
63
64
# File 'app/models/section.rb', line 55

def self.find_ancestors_by_slug(slug)
  # Find all possible ancestor slugs
  slugs = []
  while slug.size > 0
    slugs.push(slug)
    slug = slugs.last[0, slugs.last.rindex(/\.|-|^/)]
  end

  self.where(:slug => slugs).all
end

+ (Object) find_descendents_by_slug(slug)



72
73
74
75
76
# File 'app/models/section.rb', line 72

def self.find_descendents_by_slug(slug)
  prefix_test = Regexp.new("^#{Regexp.escape(slug)}[$.-]")
  sections = self.where("#{table_name}.slug LIKE ?", "#{slug}%").all
  sections.select { |section| prefix_test.match(section.slug) }
end

+ (Object) find_parent_by_slug(slug)



66
67
68
69
70
# File 'app/models/section.rb', line 66

def self.find_parent_by_slug(slug)
  # Return ancestor section with longest slug (deepest section found)
  sections = self.find_ancestors_by_slug(slug)
  sections.max { |section| section.slug.size }
end

+ (Object) for_control(control)

All Sections that could be associated with a control (in same program)



79
80
81
# File 'app/models/section.rb', line 79

def self.for_control(control)
  where(:program_id => control.program_id).order(:slug)
end

+ (Object) for_system(s)

All Sections that could be associated with a system (any company Section)



84
85
86
# File 'app/models/section.rb', line 84

def self.for_system(s)
  self
end

Instance Method Details

- (Boolean) company?

Whether this Section is associated with a company "program"

Returns:

  • (Boolean)


89
90
91
# File 'app/models/section.rb', line 89

def company?
  program.company?
end

- (Object) consolidated_controls



107
108
109
110
111
# File 'app/models/section.rb', line 107

def consolidated_controls
  controls.includes(:implementing_controls).map do |control|
    control.implementing_controls.all
  end.flatten
end

- (Object) control_ids

Return ids of related Controls (used by many2many widget)



98
99
100
# File 'app/models/section.rb', line 98

def control_ids
  controls.map { |c| c.id }
end

- (Object) display_name



93
94
95
# File 'app/models/section.rb', line 93

def display_name
  "#{slug} - #{title}"
end

- (Object) linked_controls



113
114
115
116
117
# File 'app/models/section.rb', line 113

def linked_controls
  controls.includes(:implementing_controls).map do |control|
    [control] + control.implementing_controls.all
  end.flatten
end

- (Object) system_ids

Return ids of related Systems (used by many2many widget)



103
104
105
# File 'app/models/section.rb', line 103

def system_ids
  systems.map { |s| s.id }
end

- (Object) update_child_parent_ids



29
30
31
32
33
34
35
36
# File 'app/models/section.rb', line 29

def update_child_parent_ids
  ActiveRecord::Base.transaction do
    self.class.find_descendents_by_slug(slug).each do |child|
      child.parent_id = id if child.parent_id == parent_id
      child.save
    end
  end
end

- (Object) update_parent_id



25
26
27
# File 'app/models/section.rb', line 25

def update_parent_id
  self.parent = self.class.find_parent_by_slug(slug) if parent_id.nil?
end