Sha256: abbd2a0cb6d34f69bec114fe06a8993dc3e0b41a73c489d525de1e050cbbae4f

Contents?: true

Size: 904 Bytes

Versions: 2

Compression:

Stored size: 904 Bytes

Contents

module Models::TagsMixin
  def tags; @tags ||= Models::Tags.new end
  def tags= tags; @tags = Models::Tags.new tags end

  def topics_as_string= value
    tags.topic = value
  end
  def topics_as_string
    tags.topic.as_string
  end

  inherited do
    before_validate :add_class_to_tags
    validate :validate_class_in_tags
    after_validate :copy_errors_from_tags
    after_build :build_tags

    assign :topics_as_string, String, true
  end

  protected
    def add_class_to_tags
      cname = "_class:#{self.class.name}"
      tags.push(cname).sort! unless tags.include? cname
    end

    def validate_class_in_tags
      cname = "_class:#{self.class.name}"
      errors.add :base, "no class in tags!" unless tags.include? cname
    end

    def copy_errors_from_tags
      errors[:base] = tags.errors[:base] unless tags.errors.empty?
    end

    def build_tags
      self.tags = tags
    end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rad_kit-0.0.10 lib/kit/models/tags_mixin.rb
rad_kit-0.0.9 lib/kit/models/tags_mixin.rb