Class: Language

Inherits:
ApplicationRecord show all
Includes:
Housekeeping::Timestamps, Housekeeping::Users, Shared::IsApplicationData, Shared::IsData
Defined in:
app/models/language.rb

Overview

This is a 1:1 representation of ISO 639-2. It is built on initialization with a rake task, and not further touched. Many of the languages have multiple version (e.g. there are 4 variations of German)

Instance Attribute Summary collapse

Attributes included from Housekeeping::Users

#by

Class Method Summary collapse

Methods included from Housekeeping::Timestamps

#data_breakdown_for_chartkick_recent

Methods included from Housekeeping::Users

#set_created_by_id, #set_updated_by_id

Methods inherited from ApplicationRecord

transaction_with_retry

Instance Attribute Details

#alpha_2String

Returns alpha_2 will have either a 2 character value or and empty string ('')

Returns:

  • (String)

    alpha_2 will have either a 2 character value or and empty string ('')



21
22
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
# File 'app/models/language.rb', line 21

class Language < ApplicationRecord
  include Housekeeping::Users
  include Housekeeping::Timestamps
  include Shared::IsData
  include Shared::IsApplicationData

  has_many :serials, inverse_of: :language, foreign_key: :primary_language_id
  has_many :sources, inverse_of: :source_language, class_name: 'Source::Bibtex'
  
  scope :used_recently_on_sources, -> { joins(sources: [:project_sources]).where(sources: { created_at: 1.weeks.ago..Time.now } ) }
  scope :used_recently_on_serials, -> { joins(:serials).where(serials: { created_at: 1.weeks.ago..Time.now } ) }

  scope :with_english_name_containing, ->(name) {where('english_name ILIKE ?', "%#{name}%")}  # non-case sensitive comparison

  validates_presence_of :english_name, :alpha_3_bibliographic

  def self.with_english_name_or_abbreviation(value)
    value = [value] if value.class == String

    t = Language.arel_table
    a = t[:english_name].matches_any(value)
    b = t[:alpha_2].matches_any(value)
    c = t[:alpha_3_bibliographic].matches_any(value)
    d = t[:alpha_3_terminologic].matches_any(value)
    Language.where(a.or(b).or(c).or(d).to_sql)
  end

  def self.find_for_autocomplete(params)
    term = "#{params[:term]}%"
    where('english_name ILIKE ? OR english_name = ?', term, params[:term])
  end

  # @param klass ['source' || 'serial']
  def self.select_optimized(user_id, project_id, klass = 'source')
    recent = if klass == 'source'
               Language.used_recently_on_sources.where('project_sources.project_id = ? AND sources.updated_by_id = ?', project_id, user_id).distinct.limit(10).to_a
             elsif klass == 'serial'
               Language.used_recently_on_serials.where('serials.updated_by_id = ?', user_id).distinct.limit(10).to_a
             end
    h = {
      recent: recent,
      pinboard: Language.pinned_by(user_id).pinned_in_project(project_id).to_a
    }

    h[:quick] = (Language.pinned_by(user_id).pinboard_inserted.pinned_in_project(project_id).to_a  + h[:recent][0..3]).uniq
    h
  end

end

#alpha_3_bibiographicString

Returns attribute alpha_3_bibliographic always has a distinct 3 character value

Returns:

  • (String)

    attribute alpha_3_bibliographic always has a distinct 3 character value



21
22
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
# File 'app/models/language.rb', line 21

class Language < ApplicationRecord
  include Housekeeping::Users
  include Housekeeping::Timestamps
  include Shared::IsData
  include Shared::IsApplicationData

  has_many :serials, inverse_of: :language, foreign_key: :primary_language_id
  has_many :sources, inverse_of: :source_language, class_name: 'Source::Bibtex'
  
  scope :used_recently_on_sources, -> { joins(sources: [:project_sources]).where(sources: { created_at: 1.weeks.ago..Time.now } ) }
  scope :used_recently_on_serials, -> { joins(:serials).where(serials: { created_at: 1.weeks.ago..Time.now } ) }

  scope :with_english_name_containing, ->(name) {where('english_name ILIKE ?', "%#{name}%")}  # non-case sensitive comparison

  validates_presence_of :english_name, :alpha_3_bibliographic

  def self.with_english_name_or_abbreviation(value)
    value = [value] if value.class == String

    t = Language.arel_table
    a = t[:english_name].matches_any(value)
    b = t[:alpha_2].matches_any(value)
    c = t[:alpha_3_bibliographic].matches_any(value)
    d = t[:alpha_3_terminologic].matches_any(value)
    Language.where(a.or(b).or(c).or(d).to_sql)
  end

  def self.find_for_autocomplete(params)
    term = "#{params[:term]}%"
    where('english_name ILIKE ? OR english_name = ?', term, params[:term])
  end

  # @param klass ['source' || 'serial']
  def self.select_optimized(user_id, project_id, klass = 'source')
    recent = if klass == 'source'
               Language.used_recently_on_sources.where('project_sources.project_id = ? AND sources.updated_by_id = ?', project_id, user_id).distinct.limit(10).to_a
             elsif klass == 'serial'
               Language.used_recently_on_serials.where('serials.updated_by_id = ?', user_id).distinct.limit(10).to_a
             end
    h = {
      recent: recent,
      pinboard: Language.pinned_by(user_id).pinned_in_project(project_id).to_a
    }

    h[:quick] = (Language.pinned_by(user_id).pinboard_inserted.pinned_in_project(project_id).to_a  + h[:recent][0..3]).uniq
    h
  end

end

#alpha_3_terminologicString

Returns:

  • (String)


21
22
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
# File 'app/models/language.rb', line 21

class Language < ApplicationRecord
  include Housekeeping::Users
  include Housekeeping::Timestamps
  include Shared::IsData
  include Shared::IsApplicationData

  has_many :serials, inverse_of: :language, foreign_key: :primary_language_id
  has_many :sources, inverse_of: :source_language, class_name: 'Source::Bibtex'
  
  scope :used_recently_on_sources, -> { joins(sources: [:project_sources]).where(sources: { created_at: 1.weeks.ago..Time.now } ) }
  scope :used_recently_on_serials, -> { joins(:serials).where(serials: { created_at: 1.weeks.ago..Time.now } ) }

  scope :with_english_name_containing, ->(name) {where('english_name ILIKE ?', "%#{name}%")}  # non-case sensitive comparison

  validates_presence_of :english_name, :alpha_3_bibliographic

  def self.with_english_name_or_abbreviation(value)
    value = [value] if value.class == String

    t = Language.arel_table
    a = t[:english_name].matches_any(value)
    b = t[:alpha_2].matches_any(value)
    c = t[:alpha_3_bibliographic].matches_any(value)
    d = t[:alpha_3_terminologic].matches_any(value)
    Language.where(a.or(b).or(c).or(d).to_sql)
  end

  def self.find_for_autocomplete(params)
    term = "#{params[:term]}%"
    where('english_name ILIKE ? OR english_name = ?', term, params[:term])
  end

  # @param klass ['source' || 'serial']
  def self.select_optimized(user_id, project_id, klass = 'source')
    recent = if klass == 'source'
               Language.used_recently_on_sources.where('project_sources.project_id = ? AND sources.updated_by_id = ?', project_id, user_id).distinct.limit(10).to_a
             elsif klass == 'serial'
               Language.used_recently_on_serials.where('serials.updated_by_id = ?', user_id).distinct.limit(10).to_a
             end
    h = {
      recent: recent,
      pinboard: Language.pinned_by(user_id).pinned_in_project(project_id).to_a
    }

    h[:quick] = (Language.pinned_by(user_id).pinboard_inserted.pinned_in_project(project_id).to_a  + h[:recent][0..3]).uniq
    h
  end

end

#english_nameString

Returns english_name may be more than one word long (e.g. 'English, Middle (1100-1500)', 'Filipino; Pilipino','Finno-Ugrian languages')

Returns:

  • (String)

    english_name may be more than one word long (e.g. 'English, Middle (1100-1500)', 'Filipino; Pilipino','Finno-Ugrian languages')



21
22
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
# File 'app/models/language.rb', line 21

class Language < ApplicationRecord
  include Housekeeping::Users
  include Housekeeping::Timestamps
  include Shared::IsData
  include Shared::IsApplicationData

  has_many :serials, inverse_of: :language, foreign_key: :primary_language_id
  has_many :sources, inverse_of: :source_language, class_name: 'Source::Bibtex'
  
  scope :used_recently_on_sources, -> { joins(sources: [:project_sources]).where(sources: { created_at: 1.weeks.ago..Time.now } ) }
  scope :used_recently_on_serials, -> { joins(:serials).where(serials: { created_at: 1.weeks.ago..Time.now } ) }

  scope :with_english_name_containing, ->(name) {where('english_name ILIKE ?', "%#{name}%")}  # non-case sensitive comparison

  validates_presence_of :english_name, :alpha_3_bibliographic

  def self.with_english_name_or_abbreviation(value)
    value = [value] if value.class == String

    t = Language.arel_table
    a = t[:english_name].matches_any(value)
    b = t[:alpha_2].matches_any(value)
    c = t[:alpha_3_bibliographic].matches_any(value)
    d = t[:alpha_3_terminologic].matches_any(value)
    Language.where(a.or(b).or(c).or(d).to_sql)
  end

  def self.find_for_autocomplete(params)
    term = "#{params[:term]}%"
    where('english_name ILIKE ? OR english_name = ?', term, params[:term])
  end

  # @param klass ['source' || 'serial']
  def self.select_optimized(user_id, project_id, klass = 'source')
    recent = if klass == 'source'
               Language.used_recently_on_sources.where('project_sources.project_id = ? AND sources.updated_by_id = ?', project_id, user_id).distinct.limit(10).to_a
             elsif klass == 'serial'
               Language.used_recently_on_serials.where('serials.updated_by_id = ?', user_id).distinct.limit(10).to_a
             end
    h = {
      recent: recent,
      pinboard: Language.pinned_by(user_id).pinned_in_project(project_id).to_a
    }

    h[:quick] = (Language.pinned_by(user_id).pinboard_inserted.pinned_in_project(project_id).to_a  + h[:recent][0..3]).uniq
    h
  end

end

#french_nameString

Returns french_name may be more than one word long

Returns:

  • (String)

    french_name may be more than one word long



21
22
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
# File 'app/models/language.rb', line 21

class Language < ApplicationRecord
  include Housekeeping::Users
  include Housekeeping::Timestamps
  include Shared::IsData
  include Shared::IsApplicationData

  has_many :serials, inverse_of: :language, foreign_key: :primary_language_id
  has_many :sources, inverse_of: :source_language, class_name: 'Source::Bibtex'
  
  scope :used_recently_on_sources, -> { joins(sources: [:project_sources]).where(sources: { created_at: 1.weeks.ago..Time.now } ) }
  scope :used_recently_on_serials, -> { joins(:serials).where(serials: { created_at: 1.weeks.ago..Time.now } ) }

  scope :with_english_name_containing, ->(name) {where('english_name ILIKE ?', "%#{name}%")}  # non-case sensitive comparison

  validates_presence_of :english_name, :alpha_3_bibliographic

  def self.with_english_name_or_abbreviation(value)
    value = [value] if value.class == String

    t = Language.arel_table
    a = t[:english_name].matches_any(value)
    b = t[:alpha_2].matches_any(value)
    c = t[:alpha_3_bibliographic].matches_any(value)
    d = t[:alpha_3_terminologic].matches_any(value)
    Language.where(a.or(b).or(c).or(d).to_sql)
  end

  def self.find_for_autocomplete(params)
    term = "#{params[:term]}%"
    where('english_name ILIKE ? OR english_name = ?', term, params[:term])
  end

  # @param klass ['source' || 'serial']
  def self.select_optimized(user_id, project_id, klass = 'source')
    recent = if klass == 'source'
               Language.used_recently_on_sources.where('project_sources.project_id = ? AND sources.updated_by_id = ?', project_id, user_id).distinct.limit(10).to_a
             elsif klass == 'serial'
               Language.used_recently_on_serials.where('serials.updated_by_id = ?', user_id).distinct.limit(10).to_a
             end
    h = {
      recent: recent,
      pinboard: Language.pinned_by(user_id).pinned_in_project(project_id).to_a
    }

    h[:quick] = (Language.pinned_by(user_id).pinboard_inserted.pinned_in_project(project_id).to_a  + h[:recent][0..3]).uniq
    h
  end

end

Class Method Details

.find_for_autocomplete(params) ⇒ Object



48
49
50
51
# File 'app/models/language.rb', line 48

def self.find_for_autocomplete(params)
  term = "#{params[:term]}%"
  where('english_name ILIKE ? OR english_name = ?', term, params[:term])
end

.select_optimized(user_id, project_id, klass = 'source') ⇒ Object

Parameters:

  • klass ('source' || 'serial') (defaults to: 'source')


54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/models/language.rb', line 54

def self.select_optimized(user_id, project_id, klass = 'source')
  recent = if klass == 'source'
             Language.used_recently_on_sources.where('project_sources.project_id = ? AND sources.updated_by_id = ?', project_id, user_id).distinct.limit(10).to_a
           elsif klass == 'serial'
             Language.used_recently_on_serials.where('serials.updated_by_id = ?', user_id).distinct.limit(10).to_a
           end
  h = {
    recent: recent,
    pinboard: Language.pinned_by(user_id).pinned_in_project(project_id).to_a
  }

  h[:quick] = (Language.pinned_by(user_id).pinboard_inserted.pinned_in_project(project_id).to_a  + h[:recent][0..3]).uniq
  h
end

.with_english_name_or_abbreviation(value) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'app/models/language.rb', line 37

def self.with_english_name_or_abbreviation(value)
  value = [value] if value.class == String

  t = Language.arel_table
  a = t[:english_name].matches_any(value)
  b = t[:alpha_2].matches_any(value)
  c = t[:alpha_3_bibliographic].matches_any(value)
  d = t[:alpha_3_terminologic].matches_any(value)
  Language.where(a.or(b).or(c).or(d).to_sql)
end