Sha256: bebdaf1908df9ce82ba2e41b148706ffe32840a5388b3c81562b318046dea8d6

Contents?: true

Size: 732 Bytes

Versions: 1

Compression:

Stored size: 732 Bytes

Contents

module RailsCoreExtensions
  module ActiveRecordLiquidExtensions
    def self.included(base)
      base.extend ClassMethods
    end
  end

  module ClassMethods
    def validates_liquid(field)
      field = field.to_sym
      before_validation do |record|
        begin
          Liquid::Template.parse(record.send(field), error_mode: strict)
        rescue Liquid::SyntaxError => e
          record.errors.add(field, "Liquid Syntax Error: #{e}")
        end
      end
    end

    def liquid_field(field)
      class_eval <<-CODE
        def parsed_#{field}
          Liquid::Template.parse(#{field})
        end

        def render_#{field}(*args)
          parsed_#{field}.render!(*args)
        end
      CODE
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rails_core_extensions-0.7.0 lib/rails_core_extensions/active_record_liquid_extensions.rb