class Dhatu::Section < Dhatu::ApplicationRecord # Set Table Name self.table_name = "sections" # Including the State Machine Methods include Publishable # Validations validates :name, presence: true, length: {minimum: 3, maximum: 64}, allow_blank: false validates :code, presence: true, length: {minimum: 3, maximum: 64}, allow_blank: false validates :title, presence: true, length: {minimum: 3, maximum: 256}, allow_blank: false validates :sub_title, length: {minimum: 3, maximum: 256}, allow_blank: true validates :short_description, length: {minimum: 10, maximum: 1048}, allow_blank: true validates :long_description, length: {minimum: 10}, allow_blank: true validates :button_one_text, length: {maximum: 64}, allow_blank: true validates :button_two_text, length: {maximum: 64}, allow_blank: true validates :button_one_link, length: {maximum: 512}, allow_blank: true validates :button_two_link, length: {maximum: 512}, allow_blank: true # Associations belongs_to :page, :class_name => "Dhatu::Page", optional: true has_one :cover_image, :as => :imageable, :dependent => :destroy, :class_name => "Image::CoverImage" has_many :gallery_images, :as => :imageable, :dependent => :destroy, :class_name => "Image::GalleryImage" # ------------------ # Class Methods # ------------------ scope :search, lambda {|query| where("LOWER(title) LIKE LOWER('%#{query}%') OR\ LOWER(sub_title) LIKE LOWER('%#{query}%') OR\ LOWER(short_description) LIKE LOWER('%#{query}%') OR\ LOWER(button_one_text) LIKE LOWER('%#{query}%') OR\ LOWER(button_two_text) LIKE LOWER('%#{query}%') OR\ LOWER(button_one_link) LIKE LOWER('%#{query}%') OR\ LOWER(button_two_link) LIKE LOWER('%#{query}%')") } scope :upcoming, lambda { where("created_at >= ?", Time.now) } scope :past, lambda { where("created_at < ?", Time.now) } scope :find_by_code, lambda {|c| where("code = ?", c) } 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[:code].to_s.strip.blank? section = Dhatu::Section.where("code = ?", hsh[:code].to_s.strip).first || Dhatu::Section.new section.name = hsh[:name].to_s.strip section.code = hsh[:code].to_s.strip section.title = hsh[:title].to_s.strip section.sub_title = hsh[:sub_title].to_s.strip section.short_description = hsh[:short_description].to_s.strip section.long_description = hsh[:long_description].to_s.strip section.button_one_text = hsh[:button_one_text].to_s.strip section.button_two_text = hsh[:button_two_text].to_s.strip section.button_one_link = hsh[:button_one_link].to_s.strip section.button_two_link = hsh[:button_two_link].to_s.strip section.page = Dhatu::Page.where("code = ?", hsh[:page].to_s.strip).first section.status = hsh[:status].to_s.strip || PUBLISHED section.priority = hsh[:priority].to_s.strip || 1 if section.valid? begin section.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: #{section.title}" details = "Error! #{section.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}-#{title.parameterize[0..32]}" end def display_name "#{name_was}" end # Permission Methods # ------------------ def can_be_edited? status?(:published) or status?(:unpublished) end def can_be_deleted? status?(:removed) end end