Class: Citation

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Housekeeping, Shared::Confidence, Shared::DataAttributes, Shared::IsData, Shared::Notable, Shared::Taggable
Defined in:
app/models/citation.rb

Overview

A Citation is an assertion that the subject (i.e. citation object/record/data instance), or some attribute of it, was referenced or originate in a Source.

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from Housekeeping

#has_polymorphic_relationship?

Instance Attribute Details

- (Integer) citation_object_id

Returns Rails STI, the id of the object being cited

Returns:

  • (Integer)

    Rails STI, the id of the object being cited



23
24
25
26
27
28
29
30
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
# File 'app/models/citation.rb', line 23

class Citation < ActiveRecord::Base
  include Housekeeping
  include Shared::Notable
  include Shared::Confidence
  include Shared::DataAttributes
  include Shared::Taggable
  include Shared::IsData

  belongs_to :citation_object, polymorphic: :true
  belongs_to :source, inverse_of: :citations

  has_many :citation_topics, inverse_of: :citation, dependent: :destroy
  has_many :topics, through: :citation_topics

  validates_presence_of  :source_id
  validates_uniqueness_of :source_id, scope: [:citation_object_type, :citation_object_id, :pages]

  accepts_nested_attributes_for :citation_topics, allow_destroy: true, reject_if: :reject_citation_topics
  accepts_nested_attributes_for :topics, allow_destroy: true, reject_if: :reject_topic

  before_destroy :prevent_if_required

  after_create :add_source_to_project

  def prevent_if_required
    if !marked_for_destruction? && !new_record? && citation_object.requires_citation? && citation_object.citations(true).count == 1
      errors.add(:base, 'at least one citation is required')
      return false
    end 
  end

  after_save :update_related_cached_values, if: 'is_original?'

  # @return [Scope of matching sources]
  def self.find_for_autocomplete(params)
    term    = params['term']
    ending  = term + '%'
    wrapped = '%' + term + '%'
    joins(:source).where('sources.cached ILIKE ? OR sources.cached ILIKE ? OR citation_object_type LIKE ?', ending, wrapped, ending).with_project_id(params[:project_id])
  end

  # @return [NoteObject]
  #   alias to simplify reference across classes 
  def annotated_object
    citation_object
  end

  # @return [Boolean]
  #   true if is_original is checked, false if nil/false 
  def is_original?
    is_original ? true : false
  end

  def self.generate_download(scope)
    CSV.generate do |csv|
      csv << column_names
      scope.order(id: :asc).each do |o|
        csv << o.attributes.values_at(*column_names).collect { |i|
          i.to_s.gsub(/\n/, '\n').gsub(/\t/, '\t')
        }
      end
    end
  end

  protected

  def add_source_to_project
    if !ProjectSource.where(source: source).any?
      ProjectSource.create(project: project, source: source)
    end
    true
  end

  def reject_citation_topics(attributed)
    attributes['id'].blank? && attributed['topic_id'].blank? && attributed['topic'].blank? && attributed['topic_attributes'].blank?
  end

  def reject_topic(attributed)
    attributed['name'].blank? || attributed['definition'].blank?
  end

  def update_related_cached_values
    if citation_object_type == 'TaxonName'
      citation_object.update_attribute(:cached_author_year, citation_object.get_author_and_year)
    end
    true
  end

end

- (String) citation_object_type

Returns Rails STI, the class of the object being cited

Returns:

  • (String)

    Rails STI, the class of the object being cited



23
24
25
26
27
28
29
30
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
# File 'app/models/citation.rb', line 23

class Citation < ActiveRecord::Base
  include Housekeeping
  include Shared::Notable
  include Shared::Confidence
  include Shared::DataAttributes
  include Shared::Taggable
  include Shared::IsData

  belongs_to :citation_object, polymorphic: :true
  belongs_to :source, inverse_of: :citations

  has_many :citation_topics, inverse_of: :citation, dependent: :destroy
  has_many :topics, through: :citation_topics

  validates_presence_of  :source_id
  validates_uniqueness_of :source_id, scope: [:citation_object_type, :citation_object_id, :pages]

  accepts_nested_attributes_for :citation_topics, allow_destroy: true, reject_if: :reject_citation_topics
  accepts_nested_attributes_for :topics, allow_destroy: true, reject_if: :reject_topic

  before_destroy :prevent_if_required

  after_create :add_source_to_project

  def prevent_if_required
    if !marked_for_destruction? && !new_record? && citation_object.requires_citation? && citation_object.citations(true).count == 1
      errors.add(:base, 'at least one citation is required')
      return false
    end 
  end

  after_save :update_related_cached_values, if: 'is_original?'

  # @return [Scope of matching sources]
  def self.find_for_autocomplete(params)
    term    = params['term']
    ending  = term + '%'
    wrapped = '%' + term + '%'
    joins(:source).where('sources.cached ILIKE ? OR sources.cached ILIKE ? OR citation_object_type LIKE ?', ending, wrapped, ending).with_project_id(params[:project_id])
  end

  # @return [NoteObject]
  #   alias to simplify reference across classes 
  def annotated_object
    citation_object
  end

  # @return [Boolean]
  #   true if is_original is checked, false if nil/false 
  def is_original?
    is_original ? true : false
  end

  def self.generate_download(scope)
    CSV.generate do |csv|
      csv << column_names
      scope.order(id: :asc).each do |o|
        csv << o.attributes.values_at(*column_names).collect { |i|
          i.to_s.gsub(/\n/, '\n').gsub(/\t/, '\t')
        }
      end
    end
  end

  protected

  def add_source_to_project
    if !ProjectSource.where(source: source).any?
      ProjectSource.create(project: project, source: source)
    end
    true
  end

  def reject_citation_topics(attributed)
    attributes['id'].blank? && attributed['topic_id'].blank? && attributed['topic'].blank? && attributed['topic_attributes'].blank?
  end

  def reject_topic(attributed)
    attributed['name'].blank? || attributed['definition'].blank?
  end

  def update_related_cached_values
    if citation_object_type == 'TaxonName'
      citation_object.update_attribute(:cached_author_year, citation_object.get_author_and_year)
    end
    true
  end

end

- (String) pages

Returns a specific location/localization for the data in the Source

Returns:

  • (String)

    a specific location/localization for the data in the Source



23
24
25
26
27
28
29
30
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
# File 'app/models/citation.rb', line 23

class Citation < ActiveRecord::Base
  include Housekeeping
  include Shared::Notable
  include Shared::Confidence
  include Shared::DataAttributes
  include Shared::Taggable
  include Shared::IsData

  belongs_to :citation_object, polymorphic: :true
  belongs_to :source, inverse_of: :citations

  has_many :citation_topics, inverse_of: :citation, dependent: :destroy
  has_many :topics, through: :citation_topics

  validates_presence_of  :source_id
  validates_uniqueness_of :source_id, scope: [:citation_object_type, :citation_object_id, :pages]

  accepts_nested_attributes_for :citation_topics, allow_destroy: true, reject_if: :reject_citation_topics
  accepts_nested_attributes_for :topics, allow_destroy: true, reject_if: :reject_topic

  before_destroy :prevent_if_required

  after_create :add_source_to_project

  def prevent_if_required
    if !marked_for_destruction? && !new_record? && citation_object.requires_citation? && citation_object.citations(true).count == 1
      errors.add(:base, 'at least one citation is required')
      return false
    end 
  end

  after_save :update_related_cached_values, if: 'is_original?'

  # @return [Scope of matching sources]
  def self.find_for_autocomplete(params)
    term    = params['term']
    ending  = term + '%'
    wrapped = '%' + term + '%'
    joins(:source).where('sources.cached ILIKE ? OR sources.cached ILIKE ? OR citation_object_type LIKE ?', ending, wrapped, ending).with_project_id(params[:project_id])
  end

  # @return [NoteObject]
  #   alias to simplify reference across classes 
  def annotated_object
    citation_object
  end

  # @return [Boolean]
  #   true if is_original is checked, false if nil/false 
  def is_original?
    is_original ? true : false
  end

  def self.generate_download(scope)
    CSV.generate do |csv|
      csv << column_names
      scope.order(id: :asc).each do |o|
        csv << o.attributes.values_at(*column_names).collect { |i|
          i.to_s.gsub(/\n/, '\n').gsub(/\t/, '\t')
        }
      end
    end
  end

  protected

  def add_source_to_project
    if !ProjectSource.where(source: source).any?
      ProjectSource.create(project: project, source: source)
    end
    true
  end

  def reject_citation_topics(attributed)
    attributes['id'].blank? && attributed['topic_id'].blank? && attributed['topic'].blank? && attributed['topic_attributes'].blank?
  end

  def reject_topic(attributed)
    attributed['name'].blank? || attributed['definition'].blank?
  end

  def update_related_cached_values
    if citation_object_type == 'TaxonName'
      citation_object.update_attribute(:cached_author_year, citation_object.get_author_and_year)
    end
    true
  end

end

- (Integer) project_id

the project ID

Returns:

  • (Integer)


23
24
25
26
27
28
29
30
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
# File 'app/models/citation.rb', line 23

class Citation < ActiveRecord::Base
  include Housekeeping
  include Shared::Notable
  include Shared::Confidence
  include Shared::DataAttributes
  include Shared::Taggable
  include Shared::IsData

  belongs_to :citation_object, polymorphic: :true
  belongs_to :source, inverse_of: :citations

  has_many :citation_topics, inverse_of: :citation, dependent: :destroy
  has_many :topics, through: :citation_topics

  validates_presence_of  :source_id
  validates_uniqueness_of :source_id, scope: [:citation_object_type, :citation_object_id, :pages]

  accepts_nested_attributes_for :citation_topics, allow_destroy: true, reject_if: :reject_citation_topics
  accepts_nested_attributes_for :topics, allow_destroy: true, reject_if: :reject_topic

  before_destroy :prevent_if_required

  after_create :add_source_to_project

  def prevent_if_required
    if !marked_for_destruction? && !new_record? && citation_object.requires_citation? && citation_object.citations(true).count == 1
      errors.add(:base, 'at least one citation is required')
      return false
    end 
  end

  after_save :update_related_cached_values, if: 'is_original?'

  # @return [Scope of matching sources]
  def self.find_for_autocomplete(params)
    term    = params['term']
    ending  = term + '%'
    wrapped = '%' + term + '%'
    joins(:source).where('sources.cached ILIKE ? OR sources.cached ILIKE ? OR citation_object_type LIKE ?', ending, wrapped, ending).with_project_id(params[:project_id])
  end

  # @return [NoteObject]
  #   alias to simplify reference across classes 
  def annotated_object
    citation_object
  end

  # @return [Boolean]
  #   true if is_original is checked, false if nil/false 
  def is_original?
    is_original ? true : false
  end

  def self.generate_download(scope)
    CSV.generate do |csv|
      csv << column_names
      scope.order(id: :asc).each do |o|
        csv << o.attributes.values_at(*column_names).collect { |i|
          i.to_s.gsub(/\n/, '\n').gsub(/\t/, '\t')
        }
      end
    end
  end

  protected

  def add_source_to_project
    if !ProjectSource.where(source: source).any?
      ProjectSource.create(project: project, source: source)
    end
    true
  end

  def reject_citation_topics(attributed)
    attributes['id'].blank? && attributed['topic_id'].blank? && attributed['topic'].blank? && attributed['topic_attributes'].blank?
  end

  def reject_topic(attributed)
    attributed['name'].blank? || attributed['definition'].blank?
  end

  def update_related_cached_values
    if citation_object_type == 'TaxonName'
      citation_object.update_attribute(:cached_author_year, citation_object.get_author_and_year)
    end
    true
  end

end

- (Integer) source_id

the source ID

Returns:

  • (Integer)


23
24
25
26
27
28
29
30
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
# File 'app/models/citation.rb', line 23

class Citation < ActiveRecord::Base
  include Housekeeping
  include Shared::Notable
  include Shared::Confidence
  include Shared::DataAttributes
  include Shared::Taggable
  include Shared::IsData

  belongs_to :citation_object, polymorphic: :true
  belongs_to :source, inverse_of: :citations

  has_many :citation_topics, inverse_of: :citation, dependent: :destroy
  has_many :topics, through: :citation_topics

  validates_presence_of  :source_id
  validates_uniqueness_of :source_id, scope: [:citation_object_type, :citation_object_id, :pages]

  accepts_nested_attributes_for :citation_topics, allow_destroy: true, reject_if: :reject_citation_topics
  accepts_nested_attributes_for :topics, allow_destroy: true, reject_if: :reject_topic

  before_destroy :prevent_if_required

  after_create :add_source_to_project

  def prevent_if_required
    if !marked_for_destruction? && !new_record? && citation_object.requires_citation? && citation_object.citations(true).count == 1
      errors.add(:base, 'at least one citation is required')
      return false
    end 
  end

  after_save :update_related_cached_values, if: 'is_original?'

  # @return [Scope of matching sources]
  def self.find_for_autocomplete(params)
    term    = params['term']
    ending  = term + '%'
    wrapped = '%' + term + '%'
    joins(:source).where('sources.cached ILIKE ? OR sources.cached ILIKE ? OR citation_object_type LIKE ?', ending, wrapped, ending).with_project_id(params[:project_id])
  end

  # @return [NoteObject]
  #   alias to simplify reference across classes 
  def annotated_object
    citation_object
  end

  # @return [Boolean]
  #   true if is_original is checked, false if nil/false 
  def is_original?
    is_original ? true : false
  end

  def self.generate_download(scope)
    CSV.generate do |csv|
      csv << column_names
      scope.order(id: :asc).each do |o|
        csv << o.attributes.values_at(*column_names).collect { |i|
          i.to_s.gsub(/\n/, '\n').gsub(/\t/, '\t')
        }
      end
    end
  end

  protected

  def add_source_to_project
    if !ProjectSource.where(source: source).any?
      ProjectSource.create(project: project, source: source)
    end
    true
  end

  def reject_citation_topics(attributed)
    attributes['id'].blank? && attributed['topic_id'].blank? && attributed['topic'].blank? && attributed['topic_attributes'].blank?
  end

  def reject_topic(attributed)
    attributed['name'].blank? || attributed['definition'].blank?
  end

  def update_related_cached_values
    if citation_object_type == 'TaxonName'
      citation_object.update_attribute(:cached_author_year, citation_object.get_author_and_year)
    end
    true
  end

end

Class Method Details

+ (Scope of matching sources) find_for_autocomplete(params)

Returns:

  • (Scope of matching sources)


57
58
59
60
61
62
# File 'app/models/citation.rb', line 57

def self.find_for_autocomplete(params)
  term    = params['term']
  ending  = term + '%'
  wrapped = '%' + term + '%'
  joins(:source).where('sources.cached ILIKE ? OR sources.cached ILIKE ? OR citation_object_type LIKE ?', ending, wrapped, ending).with_project_id(params[:project_id])
end

+ (Object) generate_download(scope)



76
77
78
79
80
81
82
83
84
85
# File 'app/models/citation.rb', line 76

def self.generate_download(scope)
  CSV.generate do |csv|
    csv << column_names
    scope.order(id: :asc).each do |o|
      csv << o.attributes.values_at(*column_names).collect { |i|
        i.to_s.gsub(/\n/, '\n').gsub(/\t/, '\t')
      }
    end
  end
end

Instance Method Details

- (Object) add_source_to_project (protected)



89
90
91
92
93
94
# File 'app/models/citation.rb', line 89

def add_source_to_project
  if !ProjectSource.where(source: source).any?
    ProjectSource.create(project: project, source: source)
  end
  true
end

- (NoteObject) annotated_object

Returns alias to simplify reference across classes

Returns:

  • (NoteObject)

    alias to simplify reference across classes



66
67
68
# File 'app/models/citation.rb', line 66

def annotated_object
  citation_object
end

- (Boolean) is_original?

Returns true if is_original is checked, false if nil/false

Returns:

  • (Boolean)

    true if is_original is checked, false if nil/false



72
73
74
# File 'app/models/citation.rb', line 72

def is_original?
  is_original ? true : false
end

- (Object) prevent_if_required



47
48
49
50
51
52
# File 'app/models/citation.rb', line 47

def prevent_if_required
  if !marked_for_destruction? && !new_record? && citation_object.requires_citation? && citation_object.citations(true).count == 1
    errors.add(:base, 'at least one citation is required')
    return false
  end 
end

- (Object) reject_citation_topics(attributed) (protected)



96
97
98
# File 'app/models/citation.rb', line 96

def reject_citation_topics(attributed)
  attributes['id'].blank? && attributed['topic_id'].blank? && attributed['topic'].blank? && attributed['topic_attributes'].blank?
end

- (Object) reject_topic(attributed) (protected)



100
101
102
# File 'app/models/citation.rb', line 100

def reject_topic(attributed)
  attributed['name'].blank? || attributed['definition'].blank?
end


104
105
106
107
108
109
# File 'app/models/citation.rb', line 104

def update_related_cached_values
  if citation_object_type == 'TaxonName'
    citation_object.update_attribute(:cached_author_year, citation_object.get_author_and_year)
  end
  true
end