Sha256: e647e5dd673007c42a5e54e9c50cfdc5430efc7245611bdb35a2b612608f7527

Contents?: true

Size: 1.11 KB

Versions: 5

Compression:

Stored size: 1.11 KB

Contents

module Vidibus
  module Textile
    module Mongoid
      extend ActiveSupport::Concern
      module ClassMethods

        # Define existing attribute as textile.
        def textile(*args)
          options = args.extract_options!
          for field in args
            plain_field = "#{field}_plain"
            self.send(:field, field, :type => Binary)
            self.send(:field, plain_field, :type => Binary)

            class_eval <<-EOS
              before_save :set_#{plain_field}, :if => lambda {#{field}_changed? or new_record?}

              def set_#{plain_field}
                self.#{plain_field} = #{plain_field}
              end

              def #{plain_field}
                if #{field}_changed? or new_record?
                  Vidibus::Textile.new(#{field}).to_text(:plain => true)
                else
                  read_attribute(:#{plain_field})
                end
              end

              def #{field}_html
                return "" unless #{field}
                Vidibus::Textile.new(#{field}).to_html
              end
            EOS
          end
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
vidibus-textile-0.2.4 lib/vidibus/textile/mongoid.rb
vidibus-textile-0.2.3 lib/vidibus/textile/mongoid.rb
vidibus-textile-0.2.2 lib/vidibus/textile/mongoid.rb
vidibus-textile-0.2.1 lib/vidibus/textile/mongoid.rb
vidibus-textile-0.2.0 lib/vidibus/textile/mongoid.rb