class Dhatu::Testimonial < Dhatu::ApplicationRecord # Set Table Name self.table_name = "testimonials" # Including the State Machine Methods include Publishable include Featureable # Validations validates :name, presence: true, length: {minimum: 3, maximum: 256}, allow_blank: false validates :designation, presence: true, length: {minimum: 3, maximum: 256}, allow_blank: true validates :organisation, presence: true, length: {minimum: 3, maximum: 256}, allow_blank: true validates :statement, presence: true, allow_blank: false # Associations has_one :profile_image, :as => :imageable, :dependent => :destroy, :class_name => "Image::ProfileImage" has_one :logo_image, :as => :imageable, :dependent => :destroy, :class_name => "Image::LogoImage" # ------------------ # Class Methods # ------------------ scope :search, lambda {|query| where("LOWER(name) LIKE LOWER('%#{query}%') OR\ LOWER(designation) LIKE LOWER('%#{query}%') OR\ LOWER(organisation) LIKE LOWER('%#{query}%') OR\ LOWER(status) LIKE LOWER('%#{query}%')")} scope :upcoming, lambda { where("created_at >= ?", Time.now) } scope :past, lambda { where("created_at < ?", Time.now) } 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? testimonial = Dhatu::Testimonial.find_by_name(hsh[:name].to_s.strip) || Dhatu::Testimonial.new testimonial.name = hsh[:name].to_s.strip testimonial.designation = hsh[:designation].to_s.strip testimonial.organisation = hsh[:organisation].to_s.strip testimonial.description = hsh[:description].to_s.strip # testimonial.category = Dhatu::Category.find_by_name(hsh[:category]) testimonial.status = hsh[:status].to_s.strip || PUBLISHED testimonial.featured = hsh[:featured].to_s.strip || true testimonial.priority = hsh[:priority].to_s.strip || 1 if testimonial.valid? begin testimonial.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 testimonial: #{testimonial.name}" details = "Error! #{testimonial.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? status?(:published) or status?(:unpublished) end def can_be_deleted? status?(:removed) end end