Sha256: c130fa72b5bd34b4355820bd04ccb6b24c60365acac1332b3dad66775bfb686a

Contents?: true

Size: 1.99 KB

Versions: 6

Compression:

Stored size: 1.99 KB

Contents

class Dhatu::SectionType < Dhatu::ApplicationRecord

  # Set Table Name
  self.table_name = "section_types"

  # Validations
  validates :name, presence: true, length: {minimum: 3, maximum: 64}
  validates :code, presence: true, length: {minimum: 3, maximum: 64}
  
  # ------------------
  # Class Methods
  # ------------------

  scope :search, lambda {|query| where("LOWER(name) LIKE LOWER('%#{query}%') OR\
                                        LOWER(code) LIKE LOWER('%#{query}%')")}
  
  scope :upcoming, lambda { where("created_at >= ?", Time.now) }
  scope :past, lambda { where("created_at < ?", Time.now) }

  # Associations
  has_many :sections, :class_name => "Dhatu::Section"

  def self.save_row_data(hsh)
    # Initializing error hash for displaying all errors altogether
    error_object = Kuppayam::Importer::ErrorHash.new

    return error_object if hsh[:name].to_s.strip.blank?

    section_type = Dhatu::SectionType.find_by_name(hsh[:name].to_s.strip) || Dhatu::SectionType.new
    section_type.name = hsh[:name].to_s.strip
    section_type.code = hsh[:code].to_s.strip
    
    if section_type.valid?
      begin
        section_type.save!
      rescue Exception => e
        summary = "uncaught #{e} exception while handling connection: #{e.message}"
        details = "Stack trace: #{e.backtrace.map {|l| "  #{l}\n"}.join}"
        error_object.errors << { summary: summary, details: details }        
      end
    else
      summary = "Error while saving section_type: #{section_type.name}"
      details = "Error! #{section_type.errors.full_messages.to_sentence}"
      error_object.errors << { summary: summary, details: details }
    end
    return error_object
  end

  # ------------------
  # Instance variables
  # ------------------

  # Generic Methods
  # ---------------
  def to_param
    "#{id}-#{name.parameterize[0..32]}"
  end

  def display_name
    "#{name_was}"
  end

  # Permission Methods
  # ------------------

  def can_be_edited?
    true
  end

  def can_be_deleted?
    true
  end

end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
dhatu-0.1.16 app/models/dhatu/section_type.rb
dhatu-0.1.15 app/models/dhatu/section_type.rb
dhatu-0.1.14 app/models/dhatu/section_type.rb
dhatu-0.1.13 app/models/dhatu/section_type.rb
dhatu-0.1.12 app/models/dhatu/section_type.rb
dhatu-0.1.11 app/models/dhatu/section_type.rb