Class: Goldencobra::ArticleUrl
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Goldencobra::ArticleUrl
- Defined in:
- app/models/goldencobra/article_url.rb
Class Method Summary collapse
- .recreate_all_urls ⇒ Object
- .setup(article_id) ⇒ Object
- .setup_domain_urls(article) ⇒ Object
- .setup_single_url(article) ⇒ Object
Class Method Details
.recreate_all_urls ⇒ Object
9 10 11 12 13 14 |
# File 'app/models/goldencobra/article_url.rb', line 9 def self.recreate_all_urls Goldencobra::ArticleUrl.all.each do |goldencobra_article_url| goldencobra_article_url.destroy goldencobra_article_url.article.save end end |
.setup(article_id) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'app/models/goldencobra/article_url.rb', line 16 def self.setup(article_id) article = Goldencobra::Article.find_by_id(article_id) # if an article is deleted, related has_many :urls should be deleted to by definition, # so there is nothing else to do return true unless article # are multiple domains in use? if Goldencobra::Domain.any? setup_domain_urls(article) else setup_single_url(article) end article.descendant_ids.each do |did| Goldencobra::ArticleUrl.setup(did) end end |
.setup_domain_urls(article) ⇒ Object
35 36 37 38 39 40 41 42 43 44 |
# File 'app/models/goldencobra/article_url.rb', line 35 def self.setup_domain_urls(article) # cleanup old Domain Urls Goldencobra::ArticleUrl.where(article_id: article.id).destroy_all protocol = Goldencobra::Url.protocol Goldencobra::Domain.all.each do |domain| new_url = "#{protocol}://#{domain.hostname}#{domain.url_prefix}#{article.public_url}" Goldencobra::ArticleUrl.create(article_id: article.id, url: new_url) end end |
.setup_single_url(article) ⇒ Object
46 47 48 49 50 51 52 |
# File 'app/models/goldencobra/article_url.rb', line 46 def self.setup_single_url(article) article_url = Goldencobra::ArticleUrl.where(article_id: article.id).first_or_create if article_url.url != article.absolute_public_url.downcase article_url.url = article.absolute_public_url.downcase article_url.save end end |