require "state_machine"

class SectionEdition
  include Mongoid::Document
  include Mongoid::Timestamps

  store_in "manual_section_editions"

  field :section_id,           type: String
  field :version_number,       type: Integer, default: 1
  field :title,                type: String
  field :slug,                 type: String
  field :summary, type: String
  field :body, type: String
  field :state, type: String
  field :change_note, type: String
  field :minor_update, type: Boolean
  field :public_updated_at, type: DateTime
  field :exported_at, type: DateTime

  validates :section_id, presence: true
  validates :slug, presence: true

  embeds_many :attachments, cascade_callbacks: true

  state_machine initial: :draft do
    event :publish do
      transition draft: :published
    end

    event :archive do
      transition all => :archived, :unless => :archived?
    end
  end

  scope :draft,               where(state: "draft")
  scope :published,           where(state: "published")
  scope :archived,            where(state: "archived")

  scope :with_slug_prefix, ->(slug) { where(slug: /^#{slug}.*/) }

  index "section_id"
  index "state"
  index "updated_at"

  def build_attachment(attributes)
    attachments.build(
      attributes.merge(
        filename: attributes.fetch(:file).original_filename
      )
    )
  end
end

require "state_machine"

class SectionEdition
  include Mongoid::Document
  include Mongoid::Timestamps

  store_in collection: "manual_section_editions"

  field :section_id,           type: String
  field :version_number,       type: Integer, default: 1
  field :title,                type: String
  field :slug,                 type: String
  field :summary, type: String
  field :body, type: String
  field :state, type: String
  field :change_note, type: String
  field :minor_update, type: Boolean
  field :public_updated_at, type: DateTime
  field :exported_at, type: DateTime

  validates :section_id, presence: true
  validates :slug, presence: true

  embeds_many :attachments, cascade_callbacks: true

  state_machine initial: :draft do
    event :publish do
      transition draft: :published
    end

    event :archive do
      transition all => :archived, :unless => :archived?
    end
  end

  scope :draft,               where(state: "draft")
  scope :published,           where(state: "published")
  scope :archived,            where(state: "archived")

  scope :with_slug_prefix, ->(slug) { where(slug: /^#{slug}.*/) }

  index "section_id"
  index "state"
  index "updated_at"

  def build_attachment(attributes)
    attachments.build(
      attributes.merge(
        filename: attributes.fetch(:file).original_filename
      )
    )
  end
end

Update call to store_in for Mongoid 3
Update call to store_in for Mongoid 3

This addresses the following error message I was seeing when running
`rake`:

    Problem:
      Invalid options passed to SectionEdition.store_in:
      manual_section_editions.
    Summary:
      The :store_in macro takes only a hash of parameters with the keys
      :database, :collection, or :session.
    Resolution:
      Change the options passed to store_in to match the documented API, and
      ensure all keys in the options hash are symbols.

Note that we're already passing a hash to `store_in` in the User model.
I'm fairly certain this wasn't working as expected prior to the upgrade
to Mongoid 3 as the first param of the `store_in` method was a string
containing the name of the collection. Passing a hash appears to have
resulted in the users being stored in a collection named
“{:collection=>:manuals_publisher_users}” in development. Note that this
wouldn't have affected the production database as those users are stored
in the single sign on app.
