Sha256: d410bf1da3862f9a6527162d6ae18484cff3eedf808f18f209b1c30a7e2a0317

Contents?: true

Size: 1.06 KB

Versions: 1

Compression:

Stored size: 1.06 KB

Contents

require 'yinx'
require 'yinx_sql/note'
require 'yinx_sql/tag'

module Yinx
  module SQL
    class Batch < ActiveRecord::Base
      has_many :tags, dependent: :destroy
      has_many :notes, dependent: :destroy

      def self.insert notes
        batch = Batch.new

        tags = Hash.new do |h, name|
          tag = Tag.new name: name
          batch.tags << tag
          h[name] = tag
        end

        notes.each do |note|
          n = Note.new(title: note.title,
                       book: note.book,
                       stack: note.stack,
                       content_length: note.contentLength,
                       created_at: note.created_at,
                       updated_at: note.updated_at)

          batch.notes << n

          note.tags.each do |tag_name|
            tag = tags[tag_name]
            tag.notes << n
          end
        end

        batch.save
      end

      def books
        notes.map(&:stack_book).uniq
      end

      def stacks
        notes.map{|note| note.stack.nil? ? 'NO_STACK' : note.stack}.uniq
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
yinx_sql-0.1.2 lib/yinx_sql/batch.rb