Class: Pulitzer::Version

Inherits:
ApplicationRecord show all
Includes:
ForeignOffice::Broadcaster
Defined in:
app/models/pulitzer/version.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationRecord

assoc_name, attrs_name, convert_hash_to_nested

Instance Attribute Details

#processed_element_countObject

Returns the value of attribute processed_element_count.



15
16
17
# File 'app/models/pulitzer/version.rb', line 15

def processed_element_count
  @processed_element_count
end

Class Method Details

.convert_nested_assoc(json_hash) ⇒ Object



32
33
34
35
36
37
38
39
# File 'app/models/pulitzer/version.rb', line 32

def self.convert_nested_assoc(json_hash)
  json_hash[attrs_name].map!{|v_attrs|
    new_attrs = Pulitzer::ContentElement.convert_hash_to_nested v_attrs
    new_attrs = Pulitzer::FreeFormSection.convert_hash_to_nested new_attrs
    new_attrs = Pulitzer::PostTag.convert_hash_to_nested new_attrs
  }
  json_hash
end

.export_configObject



21
22
23
24
25
26
27
28
29
30
# File 'app/models/pulitzer/version.rb', line 21

def self.export_config
  {
    except: [:id, :post_id, :created_at, :updated_at, :cloning_status],
    include: {
      content_elements: ContentElement.export_config,
      free_form_sections: FreeFormSection.export_config,
      post_tags: PostTag.export_config
    }
  }
end

Instance Method Details

#all_elementsObject



61
62
63
# File 'app/models/pulitzer/version.rb', line 61

def all_elements
  (content_elements.to_a + free_form_sections.to_a).sort_by{|e| e.sort_order || 0}
end

#content_element(label) ⇒ Object



53
54
55
# File 'app/models/pulitzer/version.rb', line 53

def content_element(label)
  self.content_elements.to_a.detect{|ce| ce.label == label}
end

#has_label(label) ⇒ Object



45
46
47
# File 'app/models/pulitzer/version.rb', line 45

def has_label(label)
  .to_a.select{|pt| pt.label_type == label.class.name && pt.label_id == label.id}.any?
end

#has_label_type(label_type) ⇒ Object



41
42
43
# File 'app/models/pulitzer/version.rb', line 41

def has_label_type(label_type)
  .to_a.select{|pt| pt.label_type == label_type}.any?
end

#missing_required_content_elementsObject



82
83
84
# File 'app/models/pulitzer/version.rb', line 82

def missing_required_content_elements
  @missing_content_elements ||= content_elements.to_a.select{|ce| ce.empty_body? && ce.required?}
end

#missing_required_content_elements?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'app/models/pulitzer/version.rb', line 90

def missing_required_content_elements?
  missing_required_partial_elements.any? || missing_required_content_elements.any? || missing_required_partial_types.any?
end

#missing_required_partial_elementsObject



86
87
88
# File 'app/models/pulitzer/version.rb', line 86

def missing_required_partial_elements
  @missing_partial_elements ||= free_form_sections.to_a.map(&:partials).flatten.map(&:content_elements).flatten.select{|ce| ce.empty_body? && ce.required?}
end

#missing_required_partial_typesObject



94
95
96
97
98
99
100
101
102
103
104
# File 'app/models/pulitzer/version.rb', line 94

def missing_required_partial_types
  missing_required_partial_types = []
  ffsts = post.post_type_version.free_form_section_types
  ffsts.each do |ffst|
    ffs = free_form_sections.find_by(name: ffst.name)
    existing_partial_post_types = ffs&.partials&.map(&:post_type_version_id)
    existing_partial_post_types ||= []
    missing_required_partial_types += ffst.partial_types.to_a.select{|pt| existing_partial_post_types.exclude? pt.post_type_version_id}
  end
  missing_required_partial_types
end

#missing_requirement_messagesObject



106
107
108
109
110
# File 'app/models/pulitzer/version.rb', line 106

def missing_requirement_messages
  missing_required_content_elements.map{|ce| "#{ce.label} is required"} +
  missing_required_partial_elements.map{|ce| "#{ce.partial.free_form_section.name} -> #{ce.partial.label} -> #{ce.label} is required"} +
  missing_required_partial_types.map{|pt| "#{pt.free_form_section_type.name} -> #{pt.post_type_version.name} is required"}
end

#post_tags_for(label_type) ⇒ Object



49
50
51
# File 'app/models/pulitzer/version.rb', line 49

def (label_type)
  .to_a.select{|pt| pt.label_type == label_type}
end

#publishable?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'app/models/pulitzer/version.rb', line 78

def publishable?
  !missing_required_content_elements?
end

#section(name) ⇒ Object



57
58
59
# File 'app/models/pulitzer/version.rb', line 57

def section(name)
  self.free_form_sections.to_a.detect{|ffs| ffs.name == name}
end

#serializeObject



73
74
75
76
# File 'app/models/pulitzer/version.rb', line 73

def serialize
  self.attributes.merge \
    processed_element_count: self.processed_element_count
end

#template_content_elementsObject



65
66
67
# File 'app/models/pulitzer/version.rb', line 65

def template_content_elements
  content_elements.template
end

#total_processing_elementsObject



69
70
71
# File 'app/models/pulitzer/version.rb', line 69

def total_processing_elements
  active_version.content_elements.count + active_version..count + active_version.free_form_sections.count + 2
end