Sha256: 1f2654f097381cc0b8e17917de2a72dd65cfa9b9b3dac484eb374ac9c20e850d

Contents?: true

Size: 1.02 KB

Versions: 6

Compression:

Stored size: 1.02 KB

Contents

module MasterModel
  def self.included(base)
    #base.extend ClassMethods
    base.send :include, InstanceMethods
    base.class_eval do
      acts_as_list
      validates_uniqueness_of :name, case_sensitive: false
      validates :name, presence: true
      validate :name do
        valid_name?
      end
      validate :display_name do
        valid_yaml?
      end
      validates :display_name, presence: true
      before_validation :set_display_name, on: :create
      normalize_attributes :name
    end
  end

  module InstanceMethods
    def set_display_name
      self.display_name = "#{I18n.locale}: #{name}" if display_name.blank?
    end

    private
    def valid_name?
      unless name =~ /\A[a-z][0-9a-z_]*[0-9a-z]\Z/
        errors.add(:name, I18n.t('page.only_lowercase_letters_and_numbers_are_allowed'))
      end
    end

    def valid_yaml?
      begin
        YAML.load(display_name)
      rescue Psych::SyntaxError
        errors.add(:display_name, I18n.t('page.cannot_parse_yaml_header'))
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
enju_leaf-1.1.0.rc20 lib/enju_leaf/master_model.rb
enju_leaf-1.1.0.rc19 lib/enju_leaf/master_model.rb
enju_leaf-1.1.0.rc18 lib/enju_leaf/master_model.rb
enju_leaf-1.1.0.rc17 lib/enju_leaf/master_model.rb
enju_leaf-1.1.0.rc16 lib/enju_leaf/master_model.rb
enju_leaf-1.1.0.rc15 lib/enju_leaf/master_model.rb