Class: Label

Inherits:
ApplicationRecord show all
Includes:
Housekeeping, Shared::Depictions, Shared::IsData, Shared::Notes, Shared::PolymorphicAnnotator, Shared::Tags
Defined in:
app/models/label.rb

Overview

Text to be printed.

Direct Known Subclasses

Code128, Generated, QrCode

Defined Under Namespace

Classes: Code128, Generated, QrCode

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Shared::IsData

#errors_excepting, #full_error_messages_excepting, #identical, #is_community?, #is_destroyable?, #is_editable?, #is_in_use?, #is_in_users_projects?, #metamorphosize, #similar

Methods included from Shared::PolymorphicAnnotator

#annotated_object_is_persisted?

Methods included from Shared::Depictions

#has_depictions?, #image_array=, #reject_depictions, #reject_images

Methods included from Shared::Tags

#reject_tags, #tag_with, #tagged?, #tagged_with?

Methods included from Shared::Notes

#concatenated_notes_string, #reject_notes

Methods included from Housekeeping

#has_polymorphic_relationship?

Methods inherited from ApplicationRecord

transaction_with_retry

Instance Attribute Details

#is_copy_editedBoolean?

Returns A curator assertion that the label has been checked and is ready for print. Not required prior to printing.

Returns:

  • (Boolean, nil)

    A curator assertion that the label has been checked and is ready for print. Not required prior to printing.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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
114
115
116
117
118
119
120
121
122
123
# File 'app/models/label.rb', line 31

class Label < ApplicationRecord

  include Housekeeping
  include Shared::Notes
  include Shared::Tags
  include Shared::Depictions
  include Shared::PolymorphicAnnotator
  include Shared::IsData

  polymorphic_annotates('label_object', presence_validate: false)

  ignore_whitespace_on(:text)

  attr_accessor :text_method

  before_validation :stub_text, if: Proc.new { |c| c.text_method.present? }

  after_save :set_text, if: Proc.new { |c| c.text_method.present? }

  validates_presence_of :text, :total

  scope :unprinted, -> { where(is_printed: false) }

  def is_generated?
    false
  end

  def self.batch_create(
    collecting_event_query_params, label_attribute, total, preview
  )
    q = Queries::CollectingEvent::Filter.new(collecting_event_query_params).all

    label_attribute = (label_attribute || '').to_sym
    if ![:verbatim_label, :document_label, :print_label].include?(label_attribute)
      raise TaxonWorks::Error, "Invalid label choice: '#{label_attribute}'"
    end

    max = 1_000
    if q.count > max
      raise TaxonWorks::Error, "At most #{max} labels can be created at once"
    end

    total = [total, 1].max

    r = BatchResponse.new({
      preview:,
      async: false,
      total_attempted: q.count
    })
    self.transaction do
      begin
        q.pluck(:id, label_attribute).each do |ce_id, label_text|
          if label_text.blank?
            r.not_updated.push ce_id
            next
          end

          begin
            Label.create!(
              text: label_text,
              total:,
              label_object_id: ce_id,
              label_object_type: 'CollectingEvent',
            )
            r.updated.push ce_id
          rescue ActiveRecord::RecordInvalid => e
            # Probably never occurs for Label given our pre-checks
            r.not_updated.push e.record.id

            r.errors[e.message] = 0 unless r.errors[e.message]
            r.errors[e.message] += 1
          end
        end

      end

      raise ActiveRecord::Rollback if r.preview
    end # end transaction

    r
  end

  protected

  def set_text
    update_column(:text, label_object.reload.send(text_method.to_sym))
  end

  def stub_text
    assign_attributes(text:  'STUB')
  end

end

#is_printedBoolean?

Returns When true the label has been sent to the printed, as asserted by the curator.

Returns:

  • (Boolean, nil)

    When true the label has been sent to the printed, as asserted by the curator.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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
114
115
116
117
118
119
120
121
122
123
# File 'app/models/label.rb', line 31

class Label < ApplicationRecord

  include Housekeeping
  include Shared::Notes
  include Shared::Tags
  include Shared::Depictions
  include Shared::PolymorphicAnnotator
  include Shared::IsData

  polymorphic_annotates('label_object', presence_validate: false)

  ignore_whitespace_on(:text)

  attr_accessor :text_method

  before_validation :stub_text, if: Proc.new { |c| c.text_method.present? }

  after_save :set_text, if: Proc.new { |c| c.text_method.present? }

  validates_presence_of :text, :total

  scope :unprinted, -> { where(is_printed: false) }

  def is_generated?
    false
  end

  def self.batch_create(
    collecting_event_query_params, label_attribute, total, preview
  )
    q = Queries::CollectingEvent::Filter.new(collecting_event_query_params).all

    label_attribute = (label_attribute || '').to_sym
    if ![:verbatim_label, :document_label, :print_label].include?(label_attribute)
      raise TaxonWorks::Error, "Invalid label choice: '#{label_attribute}'"
    end

    max = 1_000
    if q.count > max
      raise TaxonWorks::Error, "At most #{max} labels can be created at once"
    end

    total = [total, 1].max

    r = BatchResponse.new({
      preview:,
      async: false,
      total_attempted: q.count
    })
    self.transaction do
      begin
        q.pluck(:id, label_attribute).each do |ce_id, label_text|
          if label_text.blank?
            r.not_updated.push ce_id
            next
          end

          begin
            Label.create!(
              text: label_text,
              total:,
              label_object_id: ce_id,
              label_object_type: 'CollectingEvent',
            )
            r.updated.push ce_id
          rescue ActiveRecord::RecordInvalid => e
            # Probably never occurs for Label given our pre-checks
            r.not_updated.push e.record.id

            r.errors[e.message] = 0 unless r.errors[e.message]
            r.errors[e.message] += 1
          end
        end

      end

      raise ActiveRecord::Rollback if r.preview
    end # end transaction

    r
  end

  protected

  def set_text
    update_column(:text, label_object.reload.send(text_method.to_sym))
  end

  def stub_text
    assign_attributes(text:  'STUB')
  end

end

#label_object_idString

Returns Polymorphic id.

Returns:

  • (String)

    Polymorphic id



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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
114
115
116
117
118
119
120
121
122
123
# File 'app/models/label.rb', line 31

class Label < ApplicationRecord

  include Housekeeping
  include Shared::Notes
  include Shared::Tags
  include Shared::Depictions
  include Shared::PolymorphicAnnotator
  include Shared::IsData

  polymorphic_annotates('label_object', presence_validate: false)

  ignore_whitespace_on(:text)

  attr_accessor :text_method

  before_validation :stub_text, if: Proc.new { |c| c.text_method.present? }

  after_save :set_text, if: Proc.new { |c| c.text_method.present? }

  validates_presence_of :text, :total

  scope :unprinted, -> { where(is_printed: false) }

  def is_generated?
    false
  end

  def self.batch_create(
    collecting_event_query_params, label_attribute, total, preview
  )
    q = Queries::CollectingEvent::Filter.new(collecting_event_query_params).all

    label_attribute = (label_attribute || '').to_sym
    if ![:verbatim_label, :document_label, :print_label].include?(label_attribute)
      raise TaxonWorks::Error, "Invalid label choice: '#{label_attribute}'"
    end

    max = 1_000
    if q.count > max
      raise TaxonWorks::Error, "At most #{max} labels can be created at once"
    end

    total = [total, 1].max

    r = BatchResponse.new({
      preview:,
      async: false,
      total_attempted: q.count
    })
    self.transaction do
      begin
        q.pluck(:id, label_attribute).each do |ce_id, label_text|
          if label_text.blank?
            r.not_updated.push ce_id
            next
          end

          begin
            Label.create!(
              text: label_text,
              total:,
              label_object_id: ce_id,
              label_object_type: 'CollectingEvent',
            )
            r.updated.push ce_id
          rescue ActiveRecord::RecordInvalid => e
            # Probably never occurs for Label given our pre-checks
            r.not_updated.push e.record.id

            r.errors[e.message] = 0 unless r.errors[e.message]
            r.errors[e.message] += 1
          end
        end

      end

      raise ActiveRecord::Rollback if r.preview
    end # end transaction

    r
  end

  protected

  def set_text
    update_column(:text, label_object.reload.send(text_method.to_sym))
  end

  def stub_text
    assign_attributes(text:  'STUB')
  end

end

#label_object_typeString

Returns Polymorphic type.

Returns:

  • (String)

    Polymorphic type



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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
114
115
116
117
118
119
120
121
122
123
# File 'app/models/label.rb', line 31

class Label < ApplicationRecord

  include Housekeeping
  include Shared::Notes
  include Shared::Tags
  include Shared::Depictions
  include Shared::PolymorphicAnnotator
  include Shared::IsData

  polymorphic_annotates('label_object', presence_validate: false)

  ignore_whitespace_on(:text)

  attr_accessor :text_method

  before_validation :stub_text, if: Proc.new { |c| c.text_method.present? }

  after_save :set_text, if: Proc.new { |c| c.text_method.present? }

  validates_presence_of :text, :total

  scope :unprinted, -> { where(is_printed: false) }

  def is_generated?
    false
  end

  def self.batch_create(
    collecting_event_query_params, label_attribute, total, preview
  )
    q = Queries::CollectingEvent::Filter.new(collecting_event_query_params).all

    label_attribute = (label_attribute || '').to_sym
    if ![:verbatim_label, :document_label, :print_label].include?(label_attribute)
      raise TaxonWorks::Error, "Invalid label choice: '#{label_attribute}'"
    end

    max = 1_000
    if q.count > max
      raise TaxonWorks::Error, "At most #{max} labels can be created at once"
    end

    total = [total, 1].max

    r = BatchResponse.new({
      preview:,
      async: false,
      total_attempted: q.count
    })
    self.transaction do
      begin
        q.pluck(:id, label_attribute).each do |ce_id, label_text|
          if label_text.blank?
            r.not_updated.push ce_id
            next
          end

          begin
            Label.create!(
              text: label_text,
              total:,
              label_object_id: ce_id,
              label_object_type: 'CollectingEvent',
            )
            r.updated.push ce_id
          rescue ActiveRecord::RecordInvalid => e
            # Probably never occurs for Label given our pre-checks
            r.not_updated.push e.record.id

            r.errors[e.message] = 0 unless r.errors[e.message]
            r.errors[e.message] += 1
          end
        end

      end

      raise ActiveRecord::Rollback if r.preview
    end # end transaction

    r
  end

  protected

  def set_text
    update_column(:text, label_object.reload.send(text_method.to_sym))
  end

  def stub_text
    assign_attributes(text:  'STUB')
  end

end

#styleString

Returns The unique name for a corresponding CSS class. # TODO: reference vocabulary file.

Returns:

  • (String)

    The unique name for a corresponding CSS class. # TODO: reference vocabulary file



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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
114
115
116
117
118
119
120
121
122
123
# File 'app/models/label.rb', line 31

class Label < ApplicationRecord

  include Housekeeping
  include Shared::Notes
  include Shared::Tags
  include Shared::Depictions
  include Shared::PolymorphicAnnotator
  include Shared::IsData

  polymorphic_annotates('label_object', presence_validate: false)

  ignore_whitespace_on(:text)

  attr_accessor :text_method

  before_validation :stub_text, if: Proc.new { |c| c.text_method.present? }

  after_save :set_text, if: Proc.new { |c| c.text_method.present? }

  validates_presence_of :text, :total

  scope :unprinted, -> { where(is_printed: false) }

  def is_generated?
    false
  end

  def self.batch_create(
    collecting_event_query_params, label_attribute, total, preview
  )
    q = Queries::CollectingEvent::Filter.new(collecting_event_query_params).all

    label_attribute = (label_attribute || '').to_sym
    if ![:verbatim_label, :document_label, :print_label].include?(label_attribute)
      raise TaxonWorks::Error, "Invalid label choice: '#{label_attribute}'"
    end

    max = 1_000
    if q.count > max
      raise TaxonWorks::Error, "At most #{max} labels can be created at once"
    end

    total = [total, 1].max

    r = BatchResponse.new({
      preview:,
      async: false,
      total_attempted: q.count
    })
    self.transaction do
      begin
        q.pluck(:id, label_attribute).each do |ce_id, label_text|
          if label_text.blank?
            r.not_updated.push ce_id
            next
          end

          begin
            Label.create!(
              text: label_text,
              total:,
              label_object_id: ce_id,
              label_object_type: 'CollectingEvent',
            )
            r.updated.push ce_id
          rescue ActiveRecord::RecordInvalid => e
            # Probably never occurs for Label given our pre-checks
            r.not_updated.push e.record.id

            r.errors[e.message] = 0 unless r.errors[e.message]
            r.errors[e.message] += 1
          end
        end

      end

      raise ActiveRecord::Rollback if r.preview
    end # end transaction

    r
  end

  protected

  def set_text
    update_column(:text, label_object.reload.send(text_method.to_sym))
  end

  def stub_text
    assign_attributes(text:  'STUB')
  end

end

#textString

Returns The text of the label.

Returns:

  • (String)

    The text of the label.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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
114
115
116
117
118
119
120
121
122
123
# File 'app/models/label.rb', line 31

class Label < ApplicationRecord

  include Housekeeping
  include Shared::Notes
  include Shared::Tags
  include Shared::Depictions
  include Shared::PolymorphicAnnotator
  include Shared::IsData

  polymorphic_annotates('label_object', presence_validate: false)

  ignore_whitespace_on(:text)

  attr_accessor :text_method

  before_validation :stub_text, if: Proc.new { |c| c.text_method.present? }

  after_save :set_text, if: Proc.new { |c| c.text_method.present? }

  validates_presence_of :text, :total

  scope :unprinted, -> { where(is_printed: false) }

  def is_generated?
    false
  end

  def self.batch_create(
    collecting_event_query_params, label_attribute, total, preview
  )
    q = Queries::CollectingEvent::Filter.new(collecting_event_query_params).all

    label_attribute = (label_attribute || '').to_sym
    if ![:verbatim_label, :document_label, :print_label].include?(label_attribute)
      raise TaxonWorks::Error, "Invalid label choice: '#{label_attribute}'"
    end

    max = 1_000
    if q.count > max
      raise TaxonWorks::Error, "At most #{max} labels can be created at once"
    end

    total = [total, 1].max

    r = BatchResponse.new({
      preview:,
      async: false,
      total_attempted: q.count
    })
    self.transaction do
      begin
        q.pluck(:id, label_attribute).each do |ce_id, label_text|
          if label_text.blank?
            r.not_updated.push ce_id
            next
          end

          begin
            Label.create!(
              text: label_text,
              total:,
              label_object_id: ce_id,
              label_object_type: 'CollectingEvent',
            )
            r.updated.push ce_id
          rescue ActiveRecord::RecordInvalid => e
            # Probably never occurs for Label given our pre-checks
            r.not_updated.push e.record.id

            r.errors[e.message] = 0 unless r.errors[e.message]
            r.errors[e.message] += 1
          end
        end

      end

      raise ActiveRecord::Rollback if r.preview
    end # end transaction

    r
  end

  protected

  def set_text
    update_column(:text, label_object.reload.send(text_method.to_sym))
  end

  def stub_text
    assign_attributes(text:  'STUB')
  end

end

#text_methodObject

Returns the value of attribute text_method.



44
45
46
# File 'app/models/label.rb', line 44

def text_method
  @text_method
end

#totalInteger

Returns The number of copies to print.

Returns:

  • (Integer)

    The number of copies to print



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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
114
115
116
117
118
119
120
121
122
123
# File 'app/models/label.rb', line 31

class Label < ApplicationRecord

  include Housekeeping
  include Shared::Notes
  include Shared::Tags
  include Shared::Depictions
  include Shared::PolymorphicAnnotator
  include Shared::IsData

  polymorphic_annotates('label_object', presence_validate: false)

  ignore_whitespace_on(:text)

  attr_accessor :text_method

  before_validation :stub_text, if: Proc.new { |c| c.text_method.present? }

  after_save :set_text, if: Proc.new { |c| c.text_method.present? }

  validates_presence_of :text, :total

  scope :unprinted, -> { where(is_printed: false) }

  def is_generated?
    false
  end

  def self.batch_create(
    collecting_event_query_params, label_attribute, total, preview
  )
    q = Queries::CollectingEvent::Filter.new(collecting_event_query_params).all

    label_attribute = (label_attribute || '').to_sym
    if ![:verbatim_label, :document_label, :print_label].include?(label_attribute)
      raise TaxonWorks::Error, "Invalid label choice: '#{label_attribute}'"
    end

    max = 1_000
    if q.count > max
      raise TaxonWorks::Error, "At most #{max} labels can be created at once"
    end

    total = [total, 1].max

    r = BatchResponse.new({
      preview:,
      async: false,
      total_attempted: q.count
    })
    self.transaction do
      begin
        q.pluck(:id, label_attribute).each do |ce_id, label_text|
          if label_text.blank?
            r.not_updated.push ce_id
            next
          end

          begin
            Label.create!(
              text: label_text,
              total:,
              label_object_id: ce_id,
              label_object_type: 'CollectingEvent',
            )
            r.updated.push ce_id
          rescue ActiveRecord::RecordInvalid => e
            # Probably never occurs for Label given our pre-checks
            r.not_updated.push e.record.id

            r.errors[e.message] = 0 unless r.errors[e.message]
            r.errors[e.message] += 1
          end
        end

      end

      raise ActiveRecord::Rollback if r.preview
    end # end transaction

    r
  end

  protected

  def set_text
    update_column(:text, label_object.reload.send(text_method.to_sym))
  end

  def stub_text
    assign_attributes(text:  'STUB')
  end

end

Class Method Details

.batch_create(collecting_event_query_params, label_attribute, total, preview) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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
# File 'app/models/label.rb', line 58

def self.batch_create(
  collecting_event_query_params, label_attribute, total, preview
)
  q = Queries::CollectingEvent::Filter.new(collecting_event_query_params).all

  label_attribute = (label_attribute || '').to_sym
  if ![:verbatim_label, :document_label, :print_label].include?(label_attribute)
    raise TaxonWorks::Error, "Invalid label choice: '#{label_attribute}'"
  end

  max = 1_000
  if q.count > max
    raise TaxonWorks::Error, "At most #{max} labels can be created at once"
  end

  total = [total, 1].max

  r = BatchResponse.new({
    preview:,
    async: false,
    total_attempted: q.count
  })
  self.transaction do
    begin
      q.pluck(:id, label_attribute).each do |ce_id, label_text|
        if label_text.blank?
          r.not_updated.push ce_id
          next
        end

        begin
          Label.create!(
            text: label_text,
            total:,
            label_object_id: ce_id,
            label_object_type: 'CollectingEvent',
          )
          r.updated.push ce_id
        rescue ActiveRecord::RecordInvalid => e
          # Probably never occurs for Label given our pre-checks
          r.not_updated.push e.record.id

          r.errors[e.message] = 0 unless r.errors[e.message]
          r.errors[e.message] += 1
        end
      end

    end

    raise ActiveRecord::Rollback if r.preview
  end # end transaction

  r
end

Instance Method Details

#is_generated?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'app/models/label.rb', line 54

def is_generated?
  false
end

#set_textObject (protected)



115
116
117
# File 'app/models/label.rb', line 115

def set_text
  update_column(:text, label_object.reload.send(text_method.to_sym))
end

#stub_textObject (protected)



119
120
121
# File 'app/models/label.rb', line 119

def stub_text
  assign_attributes(text:  'STUB')
end