Class: Lt::Lcms::Metadata::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/lt/lcms/metadata/context.rb

Constant Summary collapse

NUM_RE =
/\d+/.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context = {}) ⇒ Context

Returns a new instance of Context.



54
55
56
# File 'lib/lt/lcms/metadata/context.rb', line 54

def initialize(context = {})
  @context = context.with_indifferent_access
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



10
11
12
# File 'lib/lt/lcms/metadata/context.rb', line 10

def context
  @context
end

Class Method Details

.update_grades_level_position_for(grades) ⇒ Object

Fix level position for grades Is used inside ‘#find_or_create_resource` method



19
20
21
# File 'lib/lt/lcms/metadata/context.rb', line 19

def update_grades_level_position_for(grades)
  update_level_position_for(grades) { |g| ::Lcms::Engine::Grades.grades.index(g.['grade']) }
end

.update_level_position_for(resources) ⇒ Object

Fix level position for in case when lower curriculum elements have been created after higher ones: Grade 8 was created before Grade 7



44
45
46
47
48
49
50
51
# File 'lib/lt/lcms/metadata/context.rb', line 44

def update_level_position_for(resources)
  resources
    .map { |m| { id: m.id, idx: yield(m) } }
    .sort_by { |a| a[:idx] }.each_with_index do |data, idx|
      resource = ::Lcms::Engine::Resource.find(data[:id])
      resource.update_columns(level_position: idx) unless resource.level_position == idx
    end
end

.update_modules_level_position_for(modules) ⇒ Object

Fix level position for modules Is used inside ‘#find_or_create_resource` method



27
28
29
30
# File 'lib/lt/lcms/metadata/context.rb', line 27

def update_modules_level_position_for(modules)
  indexes = modules.map(&:short_title).sort_by { |g| g.to_s[NUM_RE].to_i }
  update_level_position_for(modules) { |m| indexes.index(m.['module']) }
end

.update_units_level_position_for(units) ⇒ Object

Fix level position for units Is used inside ‘#find_or_create_resource` method



36
37
38
# File 'lib/lt/lcms/metadata/context.rb', line 36

def update_units_level_position_for(units)
  update_level_position_for(units) { |u| u.['unit'][NUM_RE].to_i }
end

Instance Method Details

#directoryObject



58
59
60
# File 'lib/lt/lcms/metadata/context.rb', line 58

def directory
  @directory ||= [subject, grade, mod, unit, lesson].select(&:present?)
end

#find_or_create_resourceObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/lt/lcms/metadata/context.rb', line 73

def find_or_create_resource
  # rubocop:disable Metrics/BlockLength
  ::Lcms::Engine::Resource.with_advisory_lock('find_or_create_resource') do
    # if the resource exists, return it
    resource = ::Lcms::Engine::Resource.tree.find_by_directory(directory)
    return update(resource) if resource

    # else, build missing parents until we build the resource itself.
    parent = nil
    directory.each_with_index do |name, index|
      resource = ::Lcms::Engine::Resource.tree.find_by_directory(directory[0..index])
      if resource
        parent = resource
        next
      end

      resource = build_new_resource(parent, name, index)
      unless last_item?(index)
        resource.save!
        unless resource.subject?
          self.class.send("update_#{resource.curriculum_type}s_level_position_for", resource.self_and_siblings)
        end
        parent = resource
        next
      end

      if mid_assessment?
        set_mid_assessment_position(parent, resource)
      elsif prerequisite?
        set_prerequisite_position(parent, resource)
      elsif opr?
        set_opr_position(parent, resource)
      else
        set_lesson_position(parent, resource)
      end
    end

    update resource
  end
  # rubocop:enable Metrics/BlockLength
end

#metadataObject



62
63
64
65
66
67
68
69
70
71
# File 'lib/lt/lcms/metadata/context.rb', line 62

def 
  @metadata ||= {
    'subject' => subject,
    'grade' => grade,
    'module' => mod,
    'unit' => unit,
    'lesson' => lesson,
    'assessment' => (type if assessment?)
  }.compact.stringify_keys
end