Module: WPDB::Termable

Included in:
Link, Post
Defined in:
lib/ruby-wpdb/term.rb

Instance Method Summary collapse

Instance Method Details

#add_term(term, taxonomy, description, count) ⇒ Object

For objects that have a relationship with termtaxonomies, this module can be mixed in and gives the ability to add a term directly to the model, rather than creating the relationship yourself. Used by Post and Link.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ruby-wpdb/term.rb', line 19

def add_term(term, taxonomy, description, count)
  if term.respond_to?(:term_id)
    term_id = term.term_id
  else
    term_id = term.to_i
  end

  term_taxonomy = WPDB::TermTaxonomy.where(term_id: term_id, taxonomy: taxonomy).first
  unless term_taxonomy
    term_taxonomy = WPDB::TermTaxonomy.create(
      term_id: term_id,
      taxonomy: taxonomy,
      description: description,
      count: count
    )
  else
    term_taxonomy.count += count
  end

  add_termtaxonomy(term_taxonomy)
end