Sha256: b361eaf8fac45d0b4515ef8d6102d2b98576e174db201304cb36297d8dbe4af5

Contents?: true

Size: 744 Bytes

Versions: 4

Compression:

Stored size: 744 Bytes

Contents

module RailsCoreExtensions
  module ActiveRecordLiquidExtensions
    extend ActiveSupport::Concern

    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
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rails_core_extensions-0.15.0 lib/rails_core_extensions/active_record_liquid_extensions.rb
rails_core_extensions-0.14.0 lib/rails_core_extensions/active_record_liquid_extensions.rb
rails_core_extensions-0.13.2 lib/rails_core_extensions/active_record_liquid_extensions.rb
rails_core_extensions-0.13.1 lib/rails_core_extensions/active_record_liquid_extensions.rb