Sha256: 4e72b081375258d6498258d74c408550584190531ba7ab425774d24fdcc04307

Contents?: true

Size: 1.7 KB

Versions: 5

Compression:

Stored size: 1.7 KB

Contents

# frozen_string_literal: true

module WorksCited
  # Citation
  class Citation < ApplicationRecord
    # Validations
    validates_presence_of :citation_type, :record, :title
    validates(
      :citation_type,
      inclusion: {
        in: WorksCited.configuration.valid_citation_types,
        allowed_options: WorksCited.configuration.valid_citation_types.to_sentence(
          last_word_connector: ', or '
        )
      }
    )

    # Relationships
    belongs_to :record, polymorphic: true
    has_many :works_cited_contributors, inverse_of: :works_cited_citation, class_name: 'WorksCited::Contributor',
                                        foreign_key: :works_cited_citation_id
    accepts_nested_attributes_for :works_cited_contributors, allow_destroy: true

    # Dynamic stuff (from configuration)
    # Creates scopes such as .books and methods such as #book?
    WorksCited.configuration.valid_citation_types.each do |given_type|
      scope given_type.pluralize.to_sym, -> { where(citation_type: given_type) }
      define_method("#{given_type}?".to_sym) do
        citation_type == given_type
      end
    end

    # Instance Methods
    def record=(value)
      unless value.is_a? String
        super(value)
        return
      end

      model_name, id = value.split(':')
      return unless model_name.present? && id.present?

      model = model_name.constantize
      super model.find(id)
    end

    def periodical?
      citation_type == 'periodical'
    end

    def electronic?
      citation_type == 'electronic'
    end

    def book?
      citation_type == 'book'
    end

    def interview?
      citation_type == 'interview'
    end

    def email?
      citation_type == 'email'
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
works_cited-0.1.8 app/models/works_cited/citation.rb
works_cited-0.1.7 app/models/works_cited/citation.rb
works_cited-0.1.6 app/models/works_cited/citation.rb
works_cited-0.1.5 app/models/works_cited/citation.rb
works_cited-0.1.4 app/models/works_cited/citation.rb