Sha256: b99367a83d55b59f46a50b1366a4dfe279107ade6cf33b0a5431177657744d5e

Contents?: true

Size: 1.26 KB

Versions: 187

Compression:

Stored size: 1.26 KB

Contents

module Lalala::Markdown::ActiveModel
  extend ActiveSupport::Concern

  module ClassMethods

    def markdown(*columns)
      options = columns.extract_options!
      @markdown_transformers ||= {}

      md  = Lalala::Markdown.new(options)
      mod = Module.new

      columns.each do |column|
        column = column.to_sym

        @markdown_transformers[column] = md

        mod.class_eval <<-SRC, __FILE__, __LINE__ + 1

          def #{column}_html
            value = self.#{column} || ""
            value = self.class.markdown_transformer_for_column(#{column.inspect}).render(value)
            value.html_safe
          end

          def #{column}=(value)
            value = self.class.markdown_transformer_for_column(#{column.inspect}).format(value)
            super(value)
          end

        SRC
      end

      include(mod)
    end

    def markdown_transformer_for_column(column_name)
      if @markdown_transformers and @markdown_transformers.key?(column_name)
        return @markdown_transformers[column_name]
      end

      if superclass.respond_to?(:markdown_transformer_for_column)
        return superclass.markdown_transformer_for_column(column_name)
      end

      raise "Missing markdown transformer for column `#{column_name}`"
    end

  end

end

Version data entries

187 entries across 187 versions & 1 rubygems

Version Path
lalala-4.1.0.dev.425 lib/lalala/markdown/active_model.rb
lalala-4.1.0.dev.420 lib/lalala/markdown/active_model.rb
lalala-4.1.0.dev.419 lib/lalala/markdown/active_model.rb
lalala-4.1.0.dev.418 lib/lalala/markdown/active_model.rb
lalala-4.1.0.dev.417 lib/lalala/markdown/active_model.rb
lalala-4.0.0.dev.416 lib/lalala/markdown/active_model.rb
lalala-4.1.0.dev.415 lib/lalala/markdown/active_model.rb
lalala-4.1.0.dev.414 lib/lalala/markdown/active_model.rb
lalala-4.1.0.dev.413 lib/lalala/markdown/active_model.rb
lalala-4.1.0.dev.412 lib/lalala/markdown/active_model.rb
lalala-4.1.0.dev.411 lib/lalala/markdown/active_model.rb
lalala-4.1.0.dev.410 lib/lalala/markdown/active_model.rb
lalala-4.1.0.dev.409 lib/lalala/markdown/active_model.rb
lalala-4.0.0.dev.407 lib/lalala/markdown/active_model.rb
lalala-4.1.0.dev.408 lib/lalala/markdown/active_model.rb
lalala-4.1.0.dev.398 lib/lalala/markdown/active_model.rb
lalala-4.1.0.dev.397 lib/lalala/markdown/active_model.rb
lalala-4.0.0.dev.396 lib/lalala/markdown/active_model.rb
lalala-4.0.0.dev.395 lib/lalala/markdown/active_model.rb
lalala-4.1.0.dev.394 lib/lalala/markdown/active_model.rb