Sha256: de25aeaa4ca41798d95fb31f4f4982f3527fa713b2628edec47b6d748ae02f20

Contents?: true

Size: 913 Bytes

Versions: 25

Compression:

Stored size: 913 Bytes

Contents

module MasterModel
  extend ActiveSupport::Concern

  included 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
    strip_attributes only: :name
  end

  # 表示名を設定します。
  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

Version data entries

25 entries across 24 versions & 2 rubygems

Version Path
enju_seed-0.3.6 app/models/concerns/master_model.rb
enju_seed-0.3.5 app/models/concerns/master_model.rb
enju_seed-0.3.4 app/models/concerns/master_model.rb
enju_seed-0.3.3 app/models/concerns/master_model.rb
enju_seed-0.3.2 app/models/concerns/master_model.rb
enju_seed-0.3.1 app/models/concerns/master_model.rb
enju_seed-0.3.0 app/models/concerns/master_model.rb
enju_seed-0.3.0.rc.1 app/models/concerns/master_model.rb
enju_seed-0.3.0.beta.7 app/models/concerns/master_model.rb
enju_seed-0.3.0.beta.6 app/models/concerns/master_model.rb
enju_seed-0.2.3 app/models/concerns/master_model.rb
enju_seed-0.2.2 app/models/concerns/master_model.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/enju_seed-0.2.1/app/models/concerns/master_model.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/enju_seed-0.2.0/app/models/concerns/master_model.rb
enju_seed-0.2.1 app/models/concerns/master_model.rb
enju_seed-0.2.0 app/models/concerns/master_model.rb
enju_seed-0.2.0.beta.7 app/models/concerns/master_model.rb
enju_seed-0.2.0.beta.6 app/models/concerns/master_model.rb
enju_seed-0.2.0.beta.5 app/models/concerns/master_model.rb
enju_seed-0.2.0.beta.4 app/models/concerns/master_model.rb