Sha256: b2002cfefb0e28cce6332bdd4c3f74b5790d1a4541ee2182a337c30703fda68a

Contents?: true

Size: 1.15 KB

Versions: 4

Compression:

Stored size: 1.15 KB

Contents

module MailyHerald

  # Provides some common patters for models that have both :name and :title attributes.
  # It adds some format constraints to :name and title attributes and makes sure
  # that they are always both set properly.
  #
  # If only :name is provided, it will be used also as a:title.
  # If only :title is provided, :name will be automatically generated out of it.
  #
  module Autonaming
    def self.included(base)
      base.extend ClassMethods
      base.send :include, MailyHerald::Autonaming::InstanceMethods

      base.class_eval do
        validates   :name,                presence: true, format: {with: /\A\w+\z/}, uniqueness: true
        validates   :title,               presence: true

        before_validation do
          if self.title && !self.name
            self.name = self.title.parameterize.underscore
          elsif self.name && !self.title
            self.title = self.name
          end
        end
      end
    end

    module ClassMethods
    end

    module InstanceMethods
      def self.included(base)
        base.extend ClassMethods
      end

      def to_s
        self.title || self.name
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
maily_herald-0.9.4 lib/maily_herald/autonaming.rb
maily_herald-0.9.3 lib/maily_herald/autonaming.rb
maily_herald-0.9.2 lib/maily_herald/autonaming.rb
maily_herald-0.9.1 lib/maily_herald/autonaming.rb