Sha256: f69d63cd39998d52f7e6f22eebb9dd897c1920c8af98727c94810fd22209cf68

Contents?: true

Size: 1.99 KB

Versions: 8

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 Methods
  # ------------------

  # 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

8 entries across 8 versions & 1 rubygems

Version Path
dhatu-0.1.25 app/models/dhatu/section_type.rb
dhatu-0.1.24 app/models/dhatu/section_type.rb
dhatu-0.1.23 app/models/dhatu/section_type.rb
dhatu-0.1.22 app/models/dhatu/section_type.rb
dhatu-0.1.21 app/models/dhatu/section_type.rb
dhatu-0.1.20 app/models/dhatu/section_type.rb
dhatu-0.1.19 app/models/dhatu/section_type.rb
dhatu-0.1.18 app/models/dhatu/section_type.rb