Sha256: 66d4f83a0a5ee4c626532ee19cfc90def1496dbd166ff212cc407b257a45dc84

Contents?: true

Size: 1.48 KB

Versions: 37

Compression:

Stored size: 1.48 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,
               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

37 entries across 37 versions & 1 rubygems

Version Path
decidim-initiatives-0.27.9 db/migrate/20171017095143_update_initiative_scoped_type.rb
decidim-initiatives-0.27.8 db/migrate/20171017095143_update_initiative_scoped_type.rb
decidim-initiatives-0.27.7 db/migrate/20171017095143_update_initiative_scoped_type.rb
decidim-initiatives-0.27.6 db/migrate/20171017095143_update_initiative_scoped_type.rb
decidim-initiatives-0.26.10 db/migrate/20171017095143_update_initiative_scoped_type.rb
decidim-initiatives-0.26.9 db/migrate/20171017095143_update_initiative_scoped_type.rb
decidim-initiatives-0.27.5 db/migrate/20171017095143_update_initiative_scoped_type.rb
decidim-initiatives-0.26.8 db/migrate/20171017095143_update_initiative_scoped_type.rb
decidim-initiatives-0.27.4 db/migrate/20171017095143_update_initiative_scoped_type.rb
decidim-initiatives-0.27.3 db/migrate/20171017095143_update_initiative_scoped_type.rb
decidim-initiatives-0.26.7 db/migrate/20171017095143_update_initiative_scoped_type.rb
decidim-initiatives-0.26.5 db/migrate/20171017095143_update_initiative_scoped_type.rb
decidim-initiatives-0.27.2 db/migrate/20171017095143_update_initiative_scoped_type.rb
decidim-initiatives-0.27.1 db/migrate/20171017095143_update_initiative_scoped_type.rb
decidim-initiatives-0.26.4 db/migrate/20171017095143_update_initiative_scoped_type.rb
decidim-initiatives-0.27.0 db/migrate/20171017095143_update_initiative_scoped_type.rb
decidim-initiatives-0.26.3 db/migrate/20171017095143_update_initiative_scoped_type.rb
decidim-initiatives-0.27.0.rc2 db/migrate/20171017095143_update_initiative_scoped_type.rb
decidim-initiatives-0.27.0.rc1 db/migrate/20171017095143_update_initiative_scoped_type.rb
decidim-initiatives-0.26.2 db/migrate/20171017095143_update_initiative_scoped_type.rb