Class: Dhatu::BlogPost

Inherits:
ApplicationRecord show all
Includes:
Featureable, Publishable
Defined in:
app/models/dhatu/blog_post.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.save_row_data(hsh) ⇒ Object



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)
  # Initializing error hash for displaying all errors altogether
  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

Instance Method Details

#can_be_deleted?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'app/models/dhatu/blog_post.rb', line 104

def can_be_deleted?
  status?(:removed)
end

#can_be_edited?Boolean

Permission Methods


Returns:

  • (Boolean)


100
101
102
# File 'app/models/dhatu/blog_post.rb', line 100

def can_be_edited?
  status?(:published) or status?(:unpublished)
end

#display_nameObject

Generic Methods




93
94
95
# File 'app/models/dhatu/blog_post.rb', line 93

def display_name
  "#{title_was}"
end