SlimValidation
ActiveModel
やActiveRecord
用のEachValidator
です。slim-template/slimのフォーマットにしたがってるか確認します。
render
の成功をもってvalidとしているので、レンダリング結果をそのまま他のカラムに挿入出来ます。
class Entry < ActiveRecord::Base
validates :slim,
slim: {
to: :html
}
end
entry = Entry.new(slim: "h1 title\np catch copy")
entry.valid?
=> true
entry.html
=> "<h1>title</h1><p>catch copy</p>"
entry = Entry.new(slim: " h1 title\np catch copy")
entry.valid?
=> false
entry.html
=> nil
Installation
gem 'slim_validation'
bundle install
Usage
option | value | |
---|---|---|
to | Procかattribute_name | レンダリング結果の挿入先、処理方法 |
options | Hash | Slim::Template.newにわたされるoptions |
scope | :recordか任意のオブジェクト | slimのrender時に使う変数などの参照先 |
validates :slim_proc,
slim: {
options: {pretty: false},
to: ->(record, result) {
record.html = result
},
allow_blank: true
}
validates :slim_scope,
slim: {
options: {pretty: false},
to: :html,
scope: :record,
allow_blank: true
}