Class: Wordpress::WpPost

Inherits:
WpBase
  • Object
show all
Defined in:
app/models/wordpress/wp_post.rb

Direct Known Subclasses

Attachment, Page, Post, Revision

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from WpBase

prefix_table_name

Class Method Details

.find_sti_class(type_name) ⇒ Object



23
24
25
# File 'app/models/wordpress/wp_post.rb', line 23

def self.find_sti_class type_name
  "wordpress/#{type_name}".camelize.constantize
end

.sti_nameObject



27
28
29
# File 'app/models/wordpress/wp_post.rb', line 27

def self.sti_name
  name.underscore.split("/").last
end

Instance Method Details

#assign_category_ids(category_ids) ⇒ Object



165
166
167
168
169
170
171
172
173
# File 'app/models/wordpress/wp_post.rb', line 165

def assign_category_ids category_ids
  categories = category_ids.map{ |id| Category.find(id) rescue nil }.compact
  new_categories = categories.reject{ |category| first_revision.categories.include?(category) }

  first_revision.categories.each{ |cat| cat.mark_for_destruction unless categories.include?(cat) }
  new_categories.each do |category|
    first_revision.relationships.build(taxonomy: category)
  end
end

#assign_category_names(category_names) ⇒ Object



175
176
177
178
# File 'app/models/wordpress/wp_post.rb', line 175

def assign_category_names category_names
  category_ids = category_names.map{ |name| Category.find_or_create(name).id }.compact
  assign_category_ids category_ids
end

#category_namesObject



128
129
130
# File 'app/models/wordpress/wp_post.rb', line 128

def category_names
  first_revision.categories.map(&:name)
end

#category_slugsObject



132
133
134
# File 'app/models/wordpress/wp_post.rb', line 132

def category_slugs
  first_revision.categories.map(&:slug)
end

#contentObject



68
69
70
# File 'app/models/wordpress/wp_post.rb', line 68

def content
  latest_revision.post_content
end

#created_atObject



76
77
78
# File 'app/models/wordpress/wp_post.rb', line 76

def created_at
  self.
end

#excerptObject



72
73
74
# File 'app/models/wordpress/wp_post.rb', line 72

def excerpt
  latest_revision.post_excerpt
end

#first_revisionObject



103
104
105
# File 'app/models/wordpress/wp_post.rb', line 103

def first_revision
  self.parent || self
end

#has_category?(category) ⇒ Boolean

Returns:

  • (Boolean)


140
141
142
143
144
145
146
# File 'app/models/wordpress/wp_post.rb', line 140

def has_category? category
  if category.is_a?(String)
    first_revision.categories.map(&:name).include? category
  else
    first_revision.categories.include? category
  end
end

#latest_revisionObject



60
61
62
# File 'app/models/wordpress/wp_post.rb', line 60

def latest_revision
  revisions.descending.first || self
end

#new_revision(params = {}) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/models/wordpress/wp_post.rb', line 33

def new_revision params = {}
  result = self.revisions.new \
    post_author: self.,
    post_date: self.,
    post_date_gmt: self.,
    post_content: (params[:post_content] || self.content),
    post_title: (params[:post_title] || self.title),
    post_excerpt: (params[:post_excerpt] || self.excerpt),
    post_status: 'inherit',
    ping_status: (self.ping_status || 'closed'),
    post_password: '',
    post_name: "#{self.id}-revision-v#{self.revisions.count + 1}",
    to_ping: self.to_ping,
    pinged: self.to_ping,
    post_modified: Time.now,
    post_modified_gmt: Time.now.utc,
    post_content_filtered: '',
    guid: SecureRandom.uuid,
    menu_order: self.menu_order,
    post_mime_type: '',
    comment_count: 0

  result. = params[:post_tags] if params[:post_tags]
  result.post_categories = params[:post_categories] if params[:post_categories]
  result
end

#post_categoriesObject



161
162
163
# File 'app/models/wordpress/wp_post.rb', line 161

def post_categories
  first_revision.categories.map(&:name).join(",")
end

#post_categories=(category_list) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'app/models/wordpress/wp_post.rb', line 180

def post_categories= category_list
  # turn into an array of items unless already so
  unless category_list.is_a?(Array)
    category_list = category_list.to_s.split(",")
      .map{ |name| name.strip }
      .reject{|r| r.blank?}
  end

  # its an array of ids when #to_i => #to_s yields the same
  if category_list.map(&:to_i).map(&:to_s) == category_list
    assign_category_ids category_list
  else
    assign_category_names category_list
  end
end

#post_tagsObject



148
149
150
# File 'app/models/wordpress/wp_post.rb', line 148

def 
  first_revision.tags.map(&:name).join(",")
end

#post_tags=(tag_names) ⇒ Object



152
153
154
155
156
157
158
159
# File 'app/models/wordpress/wp_post.rb', line 152

def  tag_names
  tag_names = tag_names.split(",").map{ |name| name.strip } unless tag_names.is_a?(Array)
  tags_to_assign = tag_names.map{ |tag_name| .find_or_create(tag_name) }
  new_tags = tags_to_assign.reject{ |tag| first_revision.tags.include?(tag) }

  first_revision.tags.each{ |tag| tag.mark_for_destruction unless tags_to_assign.include?(tag) }
  new_tags.each{ |tag| first_revision.relationships.build(taxonomy: tag) }
end

#save_relationshipsObject



113
114
115
116
117
118
# File 'app/models/wordpress/wp_post.rb', line 113

def save_relationships
  return if self.first_revision == self
  self.first_revision.tags.each{|item| item.destroy if item.marked_for_destruction? }
  self.first_revision.categories.each{|item| item.destroy if item.marked_for_destruction? }
  self.first_revision.save
end

#set_defaultsObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'app/models/wordpress/wp_post.rb', line 84

def set_defaults
  p = self.parent
  self.author = self.parent.try(:author) || User.first

  self. = (self.parent.try(:post_date) || Time.now) \
    unless self.

  self. = (self.parent.try(:post_date_gmt) || self..utc) \
    unless self.

  content = (self.post_content || self.parent.try(:post_content)).to_s
  self.post_excerpt = content.length >= 512 ? "#{content.slice(0, 512)}..." : content \
    unless self.post_content_changed?

  self.to_ping = '' unless self.to_ping_changed?
  self.pinged = '' unless self.pinged_changed?
  self.post_content_filtered = '' unless self.post_content_filtered_changed?
end

#tag_namesObject



120
121
122
# File 'app/models/wordpress/wp_post.rb', line 120

def tag_names
  first_revision.tags.map(&:name)
end

#tag_slugsObject



124
125
126
# File 'app/models/wordpress/wp_post.rb', line 124

def tag_slugs
  first_revision.tags.map(&:slug)
end

#titleObject



64
65
66
# File 'app/models/wordpress/wp_post.rb', line 64

def title
  latest_revision.post_title
end

#touch_valuesObject



107
108
109
110
111
# File 'app/models/wordpress/wp_post.rb', line 107

def touch_values
  self.post_modified = Time.now unless self.post_modified_changed?
  self.post_modified_gmt = self.post_modified.utc  unless self.post_modified_gmt_changed?
  self.post_name = self.post_title.parameterize  unless self.post_name_changed?
end

#updated_atObject



80
81
82
# File 'app/models/wordpress/wp_post.rb', line 80

def updated_at
  latest_revision.post_modified
end