lib/markdownizer.rb
def markdownize! attribute, options = {}
      # Check that both `:attribute` and `:rendered_attribute` columns exist.
      # If they don't, it raises an error indicating that the user should generate
      # a migration.
      unless self.column_names.include?(attribute.to_s) &&
               self.column_names.include?("rendered_#{attribute}")
        raise "#{self.name} doesn't have required attributes :#{attribute} and :rendered_#{attribute}\nPlease generate a migration to add these attributes -- both should have type :text."
      end

      # The `:hierarchy` option tells Markdownizer the smallest header tag that
      # precedes the Markdown text. If you have a blogpost with an H1 (title) and
      # an H2 (some kind of tagline), then your hierarchy is 2, and the biggest
      # header found the markdown text will be translated directly to an H3. This
      # allows for semantical coherence within the context where the markdown text
      # is to be introduced.
      hierarchy = options.delete(:hierarchy) || 0

      # Create a `before_save` callback which will convert plain text to
      # Markdownized html every time the model is saved.
      self.before_save :"render_#{attribute}"

      # Define the converter method, which will assign the rendered html to the
      # `:rendered_attribute` field.
      define_method :"render_#{attribute}" do
        value = self.send(attribute)
        unless value.nil?
          self.send(:"rendered_#{attribute}=", Markdownizer.markdown(Markdownizer.coderay(value, options), hierarchy))
        else
          self.send(:"rendered_#{attribute}=", nil)
        end
      end
    end
def markdownize! attribute, options = {}
      # Check that both `:attribute` and `:rendered_attribute` columns exist.
      # If they don't, it raises an error indicating that the user should generate
      # a migration.
      unless self.column_names.include?(attribute.to_s) &&
               self.column_names.include?("rendered_#{attribute}")
        raise "#{self.name} doesn't have required attributes :#{attribute} and :rendered_#{attribute}\nPlease generate a migration to add these attributes -- both should have type :text."
      end

      # The `:hierarchy` option tells Markdownizer the smallest header tag that
      # precedes the Markdown text. If you have a blogpost with an H1 (title) and
      # an H2 (some kind of tagline), then your hierarchy is 2, and the biggest
      # header found the markdown text will be translated directly to an H3. This
      # allows for semantical coherence within the context where the markdown text
      # is to be introduced.
      hierarchy = options.delete(:hierarchy) || 0

      # Create a `before_save` callback which will convert plain text to
      # Markdownized html every time the model is saved.
      self.before_save :"render_#{attribute}"

      # Define the converter method, which will assign the rendered html to the
      # `:rendered_attribute` field.
      define_method :"render_#{attribute}" do
        value = self.send(attribute)
        unless value.nil?
          self.send(:"rendered_#{attribute}=", Markdownizer.markdown(Markdownizer.coderay(value, options), hierarchy))
        else
          self.send(:"rendered_#{attribute}=", nil)
        end
      end
    end
def markdownize! attribute , options = { } # Check that both `:attribute` and `:rendered_attribute` columns exist. # If they don't, it raises an error indicating that the user should generate # a migration. unless self . column_names . include? ( attribute . to_s ) && self . column_names . include? ( "rendered_#{attribute}" ) raise "#{self.name} doesn't have required attributes :#{attribute} and :rendered_#{attribute}\nPlease generate a migration to add these attributes -- both should have type :text." end # The `:hierarchy` option tells Markdownizer the smallest header tag that # precedes the Markdown text. If you have a blogpost with an H1 (title) and # an H2 (some kind of tagline), then your hierarchy is 2, and the biggest # header found the markdown text will be translated directly to an H3. This # allows for semantical coherence within the context where the markdown text # is to be introduced. hierarchy = options . delete ( :hierarchy ) || 0 # Create a `before_save` callback which will convert plain text to # Markdownized html every time the model is saved. self . before_save :" #{ attribute } " # Define the converter method, which will assign the rendered html to the # `:rendered_attribute` field. define_method :" #{ attribute } " do value = self . send ( attribute ) unless value . nil? self . send ( :" #{ attribute } " , Markdownizer . markdown ( Markdownizer . coderay ( value , options ) , hierarchy ) ) else self . send ( :" #{ attribute } " , nil ) end end end
Calling `markdownize! :attribute` (where `:attribute` can be any database
 attribute with type `text`) will treat this field as Markdown.
 You can pass an `options` hash for CodeRay. An example option would be:

   * `:line_numbers => :table` (or `:inline`)

 You can check other available options in CodeRay's documentation.
Calling markdownize! : attribute ( where : attribute can be any database attribute with type text ) will treat this field as Markdown . You can pass an options hash for CodeRay . An example option would be :