43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
# File 'app/models/dhatu/blog_post.rb', line 43
def self.save_row_data(hsh)
error_object = Kuppayam::Importer::ErrorHash.new
return error_object if hsh[:title].to_s.strip.blank?
category = Dhatu::Category.find_by_name(hsh[:category].to_s.strip)
unless category
puts "Category '#{hsh[:category].to_s.strip}' not found".red
return
end
blog_post = Dhatu::BlogPost.where("title = ? AND category_id = ?", hsh[:title].to_s.strip, category.id).first || Dhatu::BlogPost.new
blog_post.title = hsh[:title].to_s.strip
blog_post.slug = blog_post.title.parameterize[0..64] if blog_post.title
blog_post.author = hsh[:author].to_s.strip
blog_post.short_description = hsh[:short_description].to_s.strip
blog_post.description = hsh[:description].to_s.strip
blog_post.posted_at = hsh[:posted_at].to_s.strip
blog_post.category = category
blog_post.featured = ["true", "t","1","yes","y"].include?(hsh[:featured].to_s.downcase.strip)
blog_post.status = hsh[:status].to_s.strip.blank? ? PUBLISHED : hsh[:status].to_s.strip
if blog_post.valid?
begin
blog_post.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 blog_post: #{blog_post.title}"
details = "Error! #{blog_post.errors.full_messages.to_sentence}"
error_object.errors << { summary: summary, details: details }
end
return error_object
end
|