app/models/feature.rb in usman-0.1.5dev4 vs app/models/feature.rb in usman-0.1.5

- old
+ new

@@ -1,7 +1,10 @@ class Feature < Usman::ApplicationRecord + require 'import_error_handler.rb' + extend Usman::ImportErrorHandler + # Constants UNPUBLISHED = "unpublished" PUBLISHED = "published" DISABLED = "disabled" @@ -21,11 +24,11 @@ has_many :permissions has_many :users, through: :permissions has_one :feature_image, :as => :imageable, :dependent => :destroy, :class_name => "Image::FeatureImage" # Validations - validates :name, presence: true, length: {minimum: 3, maximum: 250} + validates :name, presence: true validates :status, :presence => true, :inclusion => {:in => STATUS.keys, :presence_of => :status, :message => "%{value} is not a valid status" } # ------------------ # Class Methods # ------------------ @@ -42,40 +45,63 @@ scope :unpublished, -> { where(status: UNPUBLISHED) } scope :published, -> { where(status: PUBLISHED) } scope :disabled, -> { where(status: DISABLED) } - def self.save_row_data(row) + def self.save_row_data(row, base_path) + image_base_path = base_path + "images/" + row.headers.each{ |cell| row[cell] = row[cell].to_s.strip } return if row[:name].blank? feature = Feature.find_by_name(row[:name]) || Feature.new feature.name = row[:name] feature.status = Feature::UNPUBLISHED # Initializing error hash for displaying all errors altogether - error_object = Kuppayam::Importer::ErrorHash.new + error_object = Usman::ErrorHash.new - if feature.valid? + ## Adding a profile picture + begin + image_path = image_base_path + "features/#{feature.name.parameterize}.png" + image_path = image_base_path + "features/#{feature.name.parameterize}}.jpg" unless File.exists?(image_path) + if File.exists?(image_path) + feature.build_feature_image + feature.feature_image.image = File.open(image_path) + else + summary = "Feature Image not found for feature: #{feature.name}" + details = "#{image_path}/png doesn't exists" + error_object.warnings << { summary: summary, details: details } + end + rescue => e + summary = "Error during processing: #{$!}" + details = "Feature: #{feature.name}, Image Path: #{image_path}" + stack_trace = "Backtrace:\n\t#{e.backtrace.join("\n\t")}" + error_object.errors << { summary: summary, details: details, stack_trace: stack_trace } + end if feature.feature_image.blank? + + if feature.valid? && (feature.feature_image.blank? || feature.feature_image.valid?) feature.save! else summary = "Error while saving feature: #{feature.name}" details = "Error! #{feature.errors.full_messages.to_sentence}" + details << ", #{feature.feature_image.errors.full_messages.to_sentence}" if feature.feature_image error_object.errors << { summary: summary, details: details } end return error_object end - # ------------------ - # Instance Methods - # ------------------ + # * Return full name + # == Examples + # >>> feature.display_name + # => "Products" + def display_name + "#{name}" + end - # Status Methods - # -------------- - # * Return true if the user is not published, else false. # == Examples # >>> feature.published? # => true def published? @@ -123,36 +149,10 @@ # => "suspended" def suspend! self.update_attribute(:status, DISABLED) end - # Permission Methods - # ------------------ - def can_be_destroyed? return true - end - - def can_be_published? - unpublished? or disabled? - end - - def can_be_unpublished? - published? or disabled? - end - - def can_be_removed? - published? or unpublished? or disabled? - end - - # Other Methods - # ------------- - - # * Return full name - # == Examples - # >>> feature.display_name - # => "Products" - def display_name - "#{name}" end end \ No newline at end of file