Sha256: a724949a895c0f8d368e1e2bf4710de269dcd56fdd29964384fbf647a75f42e3

Contents?: true

Size: 683 Bytes

Versions: 4

Compression:

Stored size: 683 Bytes

Contents

class Backend::SnippetForm < Udongo::Form
  attr_reader :snippet

  attribute :identifier, String
  attribute :description, String

  validates :identifier, :description, presence: true
  validate :unique_identifier

  delegate :id, to: :snippet

  def self.model_name
    Snippet.model_name
  end

  def persisted?
    !@snippet.new_record?
  end

  private

  def unique_identifier
    qry = Snippet.unscoped
    qry = qry.where.not(identifier: @snippet.identifier) if persisted?

    if qry.exists?(identifier: identifier)
      errors.add :identifier, I18n.t('errors.messages.taken')
    end
  end

  def save_object
    init_object_values(@snippet)
    @snippet.save!
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
udongo-2.0.4 app/forms/backend/snippet_form.rb
udongo-2.0.3 app/forms/backend/snippet_form.rb
udongo-2.0.2 app/forms/backend/snippet_form.rb
udongo-2.0.1 app/forms/backend/snippet_form.rb