diff --git a/modules/bcf/app/models/bcf/issue.rb b/modules/bcf/app/models/bcf/issue.rb index 29f245a420..ff881d6026 100644 --- a/modules/bcf/app/models/bcf/issue.rb +++ b/modules/bcf/app/models/bcf/issue.rb @@ -5,8 +5,6 @@ module Bcf belongs_to :work_package belongs_to :project - attr_reader(:markup_doc) - after_update :invalidate_markup_cache class << self @@ -21,9 +19,9 @@ module Bcf extract_first_node(priority_path, 'priority_text'), extract_first_node(status_path, 'status_text'), extract_first_node(assignee_path, 'assignee_text'), - extract_first_node('/Markup/Topic/DueDate/text()', 'due_date_text'), - extract_first_node('/Markup/Topic/Index/text()', 'index_text'), - extract_nodes('/Markup/Topic/Labels/text()', 'labels') + extract_first_node(due_date_path, 'due_date_text'), + extract_first_node(index_path, 'index_text'), + extract_nodes(labels_path, 'labels') end def title_path @@ -46,6 +44,18 @@ module Bcf '/Markup/Topic/AssignedTo/text()' end + def due_date_path + '/Markup/Topic/DueDate/text()' + end + + def index_path + '/Markup/Topic/Index/text()' + end + + def labels_path + '/Markup/Topic/Labels/text()' + end + private def extract_first_node(path, as) @@ -61,7 +71,7 @@ module Bcf if attributes.keys.include? 'title' self[:title] else - markup_doc.xpath(self.class.title_path).first.to_s + markup_doc.xpath(self.class.title_path).first.to_s.presence end end @@ -69,7 +79,63 @@ module Bcf if attributes.keys.include? 'description' self[:description] else - markup_doc.xpath(self.class.description_path).first.to_s + markup_doc.xpath(self.class.description_path).first.to_s.presence + end + end + + def priority_text + if attributes.keys.include? 'priority_text' + self[:priority_text] + else + markup_doc.xpath(self.class.priority_path).first.to_s.presence + end + end + + def status_text + if attributes.keys.include? 'status_text' + self[:status_text] + else + markup_doc.xpath(self.class.status_path).first.to_s.presence + end + end + + def status_text + if attributes.keys.include? 'status_text' + self[:status_text] + else + markup_doc.xpath(self.class.status_path).first.to_s.presence + end + end + + def assignee_text + if attributes.keys.include? 'assignee_text' + self[:assignee_text] + else + markup_doc.xpath(self.class.assignee_path).first.to_s.presence + end + end + + def due_date_text + if attributes.keys.include? 'due_date_text' + self[:due_date_text] + else + markup_doc.xpath(self.class.due_date_path).first.to_s.presence + end + end + + def index_text + if attributes.keys.include? 'index_text' + self[:index_text] + else + markup_doc.xpath(self.class.index_path).first.to_s.presence + end + end + + def labels + if attributes.keys.include? 'labels' + self[:labels] + else + markup_doc.xpath(self.class.labels_path).map(&:to_s) end end diff --git a/modules/bcf/spec/models/bcf/issue_spec.rb b/modules/bcf/spec/models/bcf/issue_spec.rb index 0b55180bf8..bdf443d746 100644 --- a/modules/bcf/spec/models/bcf/issue_spec.rb +++ b/modules/bcf/spec/models/bcf/issue_spec.rb @@ -128,20 +128,33 @@ describe ::Bcf::Issue, type: :model do end context '#markup_doc' do + subject { issue } + it "returns a Nokogiri::XML::Document" do - expect(issue.markup_doc).to be_a Nokogiri::XML::Document + expect(subject.markup_doc).to be_a Nokogiri::XML::Document end it "caches the document" do - first_fetched_doc = issue.markup_doc - expect(issue.markup_doc).to be_eql(first_fetched_doc) + first_fetched_doc = subject.markup_doc + expect(subject.markup_doc).to be_eql(first_fetched_doc) end it "invalidates the cache after an update of the issue" do - first_fetched_doc = issue.markup_doc - issue.markup = issue.markup + ' ' - issue.save - expect(issue.markup_doc).to_not be_eql(first_fetched_doc) + first_fetched_doc = subject.markup_doc + subject.markup = subject.markup + ' ' + subject.save + expect(subject.markup_doc).to_not be_eql(first_fetched_doc) + end + + it "provides attributes" do + expect(subject.title).to be_eql 'Maximum Content' + expect(subject.description).to be_eql 'This is a topic with all informations present.' + expect(subject.priority_text).to be_eql 'High' + expect(subject.status_text).to be_eql 'Open' + expect(subject.assignee_text).to be_eql 'andy@example.com' + expect(subject.index_text).to be_eql '0' + expect(subject.labels).to contain_exactly 'Structural', 'IT Development' + expect(subject.due_date_text).to be_nil end end end