Sha256: 66a9ad05bc976a58510c3a2defd0ebdc3aeb9c9193002ed35d3a2b18081c269b

Contents?: true

Size: 1.52 KB

Versions: 38

Compression:

Stored size: 1.52 KB

Contents

# frozen_string_literal: true

class UpdateInitiativeScopedType < ActiveRecord::Migration[5.1]
  class InitiativesTypeScope < ApplicationRecord
    self.table_name = :decidim_initiatives_type_scopes
  end

  class Scope < ApplicationRecord
    self.table_name = :decidim_scopes

    # Scope to return only the top level scopes.
    #
    # Returns an ActiveRecord::Relation.
    def self.top_level
      where parent_id: nil
    end
  end

  class Organization < ApplicationRecord
    self.table_name = :decidim_organizations

    has_many :scopes, foreign_key: "decidim_organization_id", class_name: "Scope"

    # Returns top level scopes for this organization.
    #
    # Returns an ActiveRecord::Relation.
    def top_scopes
      @top_scopes ||= scopes.top_level
    end
  end

  class Initiative < ApplicationRecord
    self.table_name = :decidim_initiatives

    belongs_to :scoped_type,
               foreign_key: "scoped_type_id",
               class_name: "InitiativesTypeScope"

    belongs_to :organization,
               foreign_key: "decidim_organization_id",
               class_name: "Organization"
  end

  def up
    Initiative.find_each do |initiative|
      initiative.scoped_type = InitiativesTypeScope.find_by(
        decidim_initiatives_types_id: initiative.type_id,
        decidim_scopes_id: initiative.decidim_scope_id || initiative.organization.top_scopes.first
      )

      initiative.save!
    end
  end

  def down
    raise ActiveRecord::IrreversibleMigration, "Can't undo initialization of mandatory attribute"
  end
end

Version data entries

38 entries across 38 versions & 1 rubygems

Version Path
decidim-initiatives-0.23.6 db/migrate/20171017095143_update_initiative_scoped_type.rb
decidim-initiatives-0.23.5 db/migrate/20171017095143_update_initiative_scoped_type.rb
decidim-initiatives-0.23.4 db/migrate/20171017095143_update_initiative_scoped_type.rb
decidim-initiatives-0.23.3 db/migrate/20171017095143_update_initiative_scoped_type.rb
decidim-initiatives-0.23.2 db/migrate/20171017095143_update_initiative_scoped_type.rb
decidim-initiatives-0.23.1 db/migrate/20171017095143_update_initiative_scoped_type.rb
decidim-initiatives-0.23.1.rc1 db/migrate/20171017095143_update_initiative_scoped_type.rb
decidim-initiatives-0.23.0 db/migrate/20171017095143_update_initiative_scoped_type.rb
decidim-initiatives-0.22.0 db/migrate/20171017095143_update_initiative_scoped_type.rb
decidim-initiatives-0.21.0 db/migrate/20171017095143_update_initiative_scoped_type.rb
decidim-initiatives-0.20.1 db/migrate/20171017095143_update_initiative_scoped_type.rb
decidim-initiatives-0.20.0 db/migrate/20171017095143_update_initiative_scoped_type.rb
decidim-initiatives-0.19.1 db/migrate/20171017095143_update_initiative_scoped_type.rb
decidim-initiatives-0.18.1 db/migrate/20171017095143_update_initiative_scoped_type.rb
decidim-initiatives-0.19.0 db/migrate/20171017095143_update_initiative_scoped_type.rb
decidim-initiatives-0.17.2 db/migrate/20171017095143_update_initiative_scoped_type.rb
decidim-initiatives-0.18.0 db/migrate/20171017095143_update_initiative_scoped_type.rb
decidim-initiatives-0.17.1 db/migrate/20171017095143_update_initiative_scoped_type.rb
decidim-initiatives-0.16.1 db/migrate/20171017095143_update_initiative_scoped_type.rb
decidim-initiatives-0.17.0 db/migrate/20171017095143_update_initiative_scoped_type.rb