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

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: 1
  index state: 1
  index updated_at: 1

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

Update indexes in SectionEdition for Mongoid 3
Update indexes in SectionEdition for Mongoid 3

This avoids the following problem when running `rake`:

    Problem:
      Invalid index specification on SectionEdition: section_id, {}
    Summary:
      Indexes in Mongoid are defined as a hash of field name and
      direction/2d pairs, with a hash for any additional options.
    Resolution:
      Ensure that the index conforms to the correct syntax and has the
      correct options.

The `index` method now takes a field name and direction where it
previously took a name only.

Although the `index` macro in Mongoid 2.8.1 only takes a `name`
parameter, that was being converted to a name/direction pair in
`Collection#parse_index_spec`[1] of the underlying mongo-ruby-driver
Gem. The direction was being set to 1 (ascending) so I'm confident that
we're retaining the same behaviour in this commit.

This change to the `index` macros is mentioned in the Changelog for
3.0.0[2]

> Indexing syntax has changed. The first parameter is now a hash of
> name/direction pairs with an optional second hash parameter for
> additional options.

[1]: https://github.com/mongodb/mongo-ruby-driver/blob/1.x-stable/lib/mongo/collection.rb#L1121
[2]: https://github.com/mongodb/mongoid/blob/master/CHANGELOG.md#300
