Sha256: 46570300f669791415064eb26a1455abf26954f50eb25457d7f2ce69bc4f9162

Contents?: true

Size: 733 Bytes

Versions: 9

Compression:

Stored size: 733 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

9 entries across 9 versions & 1 rubygems

Version Path
rails_core_extensions-0.12.0 lib/rails_core_extensions/active_record_liquid_extensions.rb
rails_core_extensions-0.11.3 lib/rails_core_extensions/active_record_liquid_extensions.rb
rails_core_extensions-0.11.2 lib/rails_core_extensions/active_record_liquid_extensions.rb
rails_core_extensions-0.11.1 lib/rails_core_extensions/active_record_liquid_extensions.rb
rails_core_extensions-0.11.0.beta.11 lib/rails_core_extensions/active_record_liquid_extensions.rb
rails_core_extensions-0.10.0 lib/rails_core_extensions/active_record_liquid_extensions.rb
rails_core_extensions-0.9.0 lib/rails_core_extensions/active_record_liquid_extensions.rb
rails_core_extensions-0.8.0 lib/rails_core_extensions/active_record_liquid_extensions.rb
rails_core_extensions-0.7.1 lib/rails_core_extensions/active_record_liquid_extensions.rb