Sha256: 19daed2c0b5101fd35a9f4ef67a6f22e733644ae9f17d6137d2a60ee2b8ab7e1

Contents?: true

Size: 848 Bytes

Versions: 8

Compression:

Stored size: 848 Bytes

Contents

module PrettyUrl
  extend ActiveSupport::Concern

  included do
    after_save :update_url_name
  end

  module ClassMethods
    def find(id)
      if id.is_a?(String)
        real_id = /(.*\-)?(?<id>\d+)\Z/.match(id)[:id]
      else
        real_id = id
      end
      super(real_id)
    end
  end

  def to_param
    url_name
  end

  private
  def update_url_name
    if I18n.locale == :en
      if self.has_attribute?(:name) || self.translations.first.has_attribute?(:name)
        self.update_column :url_name, "#{name.to_url}-#{id}"
      elsif self.has_attribute?(:title) || self.translations.first.has_attribute?(:title)
        self.update_column :url_name, "#{title.to_url}-#{id}"
      else
        self.update_column :url_name, self.id
      end
    elsif url_name.blank?
      self.update_column :url_name, self.id
    end
  end

end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
tmatt_cms-0.1.7 lib/generators/tmatt_cms/templates/concerns/pretty_url.rb
tmatt_cms-0.1.6 lib/generators/tmatt_cms/templates/concerns/pretty_url.rb
tmatt_cms-0.1.5 lib/generators/tmatt_cms/templates/concerns/pretty_url.rb
tmatt_cms-0.1.4 lib/generators/tmatt_cms/templates/concerns/pretty_url.rb
tmatt_cms-0.1.3 lib/generators/tmatt_cms/templates/concerns/pretty_url.rb
tmatt_cms-0.1.2 lib/generators/tmatt_cms/templates/concerns/pretty_url.rb
tmatt_cms-0.1.1 lib/generators/tmatt_cms/templates/concerns/pretty_url.rb
tmatt_cms-0.1.0 lib/generators/tmatt_cms/templates/concerns/pretty_url.rb