Created
April 25, 2012 10:24
-
-
Save fusco/2488743 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Article < ActiveRecord::Base | |
belongs_to :employee, :foreign_key => :employee_id | |
# require gem globalize3 | |
translates :title, :content, :permalink, :keyword, :description | |
attr_accessible :title, :content, :keyword, :description, :published_at, :published, :comment_status, :employee_id, :status | |
before_validation :define_permalink | |
validates_presence_of :title, :content, :keyword, :description, :published_at | |
scope :published_at, where("published_at <= ?", Time.now) | |
scope :published, where(:status => 1) | |
#def name | |
# title | |
#end | |
def to_param | |
permalink | |
end | |
#previous link in article show view | |
def previous | |
Article.where('status = ? AND created_at <= ? AND id < ?', true, created_at, id).order('created_at DESC, id DESC').first | |
end | |
#next link in article show view | |
def next | |
Article.where('status = ? AND created_at >= ? AND id > ?', true, created_at, id).order('created_at ASC, id ASC').first | |
end | |
private | |
def define_permalink | |
unless self.title.nil? | |
#self.permalink = self.name.downcase.gsub(/[^a-z0-9]+/i, '-') | |
self.permalink = to_permalink(self.title) | |
change_permalink(self.permalink) | |
end | |
end | |
def to_permalink(str) | |
str = ActiveSupport::Multibyte::Chars.new(str) | |
str = str.normalize(:kd).gsub(/[^\x00-\x7F]/, '').to_s | |
str.gsub!(/'/, "") | |
str.gsub!(/[^-\w\d]+/sim, "-") | |
str.gsub!(/-+/sm, "-") | |
str.gsub!(/^-?(.*?)-?$/, '\1') | |
str.downcase! | |
str | |
end | |
# Change the name if it's already use in database | |
def define_name(name) | |
i = 1 | |
while not self.valid? | |
self.name = name + "-#{i}" | |
i += 1 | |
end | |
end | |
# Change the permalink if it's denied | |
def change_permalink(permalink) | |
base_permalink = permalink | |
i = 1 | |
while permalink_already_use | |
#while ensure_permalink_is_not_a_route || permalink_already_use | |
self.permalink = base_permalink + "-#{i}" | |
i += 1 | |
end | |
end | |
# Say if the permalink is already use or not. If the permalink is already use | |
# see if the permalink is not use by our own object | |
def permalink_already_use | |
if self.id.nil? | |
!Article.with_translations(I18n.locale).find_by_permalink(self.permalink).nil? | |
else | |
!Article.with_translations(I18n.locale).find_by_permalink(self.permalink, :conditions => ['id <> ?', self.id]).nil? | |
end | |
end | |
# Check if the permalink is possible for the URL /footer_pages/#{permalink} | |
# a permalinks static variable is use for performance, because the collect | |
# of routing is longer | |
#def ensure_permalink_is_not_a_route | |
# @@permalinks ||= Dacanocms::Application.routes.draw.collect { |r| | |
# r.segments.last.string_structure(r.segments[0..-2]).match(/"\/articles\/([\w]+)/)[1] rescue nil | |
# }.uniq.compact | |
# @@permalinks.each { |p| Rails.logger.debug p} | |
# @@permalinks.include?(permalink) | |
#end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment