app/models/dhatu/price.rb in dhatu-0.1.10 vs app/models/dhatu/price.rb in dhatu-0.1.11

- old
+ new

@@ -13,11 +13,11 @@ validates :price, presence: true validates :category, presence: true # Associations - belongs_to :category + belongs_to :category, optional: true has_one :cover_image, :as => :imageable, :dependent => :destroy, :class_name => "Image::CoverImage" # ------------------ # Class Methods # ------------------ @@ -28,9 +28,41 @@ scope :filter_by_category, lambda { |c| where("category_id = ?", (c.is_a? Dhatu::Category ? c.id : c)) } 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[:title].to_s.strip.blank? + + price = Dhatu::Price.find_by_title(hsh[:title].to_s.strip) || Dhatu::Price.new + price.title = hsh[:title].to_s.strip + price.sub_title = hsh[:sub_title].to_s.strip + price.price = hsh[:price].to_s.strip + + price.category = Dhatu::Category.find_by_name(hsh[:category].to_s.strip) + price.status = hsh[:status].to_s.strip || PUBLISHED + price.featured = hsh[:featured].to_s.strip == "true" + price.priority = hsh[:priority].to_s.strip || 1 + + if price.valid? + begin + price.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 price: #{price.title}" + details = "Error! #{price.errors.full_messages.to_sentence}" + error_object.errors << { summary: summary, details: details } + end + return error_object + end # ------------------ # Instance variables # ------------------ \ No newline at end of file